tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
signbit.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2// SPDX-FileCopyrightText: Copyright (C) 2019 Tobias Hienzsch
3
4#ifndef TETL_CMATH_SIGNBIT_HPP
5#define TETL_CMATH_SIGNBIT_HPP
6
7#include <etl/_config/all.hpp>
8
9#include <etl/_3rd_party/gcem/gcem.hpp>
10#include <etl/_array/array.hpp>
11#include <etl/_bit/bit_cast.hpp>
12#include <etl/_concepts/integral.hpp>
13#include <etl/_cstdint/int_t.hpp>
14#include <etl/_type_traits/is_constant_evaluated.hpp>
15
16namespace etl {
17
18namespace detail {
19
20template <typename Float>
21[[nodiscard]] constexpr auto signbit_fallback(Float arg) noexcept -> bool
22{
23 if constexpr (sizeof(Float) == 4) {
24 auto const bits = etl::bit_cast<etl::int32_t>(arg);
25 return bits < 0;
26 } else if constexpr (sizeof(Float) == 8) {
27 auto const bits = etl::bit_cast<etl::int64_t>(arg);
28 return bits < 0;
29 } else {
30 return etl::detail::gcem::signbit(arg);
31 }
32}
33
34inline constexpr struct signbit {
35 template <typename Float>
36 [[nodiscard]] constexpr auto operator()(Float arg) const noexcept -> bool
37 {
39 if constexpr (is_same_v<Float, float>) {
40#if __has_builtin(__builtin_signbitf)
41 return __builtin_signbitf(arg);
42#endif
43 }
44 if constexpr (is_same_v<Float, double>) {
45#if __has_builtin(__builtin_signbit)
46 return __builtin_signbit(arg);
47#endif
48 }
49 if constexpr (is_same_v<Float, long double>) {
50#if __has_builtin(__builtin_signbitl)
51 return __builtin_signbitl(arg);
52#endif
53 }
54 }
55 return signbit_fallback(arg);
56 }
57} signbit;
58
59} // namespace detail
60
61/// \ingroup cmath
62/// @{
63
64/// Determines if the given floating point number arg is negative.
65///
66/// This function detects the sign bit of zeroes, infinities, and NaNs.
67/// Along with etl::copysign, etl::signbit is one of the only two portable ways
68/// to examine the sign of a NaN.
69///
70/// https://en.cppreference.com/w/cpp/numeric/math/signbit
71[[nodiscard]] constexpr auto signbit(float arg) noexcept -> bool
72{
73 return etl::detail::signbit(arg);
74}
75
76/// Determines if the given floating point number arg is negative.
77///
78/// This function detects the sign bit of zeroes, infinities, and NaNs.
79/// Along with etl::copysign, etl::signbit is one of the only two portable ways
80/// to examine the sign of a NaN.
81///
82/// https://en.cppreference.com/w/cpp/numeric/math/signbit
83[[nodiscard]] constexpr auto signbit(double arg) noexcept -> bool
84{
85 return etl::detail::signbit(arg);
86}
87
88/// Determines if the given floating point number arg is negative.
89///
90/// This function detects the sign bit of zeroes, infinities, and NaNs.
91/// Along with etl::copysign, etl::signbit is one of the only two portable ways
92/// to examine the sign of a NaN.
93///
94/// https://en.cppreference.com/w/cpp/numeric/math/signbit
95[[nodiscard]] constexpr auto signbit(long double arg) noexcept -> bool
96{
97 return etl::detail::signbit(arg);
98}
99
100/// Determines if the given floating point number arg is negative.
101///
102/// This function detects the sign bit of zeroes, infinities, and NaNs.
103/// Along with etl::copysign, etl::signbit is one of the only two portable ways
104/// to examine the sign of a NaN.
105///
106/// https://en.cppreference.com/w/cpp/numeric/math/signbit
107[[nodiscard]] constexpr auto signbit(integral auto arg) noexcept -> bool
108{
109 return etl::detail::signbit(static_cast<double>(arg));
110}
111
112/// @}
113
114} // namespace etl
115
116#endif // TETL_CMATH_SIGNBIT_HPP
constexpr auto signbit(long double arg) noexcept -> bool
Determines if the given floating point number arg is negative.
Definition signbit.hpp:95
constexpr auto signbit(float arg) noexcept -> bool
Definition signbit.hpp:71
constexpr auto signbit(double arg) noexcept -> bool
Determines if the given floating point number arg is negative.
Definition signbit.hpp:83
Definition adjacent_find.hpp:9
constexpr auto is_constant_evaluated() noexcept -> bool
Detects whether the function call occurs within a constant-evaluated context. Returns true if the eva...
Definition is_constant_evaluated.hpp:17