blob: ef8bcf7e14ba41a0b3347a86d26cc43510d70766 [file] [log] [blame]
Michalis Spyrou75d47332019-11-05 17:46:49 +00001/*
Pablo Marquez Tello732c1b22023-03-29 11:42:30 +01002 * Copyright (c) 2019-2021, 2023 Arm Limited.
Michalis Spyrou75d47332019-11-05 17:46:49 +00003 *
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 Thomasmathibalanafd38f02023-09-27 17:46:17 +010028
Pablo Marquez Tello732c1b22023-03-29 11:42:30 +010029#include "support/AclRequires.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010030
31#include <limits>
Michalis Spyrou75d47332019-11-05 17:46:49 +000032
33namespace arm_compute
34{
35namespace utils
36{
37namespace 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 Arenac5a61392021-01-06 15:13:08 +000050template <typename T, ARM_COMPUTE_REQUIRES_TA(std::is_integral<T>::value)>
Michalis Spyrou75d47332019-11-05 17:46:49 +000051T safe_integer_add(T val_a, T val_b)
52{
53 T result = 0;
54
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010055 if ((val_b > 0) && (val_a > std::numeric_limits<T>::max() - val_b))
Michalis Spyrou75d47332019-11-05 17:46:49 +000056 {
57 result = std::numeric_limits<T>::max();
58 }
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010059 else if ((val_b < 0) && (val_a < std::numeric_limits<T>::min() - val_b))
Michalis Spyrou75d47332019-11-05 17:46:49 +000060 {
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 Arenac5a61392021-01-06 15:13:08 +000082template <typename T, ARM_COMPUTE_REQUIRES_TA(std::is_integral<T>::value)>
Michalis Spyrou75d47332019-11-05 17:46:49 +000083T safe_integer_sub(T val_a, T val_b)
84{
85 T result = 0;
86
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010087 if ((val_b < 0) && (val_a > std::numeric_limits<T>::max() + val_b))
Michalis Spyrou75d47332019-11-05 17:46:49 +000088 {
89 result = std::numeric_limits<T>::max();
90 }
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010091 else if ((val_b > 0) && (val_a < std::numeric_limits<T>::min() + val_b))
Michalis Spyrou75d47332019-11-05 17:46:49 +000092 {
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 Arenac5a61392021-01-06 15:13:08 +0000114template <typename T, ARM_COMPUTE_REQUIRES_TA(std::is_integral<T>::value)>
Michalis Spyrou75d47332019-11-05 17:46:49 +0000115T safe_integer_mul(T val_a, T val_b)
116{
117 T result = 0;
118
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100119 if (val_a > 0)
Michalis Spyrou75d47332019-11-05 17:46:49 +0000120 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100121 if ((val_b > 0) && (val_a > (std::numeric_limits<T>::max() / val_b)))
Michalis Spyrou75d47332019-11-05 17:46:49 +0000122 {
123 result = std::numeric_limits<T>::max();
124 }
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100125 else if (val_b < (std::numeric_limits<T>::min() / val_a))
Michalis Spyrou75d47332019-11-05 17:46:49 +0000126 {
127 result = std::numeric_limits<T>::min();
128 }
129 else
130 {
131 result = val_a * val_b;
132 }
133 }
134 else
135 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100136 if ((val_b > 0) && (val_a < (std::numeric_limits<T>::min() / val_b)))
Michalis Spyrou75d47332019-11-05 17:46:49 +0000137 {
138 result = std::numeric_limits<T>::max();
139 }
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100140 else if ((val_a != 0) && (val_b < (std::numeric_limits<T>::max() / val_a)))
Michalis Spyrou75d47332019-11-05 17:46:49 +0000141 {
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 Arenac5a61392021-01-06 15:13:08 +0000164template <typename T, ARM_COMPUTE_REQUIRES_TA(std::is_integral<T>::value)>
Michalis Spyrou75d47332019-11-05 17:46:49 +0000165T safe_integer_div(T val_a, T val_b)
166{
167 T result = 0;
168
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100169 if ((val_b == 0) || ((val_a == std::numeric_limits<T>::min()) && (val_b == -1)))
Michalis Spyrou75d47332019-11-05 17:46:49 +0000170 {
171 result = std::numeric_limits<T>::min();
172 }
173 else
174 {
175 result = val_a / val_b;
176 }
177
178 return result;
179}
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100180} // namespace math
Michalis Spyrou75d47332019-11-05 17:46:49 +0000181} // namespace utils
182} // namespace arm_compute
183#endif /* ARM_COMPUTE_UTILS_MATH_SAFE_OPS */