MCPcopy Create free account
hub / github.com/ARM-software/ComputeLibrary / safe_integer_mul

Function safe_integer_mul

arm_compute/core/utils/math/SafeOps.h:119–155  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

117 */
118template <typename T, ARM_COMPUTE_REQUIRES_TA(std::is_integral<T>::value)>
119T safe_integer_mul(T val_a, T val_b)
120{
121 T result = 0;
122
123 if (val_a > 0)
124 {
125 if ((val_b > 0) && (val_a > (std::numeric_limits<T>::max() / val_b)))
126 {
127 result = std::numeric_limits<T>::max();
128 }
129 else if (val_b < (std::numeric_limits<T>::min() / val_a))
130 {
131 result = std::numeric_limits<T>::min();
132 }
133 else
134 {
135 result = val_a * val_b;
136 }
137 }
138 else
139 {
140 if ((val_b > 0) && (val_a < (std::numeric_limits<T>::min() / val_b)))
141 {
142 result = std::numeric_limits<T>::max();
143 }
144 else if ((val_a != 0) && (val_b < (std::numeric_limits<T>::max() / val_a)))
145 {
146 result = std::numeric_limits<T>::min();
147 }
148 else
149 {
150 result = val_a * val_b;
151 }
152 }
153
154 return result;
155}
156
157/** Safe integer division between two integers. In case of an overflow
158 * the numeric max limit is return. In case of an underflow numeric max

Callers 1

TEST_CASEFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected