102 lines · plain
1// (C) Copyright Christopher Kormanyos 1999 - 2021.2// (C) Copyright Matt Borland 2021.3// Use, modification and distribution are subject to the4// Boost Software License, Version 1.0. (See accompanying file5// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)6 7#ifndef BOOST_MATH_CCMATH_FREXP_HPP8#define BOOST_MATH_CCMATH_FREXP_HPP9 10#include <boost/math/ccmath/detail/config.hpp>11 12#ifdef BOOST_MATH_NO_CCMATH13#error "The header <boost/math/frexp.hpp> can only be used in C++17 and later."14#endif15 16#include <boost/math/ccmath/isinf.hpp>17#include <boost/math/ccmath/isnan.hpp>18#include <boost/math/ccmath/isfinite.hpp>19 20namespace boost::math::ccmath {21 22namespace detail23{24 25template <typename Real>26inline constexpr Real frexp_zero_impl(Real arg, int* exp)27{28 *exp = 0;29 return arg;30}31 32template <typename Real>33inline 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 detail62 63template <typename Real, std::enable_if_t<!std::is_integral_v<Real>, bool> = true>64inline 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 else75 {76 using std::frexp;77 return frexp(arg, exp);78 }79}80 81template <typename Z, std::enable_if_t<std::is_integral_v<Z>, bool> = true>82inline constexpr double frexp(Z arg, int* exp)83{84 return boost::math::ccmath::frexp(static_cast<double>(arg), exp);85}86 87inline 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_FUNCTIONS93inline constexpr long double frexpl(long double arg, int* exp)94{95 return boost::math::ccmath::frexp(arg, exp);96}97#endif98 99}100 101#endif // BOOST_MATH_CCMATH_FREXP_HPP102