Compute Library
 21.02
arm_compute::utils::rounding Namespace Reference

Enumerations

enum  RoundingMode {
  TO_ZERO, AWAY_FROM_ZERO, HALF_TO_ZERO, HALF_AWAY_FROM_ZERO,
  HALF_UP, HALF_DOWN, HALF_EVEN
}
 Rounding mode. More...
 

Functions

template<typename T , ARM_COMPUTE_REQUIRES_TA(traits::is_floating_point< T >::value) >
round_to_zero (T value)
 Round floating-point value with round to zero. More...
 
template<typename T , ARM_COMPUTE_REQUIRES_TA(traits::is_floating_point< T >::value) >
round_away_from_zero (T value)
 Round floating-point value with round away from zero. More...
 
template<typename T , ARM_COMPUTE_REQUIRES_TA(traits::is_floating_point< T >::value) >
round_half_to_zero (T value)
 Round floating-point value with half value rounding towards zero. More...
 
template<typename T , ARM_COMPUTE_REQUIRES_TA(traits::is_floating_point< T >::value) >
round_half_away_from_zero (T value)
 Round floating-point value with half value rounding away from zero. More...
 
template<typename T , ARM_COMPUTE_REQUIRES_TA(traits::is_floating_point< T >::value) >
round_half_up (T value)
 Round floating-point value with half value rounding to positive infinity. More...
 
template<typename T , ARM_COMPUTE_REQUIRES_TA(traits::is_floating_point< T >::value) >
round_half_down (T value)
 Round floating-point value with half value rounding to negative infinity. More...
 
template<typename T , ARM_COMPUTE_REQUIRES_TA(traits::is_floating_point< T >::value) >
round_half_even (T value, T epsilon=std::numeric_limits< T >::epsilon())
 Round floating-point value with half value rounding to nearest even. More...
 
template<typename T , ARM_COMPUTE_REQUIRES_TA(traits::is_floating_point< T >::value) >
round (T value, RoundingMode rounding_mode)
 Round floating-point value given a rounding mode. More...
 

Enumeration Type Documentation

◆ RoundingMode

enum RoundingMode
strong

Rounding mode.

Enumerator
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.

Definition at line 41 of file Rounding.h.

42 {
43  TO_ZERO, /**< Round towards zero */
44  AWAY_FROM_ZERO, /**< Round away from zero */
45  HALF_TO_ZERO, /**< Round half towards from zero */
46  HALF_AWAY_FROM_ZERO, /**< Round half away from zero */
47  HALF_UP, /**< Round half towards positive infinity */
48  HALF_DOWN, /**< Round half towards negative infinity */
49  HALF_EVEN /**< Round half towards nearest even */
50 };
Round half towards negative infinity.
Round half towards positive infinity.
Truncates the least significant values that are lost in operations.

Function Documentation

◆ round()

T arm_compute::utils::rounding::round ( value,
RoundingMode  rounding_mode 
)
inline

Round floating-point value given a rounding mode.

Template Parameters
TParameter type. Should be of floating point type.
Parameters
[in]valuefloating-point value to be rounded.
[in]rounding_modeRounding mode to use.
Returns
Floating-point value of rounded value.

Definition at line 180 of file Rounding.h.

References ARM_COMPUTE_ERROR, AWAY_FROM_ZERO, HALF_AWAY_FROM_ZERO, HALF_DOWN, HALF_EVEN, HALF_TO_ZERO, HALF_UP, round_away_from_zero(), round_half_away_from_zero(), round_half_down(), round_half_even(), round_half_to_zero(), round_half_up(), round_to_zero(), and TO_ZERO.

181 {
182  switch(rounding_mode)
183  {
184  case RoundingMode::TO_ZERO:
185  return round_to_zero(value);
186  case RoundingMode::AWAY_FROM_ZERO:
187  return round_away_from_zero(value);
188  case RoundingMode::HALF_TO_ZERO:
189  return round_half_to_zero(value);
190  case RoundingMode::HALF_AWAY_FROM_ZERO:
191  return round_half_away_from_zero(value);
192  case RoundingMode::HALF_UP:
193  return round_half_up(value);
194  case RoundingMode::HALF_DOWN:
195  return round_half_down(value);
196  case RoundingMode::HALF_EVEN:
197  return round_half_even(value);
198  default:
199  ARM_COMPUTE_ERROR("Unsupported rounding mode!");
200  }
201 }
T round_half_down(T value)
Round floating-point value with half value rounding to negative infinity.
Definition: Rounding.h:135
#define ARM_COMPUTE_ERROR(msg)
Print the given message then throw an std::runtime_error.
Definition: Error.h:352
T round_half_up(T value)
Round floating-point value with half value rounding to positive infinity.
Definition: Rounding.h:121
T round_away_from_zero(T value)
Round floating-point value with round away from zero.
Definition: Rounding.h:76
T round_half_away_from_zero(T value)
Round floating-point value with half value rounding away from zero.
Definition: Rounding.h:106
T round_half_even(T value, T epsilon=std::numeric_limits< T >::epsilon())
Round floating-point value with half value rounding to nearest even.
Definition: Rounding.h:150
T round_half_to_zero(T value)
Round floating-point value with half value rounding towards zero.
Definition: Rounding.h:91
T round_to_zero(T value)
Round floating-point value with round to zero.
Definition: Rounding.h:61

