xref: /aosp_15_r20/external/ComputeLibrary/support/Rounding.h (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1*c217d954SCole Faust /*
2*c217d954SCole Faust  * Copyright (c) 2018-2021 Arm Limited.
3*c217d954SCole Faust  *
4*c217d954SCole Faust  * SPDX-License-Identifier: MIT
5*c217d954SCole Faust  *
6*c217d954SCole Faust  * Permission is hereby granted, free of charge, to any person obtaining a copy
7*c217d954SCole Faust  * of this software and associated documentation files (the "Software"), to
8*c217d954SCole Faust  * deal in the Software without restriction, including without limitation the
9*c217d954SCole Faust  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10*c217d954SCole Faust  * sell copies of the Software, and to permit persons to whom the Software is
11*c217d954SCole Faust  * furnished to do so, subject to the following conditions:
12*c217d954SCole Faust  *
13*c217d954SCole Faust  * The above copyright notice and this permission notice shall be included in all
14*c217d954SCole Faust  * copies or substantial portions of the Software.
15*c217d954SCole Faust  *
16*c217d954SCole Faust  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17*c217d954SCole Faust  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18*c217d954SCole Faust  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19*c217d954SCole Faust  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20*c217d954SCole Faust  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21*c217d954SCole Faust  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22*c217d954SCole Faust  * SOFTWARE.
23*c217d954SCole Faust  */
24*c217d954SCole Faust #ifndef ARM_COMPUTE_UTILS_ROUNDING_H
25*c217d954SCole Faust #define ARM_COMPUTE_UTILS_ROUNDING_H
26*c217d954SCole Faust 
27*c217d954SCole Faust #include "arm_compute/core/Error.h"
28*c217d954SCole Faust #include "arm_compute/core/utils/misc/Traits.h"
29*c217d954SCole Faust #include "support/Requires.h"
30*c217d954SCole Faust #include "support/ToolchainSupport.h"
31*c217d954SCole Faust 
32*c217d954SCole Faust #include <cmath>
33*c217d954SCole Faust 
34*c217d954SCole Faust namespace arm_compute
35*c217d954SCole Faust {
36*c217d954SCole Faust namespace utils
37*c217d954SCole Faust {
38*c217d954SCole Faust namespace rounding
39*c217d954SCole Faust {
40*c217d954SCole Faust /** Rounding mode */
41*c217d954SCole Faust enum class RoundingMode
42*c217d954SCole Faust {
43*c217d954SCole Faust     TO_ZERO,             /**< Round towards zero */
44*c217d954SCole Faust     AWAY_FROM_ZERO,      /**< Round away from zero */
45*c217d954SCole Faust     HALF_TO_ZERO,        /**< Round half towards from zero */
46*c217d954SCole Faust     HALF_AWAY_FROM_ZERO, /**< Round half away from zero */
47*c217d954SCole Faust     HALF_UP,             /**< Round half towards positive infinity */
48*c217d954SCole Faust     HALF_DOWN,           /**< Round half towards negative infinity */
49*c217d954SCole Faust     HALF_EVEN            /**< Round half towards nearest even */
50*c217d954SCole Faust };
51*c217d954SCole Faust 
52*c217d954SCole Faust /** Round floating-point value with round to zero
53*c217d954SCole Faust  *
54*c217d954SCole Faust  * @tparam T Parameter type. Should be of floating point type.
55*c217d954SCole Faust  *
56*c217d954SCole Faust  * @param[in] value floating-point value to be rounded.
57*c217d954SCole Faust  *
58*c217d954SCole Faust  * @return Floating-point value of rounded @p value.
59*c217d954SCole Faust  */
60*c217d954SCole Faust template <typename T, ARM_COMPUTE_REQUIRES_TA(traits::is_floating_point<T>::value)>
round_to_zero(T value)61*c217d954SCole Faust inline T round_to_zero(T value)
62*c217d954SCole Faust {
63*c217d954SCole Faust     T res = std::floor(std::fabs(value));
64*c217d954SCole Faust     return (value < 0.f) ? -res : res;
65*c217d954SCole Faust }
66*c217d954SCole Faust 
67*c217d954SCole Faust /** Round floating-point value with round away from zero
68*c217d954SCole Faust  *
69*c217d954SCole Faust  * @tparam T Parameter type. Should be of floating point type.
70*c217d954SCole Faust  *
71*c217d954SCole Faust  * @param[in] value floating-point value to be rounded.
72*c217d954SCole Faust  *
73*c217d954SCole Faust  * @return Floating-point value of rounded @p value.
74*c217d954SCole Faust  */
75*c217d954SCole Faust template <typename T, ARM_COMPUTE_REQUIRES_TA(traits::is_floating_point<T>::value)>
round_away_from_zero(T value)76*c217d954SCole Faust inline T round_away_from_zero(T value)
77*c217d954SCole Faust {
78*c217d954SCole Faust     T res = std::ceil(std::fabs(value));
79*c217d954SCole Faust     return (value < 0.f) ? -res : res;
80*c217d954SCole Faust }
81*c217d954SCole Faust 
82*c217d954SCole Faust /** Round floating-point value with half value rounding towards zero.
83*c217d954SCole Faust  *
84*c217d954SCole Faust  * @tparam T Parameter type. Should be of floating point type.
85*c217d954SCole Faust  *
86*c217d954SCole Faust  * @param[in] value floating-point value to be rounded.
87*c217d954SCole Faust  *
88*c217d954SCole Faust  * @return Floating-point value of rounded @p value.
89*c217d954SCole Faust  */
90*c217d954SCole Faust template <typename T, ARM_COMPUTE_REQUIRES_TA(traits::is_floating_point<T>::value)>
round_half_to_zero(T value)91*c217d954SCole Faust inline T round_half_to_zero(T value)
92*c217d954SCole Faust {
93*c217d954SCole Faust     T res = T(std::ceil(std::fabs(value) - 0.5f));
94*c217d954SCole Faust     return (value < 0.f) ? -res : res;
95*c217d954SCole Faust }
96*c217d954SCole Faust 
97*c217d954SCole Faust /** Round floating-point value with half value rounding away from zero.
98*c217d954SCole Faust  *
99*c217d954SCole Faust  * @tparam T Parameter type. Should be of floating point type.
100*c217d954SCole Faust  *
101*c217d954SCole Faust  * @param[in] value floating-point value to be rounded.
102*c217d954SCole Faust  *
103*c217d954SCole Faust  * @return Floating-point value of rounded @p value.
104*c217d954SCole Faust  */
105*c217d954SCole Faust template <typename T, ARM_COMPUTE_REQUIRES_TA(traits::is_floating_point<T>::value)>
round_half_away_from_zero(T value)106*c217d954SCole Faust inline T round_half_away_from_zero(T value)
107*c217d954SCole Faust {
108*c217d954SCole Faust     T res = T(std::floor(std::fabs(value) + 0.5f));
109*c217d954SCole Faust     return (value < 0.f) ? -res : res;
110*c217d954SCole Faust }
111*c217d954SCole Faust 
112*c217d954SCole Faust /** Round floating-point value with half value rounding to positive infinity.
113*c217d954SCole Faust  *
114*c217d954SCole Faust  * @tparam T Parameter type. Should be of floating point type.
115*c217d954SCole Faust  *
116*c217d954SCole Faust  * @param[in] value floating-point value to be rounded.
117*c217d954SCole Faust  *
118*c217d954SCole Faust  * @return Floating-point value of rounded @p value.
119*c217d954SCole Faust  */
120*c217d954SCole Faust template <typename T, ARM_COMPUTE_REQUIRES_TA(traits::is_floating_point<T>::value)>
round_half_up(T value)121*c217d954SCole Faust inline T round_half_up(T value)
122*c217d954SCole Faust {
123*c217d954SCole Faust     return std::floor(value + 0.5f);
124*c217d954SCole Faust }
125*c217d954SCole Faust 
126*c217d954SCole Faust /** Round floating-point value with half value rounding to negative infinity.
127*c217d954SCole Faust  *
128*c217d954SCole Faust  * @tparam T Parameter type. Should be of floating point type.
129*c217d954SCole Faust  *
130*c217d954SCole Faust  * @param[in] value floating-point value to be rounded.
131*c217d954SCole Faust  *
132*c217d954SCole Faust  * @return Floating-point value of rounded @p value.
133*c217d954SCole Faust  */
134*c217d954SCole Faust template <typename T, ARM_COMPUTE_REQUIRES_TA(traits::is_floating_point<T>::value)>
round_half_down(T value)135*c217d954SCole Faust inline T round_half_down(T value)
136*c217d954SCole Faust {
137*c217d954SCole Faust     return std::ceil(value - 0.5f);
138*c217d954SCole Faust }
139*c217d954SCole Faust 
140*c217d954SCole Faust /** Round floating-point value with half value rounding to nearest even.
141*c217d954SCole Faust  *
142*c217d954SCole Faust  * @tparam T Parameter type. Should be of floating point type.
143*c217d954SCole Faust  *
144*c217d954SCole Faust  * @param[in] value   floating-point value to be rounded.
145*c217d954SCole Faust  * @param[in] epsilon precision.
146*c217d954SCole Faust  *
147*c217d954SCole Faust  * @return Floating-point value of rounded @p value.
148*c217d954SCole Faust  */
149*c217d954SCole Faust template <typename T, ARM_COMPUTE_REQUIRES_TA(traits::is_floating_point<T>::value)>
150*c217d954SCole Faust inline T round_half_even(T value, T epsilon = std::numeric_limits<T>::epsilon())
151*c217d954SCole Faust {
152*c217d954SCole Faust     T positive_value = std::abs(value);
153*c217d954SCole Faust     T ipart          = 0;
154*c217d954SCole Faust     std::modf(positive_value, &ipart);
155*c217d954SCole Faust     // If 'value' is exactly halfway between two integers
156*c217d954SCole Faust     if(std::abs(positive_value - (ipart + 0.5f)) < epsilon)
157*c217d954SCole Faust     {
158*c217d954SCole Faust         // If 'ipart' is even then return 'ipart'
159*c217d954SCole Faust         if(std::fmod(ipart, 2.f) < epsilon)
160*c217d954SCole Faust         {
161*c217d954SCole Faust             return support::cpp11::copysign(ipart, value);
162*c217d954SCole Faust         }
163*c217d954SCole Faust         // Else return the nearest even integer
164*c217d954SCole Faust         return support::cpp11::copysign(std::ceil(ipart + 0.5f), value);
165*c217d954SCole Faust     }
166*c217d954SCole Faust     // Otherwise use the usual round to closest
167*c217d954SCole Faust     return support::cpp11::copysign(support::cpp11::round(positive_value), value);
168*c217d954SCole Faust }
169*c217d954SCole Faust 
170*c217d954SCole Faust /** Round floating-point value given a rounding mode
171*c217d954SCole Faust  *
172*c217d954SCole Faust  * @tparam T Parameter type. Should be of floating point type.
173*c217d954SCole Faust  *
174*c217d954SCole Faust  * @param[in] value         floating-point value to be rounded.
175*c217d954SCole Faust  * @param[in] rounding_mode Rounding mode to use.
176*c217d954SCole Faust  *
177*c217d954SCole Faust  * @return Floating-point value of rounded @p value.
178*c217d954SCole Faust  */
179*c217d954SCole Faust template <typename T, ARM_COMPUTE_REQUIRES_TA(traits::is_floating_point<T>::value)>
round(T value,RoundingMode rounding_mode)180*c217d954SCole Faust inline T round(T value, RoundingMode rounding_mode)
181*c217d954SCole Faust {
182*c217d954SCole Faust     switch(rounding_mode)
183*c217d954SCole Faust     {
184*c217d954SCole Faust         case RoundingMode::TO_ZERO:
185*c217d954SCole Faust             return round_to_zero(value);
186*c217d954SCole Faust         case RoundingMode::AWAY_FROM_ZERO:
187*c217d954SCole Faust             return round_away_from_zero(value);
188*c217d954SCole Faust         case RoundingMode::HALF_TO_ZERO:
189*c217d954SCole Faust             return round_half_to_zero(value);
190*c217d954SCole Faust         case RoundingMode::HALF_AWAY_FROM_ZERO:
191*c217d954SCole Faust             return round_half_away_from_zero(value);
192*c217d954SCole Faust         case RoundingMode::HALF_UP:
193*c217d954SCole Faust             return round_half_up(value);
194*c217d954SCole Faust         case RoundingMode::HALF_DOWN:
195*c217d954SCole Faust             return round_half_down(value);
196*c217d954SCole Faust         case RoundingMode::HALF_EVEN:
197*c217d954SCole Faust             return round_half_even(value);
198*c217d954SCole Faust         default:
199*c217d954SCole Faust             ARM_COMPUTE_ERROR("Unsupported rounding mode!");
200*c217d954SCole Faust     }
201*c217d954SCole Faust }
202*c217d954SCole Faust } // namespace rounding
203*c217d954SCole Faust } // namespace utils
204*c217d954SCole Faust } // namespace arm_compute
205*c217d954SCole Faust #endif /*ARM_COMPUTE_UTILS_ROUNDING_H */
206