blob: b44d0f1fd2f1621ee06d2b08091477f8227b1163 [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
Dmitry Savenkod7295b72017-11-20 22:00:08 +070047ASYMM_ROUNDING_DIVIDE_BY_POW2_IMPL(2)
Chunosovd621bca2017-11-03 17:33:15 +070048ASYMM_ROUNDING_DIVIDE_BY_POW2_IMPL(8)
49ASYMM_ROUNDING_DIVIDE_BY_POW2_IMPL(16)
50
51#define ASYMM_ROUNDING_DIVIDE_BY_POW2(x, exponent, size) asymm_rounding_divide_by_POW2_##size(x, exponent)
52
53/** Product of two numbers, interpreting them as fixed-point values in the interval [-1, 1),
54 * rounding to the nearest value, and saturating -1 * -1 to the maximum value.
55 *
56 * @param[in] size Size of vector.
57 *
58 * @return Product of two fixed-point numbers.
59 */
60#define ASYMM_MULT_IMP(size) \
61 inline VEC_DATA_TYPE(int, size) asymm_mult##size(VEC_DATA_TYPE(int, size) a, VEC_DATA_TYPE(int, size) b) \
62 { \
63 VEC_DATA_TYPE(int, size) \
64 overflow = a == b && a == INT_MIN; \
65 VEC_DATA_TYPE(long, size) \
66 a_64 = convert_long##size(a); \
67 VEC_DATA_TYPE(long, size) \
68 b_64 = convert_long##size(b); \
69 VEC_DATA_TYPE(long, size) \
70 ab_64 = a_64 * b_64; \
71 VEC_DATA_TYPE(long, size) \
72 mask1 = 1 << 30; \
73 VEC_DATA_TYPE(long, size) \
74 mask2 = 1 - (1 << 30); \
75 VEC_DATA_TYPE(long, size) \
76 nudge = select(mask2, mask1, ab_64 >= 0); \
77 VEC_DATA_TYPE(long, size) \
78 mask = 1ll << 31; \
79 VEC_DATA_TYPE(int, size) \
80 ab_x2_high32 = convert_int##size((ab_64 + nudge) / mask); \
81 return select(ab_x2_high32, INT_MAX, overflow); \
82 }
83
Dmitry Savenkod7295b72017-11-20 22:00:08 +070084ASYMM_MULT_IMP(2)
Chunosovd621bca2017-11-03 17:33:15 +070085ASYMM_MULT_IMP(8)
86ASYMM_MULT_IMP(16)
87
88#define ASYMM_MULT(a, b, size) asymm_mult##size(a, b)
89
90#define ASYMM_MULT_BY_QUANT_MULTIPLIER_LESS_THAN_ONE(x, quantized_multiplier, right_shift, size) \
91 ASYMM_ROUNDING_DIVIDE_BY_POW2(ASYMM_MULT(x, quantized_multiplier, size), right_shift, size)
92
93#endif // ARM_COMPUTE_HELPERS_ASYMM_H