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