| 117 | */ |
| 118 | template <typename T, ARM_COMPUTE_REQUIRES_TA(std::is_integral<T>::value)> |
| 119 | T 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 |