ilogb.hpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. #ifndef BOOST_MATH_CCMATH_ILOGB_HPP
  6. #define BOOST_MATH_CCMATH_ILOGB_HPP
  7. #include <cmath>
  8. #include <type_traits>
  9. #include <boost/math/tools/is_constant_evaluated.hpp>
  10. #include <boost/math/ccmath/logb.hpp>
  11. #include <boost/math/ccmath/isinf.hpp>
  12. #include <boost/math/ccmath/isnan.hpp>
  13. #include <boost/math/ccmath/abs.hpp>
  14. namespace boost::math::ccmath {
  15. // If arg is not zero, infinite, or NaN, the value returned is exactly equivalent to static_cast<int>(std::logb(arg))
  16. template <typename Real, std::enable_if_t<!std::is_integral_v<Real>, bool> = true>
  17. inline constexpr int ilogb(Real arg) noexcept
  18. {
  19. if(BOOST_MATH_IS_CONSTANT_EVALUATED(arg))
  20. {
  21. return boost::math::ccmath::abs(arg) == Real(0) ? FP_ILOGB0 :
  22. boost::math::ccmath::isinf(arg) ? INT_MAX :
  23. boost::math::ccmath::isnan(arg) ? FP_ILOGBNAN :
  24. static_cast<int>(boost::math::ccmath::logb(arg));
  25. }
  26. else
  27. {
  28. using std::ilogb;
  29. return ilogb(arg);
  30. }
  31. }
  32. template <typename Z, std::enable_if_t<std::is_integral_v<Z>, bool> = true>
  33. inline constexpr int ilogb(Z arg) noexcept
  34. {
  35. return boost::math::ccmath::ilogb(static_cast<double>(arg));
  36. }
  37. inline constexpr int ilogbf(float arg) noexcept
  38. {
  39. return boost::math::ccmath::ilogb(arg);
  40. }
  41. #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
  42. inline constexpr int ilogbl(long double arg) noexcept
  43. {
  44. return boost::math::ccmath::ilogb(arg);
  45. }
  46. #endif
  47. } // Namespaces
  48. #endif // BOOST_MATH_CCMATH_ILOGB_HPP