Michalis Spyrou | 75d4733 | 2019-11-05 17:46:49 +0000 | [diff] [blame] | 1 | /* |
Pablo Marquez Tello | 732c1b2 | 2023-03-29 11:42:30 +0100 | [diff] [blame] | 2 | * Copyright (c) 2019-2021, 2023 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" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 28 | |
Pablo Marquez Tello | 732c1b2 | 2023-03-29 11:42:30 +0100 | [diff] [blame] | 29 | #include "support/AclRequires.h" |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 30 | |
| 31 | #include <limits> |
Michalis Spyrou | 75d4733 | 2019-11-05 17:46:49 +0000 | [diff] [blame] | 32 | |
| 33 | namespace arm_compute |
| 34 | { |
| 35 | namespace utils |
| 36 | { |
| 37 | namespace math |
| 38 | { |
| 39 | /** Safe integer addition between two integers. In case of an overflow |
| 40 | * the numeric max limit is return. In case of an underflow numeric max |
| 41 | * limit is return. |
| 42 | * |
| 43 | * @tparam T Integer types to add |
| 44 | * |
| 45 | * @param[in] val_a First value to add |
| 46 | * @param[in] val_b Second value to add |
| 47 | * |
| 48 | * @return The addition result |
| 49 | */ |
Giorgio Arena | c5a6139 | 2021-01-06 15:13:08 +0000 | [diff] [blame] | 50 | template <typename T, ARM_COMPUTE_REQUIRES_TA(std::is_integral<T>::value)> |
Michalis Spyrou | 75d4733 | 2019-11-05 17:46:49 +0000 | [diff] [blame] | 51 | T safe_integer_add(T val_a, T val_b) |
| 52 | { |
| 53 | T result = 0; |
| 54 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 55 | if ((val_b > 0) && (val_a > std::numeric_limits<T>::max() - val_b)) |
Michalis Spyrou | 75d4733 | 2019-11-05 17:46:49 +0000 | [diff] [blame] | 56 | { |
| 57 | result = std::numeric_limits<T>::max(); |
| 58 | } |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 59 | else if ((val_b < 0) && (val_a < std::numeric_limits<T>::min() - val_b)) |
Michalis Spyrou | 75d4733 | 2019-11-05 17:46:49 +0000 | [diff] [blame] | 60 | { |
| 61 | result = std::numeric_limits<T>::min(); |
| 62 | } |
| 63 | else |
| 64 | { |
| 65 | result = val_a + val_b; |
| 66 | } |
| 67 | |
| 68 | return result; |
| 69 | } |
| 70 | |
| 71 | /** Safe integer subtraction between two integers. In case of an overflow |
| 72 | * the numeric max limit is return. In case of an underflow numeric max |
| 73 | * limit is return. |
| 74 | * |
| 75 | * @tparam T Integer types to subtract |
| 76 | * |
| 77 | * @param[in] val_a Value to subtract from |
| 78 | * @param[in] val_b Value to subtract |
| 79 | * |
| 80 | * @return The subtraction result |
| 81 | */ |
Giorgio Arena | c5a6139 | 2021-01-06 15:13:08 +0000 | [diff] [blame] | 82 | template <typename T, ARM_COMPUTE_REQUIRES_TA(std::is_integral<T>::value)> |
Michalis Spyrou | 75d4733 | 2019-11-05 17:46:49 +0000 | [diff] [blame] | 83 | T safe_integer_sub(T val_a, T val_b) |
| 84 | { |
| 85 | T result = 0; |
| 86 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 87 | if ((val_b < 0) && (val_a > std::numeric_limits<T>::max() + val_b)) |
Michalis Spyrou | 75d4733 | 2019-11-05 17:46:49 +0000 | [diff] [blame] | 88 | { |
| 89 | result = std::numeric_limits<T>::max(); |
| 90 | } |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 91 | else if ((val_b > 0) && (val_a < std::numeric_limits<T>::min() + val_b)) |
Michalis Spyrou | 75d4733 | 2019-11-05 17:46:49 +0000 | [diff] [blame] | 92 | { |
| 93 | result = std::numeric_limits<T>::min(); |
| 94 | } |
| 95 | else |
| 96 | { |
| 97 | result = val_a - val_b; |
| 98 | } |
| 99 | |
| 100 | return result; |
| 101 | } |
| 102 | |
| 103 | /** Safe integer multiplication between two integers. In case of an overflow |
| 104 | * the numeric max limit is return. In case of an underflow numeric max |
| 105 | * limit is return. |
| 106 | * |
| 107 | * @tparam T Integer types to multiply |
| 108 | * |
| 109 | * @param[in] val_a First value to multiply |
| 110 | * @param[in] val_b Second value to multiply |
| 111 | * |
| 112 | * @return The multiplication result |
| 113 | */ |
Giorgio Arena | c5a6139 | 2021-01-06 15:13:08 +0000 | [diff] [blame] | 114 | template <typename T, ARM_COMPUTE_REQUIRES_TA(std::is_integral<T>::value)> |
Michalis Spyrou | 75d4733 | 2019-11-05 17:46:49 +0000 | [diff] [blame] | 115 | T safe_integer_mul(T val_a, T val_b) |
| 116 | { |
| 117 | T result = 0; |
| 118 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 119 | if (val_a > 0) |
Michalis Spyrou | 75d4733 | 2019-11-05 17:46:49 +0000 | [diff] [blame] | 120 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 121 | if ((val_b > 0) && (val_a > (std::numeric_limits<T>::max() / val_b))) |
Michalis Spyrou | 75d4733 | 2019-11-05 17:46:49 +0000 | [diff] [blame] | 122 | { |
| 123 | result = std::numeric_limits<T>::max(); |
| 124 | } |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 125 | else if (val_b < (std::numeric_limits<T>::min() / val_a)) |
Michalis Spyrou | 75d4733 | 2019-11-05 17:46:49 +0000 | [diff] [blame] | 126 | { |
| 127 | result = std::numeric_limits<T>::min(); |
| 128 | } |
| 129 | else |
| 130 | { |
| 131 | result = val_a * val_b; |
| 132 | } |
| 133 | } |
| 134 | else |
| 135 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 136 | if ((val_b > 0) && (val_a < (std::numeric_limits<T>::min() / val_b))) |
Michalis Spyrou | 75d4733 | 2019-11-05 17:46:49 +0000 | [diff] [blame] | 137 | { |
| 138 | result = std::numeric_limits<T>::max(); |
| 139 | } |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 140 | else if ((val_a != 0) && (val_b < (std::numeric_limits<T>::max() / val_a))) |
Michalis Spyrou | 75d4733 | 2019-11-05 17:46:49 +0000 | [diff] [blame] | 141 | { |
| 142 | result = std::numeric_limits<T>::min(); |
| 143 | } |
| 144 | else |
| 145 | { |
| 146 | result = val_a * val_b; |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | return result; |
| 151 | } |
| 152 | |
| 153 | /** Safe integer division between two integers. In case of an overflow |
| 154 | * the numeric max limit is return. In case of an underflow numeric max |
| 155 | * limit is return. |
| 156 | * |
| 157 | * @tparam T Integer types to divide |
| 158 | * |
| 159 | * @param[in] val_a Dividend value |
| 160 | * @param[in] val_b Divisor value |
| 161 | * |
| 162 | * @return The quotient |
| 163 | */ |
Giorgio Arena | c5a6139 | 2021-01-06 15:13:08 +0000 | [diff] [blame] | 164 | template <typename T, ARM_COMPUTE_REQUIRES_TA(std::is_integral<T>::value)> |
Michalis Spyrou | 75d4733 | 2019-11-05 17:46:49 +0000 | [diff] [blame] | 165 | T safe_integer_div(T val_a, T val_b) |
| 166 | { |
| 167 | T result = 0; |
| 168 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 169 | if ((val_b == 0) || ((val_a == std::numeric_limits<T>::min()) && (val_b == -1))) |
Michalis Spyrou | 75d4733 | 2019-11-05 17:46:49 +0000 | [diff] [blame] | 170 | { |
| 171 | result = std::numeric_limits<T>::min(); |
| 172 | } |
| 173 | else |
| 174 | { |
| 175 | result = val_a / val_b; |
| 176 | } |
| 177 | |
| 178 | return result; |
| 179 | } |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 180 | } // namespace math |
Michalis Spyrou | 75d4733 | 2019-11-05 17:46:49 +0000 | [diff] [blame] | 181 | } // namespace utils |
| 182 | } // namespace arm_compute |
| 183 | #endif /* ARM_COMPUTE_UTILS_MATH_SAFE_OPS */ |