Michalis Spyrou | 75d4733 | 2019-11-05 17:46:49 +0000 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | d9eaf61 | 2020-07-08 11:12:57 +0100 | [diff] [blame^] | 2 | * Copyright (c) 2019 Arm Limited. |
Michalis Spyrou | 75d4733 | 2019-11-05 17:46:49 +0000 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: MIT |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to |
| 8 | * deal in the Software without restriction, including without limitation the |
| 9 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 10 | * sell copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in all |
| 14 | * copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | * SOFTWARE. |
| 23 | */ |
| 24 | #ifndef ARM_COMPUTE_UTILS_MATH_SAFE_OPS |
| 25 | #define ARM_COMPUTE_UTILS_MATH_SAFE_OPS |
| 26 | |
| 27 | #include "arm_compute/core/Error.h" |
| 28 | #include "arm_compute/core/utils/misc/Requires.h" |
| 29 | |
| 30 | namespace arm_compute |
| 31 | { |
| 32 | namespace utils |
| 33 | { |
| 34 | namespace math |
| 35 | { |
| 36 | /** Safe integer addition between two integers. In case of an overflow |
| 37 | * the numeric max limit is return. In case of an underflow numeric max |
| 38 | * limit is return. |
| 39 | * |
| 40 | * @tparam T Integer types to add |
| 41 | * |
| 42 | * @param[in] val_a First value to add |
| 43 | * @param[in] val_b Second value to add |
| 44 | * |
| 45 | * @return The addition result |
| 46 | */ |
| 47 | template <typename T, REQUIRES_TA(std::is_integral<T>::value)> |
| 48 | T safe_integer_add(T val_a, T val_b) |
| 49 | { |
| 50 | T result = 0; |
| 51 | |
| 52 | if((val_b > 0) && (val_a > std::numeric_limits<T>::max() - val_b)) |
| 53 | { |
| 54 | result = std::numeric_limits<T>::max(); |
| 55 | } |
| 56 | else if((val_b < 0) && (val_a < std::numeric_limits<T>::min() - val_b)) |
| 57 | { |
| 58 | result = std::numeric_limits<T>::min(); |
| 59 | } |
| 60 | else |
| 61 | { |
| 62 | result = val_a + val_b; |
| 63 | } |
| 64 | |
| 65 | return result; |
| 66 | } |
| 67 | |
| 68 | /** Safe integer subtraction between two integers. In case of an overflow |
| 69 | * the numeric max limit is return. In case of an underflow numeric max |
| 70 | * limit is return. |
| 71 | * |
| 72 | * @tparam T Integer types to subtract |
| 73 | * |
| 74 | * @param[in] val_a Value to subtract from |
| 75 | * @param[in] val_b Value to subtract |
| 76 | * |
| 77 | * @return The subtraction result |
| 78 | */ |
| 79 | template <typename T, REQUIRES_TA(std::is_integral<T>::value)> |
| 80 | T safe_integer_sub(T val_a, T val_b) |
| 81 | { |
| 82 | T result = 0; |
| 83 | |
| 84 | if((val_b < 0) && (val_a > std::numeric_limits<T>::max() + val_b)) |
| 85 | { |
| 86 | result = std::numeric_limits<T>::max(); |
| 87 | } |
| 88 | else if((val_b > 0) && (val_a < std::numeric_limits<T>::min() + val_b)) |
| 89 | { |
| 90 | result = std::numeric_limits<T>::min(); |
| 91 | } |
| 92 | else |
| 93 | { |
| 94 | result = val_a - val_b; |
| 95 | } |
| 96 | |
| 97 | return result; |
| 98 | } |
| 99 | |
| 100 | /** Safe integer multiplication between two integers. In case of an overflow |
| 101 | * the numeric max limit is return. In case of an underflow numeric max |
| 102 | * limit is return. |
| 103 | * |
| 104 | * @tparam T Integer types to multiply |
| 105 | * |
| 106 | * @param[in] val_a First value to multiply |
| 107 | * @param[in] val_b Second value to multiply |
| 108 | * |
| 109 | * @return The multiplication result |
| 110 | */ |
| 111 | template <typename T, REQUIRES_TA(std::is_integral<T>::value)> |
| 112 | T safe_integer_mul(T val_a, T val_b) |
| 113 | { |
| 114 | T result = 0; |
| 115 | |
| 116 | if(val_a > 0) |
| 117 | { |
| 118 | if((val_b > 0) && (val_a > (std::numeric_limits<T>::max() / val_b))) |
| 119 | { |
| 120 | result = std::numeric_limits<T>::max(); |
| 121 | } |
| 122 | else if(val_b < (std::numeric_limits<T>::min() / val_a)) |
| 123 | { |
| 124 | result = std::numeric_limits<T>::min(); |
| 125 | } |
| 126 | else |
| 127 | { |
| 128 | result = val_a * val_b; |
| 129 | } |
| 130 | } |
| 131 | else |
| 132 | { |
| 133 | if((val_b > 0) && (val_a < (std::numeric_limits<T>::min() / val_b))) |
| 134 | { |
| 135 | result = std::numeric_limits<T>::max(); |
| 136 | } |
| 137 | else if((val_a != 0) && (val_b < (std::numeric_limits<T>::max() / val_a))) |
| 138 | { |
| 139 | result = std::numeric_limits<T>::min(); |
| 140 | } |
| 141 | else |
| 142 | { |
| 143 | result = val_a * val_b; |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | return result; |
| 148 | } |
| 149 | |
| 150 | /** Safe integer division between two integers. In case of an overflow |
| 151 | * the numeric max limit is return. In case of an underflow numeric max |
| 152 | * limit is return. |
| 153 | * |
| 154 | * @tparam T Integer types to divide |
| 155 | * |
| 156 | * @param[in] val_a Dividend value |
| 157 | * @param[in] val_b Divisor value |
| 158 | * |
| 159 | * @return The quotient |
| 160 | */ |
| 161 | template <typename T, REQUIRES_TA(std::is_integral<T>::value)> |
| 162 | T safe_integer_div(T val_a, T val_b) |
| 163 | { |
| 164 | T result = 0; |
| 165 | |
| 166 | if((val_b == 0) || ((val_a == std::numeric_limits<T>::min()) && (val_b == -1))) |
| 167 | { |
| 168 | result = std::numeric_limits<T>::min(); |
| 169 | } |
| 170 | else |
| 171 | { |
| 172 | result = val_a / val_b; |
| 173 | } |
| 174 | |
| 175 | return result; |
| 176 | } |
| 177 | } // namespace cast |
| 178 | } // namespace utils |
| 179 | } // namespace arm_compute |
| 180 | #endif /* ARM_COMPUTE_UTILS_MATH_SAFE_OPS */ |