Debian Codegraph @

third-party/boost-math/include/boost/math/tools/big_constant.hpp

raw
1
2 // Copyright (c) 2011 John Maddock
3 // Use, modification and distribution are subject to the
4 // Boost Software License, Version 1.0. (See accompanying file
5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6
7 #ifndef BOOST_MATH_TOOLS_BIG_CONSTANT_HPP
8 #define BOOST_MATH_TOOLS_BIG_CONSTANT_HPP
9
10 #include <boost/math/tools/config.hpp>
11
12 // On NVRTC we don't need any of this
13 // We just have a simple definition of the macro since the largest float
14 // type on the platform is a 64-bit double
15 #ifndef BOOST_MATH_HAS_NVRTC
16
17 #ifndef BOOST_MATH_STANDALONE
18 #include <boost/lexical_cast.hpp>
19 #endif
20
21 #include <cstdlib>
22 #include <type_traits>
23 #include <limits>
24
25 namespace boost{ namespace math{
26
27 namespace tools{
28
29 template <class T>
30 struct numeric_traits : public std::numeric_limits< T > {};
31
32 #ifdef BOOST_MATH_USE_FLOAT128
33 typedef __float128 largest_float;
34 #define BOOST_MATH_LARGEST_FLOAT_C(x) x##Q
35 template <>
36 struct numeric_traits<__float128>
37 {
38 static const int digits = 113;
39 static const int digits10 = 33;
40 static const int max_exponent = 16384;
41 static const bool is_specialized = true;
42 };
43 #elif LDBL_DIG > DBL_DIG
44 typedef long double largest_float;
45 #define BOOST_MATH_LARGEST_FLOAT_C(x) x##L
46 #else
47 typedef double largest_float;
48 #define BOOST_MATH_LARGEST_FLOAT_C(x) x
49 #endif
50
51 template <class T>
52 BOOST_MATH_GPU_ENABLED constexpr T make_big_value(largest_float v, const char*, std::true_type const&, std::false_type const&) BOOST_MATH_NOEXCEPT(T)
53 {
54 return static_cast<T>(v);
55 }
56 template <class T>
57 BOOST_MATH_GPU_ENABLED constexpr T make_big_value(largest_float v, const char*, std::true_type const&, std::true_type const&) BOOST_MATH_NOEXCEPT(T)
58 {
59 return static_cast<T>(v);
60 }
61 #ifndef BOOST_MATH_NO_LEXICAL_CAST
62 template <class T>
63 inline T make_big_value(largest_float, const char* s, std::false_type const&, std::false_type const&)
64 {
65 return boost::lexical_cast<T>(s);
66 }
67 #else
68 template <typename T>
69 inline T make_big_value(largest_float, const char*, std::false_type const&, std::false_type const&)
70 {
71 static_assert(sizeof(T) == 0, "Type is unsupported in standalone mode. Please disable and try again.");
72 }
73 #endif
74 template <class T>
75 inline constexpr T make_big_value(largest_float, const char* s, std::false_type const&, std::true_type const&) BOOST_MATH_NOEXCEPT(T)
76 {
77 return T(s);
78 }
79
80 //
81 // For constants which might fit in a long double (if it's big enough):
82 //
83 // Note that gcc-13 has std::is_convertible<long double, std::float64_t>::value false, likewise
84 // std::is_constructible<std::float64_t, long double>::value, even though the conversions do
85 // actually work. Workaround is the || std::is_floating_point<T>::value part which thankfully is true.
86 //
87 #define BOOST_MATH_BIG_CONSTANT(T, D, x)\
88 boost::math::tools::make_big_value<T>(\
89 BOOST_MATH_LARGEST_FLOAT_C(x), \
90 BOOST_MATH_STRINGIZE(x), \
91 std::integral_constant<bool, (std::is_convertible<boost::math::tools::largest_float, T>::value || std::is_floating_point<T>::value) && \
92 ((D <= boost::math::tools::numeric_traits<boost::math::tools::largest_float>::digits) \
93 || std::is_floating_point<T>::value \
94 || (boost::math::tools::numeric_traits<T>::is_specialized && \
95 (boost::math::tools::numeric_traits<T>::digits10 <= boost::math::tools::numeric_traits<boost::math::tools::largest_float>::digits10))) >(), \
96 std::is_constructible<T, const char*>())
97 //
98 // For constants too huge for any conceivable long double (and which generate compiler errors if we try and declare them as such):
99 //
100 #define BOOST_MATH_HUGE_CONSTANT(T, D, x)\
101 boost::math::tools::make_big_value<T>(0.0L, BOOST_MATH_STRINGIZE(x), \
102 std::integral_constant<bool, std::is_floating_point<T>::value || (boost::math::tools::numeric_traits<T>::is_specialized && boost::math::tools::numeric_traits<T>::max_exponent <= boost::math::tools::numeric_traits<boost::math::tools::largest_float>::max_exponent && boost::math::tools::numeric_traits<T>::digits <= boost::math::tools::numeric_traits<boost::math::tools::largest_float>::digits)>(), \
103 std::is_constructible<T, const char*>())
104
105 }}} // namespaces
106
107 #endif // BOOST_MATH_HAS_NVRTC
108
109 #endif
110