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