Compute Library
 22.08
arm_compute::utils::math Namespace Reference

Functions

template<typename T , typename = typename std::enable_if<( std::is_integral<T>::value ), arm_compute::utils::requires::detail::enabler>::type>
safe_integer_add (T val_a, T val_b)
 Safe integer addition between two integers. More...
 
template<typename T , typename = typename std::enable_if<( std::is_integral<T>::value ), arm_compute::utils::requires::detail::enabler>::type>
safe_integer_sub (T val_a, T val_b)
 Safe integer subtraction between two integers. More...
 
template<typename T , typename = typename std::enable_if<( std::is_integral<T>::value ), arm_compute::utils::requires::detail::enabler>::type>
safe_integer_mul (T val_a, T val_b)
 Safe integer multiplication between two integers. More...
 
template<typename T , typename = typename std::enable_if<( std::is_integral<T>::value ), arm_compute::utils::requires::detail::enabler>::type>
safe_integer_div (T val_a, T val_b)
 Safe integer division between two integers. More...
 

Function Documentation

◆ safe_integer_add()

T arm_compute::utils::math::safe_integer_add ( val_a,
val_b 
)

Safe integer addition between two integers.

In case of an overflow the numeric max limit is return. In case of an underflow numeric max limit is return.

Template Parameters
TInteger types to add
Parameters
[in]val_aFirst value to add
[in]val_bSecond value to add
Returns
The addition result

Definition at line 50 of file SafeOps.h.

Referenced by arm_compute::test::validation::TEST_CASE().

51 {
52  T result = 0;
53 
54  if((val_b > 0) && (val_a > std::numeric_limits<T>::max() - val_b))
55  {
56  result = std::numeric_limits<T>::max();
57  }
58  else if((val_b < 0) && (val_a < std::numeric_limits<T>::min() - val_b))
59  {
60  result = std::numeric_limits<T>::min();
61  }
62  else
63  {
64  result = val_a + val_b;
65  }
66 
67  return result;
68 }

◆ safe_integer_div()

T arm_compute::utils::math::safe_integer_div ( val_a,
val_b 
)

Safe integer division between two integers.

In case of an overflow the numeric max limit is return. In case of an underflow numeric max limit is return.

Template Parameters
TInteger types to divide
Parameters
[in]val_aDividend value
[in]val_bDivisor value
Returns
The quotient

Definition at line 164 of file SafeOps.h.

Referenced by arm_compute::test::validation::TEST_CASE().

165 {
166  T result = 0;
167 
168  if((val_b == 0) || ((val_a == std::numeric_limits<T>::min()) && (val_b == -1)))
169  {
170  result = std::numeric_limits<T>::min();
171  }
172  else
173  {
174  result = val_a / val_b;
175  }
176 
177  return result;
178 }

◆ safe_integer_mul()

T arm_compute::utils::math::safe_integer_mul ( val_a,
val_b 
)

Safe integer multiplication between two integers.

In case of an overflow the numeric max limit is return. In case of an underflow numeric max limit is return.

Template Parameters
TInteger types to multiply
Parameters
[in]val_aFirst value to multiply
[in]val_bSecond value to multiply
Returns
The multiplication result

Definition at line 114 of file SafeOps.h.

Referenced by arm_compute::test::validation::TEST_CASE().

115 {
116  T result = 0;
117 
118  if(val_a > 0)
119  {
120  if((val_b > 0) && (val_a > (std::numeric_limits<T>::max() / val_b)))
121  {
122  result = std::numeric_limits<T>::max();
123  }
124  else if(val_b < (std::numeric_limits<T>::min() / val_a))
125  {
126  result = std::numeric_limits<T>::min();
127  }
128  else
129  {
130  result = val_a * val_b;
131  }
132  }
133  else
134  {
135  if((val_b > 0) && (val_a < (std::numeric_limits<T>::min() / val_b)))
136  {
137  result = std::numeric_limits<T>::max();
138  }
139  else if((val_a != 0) && (val_b < (std::numeric_limits<T>::max() / val_a)))
140  {
141  result = std::numeric_limits<T>::min();
142  }
143  else
144  {
145  result = val_a * val_b;
146  }
147  }
148 
149  return result;
150 }

◆ safe_integer_sub()

T arm_compute::utils::math::safe_integer_sub ( val_a,
val_b 
)

Safe integer subtraction between two integers.

In case of an overflow the numeric max limit is return. In case of an underflow numeric max limit is return.

Template Parameters
TInteger types to subtract
Parameters
[in]val_aValue to subtract from
[in]val_bValue to subtract
Returns
The subtraction result

Definition at line 82 of file SafeOps.h.

Referenced by arm_compute::test::validation::TEST_CASE().

83 {
84  T result = 0;
85 
86  if((val_b < 0) && (val_a > std::numeric_limits<T>::max() + val_b))
87  {
88  result = std::numeric_limits<T>::max();
89  }
90  else if((val_b > 0) && (val_a < std::numeric_limits<T>::min() + val_b))
91  {
92  result = std::numeric_limits<T>::min();
93  }
94  else
95  {
96  result = val_a - val_b;
97  }
98 
99  return result;
100 }