Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | d9eaf61 | 2020-07-08 11:12:57 +0100 | [diff] [blame] | 2 | * Copyright (c) 2017-2019 Arm Limited. |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [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 | */ |
Michele Di Giorgio | 14cbfb2 | 2019-10-23 10:53:10 +0100 | [diff] [blame] | 24 | #ifndef ARM_COMPUTE_TEST_VALIDATION_UTILS_QUANTIZED_ASYMM_H |
| 25 | #define ARM_COMPUTE_TEST_VALIDATION_UTILS_QUANTIZED_ASYMM_H |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 26 | |
| 27 | #include <cstdint> |
| 28 | |
| 29 | namespace arm_compute |
| 30 | { |
| 31 | namespace test |
| 32 | { |
| 33 | namespace validation |
| 34 | { |
| 35 | /** Rounded to nearest division by a power-of-two. */ |
| 36 | inline int32_t asymm_rounding_divide_by_pow2(int32_t x, int exponent) |
| 37 | { |
| 38 | const int32_t mask = (1 << exponent) - 1; |
| 39 | const int32_t threshold = (mask >> 1) + (x < 0 ? 1 : 0); |
| 40 | return (x >> exponent) + ((x & mask) > threshold ? 1 : 0); |
| 41 | } |
| 42 | |
| 43 | /** Multiplication of two integers. The same as ARMv7 NEON VQRDMULH instruction. */ |
| 44 | inline int32_t asymm_int_mult(int32_t a, int32_t b) |
| 45 | { |
| 46 | bool overflow = a == b && a == std::numeric_limits<int32_t>::min(); |
| 47 | int64_t a_64(a); |
| 48 | int64_t b_64(b); |
| 49 | int64_t ab_64 = a_64 * b_64; |
| 50 | int32_t nudge = ab_64 >= 0 ? (1 << 30) : (1 - (1 << 30)); |
| 51 | int32_t ab_x2_high32 = static_cast<int32_t>((ab_64 + nudge) / (1ll << 31)); |
| 52 | return overflow ? std::numeric_limits<int32_t>::max() : ab_x2_high32; |
| 53 | } |
Michele Di Giorgio | 14cbfb2 | 2019-10-23 10:53:10 +0100 | [diff] [blame] | 54 | |
| 55 | /** Quantize down the input value in range [min, max]. */ |
| 56 | inline int32_t quantize_down_scale_by_fixedpoint(int32_t val, int32_t result_mult_int, int32_t result_shift, |
| 57 | int32_t result_offset_after_shift, int32_t min, int32_t max) |
| 58 | { |
| 59 | int32_t res = 0; |
| 60 | if(result_shift < 0) |
| 61 | { |
| 62 | res = asymm_int_mult(val * (1 << (-result_shift)), result_mult_int); |
| 63 | } |
| 64 | else |
| 65 | { |
| 66 | res = asymm_rounding_divide_by_pow2(asymm_int_mult(val, result_mult_int), result_shift); |
| 67 | } |
| 68 | res += result_offset_after_shift; |
| 69 | res = utility::clamp<int32_t>(res, min, max); |
| 70 | return res; |
| 71 | } |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 72 | } // namespace validation |
| 73 | } // namespace test |
| 74 | } // namespace arm_compute |
Michele Di Giorgio | 14cbfb2 | 2019-10-23 10:53:10 +0100 | [diff] [blame] | 75 | #endif /* ARM_COMPUTE_TEST_VALIDATION_UTILS_QUANTIZED_ASYMM_H */ |