24 #ifndef ARM_COMPUTE_HELPERS_ASYMM_H 25 #define ARM_COMPUTE_HELPERS_ASYMM_H 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) \ 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); \ 54 #define ASYMM_MULT_IMPL(size) \ 55 inline VEC_DATA_TYPE(int, size) asymm_mult##size(VEC_DATA_TYPE(int, size) a, VEC_DATA_TYPE(int, size) b) \ 57 VEC_DATA_TYPE(int, size) \ 58 overflow = a == b && a == INT_MIN; \ 59 VEC_DATA_TYPE(long, size) \ 60 a_64 = convert_long##size(a); \ 61 VEC_DATA_TYPE(long, size) \ 62 b_64 = convert_long##size(b); \ 63 VEC_DATA_TYPE(long, size) \ 64 ab_64 = a_64 * b_64; \ 65 VEC_DATA_TYPE(int, size) \ 66 ab_x2_high32 = convert_int##size(((ab_64 + (1 << 30)) >> 31)); \ 67 return select(ab_x2_high32, INT_MAX, overflow); \ 76 #define ASYMM_EXP_ON_INTERVAL_BETWEEN_NEGATIVE_ONE_QUARTER_AND_0_EXCL_IMPL(size) \ 77 inline VEC_DATA_TYPE(int, size) asymm_exp_on_interval_between_negative_one_quarter_and_0_excl##size(VEC_DATA_TYPE(int, size) a) \ 79 const VEC_DATA_TYPE(int, size) constant_term = 1895147668; \ 80 const VEC_DATA_TYPE(int, size) constant_1_over_3 = 715827883; \ 81 const int k_fractional_bits = 31; \ 82 VEC_DATA_TYPE(int, size) \ 83 x = a + (1 << (k_fractional_bits - 3)); \ 84 VEC_DATA_TYPE(int, size) \ 85 x2 = ASYMM_MULT(x, x, size); \ 86 VEC_DATA_TYPE(int, size) \ 87 x3 = ASYMM_MULT(x2, x, size); \ 88 VEC_DATA_TYPE(int, size) \ 89 x4 = ASYMM_MULT(x2, x2, size); \ 90 VEC_DATA_TYPE(int, size) \ 91 x4_over_4 = ASYMM_ROUNDING_DIVIDE_BY_POW2(x4, 2, size); \ 92 VEC_DATA_TYPE(int, size) \ 93 x4_over_24_plus_x3_over_6_plus_x2 = ASYMM_MULT((x4_over_4 + x3), constant_1_over_3, size) + x2; \ 94 VEC_DATA_TYPE(int, size) \ 95 x4_over_24_plus_x3_over_6_plus_x2_over_2 = ASYMM_ROUNDING_DIVIDE_BY_POW2(x4_over_24_plus_x3_over_6_plus_x2, 1, size); \ 96 return constant_term + ASYMM_MULT(constant_term, x + x4_over_24_plus_x3_over_6_plus_x2_over_2, size); \ 107 #define ASYMM_SELECT_USING_MASK_IMPL(size) \ 108 inline VEC_DATA_TYPE(int, size) asymm_select_using_mask##size(VEC_DATA_TYPE(int, size) if_mask, VEC_DATA_TYPE(int, size) then_val, VEC_DATA_TYPE(int, size) else_val) \ 110 return (if_mask & then_val) ^ (~if_mask & else_val); \ 120 #define ASYMM_MASK_IF_ZERO_IMPL(size) \ 121 inline VEC_DATA_TYPE(int, size) asymm_mask_if_zero##size(VEC_DATA_TYPE(int, size) a) \ 123 const VEC_DATA_TYPE(int, size) all_zeros = 0; \ 124 const VEC_DATA_TYPE(int, size) all_ones = ~0; \ 125 return select(all_zeros, all_ones, a == 0); \ 135 #define ASYMM_MASK_IF_NON_ZERO_IMPL(size) \ 136 inline VEC_DATA_TYPE(int, size) asymm_mask_if_non_zero##size(VEC_DATA_TYPE(int, size) a) \ 138 const VEC_DATA_TYPE(int, size) all_zeros = 0; \ 139 const VEC_DATA_TYPE(int, size) all_ones = ~0; \ 140 return select(all_zeros, all_ones, a != 0); \ 143 #define EXP_BARREL_SHIFTER_IMPL(size) \ 144 inline VEC_DATA_TYPE(int, size) exp_barrel_shifter##size(VEC_DATA_TYPE(int, size) result, int exponent, int fp_multiplier, int k_integer_bits, int k_fractional_bits, VEC_DATA_TYPE(int, size) remainder) \ 146 if(k_integer_bits > exponent) \ 148 const int k_shift_amount = k_integer_bits > exponent ? k_fractional_bits + exponent : 0; \ 149 return ASYMM_SELECT_USING_MASK( \ 150 ASYMM_MASK_IF_NON_ZERO(remainder & (1 << k_shift_amount), size), \ 151 ASYMM_MULT(result, fp_multiplier, size), result, size); \ 163 #define ASYMM_EXP_ON_NEGATIVE_VALUES_IMPL(size) \ 164 inline VEC_DATA_TYPE(int, size) asymm_exp_on_negative_values##size(VEC_DATA_TYPE(int, size) a, int k_integer_bits) \ 166 const int k_fractional_bits = 31 - k_integer_bits; \ 167 VEC_DATA_TYPE(int, size) \ 168 k_one_quarter = 1 << (k_fractional_bits - 2); \ 169 VEC_DATA_TYPE(int, size) \ 170 mask = k_one_quarter - 1; \ 171 VEC_DATA_TYPE(int, size) \ 172 a_mod_quarter_minus_one_quarter = (a & mask) - k_one_quarter; \ 173 VEC_DATA_TYPE(int, size) \ 174 a_mod_quarter_minus_one_quarter_scaled = a_mod_quarter_minus_one_quarter << k_integer_bits; \ 175 VEC_DATA_TYPE(int, size) \ 176 result = ASYMM_EXP_ON_INTERVAL_BETWEEN_NEGATIVE_ONE_QUARTER_AND_0_EXCL(a_mod_quarter_minus_one_quarter_scaled, size); \ 177 VEC_DATA_TYPE(int, size) \ 178 remainder = a_mod_quarter_minus_one_quarter - a; \ 180 result = EXP_BARREL_SHIFTER(result, -2, 1672461947, k_integer_bits, k_fractional_bits, remainder, size); \ 181 result = EXP_BARREL_SHIFTER(result, -1, 1302514674, k_integer_bits, k_fractional_bits, remainder, size); \ 182 result = EXP_BARREL_SHIFTER(result, +0, 790015084, k_integer_bits, k_fractional_bits, remainder, size); \ 183 result = EXP_BARREL_SHIFTER(result, +1, 290630308, k_integer_bits, k_fractional_bits, remainder, size); \ 184 result = EXP_BARREL_SHIFTER(result, +2, 39332535, k_integer_bits, k_fractional_bits, remainder, size); \ 185 result = EXP_BARREL_SHIFTER(result, +3, 720401, k_integer_bits, k_fractional_bits, remainder, size); \ 186 result = EXP_BARREL_SHIFTER(result, +4, 242, k_integer_bits, k_fractional_bits, remainder, size); \ 188 if(k_integer_bits > 5) \ 190 const VEC_DATA_TYPE(int, size) clamp = -(1 << (k_fractional_bits + 5)); \ 191 result = ASYMM_SELECT_USING_MASK(ASYMM_MASK_IF_NON_ZERO(a < clamp, size), 0, result, size); \ 194 const VEC_DATA_TYPE(int, size) Q0_one = INT_MAX; \ 195 return ASYMM_SELECT_USING_MASK(ASYMM_MASK_IF_ZERO(a, size), Q0_one, result, size); \ 206 #define ASYMM_SATURATING_ROUNDING_MULT_BY_POW2_IMPL(size) \ 207 inline VEC_DATA_TYPE(int, size) asymm_saturating_rounding_mult_by_pow2##size(VEC_DATA_TYPE(int, size) x, int exponent) \ 211 return ASYMM_ROUNDING_DIVIDE_BY_POW2(x, -exponent, size); \ 214 const VEC_DATA_TYPE(int, size) min = INT_MIN; \ 215 const VEC_DATA_TYPE(int, size) max = INT_MAX; \ 216 int threshold = ((1 << (31 - exponent)) - 1); \ 217 VEC_DATA_TYPE(int, size) \ 218 positive_mask = ASYMM_MASK_IF_NON_ZERO(x > threshold, size); \ 219 VEC_DATA_TYPE(int, size) \ 220 negative_mask = ASYMM_MASK_IF_NON_ZERO(x < -threshold, size); \ 221 VEC_DATA_TYPE(int, size) \ 222 result = x << exponent; \ 223 result = ASYMM_SELECT_USING_MASK(positive_mask, max, result, size); \ 224 result = ASYMM_SELECT_USING_MASK(negative_mask, min, result, size); \ 235 #define ASYMM_ROUNDING_HALF_SUM_IMPL(size) \ 236 inline VEC_DATA_TYPE(int, size) asymm_rounding_half_sum##size(VEC_DATA_TYPE(int, size) a, VEC_DATA_TYPE(int, size) b) \ 238 VEC_DATA_TYPE(long, size) \ 239 a64 = convert_long##size(a); \ 240 VEC_DATA_TYPE(long, size) \ 241 b64 = convert_long##size(b); \ 242 VEC_DATA_TYPE(long, size) \ 244 const VEC_DATA_TYPE(long, size) one = 1; \ 245 const VEC_DATA_TYPE(long, size) minus_one = -1; \ 246 VEC_DATA_TYPE(long, size) \ 247 sign = select(minus_one, one, sum >= 0); \ 248 return convert_int##size((sum + sign) / 2); \ 257 #define ASYMM_ONE_OVER_ONE_PLUS_X_FOR_X_IN_0_1_IMPL(size) \ 258 inline VEC_DATA_TYPE(int, size) asymm_one_over_one_plus_x_for_x_in_0_1##size(VEC_DATA_TYPE(int, size) a) \ 260 const VEC_DATA_TYPE(int, size) Q0_one = INT_MAX; \ 261 const VEC_DATA_TYPE(int, size) Q2_one = 1 << (31 - 2); \ 262 VEC_DATA_TYPE(int, size) \ 263 half_denominator = ASYMM_ROUNDING_HALF_SUM(a, Q0_one, size); \ 264 const VEC_DATA_TYPE(int, size) Q2_48_over_17 = 1515870810; \ 265 const VEC_DATA_TYPE(int, size) Q2_neg_32_over_17 = -1010580540; \ 266 VEC_DATA_TYPE(int, size) \ 267 x = Q2_48_over_17 + ASYMM_MULT(half_denominator, Q2_neg_32_over_17, size); \ 268 for(int i = 0; i < 3; i++) \ 270 VEC_DATA_TYPE(int, size) \ 271 half_denominator_times_x = ASYMM_MULT(half_denominator, x, size); \ 272 VEC_DATA_TYPE(int, size) \ 273 one_minus_half_denominator_times_x = Q2_one - half_denominator_times_x; \ 274 VEC_DATA_TYPE(int, size) \ 275 tmp = ASYMM_MULT(x, one_minus_half_denominator_times_x, size); \ 276 x = x + ASYMM_SATURATING_ROUNDING_MULT_BY_POW2(tmp, 2, size); \ 278 return ASYMM_SATURATING_ROUNDING_MULT_BY_POW2(x, 1, size); \ 287 #define ASYMM_RESCALE_IMPL(size) \ 288 inline VEC_DATA_TYPE(int, size) asymm_rescale##size(VEC_DATA_TYPE(int, size) value, int src_integer_bits, int dst_integer_bits) \ 290 int exponent = src_integer_bits - dst_integer_bits; \ 291 return ASYMM_SATURATING_ROUNDING_MULT_BY_POW2(value, exponent, size); \ 294 #define ASYMM_ROUNDING_DIVIDE_BY_POW2(x, exponent, size) asymm_rounding_divide_by_POW2_##size(x, exponent) 295 #define ASYMM_MULT(a, b, size) asymm_mult##size(a, b) 296 #define ASYMM_MULT_BY_QUANT_MULTIPLIER_LESS_THAN_ONE(x, quantized_multiplier, right_shift, size) \ 297 ASYMM_ROUNDING_DIVIDE_BY_POW2(ASYMM_MULT(x, quantized_multiplier, size), right_shift, size) 298 #define ASYMM_EXP_ON_INTERVAL_BETWEEN_NEGATIVE_ONE_QUARTER_AND_0_EXCL(a, size) asymm_exp_on_interval_between_negative_one_quarter_and_0_excl##size(a) 299 #define ASYMM_SELECT_USING_MASK(if_mask, then_val, else_val, size) asymm_select_using_mask##size(if_mask, then_val, else_val) 300 #define ASYMM_MASK_IF_ZERO(a, size) asymm_mask_if_zero##size(a) 301 #define ASYMM_MASK_IF_NON_ZERO(a, size) asymm_mask_if_non_zero##size(a) 302 #define EXP_BARREL_SHIFTER(result, exponent, fp_multiplier, k_integer_bits, k_fractional_bits, remainder, size) exp_barrel_shifter##size(result, exponent, fp_multiplier, k_integer_bits, k_fractional_bits, remainder) 303 #define ASYMM_EXP_ON_NEGATIVE_VALUES(a, k_integer_bits, size) asymm_exp_on_negative_values##size(a, k_integer_bits) 304 #define ASYMM_ONE_OVER_ONE_PLUS_X_FOR_X_IN_0_1(a, size) asymm_one_over_one_plus_x_for_x_in_0_1##size(a) 305 #define ASYMM_SATURATING_ROUNDING_MULT_BY_POW2(x, exponent, size) asymm_saturating_rounding_mult_by_pow2##size(x, exponent) 306 #define ASYMM_ROUNDING_HALF_SUM(a, b, size) asymm_rounding_half_sum##size(a, b) 307 #define ASYMM_RESCALE(value, src_integer_bits, dst_integer_bits, size) asymm_rescale##size(value, src_integer_bits, dst_integer_bits) 369 #endif // ARM_COMPUTE_HELPERS_ASYMM_H #define ASYMM_ROUNDING_HALF_SUM_IMPL(size)
Calculates (a+b)/2, rounded to the nearest integer.
#define ASYMM_EXP_ON_NEGATIVE_VALUES_IMPL(size)
Calculates for x < 0.
#define EXP_BARREL_SHIFTER_IMPL(size)
#define ASYMM_SELECT_USING_MASK_IMPL(size)
Each bit of the result is set to the corresponding bit of either then_val or else_val depending on wh...
#define ASYMM_EXP_ON_INTERVAL_BETWEEN_NEGATIVE_ONE_QUARTER_AND_0_EXCL_IMPL(size)
Calculates for x in [-1/4, 0).
#define ASYMM_ONE_OVER_ONE_PLUS_X_FOR_X_IN_0_1_IMPL(size)
Calculates for x in (0, 1).
#define ASYMM_MASK_IF_ZERO_IMPL(size)
For each element of input vector, the corresponding bits of the result item are set if the input item...
#define ASYMM_ROUNDING_DIVIDE_BY_POW2_IMPL(size)
Correctly-rounded-to-nearest division by a power-of-two.
#define ASYMM_RESCALE_IMPL(size)
Considering the integer value as fixed-point, change the number of integer bits and update value acco...
#define ASYMM_MASK_IF_NON_ZERO_IMPL(size)
For each element of input vector, the corresponding bits of the result item are set if the input item...
#define ASYMM_MULT_IMPL(size)
Product of two numbers, interpreting them as fixed-point values in the interval [-1, 1), rounding to the nearest value, and saturating -1 * -1 to the maximum value.
#define ASYMM_SATURATING_ROUNDING_MULT_BY_POW2_IMPL(size)
Calculates the product of a integer value by a power of two, with either a positive exponent (equival...