]>
Commit | Line | Data |
---|---|---|
1e59de90 TL |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Copyright 2022 Matt Borland. Distributed under the Boost | |
3 | // Software License, Version 1.0. (See accompanying file | |
4 | // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |
5 | ||
6 | #ifndef BOOST_MP_DETAIL_FPCLASSIFY_HPP | |
7 | #define BOOST_MP_DETAIL_FPCLASSIFY_HPP | |
8 | ||
9 | #include <cmath> | |
10 | #include <limits> | |
11 | #include <type_traits> | |
12 | #include <boost/multiprecision/detail/standalone_config.hpp> | |
13 | #include <boost/multiprecision/detail/float128_functions.hpp> | |
14 | ||
15 | #ifdef BOOST_MP_MATH_AVAILABLE | |
16 | #include <boost/math/special_functions/fpclassify.hpp> | |
17 | ||
18 | #define BOOST_MP_ISNAN(x) (boost::math::isnan)(x) | |
19 | #define BOOST_MP_ISINF(x) (boost::math::isinf)(x) | |
20 | #define BOOST_MP_FPCLASSIFY(x) (boost::math::fpclassify)(x) | |
21 | #define BOOST_MP_ISFINITE(x) (!(boost::math::isnan)(x) && !(boost::math::isinf)(x)) | |
22 | ||
23 | #else | |
24 | ||
25 | namespace boost { namespace multiprecision { namespace detail { | |
26 | ||
27 | template <typename T, typename std::enable_if<std::is_floating_point<T>::value | |
28 | #ifdef BOOST_HAS_FLOAT128 | |
29 | || std::is_same<T, float128_type>::value | |
30 | #endif | |
31 | , bool>::type = true> | |
32 | inline bool isnan BOOST_PREVENT_MACRO_SUBSTITUTION (const T x) | |
33 | { | |
34 | BOOST_MP_FLOAT128_USING; | |
35 | using std::isnan; | |
36 | return static_cast<bool>((isnan)(x)); | |
37 | } | |
38 | ||
39 | template <typename T, typename std::enable_if<!std::is_floating_point<T>::value | |
40 | #ifdef BOOST_HAS_FLOAT128 | |
41 | && !std::is_same<T, float128_type>::value | |
42 | #endif | |
43 | , bool>::type = true> | |
44 | inline bool isnan BOOST_PREVENT_MACRO_SUBSTITUTION (const T x) | |
45 | { | |
46 | return x != x; | |
47 | } | |
48 | ||
49 | template <typename T, typename std::enable_if<std::is_floating_point<T>::value | |
50 | #ifdef BOOST_HAS_FLOAT128 | |
51 | || std::is_same<T, float128_type>::value | |
52 | #endif | |
53 | , bool>::type = true> | |
54 | inline bool isinf BOOST_PREVENT_MACRO_SUBSTITUTION (const T x) | |
55 | { | |
56 | BOOST_MP_FLOAT128_USING; | |
57 | using std::isinf; | |
58 | return static_cast<bool>((isinf)(x)); | |
59 | } | |
60 | ||
61 | template <typename T, typename std::enable_if<!std::is_floating_point<T>::value | |
62 | #ifdef BOOST_HAS_FLOAT128 | |
63 | && !std::is_same<T, float128_type>::value | |
64 | #endif | |
65 | , bool>::type = true> | |
66 | inline bool isinf BOOST_PREVENT_MACRO_SUBSTITUTION (const T x) | |
67 | { | |
68 | return x == std::numeric_limits<T>::infinity() || x == -std::numeric_limits<T>::infinity(); | |
69 | } | |
70 | ||
71 | template <typename T, typename std::enable_if<std::is_floating_point<T>::value, bool>::type = true> | |
72 | inline int fpclassify BOOST_PREVENT_MACRO_SUBSTITUTION (const T x) | |
73 | { | |
74 | using std::fpclassify; | |
75 | return fpclassify(x); | |
76 | } | |
77 | ||
78 | template <typename T, typename std::enable_if<!std::is_floating_point<T>::value, bool>::type = true> | |
79 | inline int fpclassify BOOST_PREVENT_MACRO_SUBSTITUTION (const T x) | |
80 | { | |
81 | BOOST_MP_FLOAT128_USING; | |
82 | using std::isnan; | |
83 | using std::isinf; | |
84 | using std::abs; | |
85 | ||
86 | return (isnan)(x) ? FP_NAN : | |
87 | (isinf)(x) ? FP_INFINITE : | |
88 | abs(x) == T(0) ? FP_ZERO : | |
89 | abs(x) > 0 && abs(x) < (std::numeric_limits<T>::min)() ? FP_SUBNORMAL : FP_NORMAL; | |
90 | } | |
91 | ||
92 | }}} // Namespace boost::multiprecision::detail | |
93 | ||
94 | #define BOOST_MP_ISNAN(x) (boost::multiprecision::detail::isnan)(x) | |
95 | #define BOOST_MP_ISINF(x) (boost::multiprecision::detail::isinf)(x) | |
96 | #define BOOST_MP_FPCLASSIFY(x) (boost::multiprecision::detail::fpclassify)(x) | |
97 | #define BOOST_MP_ISFINITE(x) (!(boost::multiprecision::detail::isnan)(x) && !(boost::multiprecision::detail::isinf)(x)) | |
98 | ||
99 | #endif | |
100 | ||
101 | #endif // BOOST_MP_DETAIL_FPCLASSIFY_HPP |