| 1 |
// (C) Copyright Christopher Kormanyos 1999 - 2021. |
| 2 |
// (C) Copyright Matt Borland 2021. |
| 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_CCMATH_FREXP_HPP |
| 8 |
#define BOOST_MATH_CCMATH_FREXP_HPP |
| 9 |
|
| 10 |
#include <boost/math/ccmath/detail/config.hpp> |
| 11 |
|
| 12 |
#ifdef BOOST_MATH_NO_CCMATH |
| 13 |
#error "The header <boost/math/frexp.hpp> can only be used in C++17 and later." |
| 14 |
#endif |
| 15 |
|
| 16 |
#include <boost/math/ccmath/isinf.hpp> |
| 17 |
#include <boost/math/ccmath/isnan.hpp> |
| 18 |
#include <boost/math/ccmath/isfinite.hpp> |
| 19 |
|
| 20 |
namespace boost::math::ccmath { |
| 21 |
|
| 22 |
namespace detail |
| 23 |
{ |
| 24 |
|
| 25 |
template <typename Real> |
| 26 |
inline constexpr Real frexp_zero_impl(Real arg, int* exp) |
| 27 |
{ |
| 28 |
*exp = 0; |
| 29 |
return arg; |
| 30 |
} |
| 31 |
|
| 32 |
template <typename Real> |
| 33 |
inline constexpr Real frexp_impl(Real arg, int* exp) |
| 34 |
{ |
| 35 |
const bool negative_arg = (arg < Real(0)); |
| 36 |
|
| 37 |
Real f = negative_arg ? -arg : arg; |
| 38 |
int e2 = 0; |
| 39 |
constexpr Real two_pow_32 = Real(4294967296); |
| 40 |
|
| 41 |
while (f >= two_pow_32) |
| 42 |
{ |
| 43 |
f = f / two_pow_32; |
| 44 |
e2 += 32; |
| 45 |
} |
| 46 |
|
| 47 |
while(f >= Real(1)) |
| 48 |
{ |
| 49 |
f = f / Real(2); |
| 50 |
++e2; |
| 51 |
} |
| 52 |
|
| 53 |
if(exp != nullptr) |
| 54 |
{ |
| 55 |
*exp = e2; |
| 56 |
} |
| 57 |
|
| 58 |
return !negative_arg ? f : -f; |
| 59 |
} |
| 60 |
|
| 61 |
} // namespace detail |
| 62 |
|
| 63 |
template <typename Real, std::enable_if_t<!std::is_integral_v<Real>, bool> = true> |
| 64 |
inline constexpr Real frexp(Real arg, int* exp) |
| 65 |
{ |
| 66 |
if(BOOST_MATH_IS_CONSTANT_EVALUATED(arg)) |
| 67 |
{ |
| 68 |
return arg == Real(0) ? detail::frexp_zero_impl(arg, exp) : |
| 69 |
arg == Real(-0) ? detail::frexp_zero_impl(arg, exp) : |
| 70 |
boost::math::ccmath::isinf(arg) ? detail::frexp_zero_impl(arg, exp) : |
| 71 |
boost::math::ccmath::isnan(arg) ? detail::frexp_zero_impl(arg, exp) : |
| 72 |
boost::math::ccmath::detail::frexp_impl(arg, exp); |
| 73 |
} |
| 74 |
else |
| 75 |
{ |
| 76 |
using std::frexp; |
| 77 |
return frexp(arg, exp); |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
template <typename Z, std::enable_if_t<std::is_integral_v<Z>, bool> = true> |
| 82 |
inline constexpr double frexp(Z arg, int* exp) |
| 83 |
{ |
| 84 |
return boost::math::ccmath::frexp(static_cast<double>(arg), exp); |
| 85 |
} |
| 86 |
|
| 87 |
inline constexpr float frexpf(float arg, int* exp) |
| 88 |
{ |
| 89 |
return boost::math::ccmath::frexp(arg, exp); |
| 90 |
} |
| 91 |
|
| 92 |
#ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS |
| 93 |
inline constexpr long double frexpl(long double arg, int* exp) |
| 94 |
{ |
| 95 |
return boost::math::ccmath::frexp(arg, exp); |
| 96 |
} |
| 97 |
#endif |
| 98 |
|
| 99 |
} |
| 100 |
|
| 101 |
#endif // BOOST_MATH_CCMATH_FREXP_HPP |