]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/math/ccmath/round.hpp
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / boost / boost / math / ccmath / round.hpp
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
6 #ifndef BOOST_MATH_CCMATH_ROUND_HPP
7 #define BOOST_MATH_CCMATH_ROUND_HPP
8
9 #include <cmath>
10 #include <type_traits>
11 #include <stdexcept>
12 #include <boost/math/tools/is_constant_evaluated.hpp>
13 #include <boost/math/ccmath/abs.hpp>
14 #include <boost/math/ccmath/isinf.hpp>
15 #include <boost/math/ccmath/isnan.hpp>
16 #include <boost/math/ccmath/modf.hpp>
17
18 namespace boost::math::ccmath {
19
20 namespace detail {
21
22 // Computes the nearest integer value to arg (in floating-point format),
23 // rounding halfway cases away from zero, regardless of the current rounding mode.
24 template <typename T>
25 inline constexpr T round_impl(T arg) noexcept
26 {
27 T iptr = 0;
28 const T x = boost::math::ccmath::modf(arg, &iptr);
29 constexpr T half = T(1)/2;
30
31 if(x >= half && iptr > 0)
32 {
33 return iptr + 1;
34 }
35 else if(boost::math::ccmath::abs(x) >= half && iptr < 0)
36 {
37 return iptr - 1;
38 }
39 else
40 {
41 return iptr;
42 }
43 }
44
45 template <typename ReturnType, typename T>
46 inline constexpr ReturnType int_round_impl(T arg)
47 {
48 const T rounded_arg = round_impl(arg);
49
50 if(rounded_arg > static_cast<T>((std::numeric_limits<ReturnType>::max)()))
51 {
52 if constexpr (std::is_same_v<ReturnType, long long>)
53 {
54 throw std::domain_error("Rounded value cannot be represented by a long long type without overflow");
55 }
56 else
57 {
58 throw std::domain_error("Rounded value cannot be represented by a long type without overflow");
59 }
60 }
61 else
62 {
63 return static_cast<ReturnType>(rounded_arg);
64 }
65 }
66
67 } // Namespace detail
68
69 template <typename Real, std::enable_if_t<!std::is_integral_v<Real>, bool> = true>
70 inline constexpr Real round(Real arg) noexcept
71 {
72 if(BOOST_MATH_IS_CONSTANT_EVALUATED(arg))
73 {
74 return boost::math::ccmath::abs(arg) == Real(0) ? arg :
75 boost::math::ccmath::isinf(arg) ? arg :
76 boost::math::ccmath::isnan(arg) ? arg :
77 boost::math::ccmath::detail::round_impl(arg);
78 }
79 else
80 {
81 using std::round;
82 return round(arg);
83 }
84 }
85
86 template <typename Z, std::enable_if_t<std::is_integral_v<Z>, bool> = true>
87 inline constexpr double round(Z arg) noexcept
88 {
89 return boost::math::ccmath::round(static_cast<double>(arg));
90 }
91
92 inline constexpr float roundf(float arg) noexcept
93 {
94 return boost::math::ccmath::round(arg);
95 }
96
97 #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
98 inline constexpr long double roundl(long double arg) noexcept
99 {
100 return boost::math::ccmath::round(arg);
101 }
102 #endif
103
104 template <typename Real, std::enable_if_t<!std::is_integral_v<Real>, bool> = true>
105 inline constexpr long lround(Real arg)
106 {
107 if(BOOST_MATH_IS_CONSTANT_EVALUATED(arg))
108 {
109 return boost::math::ccmath::abs(arg) == Real(0) ? 0l :
110 boost::math::ccmath::isinf(arg) ? 0l :
111 boost::math::ccmath::isnan(arg) ? 0l :
112 boost::math::ccmath::detail::int_round_impl<long>(arg);
113 }
114 else
115 {
116 using std::lround;
117 return lround(arg);
118 }
119 }
120
121 template <typename Z, std::enable_if_t<std::is_integral_v<Z>, bool> = true>
122 inline constexpr long lround(Z arg)
123 {
124 return boost::math::ccmath::lround(static_cast<double>(arg));
125 }
126
127 inline constexpr long lroundf(float arg)
128 {
129 return boost::math::ccmath::lround(arg);
130 }
131
132 #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
133 inline constexpr long lroundl(long double arg)
134 {
135 return boost::math::ccmath::lround(arg);
136 }
137 #endif
138
139 template <typename Real, std::enable_if_t<!std::is_integral_v<Real>, bool> = true>
140 inline constexpr long long llround(Real arg)
141 {
142 if(BOOST_MATH_IS_CONSTANT_EVALUATED(arg))
143 {
144 return boost::math::ccmath::abs(arg) == Real(0) ? 0ll :
145 boost::math::ccmath::isinf(arg) ? 0ll :
146 boost::math::ccmath::isnan(arg) ? 0ll :
147 boost::math::ccmath::detail::int_round_impl<long long>(arg);
148 }
149 else
150 {
151 using std::llround;
152 return llround(arg);
153 }
154 }
155
156 template <typename Z, std::enable_if_t<std::is_integral_v<Z>, bool> = true>
157 inline constexpr long llround(Z arg)
158 {
159 return boost::math::ccmath::llround(static_cast<double>(arg));
160 }
161
162 inline constexpr long long llroundf(float arg)
163 {
164 return boost::math::ccmath::llround(arg);
165 }
166
167 #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
168 inline constexpr long long llroundl(long double arg)
169 {
170 return boost::math::ccmath::llround(arg);
171 }
172 #endif
173
174 } // Namespaces
175
176 #endif // BOOST_MATH_CCMATH_ROUND_HPP