2 * Copyright (c) 2017 ARM Limited.
4 * SPDX-License-Identifier: MIT
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:
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
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
24 #include "arm_compute/core/Error.h"
31 template <typename TpIn, typename TpSat>
32 inline TpSat saturate_convert(TpIn a)
34 if(a > std::numeric_limits<TpSat>::max())
36 a = std::numeric_limits<TpSat>::max();
38 if(a < std::numeric_limits<TpSat>::min())
40 a = std::numeric_limits<TpSat>::min();
42 return static_cast<TpSat>(a);
48 inline qint8_t sqshl_qs8(qint8_t a, int shift)
50 qint16_t tmp = static_cast<qint16_t>(a) << shift;
52 // Saturate the result in case of overflow and cast to qint8_t
53 return saturate_convert<qint16_t, qint8_t>(tmp);
56 inline qint16_t sqshl_qs16(qint16_t a, int shift)
58 qint32_t tmp = static_cast<qint32_t>(a) << shift;
60 // Saturate the result in case of overflow and cast to qint16_t
61 return saturate_convert<qint32_t, qint16_t>(tmp);
64 inline qint8_t sshr_qs8(qint8_t a, int shift)
66 ARM_COMPUTE_ERROR_ON_MSG(shift == 0, "Shift should not be zero");
67 const qint8_t round_val = 1 << (shift - 1);
68 return sqadd_qs8(a, round_val) >> shift;
71 inline qint16_t sshr_qs16(qint16_t a, int shift)
73 ARM_COMPUTE_ERROR_ON_MSG(shift == 0, "Shift should not be zero");
74 const qint16_t round_val = 1 << (shift - 1);
75 return sqadd_qs16(a, round_val) >> shift;
78 inline qint8_t sabs_qs8(qint8_t a)
80 return (a < 0) ? (a == std::numeric_limits<int8_t>::min()) ? std::numeric_limits<int8_t>::max() : -a : a;
83 inline qint16_t sabs_qs16(qint16_t a)
85 return (a < 0) ? (a == std::numeric_limits<int16_t>::min()) ? std::numeric_limits<int16_t>::max() : -a : a;
88 inline qint8_t sadd_qs8(qint8_t a, qint8_t b)
93 inline qint16_t sadd_qs16(qint16_t a, qint16_t b)
98 inline qint8_t sqadd_qs8(qint8_t a, qint8_t b)
100 // We need to store the temporary result in qint16_t otherwise we cannot evaluate the overflow
101 qint16_t tmp = (static_cast<qint16_t>(a) + static_cast<qint16_t>(b));
103 // Saturate the result in case of overflow and cast to qint8_t
104 return saturate_convert<qint16_t, qint8_t>(tmp);
107 inline qint16_t sqadd_qs16(qint16_t a, qint16_t b)
109 // We need to store the temporary result in qint32_t otherwise we cannot evaluate the overflow
110 qint32_t tmp = (static_cast<qint32_t>(a) + static_cast<qint32_t>(b));
112 // Saturate the result in case of overflow and cast to qint16_t
113 return saturate_convert<qint32_t, qint16_t>(tmp);
116 inline qint32_t sqadd_qs32(qint32_t a, qint32_t b)
118 // We need to store the temporary result in qint64_t otherwise we cannot evaluate the overflow
119 qint64_t tmp = (static_cast<qint64_t>(a) + static_cast<qint64_t>(b));
121 // Saturate the result in case of overflow and cast to qint32_t
122 return saturate_convert<qint64_t, qint32_t>(tmp);
125 inline qint8_t ssub_qs8(qint8_t a, qint8_t b)
130 inline qint16_t ssub_qs16(qint16_t a, qint16_t b)
135 inline qint8_t sqsub_qs8(qint8_t a, qint8_t b)
137 // We need to store the temporary result in uint16_t otherwise we cannot evaluate the overflow
138 qint16_t tmp = static_cast<qint16_t>(a) - static_cast<qint16_t>(b);
140 // Saturate the result in case of overflow and cast to qint8_t
141 return saturate_convert<qint16_t, qint8_t>(tmp);
144 inline qint16_t sqsub_qs16(qint16_t a, qint16_t b)
146 // We need to store the temporary result in qint32_t otherwise we cannot evaluate the overflow
147 qint32_t tmp = static_cast<qint32_t>(a) - static_cast<qint32_t>(b);
149 // Saturate the result in case of overflow and cast to qint16_t
150 return saturate_convert<qint32_t, qint16_t>(tmp);
153 inline qint8_t smul_qs8(qint8_t a, qint8_t b, int fixed_point_position)
155 const qint16_t round_up_const = (1 << (fixed_point_position - 1));
157 qint16_t tmp = static_cast<qint16_t>(a) * static_cast<qint16_t>(b);
160 tmp += round_up_const;
162 return static_cast<qint8_t>(tmp >> fixed_point_position);
165 inline qint16_t smul_qs16(qint16_t a, qint16_t b, int fixed_point_position)
167 const qint32_t round_up_const = (1 << (fixed_point_position - 1));
169 qint32_t tmp = static_cast<qint32_t>(a) * static_cast<qint32_t>(b);
172 tmp += round_up_const;
174 return static_cast<qint16_t>(tmp >> fixed_point_position);
177 inline qint8_t sqmul_qs8(qint8_t a, qint8_t b, int fixed_point_position)
179 const qint16_t round_up_const = (1 << (fixed_point_position - 1));
181 qint16_t tmp = static_cast<qint16_t>(a) * static_cast<qint16_t>(b);
184 tmp += round_up_const;
186 return saturate_convert<qint16_t, qint8_t>(tmp >> fixed_point_position);
189 inline qint16_t sqmul_qs16(qint16_t a, qint16_t b, int fixed_point_position)
191 const qint32_t round_up_const = (1 << (fixed_point_position - 1));
193 qint32_t tmp = static_cast<qint32_t>(a) * static_cast<qint32_t>(b);
196 tmp += round_up_const;
198 return saturate_convert<qint32_t, qint16_t>(tmp >> fixed_point_position);
201 inline qint16_t sqmull_qs8(qint8_t a, qint8_t b, int fixed_point_position)
203 const qint16_t round_up_const = (1 << (fixed_point_position - 1));
205 qint16_t tmp = static_cast<qint16_t>(a) * static_cast<qint16_t>(b);
208 tmp += round_up_const;
210 return tmp >> fixed_point_position;
213 inline qint32_t sqmull_qs16(qint16_t a, qint16_t b, int fixed_point_position)
215 const qint32_t round_up_const = (1 << (fixed_point_position - 1));
217 qint32_t tmp = static_cast<qint32_t>(a) * static_cast<qint32_t>(b);
220 tmp += round_up_const;
222 return tmp >> fixed_point_position;
225 inline qint8_t sinvsqrt_qs8(qint8_t a, int fixed_point_position)
227 const qint8_t shift = 8 - (fixed_point_position + (__builtin_clz(a) - 24));
229 const qint8_t const_three = (3 << fixed_point_position);
230 qint8_t temp = shift < 0 ? (a << -shift) : (a >> shift);
233 // We need three iterations to find the result
234 for(int i = 0; i < 3; ++i)
236 qint8_t three_minus_dx = ssub_qs8(const_three, smul_qs8(temp, smul_qs8(x2, x2, fixed_point_position), fixed_point_position));
237 x2 = (smul_qs8(x2, three_minus_dx, fixed_point_position) >> 1);
240 temp = shift < 0 ? (x2 << (-shift >> 1)) : (x2 >> (shift >> 1));
245 inline qint16_t sinvsqrt_qs16(qint16_t a, int fixed_point_position)
247 const qint16_t shift = 16 - (fixed_point_position + (__builtin_clz(a) - 16));
249 const qint16_t const_three = (3 << fixed_point_position);
250 qint16_t temp = shift < 0 ? (a << -shift) : (a >> shift);
253 // We need three iterations to find the result
254 for(int i = 0; i < 3; ++i)
256 qint16_t three_minus_dx = ssub_qs16(const_three, smul_qs16(temp, smul_qs16(x2, x2, fixed_point_position), fixed_point_position));
257 x2 = smul_qs16(x2, three_minus_dx, fixed_point_position) >> 1;
260 temp = shift < 0 ? (x2 << ((-shift) >> 1)) : (x2 >> (shift >> 1));
265 inline qint8_t sdiv_qs8(qint8_t a, qint8_t b, int fixed_point_position)
267 const qint16_t temp = a << fixed_point_position;
268 return static_cast<qint8_t>(temp / b);
271 inline qint16_t sdiv_qs16(qint16_t a, qint16_t b, int fixed_point_position)
273 const qint32_t temp = a << fixed_point_position;
274 return static_cast<qint16_t>(temp / b);
277 inline qint8_t sqexp_qs8(qint8_t a, int fixed_point_position)
280 const qint8_t const_one = (1 << fixed_point_position);
281 const qint8_t ln2 = ((0x58 >> (6 - fixed_point_position)) + 1) >> 1;
282 const qint8_t inv_ln2 = (((0x38 >> (6 - fixed_point_position)) + 1) >> 1) | const_one;
283 const qint8_t A = ((0x7F >> (6 - fixed_point_position)) + 1) >> 1;
284 const qint8_t B = ((0x3F >> (6 - fixed_point_position)) + 1) >> 1;
285 const qint8_t C = ((0x16 >> (6 - fixed_point_position)) + 1) >> 1;
286 const qint8_t D = ((0x05 >> (6 - fixed_point_position)) + 1) >> 1;
288 // Polynomial expansion
289 const int dec_a = (sqmul_qs8(a, inv_ln2, fixed_point_position) >> fixed_point_position);
290 const qint8_t alpha = sabs_qs8(sqsub_qs8(a, sqmul_qs8(ln2, sqshl_qs8(dec_a, fixed_point_position), fixed_point_position)));
291 qint8_t sum = sqadd_qs8(sqmul_qs8(alpha, D, fixed_point_position), C);
292 sum = sqadd_qs8(sqmul_qs8(alpha, sum, fixed_point_position), B);
293 sum = sqadd_qs8(sqmul_qs8(alpha, sum, fixed_point_position), A);
294 sum = sqmul_qs8(alpha, sum, fixed_point_position);
295 sum = sqadd_qs8(sum, const_one);
297 return (dec_a < 0) ? (sum >> -dec_a) : sqshl_qs8(sum, dec_a);
300 inline qint16_t sqexp_qs16(qint16_t a, int fixed_point_position)
303 const qint16_t const_one = (1 << fixed_point_position);
304 const qint16_t ln2 = ((0x58B9 >> (14 - fixed_point_position)) + 1) >> 1;
305 const qint16_t inv_ln2 = (((0x38AA >> (14 - fixed_point_position)) + 1) >> 1) | const_one;
306 const qint16_t A = ((0x7FBA >> (14 - fixed_point_position)) + 1) >> 1;
307 const qint16_t B = ((0x3FE9 >> (14 - fixed_point_position)) + 1) >> 1;
308 const qint16_t C = ((0x1693 >> (14 - fixed_point_position)) + 1) >> 1;
309 const qint16_t D = ((0x0592 >> (14 - fixed_point_position)) + 1) >> 1;
311 // Polynomial expansion
312 const int dec_a = (sqmul_qs16(a, inv_ln2, fixed_point_position) >> fixed_point_position);
313 const qint16_t alpha = sabs_qs16(sqsub_qs16(a, sqmul_qs16(ln2, sqshl_qs16(dec_a, fixed_point_position), fixed_point_position)));
314 qint16_t sum = sqadd_qs16(sqmul_qs16(alpha, D, fixed_point_position), C);
315 sum = sqadd_qs16(sqmul_qs16(alpha, sum, fixed_point_position), B);
316 sum = sqadd_qs16(sqmul_qs16(alpha, sum, fixed_point_position), A);
317 sum = sqmul_qs16(alpha, sum, fixed_point_position);
318 sum = sqadd_qs16(sum, const_one);
320 return (dec_a < 0) ? (sum >> -dec_a) : sqshl_qs16(sum, dec_a);
323 inline qint8_t slog_qs8(qint8_t a, int fixed_point_position)
326 qint8_t const_one = (1 << fixed_point_position);
327 qint8_t ln2 = (0x58 >> (7 - fixed_point_position));
328 qint8_t A = (0x5C >> (7 - fixed_point_position - 1));
329 qint8_t B = -(0x56 >> (7 - fixed_point_position));
330 qint8_t C = (0x29 >> (7 - fixed_point_position));
331 qint8_t D = -(0x0A >> (7 - fixed_point_position));
333 if((const_one == a) || (a < 0))
337 else if(a < const_one)
339 return -slog_qs8(sdiv_qs8(const_one, a, fixed_point_position), fixed_point_position);
342 // Remove even powers of 2
343 qint8_t shift_val = 31 - __builtin_clz(a >> fixed_point_position);
345 a = ssub_qs8(a, const_one);
347 // Polynomial expansion
348 qint8_t sum = sqadd_qs8(sqmul_qs8(a, D, fixed_point_position), C);
349 sum = sqadd_qs8(sqmul_qs8(a, sum, fixed_point_position), B);
350 sum = sqadd_qs8(sqmul_qs8(a, sum, fixed_point_position), A);
351 sum = sqmul_qs8(a, sum, fixed_point_position);
353 return smul_qs8(sadd_qs8(sum, shift_val << fixed_point_position), ln2, fixed_point_position);
356 inline qint16_t slog_qs16(qint16_t a, int fixed_point_position)
359 qint16_t const_one = (1 << fixed_point_position);
360 qint16_t ln2 = (0x58B9 >> (7 - fixed_point_position));
361 qint16_t A = (0x5C0F >> (7 - fixed_point_position - 1));
362 qint16_t B = -(0x56AE >> (7 - fixed_point_position));
363 qint16_t C = (0x2933 >> (7 - fixed_point_position));
364 qint16_t D = -(0x0AA7 >> (7 - fixed_point_position));
366 if((const_one == a) || (a < 0))
370 else if(a < const_one)
372 return -slog_qs16(sdiv_qs16(const_one, a, fixed_point_position), fixed_point_position);
375 // Remove even powers of 2
376 qint16_t shift_val = 31 - __builtin_clz(a >> fixed_point_position);
378 a = ssub_qs16(a, const_one);
380 // Polynomial expansion
381 qint16_t sum = sqadd_qs16(sqmul_qs16(a, D, fixed_point_position), C);
382 sum = sqadd_qs16(sqmul_qs16(a, sum, fixed_point_position), B);
383 sum = sqadd_qs16(sqmul_qs16(a, sum, fixed_point_position), A);
384 sum = sqmul_qs16(a, sum, fixed_point_position);
386 return smul_qs16(sadd_qs16(sum, shift_val << fixed_point_position), ln2, fixed_point_position);
389 inline float scvt_f32_qs8(qint8_t a, int fixed_point_position)
391 return static_cast<float>(a) / (1 << fixed_point_position);
394 inline qint8_t sqcvt_qs8_f32(float a, int fixed_point_position)
396 // round_nearest_integer(a * 2^(fixed_point_position))
397 return saturate_convert<float, qint8_t>(a * (1 << fixed_point_position) + ((a >= 0) ? 0.5 : -0.5));
400 inline float scvt_f32_qs16(qint16_t a, int fixed_point_position)
402 return static_cast<float>(a) / (1 << fixed_point_position);
405 inline qint16_t sqcvt_qs16_f32(float a, int fixed_point_position)
407 // round_nearest_integer(a * 2^(fixed_point_position))
408 return saturate_convert<float, qint16_t>(a * (1 << fixed_point_position) + ((a >= 0) ? 0.5 : -0.5));
411 inline qint8_t sqmovn_qs16(qint16_t a)
413 // Saturate the result in case of overflow and cast to qint8_t
414 return saturate_convert<qint16_t, qint8_t>(a);
417 inline qint16_t sqmovn_qs32(qint32_t a)
419 // Saturate the result in case of overflow and cast to qint16_t
420 return saturate_convert<qint32_t, qint16_t>(a);