File indexing completed on 2025-01-30 09:45:15
0001
0002
0003
0004
0005
0006
0007 #ifndef BOOST_MATH_CCMATH_FREXP_HPP
0008 #define BOOST_MATH_CCMATH_FREXP_HPP
0009
0010 #include <boost/math/ccmath/detail/config.hpp>
0011
0012 #ifdef BOOST_MATH_NO_CCMATH
0013 #error "The header <boost/math/frexp.hpp> can only be used in C++17 and later."
0014 #endif
0015
0016 #include <boost/math/ccmath/isinf.hpp>
0017 #include <boost/math/ccmath/isnan.hpp>
0018 #include <boost/math/ccmath/isfinite.hpp>
0019
0020 namespace boost::math::ccmath {
0021
0022 namespace detail
0023 {
0024
0025 template <typename Real>
0026 inline constexpr Real frexp_zero_impl(Real arg, int* exp)
0027 {
0028 *exp = 0;
0029 return arg;
0030 }
0031
0032 template <typename Real>
0033 inline constexpr Real frexp_impl(Real arg, int* exp)
0034 {
0035 const bool negative_arg = (arg < Real(0));
0036
0037 Real f = negative_arg ? -arg : arg;
0038 int e2 = 0;
0039 constexpr Real two_pow_32 = Real(4294967296);
0040
0041 while (f >= two_pow_32)
0042 {
0043 f = f / two_pow_32;
0044 e2 += 32;
0045 }
0046
0047 while(f >= Real(1))
0048 {
0049 f = f / Real(2);
0050 ++e2;
0051 }
0052
0053 if(exp != nullptr)
0054 {
0055 *exp = e2;
0056 }
0057
0058 return !negative_arg ? f : -f;
0059 }
0060
0061 }
0062
0063 template <typename Real, std::enable_if_t<!std::is_integral_v<Real>, bool> = true>
0064 inline constexpr Real frexp(Real arg, int* exp)
0065 {
0066 if(BOOST_MATH_IS_CONSTANT_EVALUATED(arg))
0067 {
0068 return arg == Real(0) ? detail::frexp_zero_impl(arg, exp) :
0069 arg == Real(-0) ? detail::frexp_zero_impl(arg, exp) :
0070 boost::math::ccmath::isinf(arg) ? detail::frexp_zero_impl(arg, exp) :
0071 boost::math::ccmath::isnan(arg) ? detail::frexp_zero_impl(arg, exp) :
0072 boost::math::ccmath::detail::frexp_impl(arg, exp);
0073 }
0074 else
0075 {
0076 using std::frexp;
0077 return frexp(arg, exp);
0078 }
0079 }
0080
0081 template <typename Z, std::enable_if_t<std::is_integral_v<Z>, bool> = true>
0082 inline constexpr double frexp(Z arg, int* exp)
0083 {
0084 return boost::math::ccmath::frexp(static_cast<double>(arg), exp);
0085 }
0086
0087 inline constexpr float frexpf(float arg, int* exp)
0088 {
0089 return boost::math::ccmath::frexp(arg, exp);
0090 }
0091
0092 #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
0093 inline constexpr long double frexpl(long double arg, int* exp)
0094 {
0095 return boost::math::ccmath::frexp(arg, exp);
0096 }
0097 #endif
0098
0099 }
0100
0101 #endif