ilogb.hpp 1.8 KB

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