Debian Codegraph @

third-party/boost-math/include/boost/math/ccmath/ilogb.hpp

raw
1 // (C) Copyright Matt Borland 2021.
2 // Use, modification and distribution are subject to the
3 // Boost 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_MATH_CCMATH_ILOGB_HPP
7 #define BOOST_MATH_CCMATH_ILOGB_HPP
8
9 #include <boost/math/ccmath/detail/config.hpp>
10
11 #ifdef BOOST_MATH_NO_CCMATH
12 #error "The header <boost/math/ilogb.hpp> can only be used in C++17 and later."
13 #endif
14
15 #include <boost/math/ccmath/logb.hpp>
16 #include <boost/math/ccmath/isinf.hpp>
17 #include <boost/math/ccmath/isnan.hpp>
18 #include <boost/math/ccmath/abs.hpp>
19
20 namespace boost::math::ccmath {
21
22 // If arg is not zero, infinite, or NaN, the value returned is exactly equivalent to static_cast<int>(std::logb(arg))
23 template <typename Real, std::enable_if_t<!std::is_integral_v<Real>, bool> = true>
24 inline constexpr int ilogb(Real arg) noexcept
25 {
26 if(BOOST_MATH_IS_CONSTANT_EVALUATED(arg))
27 {
28 return boost::math::ccmath::abs(arg) == Real(0) ? FP_ILOGB0 :
29 boost::math::ccmath::isinf(arg) ? INT_MAX :
30 boost::math::ccmath::isnan(arg) ? FP_ILOGBNAN :
31 static_cast<int>(boost::math::ccmath::logb(arg));
32 }
33 else
34 {
35 using std::ilogb;
36 return ilogb(arg);
37 }
38 }
39
40 template <typename Z, std::enable_if_t<std::is_integral_v<Z>, bool> = true>
41 inline constexpr int ilogb(Z arg) noexcept
42 {
43 return boost::math::ccmath::ilogb(static_cast<double>(arg));
44 }
45
46 inline constexpr int ilogbf(float arg) noexcept
47 {
48 return boost::math::ccmath::ilogb(arg);
49 }
50
51 #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
52 inline constexpr int ilogbl(long double arg) noexcept
53 {
54 return boost::math::ccmath::ilogb(arg);
55 }
56 #endif
57
58 } // Namespaces
59
60 #endif // BOOST_MATH_CCMATH_ILOGB_HPP