◆ round_away_from_zero()

T arm_compute::utils::rounding::round_away_from_zero ( value)
inline

Round floating-point value with round away from zero.

Template Parameters
TParameter type. Should be of floating point type.
Parameters
[in]valuefloating-point value to be rounded.
Returns
Floating-point value of rounded value.

Definition at line 76 of file Rounding.h.

Referenced by round().

77 {
78  T res = std::ceil(std::fabs(value));
79  return (value < 0.f) ? -res : res;
80 }

◆ round_half_away_from_zero()

T arm_compute::utils::rounding::round_half_away_from_zero ( value)
inline

Round floating-point value with half value rounding away from zero.

Template Parameters
TParameter type. Should be of floating point type.
Parameters
[in]valuefloating-point value to be rounded.
Returns
Floating-point value of rounded value.

Definition at line 106 of file Rounding.h.

Referenced by NEScaleKernel::configure(), arm_compute::cpu::nearest_neon_scale(), round(), and arm_compute::test::validation::reference::scale_core().

107 {
108  T res = T(std::floor(std::fabs(value) + 0.5f));
109  return (value < 0.f) ? -res : res;
110 }

◆ round_half_down()

T arm_compute::utils::rounding::round_half_down ( value)
inline

Round floating-point value with half value rounding to negative infinity.

Template Parameters
TParameter type. Should be of floating point type.
Parameters
[in]valuefloating-point value to be rounded.
Returns
Floating-point value of rounded value.

Definition at line 135 of file Rounding.h.

Referenced by round().

136 {
137  return std::ceil(value - 0.5f);
138 }

◆ round_half_even()

T arm_compute::utils::rounding::round_half_even ( value,
epsilon = std::numeric_limits<T>::epsilon() 
)
inline

Round floating-point value with half value rounding to nearest even.

Template Parameters
TParameter type. Should be of floating point type.
Parameters
[in]valuefloating-point value to be rounded.
[in]epsilonprecision.
Returns
Floating-point value of rounded value.

Definition at line 150 of file Rounding.h.

References arm_compute::support::cpp11::copysign(), arm_compute::quantization::epsilon, and arm_compute::support::cpp11::round().

Referenced by round().

151 {
152  T positive_value = std::abs(value);
153  T ipart = 0;
154  std::modf(positive_value, &ipart);
155  // If 'value' is exactly halfway between two integers
156  if(std::abs(positive_value - (ipart + 0.5f)) < epsilon)
157  {
158  // If 'ipart' is even then return 'ipart'
159  if(std::fmod(ipart, 2.f) < epsilon)
160  {
161  return support::cpp11::copysign(ipart, value);
162  }
163  // Else return the nearest even integer
164  return support::cpp11::copysign(std::ceil(ipart + 0.5f), value);
165  }
166  // Otherwise use the usual round to closest
167  return support::cpp11::copysign(support::cpp11::round(positive_value), value);
168 }
T copysign(T x, T y)
Composes a floating point value with the magnitude of x and the sign of y.
T round(T value, RoundingMode rounding_mode)
Round floating-point value given a rounding mode.
Definition: Rounding.h:180

◆ round_half_to_zero()

T arm_compute::utils::rounding::round_half_to_zero ( value)
inline

Round floating-point value with half value rounding towards zero.

Template Parameters
TParameter type. Should be of floating point type.
Parameters
[in]valuefloating-point value to be rounded.
Returns
Floating-point value of rounded value.

Definition at line 91 of file Rounding.h.

Referenced by round().

92 {
93  T res = T(std::ceil(std::fabs(value) - 0.5f));
94  return (value < 0.f) ? -res : res;
95 }

◆ round_half_up()

T arm_compute::utils::rounding::round_half_up ( value)
inline

Round floating-point value with half value rounding to positive infinity.

Template Parameters
TParameter type. Should be of floating point type.
Parameters
[in]valuefloating-point value to be rounded.
Returns
Floating-point value of rounded value.

Definition at line 121 of file Rounding.h.

Referenced by round().

122 {
123  return std::floor(value + 0.5f);
124 }

◆ round_to_zero()

T arm_compute::utils::rounding::round_to_zero ( value)
inline

Round floating-point value with round to zero.

Template Parameters
TParameter type. Should be of floating point type.
Parameters
[in]valuefloating-point value to be rounded.
Returns
Floating-point value of rounded value.

Definition at line 61 of file Rounding.h.

Referenced by round().

62 {
63  T res = std::floor(std::fabs(value));
64  return (value < 0.f) ? -res : res;
65 }