Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 09:45:15

0001 //  (C) Copyright Matt Borland 2021.
0002 //  Use, modification and distribution are subject to the
0003 //  Boost Software License, Version 1.0. (See accompanying file
0004 //  LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0005 
0006 #ifndef BOOST_MATH_CCMATH_LOGB_HPP
0007 #define BOOST_MATH_CCMATH_LOGB_HPP
0008 
0009 #include <boost/math/ccmath/detail/config.hpp>
0010 
0011 #ifdef BOOST_MATH_NO_CCMATH
0012 #error "The header <boost/math/logb.hpp> can only be used in C++17 and later."
0013 #endif
0014 
0015 #include <boost/math/ccmath/frexp.hpp>
0016 #include <boost/math/ccmath/isinf.hpp>
0017 #include <boost/math/ccmath/isnan.hpp>
0018 #include <boost/math/ccmath/abs.hpp>
0019 
0020 namespace boost::math::ccmath {
0021 
0022 namespace detail {
0023 
0024 // The value of the exponent returned by std::logb is always 1 less than the exponent returned by 
0025 // std::frexp because of the different normalization requirements: for the exponent e returned by std::logb, 
0026 // |arg*r^-e| is between 1 and r (typically between 1 and 2), but for the exponent e returned by std::frexp, 
0027 // |arg*2^-e| is between 0.5 and 1. 
0028 template <typename T>
0029 constexpr T logb_impl(T arg) noexcept
0030 {
0031     int exp = 0;
0032     boost::math::ccmath::frexp(arg, &exp);
0033 
0034     return static_cast<T>(exp - 1);
0035 }
0036 
0037 } // Namespace detail
0038 
0039 template <typename Real, std::enable_if_t<!std::is_integral_v<Real>, bool> = true>
0040 constexpr Real logb(Real arg) noexcept
0041 {
0042     if(BOOST_MATH_IS_CONSTANT_EVALUATED(arg))
0043     {
0044         if (boost::math::ccmath::abs(arg) == Real(0))
0045         {
0046             return -std::numeric_limits<Real>::infinity();
0047         }
0048         else if (boost::math::ccmath::isinf(arg))
0049         {
0050             return std::numeric_limits<Real>::infinity();
0051         }
0052         else if (boost::math::ccmath::isnan(arg))
0053         {
0054             return arg;
0055         }
0056         
0057         return boost::math::ccmath::detail::logb_impl(arg);
0058     }
0059     else
0060     {
0061         using std::logb;
0062         return logb(arg);
0063     }
0064 }
0065 
0066 template <typename Z, std::enable_if_t<std::is_integral_v<Z>, bool> = true>
0067 constexpr double logb(Z arg) noexcept
0068 {
0069     return boost::math::ccmath::logb(static_cast<double>(arg));
0070 }
0071 
0072 constexpr float logbf(float arg) noexcept
0073 {
0074     return boost::math::ccmath::logb(arg);
0075 }
0076 
0077 #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
0078 constexpr long double logbl(long double arg) noexcept
0079 {
0080     return boost::math::ccmath::logb(arg);
0081 }
0082 #endif
0083 
0084 } // Namespaces
0085 
0086 #endif // BOOST_MATH_CCMATH_LOGB_HPP