brintos

brintos / llvm-project-archived public Read only

0
0
Text · 6.1 KiB · bb12a5b Raw
220 lines · plain
1//  (C) Copyright Matt Borland 2022.2//  Use, modification and distribution are subject to the3//  Boost Software License, Version 1.0. (See accompanying file4//  LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)5 6#ifndef BOOST_MATH_CCMATH_SIGNBIT_HPP7#define BOOST_MATH_CCMATH_SIGNBIT_HPP8 9#include <boost/math/ccmath/detail/config.hpp>10 11#ifdef BOOST_MATH_NO_CCMATH12#error "The header <boost/math/signbit.hpp> can only be used in C++17 and later."13#endif14 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_include22#  if __has_include(<bit>)23#    include <bit>24#    if __cpp_lib_bit_cast >= 201806L25#      define BOOST_MATH_BIT_CAST(T, x) std::bit_cast<T>(x)26#    endif27#  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#    endif31#  endif32#endif33 34/*35The following error is given using Apple Clang version 13.1.6, and Clang 13, and 14 on Ubuntu 22.04.0136TODO: Remove the following undef when Apple Clang supports37 38ccmath_signbit_test.cpp:32:19: error: static_assert expression is not an integral constant expression39    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 supported42        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_CAST51#endif52 53namespace boost::math::ccmath {54 55namespace detail {56 57#ifdef BOOST_MATH_BIT_CAST58 59struct IEEEf2bits60{61#if BOOST_MATH_ENDIAN_LITTLE_BYTE62    std::uint32_t mantissa : 23;63    std::uint32_t exponent : 8;64    std::uint32_t sign : 1;65#else // Big endian66    std::uint32_t sign : 1;67    std::uint32_t exponent : 8;68    std::uint32_t mantissa : 23;69#endif 70};71 72struct IEEEd2bits73{74#if BOOST_MATH_ENDIAN_LITTLE_BYTE75    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 endian80    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#endif85};86 87// 80 bit long double88#if LDBL_MANT_DIG == 64 && LDBL_MAX_EXP == 1638489 90struct IEEEl2bits91{92#if BOOST_MATH_ENDIAN_LITTLE_BYTE93    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 endian99    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#endif105};106 107// 128 bit long double108#elif LDBL_MANT_DIG == 113 && LDBL_MAX_EXP == 16384109 110struct IEEEl2bits111{112#if BOOST_MATH_ENDIAN_LITTLE_BYTE113    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 endian118    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#endif123};124 125// 64 bit long double (double == long double on ARM)126#elif LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024127 128struct IEEEl2bits129{130#if BOOST_MATH_ENDIAN_LITTLE_BYTE131    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 endian136    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#endif141};142 143#else // Unsupported long double representation144#  define BOOST_MATH_UNSUPPORTED_LONG_DOUBLE145#endif146 147template <typename T>148constexpr 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_DOUBLE161    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    #endif167    else168    {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#else177 178// Typical implementations of signbit involve type punning via union and manipulating179// overflow (see libc++ or musl). Neither of these are allowed in constexpr contexts180// (technically type punning via union in general is UB in c++ but well defined in C) 181// therefore we static assert these cases.182 183template <typename T>184constexpr 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#endif193 194}195 196// Return value: true if arg is negative, false if arg is 0, NAN, or positive197template <typename Real, std::enable_if_t<!std::is_integral_v<Real>, bool> = true>198constexpr 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    else205    {206        using std::signbit;207        return signbit(arg);208    }209}210 211template <typename Z, std::enable_if_t<std::is_integral_v<Z>, bool> = true>212constexpr bool signbit(Z arg)213{214    return boost::math::ccmath::signbit(static_cast<double>(arg));215}216 217} // Namespaces218 219#endif // BOOST_MATH_CCMATH_SIGNBIT_HPP220