123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219 |
- #ifndef BOOST_MATH_CCMATH_SIGNBIT_HPP
- #define BOOST_MATH_CCMATH_SIGNBIT_HPP
- #include <boost/math/ccmath/detail/config.hpp>
- #ifdef BOOST_MATH_NO_CCMATH
- #error "The header <boost/math/signbit.hpp> can only be used in C++17 and later."
- #endif
- #include <cstdint>
- #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
- #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
- 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
- std::uint32_t sign : 1;
- std::uint32_t exponent : 11;
- std::uint32_t mantissa_h : 20;
- std::uint32_t mantissa_l : 32;
- #endif
- };
- #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
- 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
- };
- #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
- std::uint32_t sign : 1;
- std::uint32_t exponent : 15;
- std::uint64_t mantissa_h : 48;
- std::uint64_t mantissa_l : 64;
- #endif
- };
- #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
- std::uint32_t sign : 1;
- std::uint32_t exponent : 11;
- std::uint32_t mantissa_h : 20;
- std::uint32_t mantissa_l : 32;
- #endif
- };
- #else
- # 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
- 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
- }
- 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));
- }
- }
- #endif
|