123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587 |
- #ifndef BOOST_MATH_FP_TRAITS_HPP
- #define BOOST_MATH_FP_TRAITS_HPP
- #if defined(__vms) && defined(__DECCXX) && !__IEEE_FLOAT
- # define BOOST_FPCLASSIFY_VAX_FORMAT
- #endif
- #include <cstring>
- #include <cstdint>
- #include <limits>
- #include <type_traits>
- #include <boost/math/tools/is_standalone.hpp>
- #include <boost/math/tools/assert.hpp>
- #ifndef BOOST_MATH_STANDALONE
- #include <boost/predef/other/endian.h>
- #define BOOST_MATH_ENDIAN_BIG_BYTE BOOST_ENDIAN_BIG_BYTE
- #define BOOST_MATH_ENDIAN_LITTLE_BYTE BOOST_ENDIAN_LITTLE_BYTE
- #elif (__cplusplus >= 202002L || _MSVC_LANG >= 202002L)
- #if __has_include(<bit>)
- #include <bit>
- #define BOOST_MATH_ENDIAN_BIG_BYTE (std::endian::native == std::endian::big)
- #define BOOST_MATH_ENDIAN_LITTLE_BYTE (std::endian::native == std::endian::little)
- #else
- #error Missing <bit> header. Please disable standalone mode, and file an issue at https://github.com/boostorg/math
- #endif
- #elif defined(_WIN32)
- #define BOOST_MATH_ENDIAN_BIG_BYTE 0
- #define BOOST_MATH_ENDIAN_LITTLE_BYTE 1
- #elif defined(__BYTE_ORDER__)
- #define BOOST_MATH_ENDIAN_BIG_BYTE (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
- #define BOOST_MATH_ENDIAN_LITTLE_BYTE (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
- #else
- #error Could not determine endian type. Please disable standalone mode, and file an issue at https:
- #endif
- static_assert((BOOST_MATH_ENDIAN_BIG_BYTE || BOOST_MATH_ENDIAN_LITTLE_BYTE)
- && !(BOOST_MATH_ENDIAN_BIG_BYTE && BOOST_MATH_ENDIAN_LITTLE_BYTE),
- "Inconsistent endianness detected. Please disable standalone mode, and file an issue at https://github.com/boostorg/math");
- #ifdef BOOST_NO_STDC_NAMESPACE
- namespace std{ using ::memcpy; }
- #endif
- #ifndef FP_NORMAL
- #define FP_ZERO 0
- #define FP_NORMAL 1
- #define FP_INFINITE 2
- #define FP_NAN 3
- #define FP_SUBNORMAL 4
- #else
- #define BOOST_HAS_FPCLASSIFY
- #ifndef fpclassify
- # if (defined(__GLIBCPP__) || defined(__GLIBCXX__)) \
- && defined(_GLIBCXX_USE_C99_MATH) \
- && !(defined(_GLIBCXX_USE_C99_FP_MACROS_DYNAMIC) \
- && (_GLIBCXX_USE_C99_FP_MACROS_DYNAMIC != 0))
- # ifdef _STLP_VENDOR_CSTD
- # if _STLPORT_VERSION >= 0x520
- # define BOOST_FPCLASSIFY_PREFIX ::__std_alias::
- # else
- # define BOOST_FPCLASSIFY_PREFIX ::_STLP_VENDOR_CSTD::
- # endif
- # else
- # define BOOST_FPCLASSIFY_PREFIX ::std::
- # endif
- # else
- # undef BOOST_HAS_FPCLASSIFY
- # define BOOST_FPCLASSIFY_PREFIX
- # endif
- #elif (defined(__HP_aCC) && !defined(__hppa))
- # define BOOST_FPCLASSIFY_PREFIX ::
- #else
- # define BOOST_FPCLASSIFY_PREFIX
- #endif
- #ifdef __MINGW32__
- # undef BOOST_HAS_FPCLASSIFY
- #endif
- #endif
- namespace boost {
- namespace math {
- namespace detail {
- struct native_tag {};
- template <bool has_limits>
- struct generic_tag {};
- struct ieee_tag {};
- struct ieee_copy_all_bits_tag : public ieee_tag {};
- struct ieee_copy_leading_bits_tag : public ieee_tag {};
- #ifdef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
- inline bool is_generic_tag_false(const generic_tag<false>*)
- {
- return true;
- }
- inline bool is_generic_tag_false(const void*)
- {
- return false;
- }
- #endif
- struct unknown_precision{};
- struct single_precision {};
- struct double_precision {};
- struct extended_double_precision {};
- template<class T> struct fp_traits_native
- {
- typedef native_tag method;
- };
- template<class T, class U> struct fp_traits_non_native
- {
- #ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
- typedef generic_tag<std::numeric_limits<T>::is_specialized> method;
- #else
- typedef generic_tag<false> method;
- #endif
- };
- #ifndef BOOST_FPCLASSIFY_VAX_FORMAT
- template<> struct fp_traits_non_native<float, single_precision>
- {
- typedef ieee_copy_all_bits_tag method;
- static constexpr uint32_t sign = 0x80000000u;
- static constexpr uint32_t exponent = 0x7f800000;
- static constexpr uint32_t flag = 0x00000000;
- static constexpr uint32_t significand = 0x007fffff;
- typedef uint32_t bits;
- static void get_bits(float x, uint32_t& a) { std::memcpy(&a, &x, 4); }
- static void set_bits(float& x, uint32_t a) { std::memcpy(&x, &a, 4); }
- };
- #if defined(BOOST_NO_INT64_T) || defined(BOOST_NO_INCLASS_MEMBER_INITIALIZATION) \
- || defined(BOOST_BORLANDC) || defined(__CODEGEAR__)
- template<> struct fp_traits_non_native<double, double_precision>
- {
- typedef ieee_copy_leading_bits_tag method;
- static constexpr uint32_t sign = 0x80000000u;
- static constexpr uint32_t exponent = 0x7ff00000;
- static constexpr uint32_t flag = 0;
- static constexpr uint32_t significand = 0x000fffff;
- typedef uint32_t bits;
- static void get_bits(double x, uint32_t& a)
- {
- std::memcpy(&a, reinterpret_cast<const unsigned char*>(&x) + offset_, 4);
- }
- static void set_bits(double& x, uint32_t a)
- {
- std::memcpy(reinterpret_cast<unsigned char*>(&x) + offset_, &a, 4);
- }
- private:
- static constexpr int offset_ = BOOST_MATH_ENDIAN_BIG_BYTE ? 0 : 4;
- };
- #else
- template<> struct fp_traits_non_native<double, double_precision>
- {
- typedef ieee_copy_all_bits_tag method;
- static constexpr uint64_t sign = static_cast<uint64_t>(0x80000000u) << 32;
- static constexpr uint64_t exponent = static_cast<uint64_t>(0x7ff00000) << 32;
- static constexpr uint64_t flag = 0;
- static constexpr uint64_t significand
- = (static_cast<uint64_t>(0x000fffff) << 32) + static_cast<uint64_t>(0xffffffffu);
- typedef uint64_t bits;
- static void get_bits(double x, uint64_t& a) { std::memcpy(&a, &x, 8); }
- static void set_bits(double& x, uint64_t a) { std::memcpy(&x, &a, 8); }
- };
- #endif
- #endif
- #if defined(BOOST_NO_INT64_T) || defined(BOOST_NO_INCLASS_MEMBER_INITIALIZATION)\
- || defined(BOOST_BORLANDC) || defined(__CODEGEAR__)
- template<> struct fp_traits_non_native<long double, double_precision>
- {
- typedef ieee_copy_leading_bits_tag method;
- static constexpr uint32_t sign = 0x80000000u;
- static constexpr uint32_t exponent = 0x7ff00000;
- static constexpr uint32_t flag = 0;
- static constexpr uint32_t significand = 0x000fffff;
- typedef uint32_t bits;
- static void get_bits(long double x, uint32_t& a)
- {
- std::memcpy(&a, reinterpret_cast<const unsigned char*>(&x) + offset_, 4);
- }
- static void set_bits(long double& x, uint32_t a)
- {
- std::memcpy(reinterpret_cast<unsigned char*>(&x) + offset_, &a, 4);
- }
- private:
- static constexpr int offset_ = BOOST_MATH_ENDIAN_BIG_BYTE ? 0 : 4;
- };
- #else
- template<> struct fp_traits_non_native<long double, double_precision>
- {
- typedef ieee_copy_all_bits_tag method;
- static const uint64_t sign = static_cast<uint64_t>(0x80000000u) << 32;
- static const uint64_t exponent = static_cast<uint64_t>(0x7ff00000) << 32;
- static const uint64_t flag = 0;
- static const uint64_t significand
- = (static_cast<uint64_t>(0x000fffff) << 32) + static_cast<uint64_t>(0xffffffffu);
- typedef uint64_t bits;
- static void get_bits(long double x, uint64_t& a) { std::memcpy(&a, &x, 8); }
- static void set_bits(long double& x, uint64_t a) { std::memcpy(&x, &a, 8); }
- };
- #endif
- #if defined(__i386) || defined(__i386__) || defined(_M_IX86) \
- || defined(__amd64) || defined(__amd64__) || defined(_M_AMD64) \
- || defined(__x86_64) || defined(__x86_64__) || defined(_M_X64)
- template<>
- struct fp_traits_non_native<long double, extended_double_precision>
- {
- typedef ieee_copy_leading_bits_tag method;
- static constexpr uint32_t sign = 0x80000000u;
- static constexpr uint32_t exponent = 0x7fff0000;
- static constexpr uint32_t flag = 0x00008000;
- static constexpr uint32_t significand = 0x00007fff;
- typedef uint32_t bits;
- static void get_bits(long double x, uint32_t& a)
- {
- std::memcpy(&a, reinterpret_cast<const unsigned char*>(&x) + 6, 4);
- }
- static void set_bits(long double& x, uint32_t a)
- {
- std::memcpy(reinterpret_cast<unsigned char*>(&x) + 6, &a, 4);
- }
- };
- #elif defined(__ia64) || defined(__ia64__) || defined(_M_IA64)
- #elif defined(__GNUC__) && (LDBL_MANT_DIG == 106)
- #elif defined(__powerpc) || defined(__powerpc__) || defined(__POWERPC__) \
- || defined(__ppc) || defined(__ppc__) || defined(__PPC__)
- template<>
- struct fp_traits_non_native<long double, extended_double_precision>
- {
- typedef ieee_copy_leading_bits_tag method;
- static constexpr uint32_t sign = 0x80000000u;
- static constexpr uint32_t exponent = 0x7ff00000;
- static constexpr uint32_t flag = 0x00000000;
- static constexpr uint32_t significand = 0x000fffff;
- typedef uint32_t bits;
- static void get_bits(long double x, uint32_t& a)
- {
- std::memcpy(&a, reinterpret_cast<const unsigned char*>(&x) + offset_, 4);
- }
- static void set_bits(long double& x, uint32_t a)
- {
- std::memcpy(reinterpret_cast<unsigned char*>(&x) + offset_, &a, 4);
- }
- private:
- static constexpr int offset_ = BOOST_MATH_ENDIAN_BIG_BYTE ? 0 : 12;
- };
- #elif defined(__m68k) || defined(__m68k__) \
- || defined(__mc68000) || defined(__mc68000__) \
- template<>
- struct fp_traits_non_native<long double, extended_double_precision>
- {
- typedef ieee_copy_leading_bits_tag method;
- static constexpr uint32_t sign = 0x80000000u;
- static constexpr uint32_t exponent = 0x7fff0000;
- static constexpr uint32_t flag = 0x00008000;
- static constexpr uint32_t significand = 0x00007fff;
-
- typedef uint32_t bits;
- static void get_bits(long double x, uint32_t& a)
- {
- std::memcpy(&a, &x, 2);
- std::memcpy(reinterpret_cast<unsigned char*>(&a) + 2,
- reinterpret_cast<const unsigned char*>(&x) + 4, 2);
- }
- static void set_bits(long double& x, uint32_t a)
- {
- std::memcpy(&x, &a, 2);
- std::memcpy(reinterpret_cast<unsigned char*>(&x) + 4,
- reinterpret_cast<const unsigned char*>(&a) + 2, 2);
- }
- };
- #else
- template<>
- struct fp_traits_non_native<long double, extended_double_precision>
- {
- typedef ieee_copy_leading_bits_tag method;
- static constexpr uint32_t sign = 0x80000000u;
- static constexpr uint32_t exponent = 0x7fff0000;
- static constexpr uint32_t flag = 0x00000000;
- static constexpr uint32_t significand = 0x0000ffff;
- typedef uint32_t bits;
- static void get_bits(long double x, uint32_t& a)
- {
- std::memcpy(&a, reinterpret_cast<const unsigned char*>(&x) + offset_, 4);
- }
- static void set_bits(long double& x, uint32_t a)
- {
- std::memcpy(reinterpret_cast<unsigned char*>(&x) + offset_, &a, 4);
- }
- private:
- static constexpr int offset_ = BOOST_MATH_ENDIAN_BIG_BYTE ? 0 : 12;
- };
- #endif
- template<size_t n, bool fp> struct size_to_precision
- {
- typedef unknown_precision type;
- };
- template<> struct size_to_precision<4, true>
- {
- typedef single_precision type;
- };
- template<> struct size_to_precision<8, true>
- {
- typedef double_precision type;
- };
- template<> struct size_to_precision<10, true>
- {
- typedef extended_double_precision type;
- };
- template<> struct size_to_precision<12, true>
- {
- typedef extended_double_precision type;
- };
- template<> struct size_to_precision<16, true>
- {
- typedef extended_double_precision type;
- };
- template <class T>
- struct select_native
- {
- typedef typename size_to_precision<sizeof(T), ::std::is_floating_point<T>::value>::type precision;
- typedef fp_traits_non_native<T, precision> type;
- };
- template<>
- struct select_native<float>
- {
- typedef fp_traits_native<float> type;
- };
- template<>
- struct select_native<double>
- {
- typedef fp_traits_native<double> type;
- };
- template<>
- struct select_native<long double>
- {
- typedef fp_traits_native<long double> type;
- };
- #if (defined(BOOST_MATH_USE_C99) && !(defined(__GNUC__) && (__GNUC__ < 4))) \
- && !defined(__hpux) \
- && !defined(__DECCXX)\
- && !defined(__osf__) \
- && !defined(__SGI_STL_PORT) && !defined(_STLPORT_VERSION)\
- && !defined(__FAST_MATH__)\
- && !defined(BOOST_MATH_DISABLE_STD_FPCLASSIFY)\
- && !defined(__INTEL_COMPILER)\
- && !defined(sun)\
- && !defined(__VXWORKS__)
- # define BOOST_MATH_USE_STD_FPCLASSIFY
- #endif
- template<class T> struct fp_traits
- {
- typedef typename size_to_precision<sizeof(T), ::std::is_floating_point<T>::value>::type precision;
- #if defined(BOOST_MATH_USE_STD_FPCLASSIFY) && !defined(BOOST_MATH_DISABLE_STD_FPCLASSIFY)
- typedef typename select_native<T>::type type;
- #else
- typedef fp_traits_non_native<T, precision> type;
- #endif
- typedef fp_traits_non_native<T, precision> sign_change_type;
- };
- }
- }
- }
- #endif
|