blob: b7b69d588a024f6fd69d91f65ca0911fc9f1c35a [file] [log] [blame]
Chunosovd621bca2017-11-03 17:33:15 +07001/*
2 * Copyright (c) 2017 ARM Limited.
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_TEST_VALIDATION_UTILS_QUANTIZED_ASYMM_H__
25#define __ARM_COMPUTE_TEST_VALIDATION_UTILS_QUANTIZED_ASYMM_H__
26
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}
54} // namespace validation
55} // namespace test
56} // namespace arm_compute
57#endif /* __ARM_COMPUTE_TEST_VALIDATION_UTILS_QUANTIZED_ASYMM_H__ */