blob: f07e7c950db7e93799c4f215d84a5573c51a1ace [file] [log] [blame]
Chunosovd621bca2017-11-03 17:33:15 +07001/*
Giorgio Arena6232d042018-02-12 14:46:00 +00002 * Copyright (c) 2017-2018 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 */
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)
Giorgio Arena6232d042018-02-12 14:46:00 +000048ASYMM_ROUNDING_DIVIDE_BY_POW2_IMPL(4)
Chunosovd621bca2017-11-03 17:33:15 +070049ASYMM_ROUNDING_DIVIDE_BY_POW2_IMPL(8)
50ASYMM_ROUNDING_DIVIDE_BY_POW2_IMPL(16)
51
52#define ASYMM_ROUNDING_DIVIDE_BY_POW2(x, exponent, size) asymm_rounding_divide_by_POW2_##size(x, exponent)
53
54/** Product of two numbers, interpreting them as fixed-point values in the interval [-1, 1),
55 * rounding to the nearest value, and saturating -1 * -1 to the maximum value.
56 *
57 * @param[in] size Size of vector.
58 *
59 * @return Product of two fixed-point numbers.
60 */
61#define ASYMM_MULT_IMP(size) \
62 inline VEC_DATA_TYPE(int, size) asymm_mult##size(VEC_DATA_TYPE(int, size) a, VEC_DATA_TYPE(int, size) b) \
63 { \
64 VEC_DATA_TYPE(int, size) \
65 overflow = a == b && a == INT_MIN; \
66 VEC_DATA_TYPE(long, size) \
67 a_64 = convert_long##size(a); \
68 VEC_DATA_TYPE(long, size) \
69 b_64 = convert_long##size(b); \
70 VEC_DATA_TYPE(long, size) \
71 ab_64 = a_64 * b_64; \
Chunosovd621bca2017-11-03 17:33:15 +070072 VEC_DATA_TYPE(int, size) \
Giorgio Arena6232d042018-02-12 14:46:00 +000073 /* COMPMID-907 */ \
74 ab_x2_high32 = convert_int##size(((ab_64 + (1 << 30)) >> 31)); \
Chunosovd621bca2017-11-03 17:33:15 +070075 return select(ab_x2_high32, INT_MAX, overflow); \
76 }
77
Dmitry Savenkod7295b72017-11-20 22:00:08 +070078ASYMM_MULT_IMP(2)
Giorgio Arena6232d042018-02-12 14:46:00 +000079ASYMM_MULT_IMP(4)
Chunosovd621bca2017-11-03 17:33:15 +070080ASYMM_MULT_IMP(8)
81ASYMM_MULT_IMP(16)
82
83#define ASYMM_MULT(a, b, size) asymm_mult##size(a, b)
84
85#define ASYMM_MULT_BY_QUANT_MULTIPLIER_LESS_THAN_ONE(x, quantized_multiplier, right_shift, size) \
86 ASYMM_ROUNDING_DIVIDE_BY_POW2(ASYMM_MULT(x, quantized_multiplier, size), right_shift, size)
87
88#endif // ARM_COMPUTE_HELPERS_ASYMM_H