Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 09:45:14

0001 //  (C) Copyright Matt Borland 2021.
0002 //  Use, modification and distribution are subject to the
0003 //  Boost Software License, Version 1.0. (See accompanying file
0004 //  LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0005 
0006 #ifndef BOOST_MATH_CCMATH_FLOOR_HPP
0007 #define BOOST_MATH_CCMATH_FLOOR_HPP
0008 
0009 #include <boost/math/ccmath/detail/config.hpp>
0010 
0011 #ifdef BOOST_MATH_NO_CCMATH
0012 #error "The header <boost/math/floor.hpp> can only be used in C++17 and later."
0013 #endif
0014 
0015 #include <boost/math/ccmath/abs.hpp>
0016 #include <boost/math/ccmath/isinf.hpp>
0017 #include <boost/math/ccmath/isnan.hpp>
0018 
0019 namespace boost::math::ccmath {
0020 
0021 namespace detail {
0022 
0023 template <typename T>
0024 inline constexpr T floor_pos_impl(T arg) noexcept
0025 {
0026     T result = 1;
0027 
0028     if(result < arg)
0029     {
0030         while(result < arg)
0031         {
0032             result *= 2;
0033         }
0034         while(result > arg)
0035         {
0036             --result;
0037         }
0038 
0039         return result;
0040     }
0041     else
0042     {
0043         return T(0);
0044     }
0045 }
0046 
0047 template <typename T>
0048 inline constexpr T floor_neg_impl(T arg) noexcept
0049 {
0050     T result = -1;
0051 
0052     if(result > arg)
0053     {
0054         while(result > arg)
0055         {
0056             result *= 2;
0057         }
0058         while(result < arg)
0059         {
0060             ++result;
0061         }
0062         if(result != arg)
0063         {
0064             --result;
0065         }
0066     }
0067 
0068     return result;
0069 }
0070 
0071 template <typename T>
0072 inline constexpr T floor_impl(T arg) noexcept
0073 {
0074     if(arg > 0)
0075     {
0076         return floor_pos_impl(arg);
0077     }
0078     else
0079     {
0080         return floor_neg_impl(arg);
0081     }
0082 }
0083 
0084 } // Namespace detail
0085 
0086 template <typename Real, std::enable_if_t<!std::is_integral_v<Real>, bool> = true>
0087 inline constexpr Real floor(Real arg) noexcept
0088 {
0089     if(BOOST_MATH_IS_CONSTANT_EVALUATED(arg))
0090     {
0091         return boost::math::ccmath::abs(arg) == Real(0) ? arg :
0092                boost::math::ccmath::isinf(arg) ? arg :
0093                boost::math::ccmath::isnan(arg) ? arg :
0094                boost::math::ccmath::detail::floor_impl(arg);
0095     }
0096     else
0097     {
0098         using std::floor;
0099         return floor(arg);
0100     }
0101 }
0102 
0103 template <typename Z, std::enable_if_t<std::is_integral_v<Z>, bool> = true>
0104 inline constexpr double floor(Z arg) noexcept
0105 {
0106     return boost::math::ccmath::floor(static_cast<double>(arg));
0107 }
0108 
0109 inline constexpr float floorf(float arg) noexcept
0110 {
0111     return boost::math::ccmath::floor(arg);
0112 }
0113 
0114 #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
0115 inline constexpr long double floorl(long double arg) noexcept
0116 {
0117     return boost::math::ccmath::floor(arg);
0118 }
0119 #endif
0120 
0121 } // Namespaces
0122 
0123 #endif // BOOST_MATH_CCMATH_FLOOR_HPP