blob: 3c1d58bda1f93b100a9594ca0fa255f825204812 [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_HELPERS_ASYMM_H
25#define ARM_COMPUTE_HELPERS_ASYMM_H
26
27#include "helpers.h"
28
29/** Correctly-rounded-to-nearest division by a power-of-two.
30 *
31 * @param[in] size Size of vector.
32 *
33 * @return Correctly-rounded-to-nearest division by a power-of-two.
34 */
35#define ASYMM_ROUNDING_DIVIDE_BY_POW2_IMPL(size) \
36 inline VEC_DATA_TYPE(int, size) asymm_rounding_divide_by_POW2_##size(VEC_DATA_TYPE(int, size) x, int exponent) \
37 { \
38 VEC_DATA_TYPE(int, size) \
39 mask = (1 << exponent) - 1; \
40 const VEC_DATA_TYPE(int, size) zero = 0; \
41 const VEC_DATA_TYPE(int, size) one = 1; \
42 VEC_DATA_TYPE(int, size) \
43 threshold = (mask >> 1) + select(zero, one, x < 0); \
44 return (x >> exponent) + select(zero, one, (x & mask) > threshold); \
45 }
46
47ASYMM_ROUNDING_DIVIDE_BY_POW2_IMPL(8)
48ASYMM_ROUNDING_DIVIDE_BY_POW2_IMPL(16)
49
50#define ASYMM_ROUNDING_DIVIDE_BY_POW2(x, exponent, size) asymm_rounding_divide_by_POW2_##size(x, exponent)
51
52/** Product of two numbers, interpreting them as fixed-point values in the interval [-1, 1),
53 * rounding to the nearest value, and saturating -1 * -1 to the maximum value.
54 *
55 * @param[in] size Size of vector.
56 *
57 * @return Product of two fixed-point numbers.
58 */
59#define ASYMM_MULT_IMP(size) \
60 inline VEC_DATA_TYPE(int, size) asymm_mult##size(VEC_DATA_TYPE(int, size) a, VEC_DATA_TYPE(int, size) b) \
61 { \
62 VEC_DATA_TYPE(int, size) \
63 overflow = a == b && a == INT_MIN; \
64 VEC_DATA_TYPE(long, size) \
65 a_64 = convert_long##size(a); \
66 VEC_DATA_TYPE(long, size) \
67 b_64 = convert_long##size(b); \
68 VEC_DATA_TYPE(long, size) \
69 ab_64 = a_64 * b_64; \
70 VEC_DATA_TYPE(long, size) \
71 mask1 = 1 << 30; \
72 VEC_DATA_TYPE(long, size) \
73 mask2 = 1 - (1 << 30); \
74 VEC_DATA_TYPE(long, size) \
75 nudge = select(mask2, mask1, ab_64 >= 0); \
76 VEC_DATA_TYPE(long, size) \
77 mask = 1ll << 31; \
78 VEC_DATA_TYPE(int, size) \
79 ab_x2_high32 = convert_int##size((ab_64 + nudge) / mask); \
80 return select(ab_x2_high32, INT_MAX, overflow); \
81 }
82
83ASYMM_MULT_IMP(8)
84ASYMM_MULT_IMP(16)
85
86#define ASYMM_MULT(a, b, size) asymm_mult##size(a, b)
87
88#define ASYMM_MULT_BY_QUANT_MULTIPLIER_LESS_THAN_ONE(x, quantized_multiplier, right_shift, size) \
89 ASYMM_ROUNDING_DIVIDE_BY_POW2(ASYMM_MULT(x, quantized_multiplier, size), right_shift, size)
90
91#endif // ARM_COMPUTE_HELPERS_ASYMM_H