Debian Codegraph @

third-party/boost-math/include/boost/math/ccmath/signbit.hpp

raw
1 // (C) Copyright Matt Borland 2022.
2 // Use, modification and distribution are subject to the
3 // Boost Software License, Version 1.0. (See accompanying file
4 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5
6 #ifndef BOOST_MATH_CCMATH_SIGNBIT_HPP
7 #define BOOST_MATH_CCMATH_SIGNBIT_HPP
8
9 #include <boost/math/ccmath/detail/config.hpp>
10
11 #ifdef BOOST_MATH_NO_CCMATH
12 #error "The header <boost/math/signbit.hpp> can only be used in C++17 and later."
13 #endif
14
15 #include <cstdint>
16 #include <boost/math/tools/assert.hpp>
17 #include <boost/math/special_functions/detail/fp_traits.hpp>
18 #include <boost/math/ccmath/isnan.hpp>
19 #include <boost/math/ccmath/abs.hpp>
20
21 #ifdef __has_include
22 # if __has_include(<bit>)
23 # include <bit>
24 # if __cpp_lib_bit_cast >= 201806L
25 # define BOOST_MATH_BIT_CAST(T, x) std::bit_cast<T>(x)
26 # endif
27 # elif defined(__has_builtin)
28 # if __has_builtin(__builtin_bit_cast)
29 # define BOOST_MATH_BIT_CAST(T, x) __builtin_bit_cast(T, x)
30 # endif
31 # endif
32 #endif
33
34 /*
35 The following error is given using Apple Clang version 13.1.6, and Clang 13, and 14 on Ubuntu 22.04.01
36 TODO: Remove the following undef when Apple Clang supports
37
38 ccmath_signbit_test.cpp:32:19: error: static_assert expression is not an integral constant expression
39 static_assert(boost::math::ccmath::signbit(T(-1)) == true);
40 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
41 ../../../boost/math/ccmath/signbit.hpp:62:24: note: constexpr bit_cast involving bit-field is not yet supported
42 const auto u = BOOST_MATH_BIT_CAST(float_bits, arg);
43 ^
44 ../../../boost/math/ccmath/signbit.hpp:20:37: note: expanded from macro 'BOOST_MATH_BIT_CAST'
45 # define BOOST_MATH_BIT_CAST(T, x) __builtin_bit_cast(T, x)
46 ^
47 */
48
49 #if defined(__clang__) && defined(BOOST_MATH_BIT_CAST)
50 # undef BOOST_MATH_BIT_CAST
51 #endif
52
53 namespace boost::math::ccmath {
54
55 namespace detail {
56
57 #ifdef BOOST_MATH_BIT_CAST
58
59 struct IEEEf2bits
60 {
61 #if BOOST_MATH_ENDIAN_LITTLE_BYTE
62 std::uint32_t mantissa : 23;
63 std::uint32_t exponent : 8;
64 std::uint32_t sign : 1;
65 #else // Big endian
66 std::uint32_t sign : 1;
67 std::uint32_t exponent : 8;
68 std::uint32_t mantissa : 23;
69 #endif
70 };
71
72 struct IEEEd2bits
73 {
74 #if BOOST_MATH_ENDIAN_LITTLE_BYTE
75 std::uint32_t mantissa_l : 32;
76 std::uint32_t mantissa_h : 20;
77 std::uint32_t exponent : 11;
78 std::uint32_t sign : 1;
79 #else // Big endian
80 std::uint32_t sign : 1;
81 std::uint32_t exponent : 11;
82 std::uint32_t mantissa_h : 20;
83 std::uint32_t mantissa_l : 32;
84 #endif
85 };
86
87 // 80 bit long double
88 #if LDBL_MANT_DIG == 64 && LDBL_MAX_EXP == 16384
89
90 struct IEEEl2bits
91 {
92 #if BOOST_MATH_ENDIAN_LITTLE_BYTE
93 std::uint32_t mantissa_l : 32;
94 std::uint32_t mantissa_h : 32;
95 std::uint32_t exponent : 15;
96 std::uint32_t sign : 1;
97 std::uint32_t pad : 32;
98 #else // Big endian
99 std::uint32_t pad : 32;
100 std::uint32_t sign : 1;
101 std::uint32_t exponent : 15;
102 std::uint32_t mantissa_h : 32;
103 std::uint32_t mantissa_l : 32;
104 #endif
105 };
106
107 // 128 bit long double
108 #elif LDBL_MANT_DIG == 113 && LDBL_MAX_EXP == 16384
109
110 struct IEEEl2bits
111 {
112 #if BOOST_MATH_ENDIAN_LITTLE_BYTE
113 std::uint64_t mantissa_l : 64;
114 std::uint64_t mantissa_h : 48;
115 std::uint32_t exponent : 15;
116 std::uint32_t sign : 1;
117 #else // Big endian
118 std::uint32_t sign : 1;
119 std::uint32_t exponent : 15;
120 std::uint64_t mantissa_h : 48;
121 std::uint64_t mantissa_l : 64;
122 #endif
123 };
124
125 // 64 bit long double (double == long double on ARM)
126 #elif LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024
127
128 struct IEEEl2bits
129 {
130 #if BOOST_MATH_ENDIAN_LITTLE_BYTE
131 std::uint32_t mantissa_l : 32;
132 std::uint32_t mantissa_h : 20;
133 std::uint32_t exponent : 11;
134 std::uint32_t sign : 1;
135 #else // Big endian
136 std::uint32_t sign : 1;
137 std::uint32_t exponent : 11;
138 std::uint32_t mantissa_h : 20;
139 std::uint32_t mantissa_l : 32;
140 #endif
141 };
142
143 #else // Unsupported long double representation
144 # define BOOST_MATH_UNSUPPORTED_LONG_DOUBLE
145 #endif
146
147 template <typename T>
148 constexpr bool signbit_impl(T arg)
149 {
150 if constexpr (std::is_same_v<T, float>)
151 {
152 const auto u = BOOST_MATH_BIT_CAST(IEEEf2bits, arg);
153 return u.sign;
154 }
155 else if constexpr (std::is_same_v<T, double>)
156 {
157 const auto u = BOOST_MATH_BIT_CAST(IEEEd2bits, arg);
158 return u.sign;
159 }
160 #ifndef BOOST_MATH_UNSUPPORTED_LONG_DOUBLE
161 else if constexpr (std::is_same_v<T, long double>)
162 {
163 const auto u = BOOST_MATH_BIT_CAST(IEEEl2bits, arg);
164 return u.sign;
165 }
166 #endif
167 else
168 {
169 BOOST_MATH_ASSERT_MSG(!boost::math::ccmath::isnan(arg), "NAN is not supported with this type or platform");
170 BOOST_MATH_ASSERT_MSG(boost::math::ccmath::abs(arg) != 0, "Signed 0 is not support with this type or platform");
171
172 return arg < static_cast<T>(0);
173 }
174 }
175
176 #else
177
178 // Typical implementations of signbit involve type punning via union and manipulating
179 // overflow (see libc++ or musl). Neither of these are allowed in constexpr contexts
180 // (technically type punning via union in general is UB in c++ but well defined in C)
181 // therefore we static assert these cases.
182
183 template <typename T>
184 constexpr bool signbit_impl(T arg)
185 {
186 BOOST_MATH_ASSERT_MSG(!boost::math::ccmath::isnan(arg), "NAN is not supported without __builtin_bit_cast or std::bit_cast");
187 BOOST_MATH_ASSERT_MSG(boost::math::ccmath::abs(arg) != 0, "Signed 0 is not support without __builtin_bit_cast or std::bit_cast");
188
189 return arg < static_cast<T>(0);
190 }
191
192 #endif
193
194 }
195
196 // Return value: true if arg is negative, false if arg is 0, NAN, or positive
197 template <typename Real, std::enable_if_t<!std::is_integral_v<Real>, bool> = true>
198 constexpr bool signbit(Real arg)
199 {
200 if (BOOST_MATH_IS_CONSTANT_EVALUATED(arg))
201 {
202 return boost::math::ccmath::detail::signbit_impl(arg);
203 }
204 else
205 {
206 using std::signbit;
207 return signbit(arg);
208 }
209 }
210
211 template <typename Z, std::enable_if_t<std::is_integral_v<Z>, bool> = true>
212 constexpr bool signbit(Z arg)
213 {
214 return boost::math::ccmath::signbit(static_cast<double>(arg));
215 }
216
217 } // Namespaces
218
219 #endif // BOOST_MATH_CCMATH_SIGNBIT_HPP