blob: 91d0abfb1f4be72028fa6040ecd6ccd5e001af18 [file] [log] [blame]
Chunosovd621bca2017-11-03 17:33:15 +07001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2017-2019 Arm Limited.
Chunosovd621bca2017-11-03 17:33:15 +07003 *
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 Giorgio14cbfb22019-10-23 10:53:10 +010024#ifndef ARM_COMPUTE_TEST_VALIDATION_UTILS_QUANTIZED_ASYMM_H
25#define ARM_COMPUTE_TEST_VALIDATION_UTILS_QUANTIZED_ASYMM_H
Chunosovd621bca2017-11-03 17:33:15 +070026
27#include <cstdint>
28
29namespace arm_compute
30{
31namespace test
32{
33namespace validation
34{
35/** Rounded to nearest division by a power-of-two. */
36inline 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. */
44inline 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 Giorgio14cbfb22019-10-23 10:53:10 +010054
55/** Quantize down the input value in range [min, max]. */
56inline 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}
Chunosovd621bca2017-11-03 17:33:15 +070072} // namespace validation
73} // namespace test
74} // namespace arm_compute
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +010075#endif /* ARM_COMPUTE_TEST_VALIDATION_UTILS_QUANTIZED_ASYMM_H */