Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 1 | /* |
Giorgio Arena | 6232d04 | 2018-02-12 14:46:00 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 ARM Limited. |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 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 Savenko | d7295b7 | 2017-11-20 22:00:08 +0700 | [diff] [blame] | 47 | ASYMM_ROUNDING_DIVIDE_BY_POW2_IMPL(2) |
Giorgio Arena | 6232d04 | 2018-02-12 14:46:00 +0000 | [diff] [blame] | 48 | ASYMM_ROUNDING_DIVIDE_BY_POW2_IMPL(4) |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 49 | ASYMM_ROUNDING_DIVIDE_BY_POW2_IMPL(8) |
| 50 | ASYMM_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; \ |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 72 | VEC_DATA_TYPE(int, size) \ |
Giorgio Arena | 6232d04 | 2018-02-12 14:46:00 +0000 | [diff] [blame] | 73 | /* COMPMID-907 */ \ |
| 74 | ab_x2_high32 = convert_int##size(((ab_64 + (1 << 30)) >> 31)); \ |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 75 | return select(ab_x2_high32, INT_MAX, overflow); \ |
| 76 | } |
| 77 | |
Dmitry Savenko | d7295b7 | 2017-11-20 22:00:08 +0700 | [diff] [blame] | 78 | ASYMM_MULT_IMP(2) |
Giorgio Arena | 6232d04 | 2018-02-12 14:46:00 +0000 | [diff] [blame] | 79 | ASYMM_MULT_IMP(4) |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 80 | ASYMM_MULT_IMP(8) |
| 81 | ASYMM_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 |