32 const std::array<float32x4_t, 8>
exp_tab =
36 vdupq_n_f32(0.0416598916054f),
37 vdupq_n_f32(0.500000596046f),
38 vdupq_n_f32(0.0014122662833f),
39 vdupq_n_f32(1.00000011921f),
40 vdupq_n_f32(0.00833693705499f),
41 vdupq_n_f32(0.166665703058f),
42 vdupq_n_f32(0.000195780929062f),
47 const std::array<float32x4_t, 8>
log_tab =
50 vdupq_n_f32(-2.29561495781f),
51 vdupq_n_f32(-2.47071170807f),
52 vdupq_n_f32(-5.68692588806f),
53 vdupq_n_f32(-0.165253549814f),
54 vdupq_n_f32(5.17591238022f),
55 vdupq_n_f32(0.844007015228f),
56 vdupq_n_f32(4.58445882797f),
57 vdupq_n_f32(0.0141278216615f),
67 #ifndef DOXYGEN_SKIP_THIS 70 static const float32x4_t CONST_1 = vdupq_n_f32(1.f);
72 const int32x4_t z = vcvtq_s32_f32(val);
73 const float32x4_t r = vcvtq_f32_s32(z);
75 return vbslq_f32(vcgtq_f32(r, val), vsubq_f32(r, CONST_1), r);
81 return vrndnq_f32(val);
83 static const float32x4_t CONST_HALF_FLOAT = vdupq_n_f32(0.5f);
84 static const float32x4_t CONST_1_FLOAT = vdupq_n_f32(1.f);
85 static const int32x4_t CONST_1_INT = vdupq_n_s32(1);
87 const float32x4_t diff = vsubq_f32(val, floor_val);
94 return vbslq_f32(vorrq_u32(vcltq_f32(diff, CONST_HALF_FLOAT), vandq_u32(vceqq_f32(diff, CONST_HALF_FLOAT), vmvnq_u32(vtstq_s32(vandq_s32(vcvtq_s32_f32(floor_val), CONST_1_INT), CONST_1_INT)))),
95 floor_val, vaddq_f32(floor_val, CONST_1_FLOAT));
101 float32x2_t sqrt_reciprocal = vrsqrte_f32(x);
102 sqrt_reciprocal = vmul_f32(vrsqrts_f32(vmul_f32(x, sqrt_reciprocal), sqrt_reciprocal), sqrt_reciprocal);
103 sqrt_reciprocal = vmul_f32(vrsqrts_f32(vmul_f32(x, sqrt_reciprocal), sqrt_reciprocal), sqrt_reciprocal);
105 return sqrt_reciprocal;
110 float32x4_t sqrt_reciprocal = vrsqrteq_f32(x);
111 sqrt_reciprocal = vmulq_f32(vrsqrtsq_f32(vmulq_f32(x, sqrt_reciprocal), sqrt_reciprocal), sqrt_reciprocal);
112 sqrt_reciprocal = vmulq_f32(vrsqrtsq_f32(vmulq_f32(x, sqrt_reciprocal), sqrt_reciprocal), sqrt_reciprocal);
114 return sqrt_reciprocal;
117 inline float32x2_t
vinv_f32(float32x2_t x)
119 float32x2_t recip = vrecpe_f32(x);
120 recip = vmul_f32(vrecps_f32(x, recip), recip);
121 recip = vmul_f32(vrecps_f32(x, recip), recip);
125 inline float32x4_t
vinvq_f32(float32x4_t x)
127 float32x4_t recip = vrecpeq_f32(x);
128 recip = vmulq_f32(vrecpsq_f32(x, recip), recip);
129 recip = vmulq_f32(vrecpsq_f32(x, recip), recip);
133 inline float32x4_t
vtaylor_polyq_f32(float32x4_t x,
const std::array<float32x4_t, 8> &coeffs)
135 float32x4_t
A = vmlaq_f32(coeffs[0], coeffs[4], x);
136 float32x4_t
B = vmlaq_f32(coeffs[2], coeffs[6], x);
137 float32x4_t C = vmlaq_f32(coeffs[1], coeffs[5], x);
138 float32x4_t D = vmlaq_f32(coeffs[3], coeffs[7], x);
139 float32x4_t x2 = vmulq_f32(x, x);
140 float32x4_t x4 = vmulq_f32(x2, x2);
141 float32x4_t res = vmlaq_f32(vmlaq_f32(A, B, x2), vmlaq_f32(C, D, x2), x4);
145 inline float32x4_t
vexpq_f32(float32x4_t x)
147 static const float32x4_t CONST_LN2 = vdupq_n_f32(0.6931471805f);
148 static const float32x4_t CONST_INV_LN2 = vdupq_n_f32(1.4426950408f);
149 static const float32x4_t CONST_INF = vdupq_n_f32(std::numeric_limits<float>::infinity());
150 static const float32x4_t CONST_MAX_INPUT = vdupq_n_f32(88.7f);
151 static const float32x4_t CONST_0 = vdupq_n_f32(0.f);
152 static const int32x4_t CONST_NEGATIVE_126 = vdupq_n_s32(-126);
155 int32x4_t m = vcvtq_s32_f32(vmulq_f32(x, CONST_INV_LN2));
156 float32x4_t val = vmlsq_f32(x, vcvtq_f32_s32(m), CONST_LN2);
162 poly = vreinterpretq_f32_s32(vqaddq_s32(vreinterpretq_s32_f32(poly), vqshlq_n_s32(m, 23)));
163 poly = vbslq_f32(vcltq_s32(m, CONST_NEGATIVE_126), CONST_0, poly);
164 poly = vbslq_f32(vcgtq_f32(x, CONST_MAX_INPUT), CONST_INF, poly);
169 inline float32x4_t
vlogq_f32(float32x4_t x)
171 static const int32x4_t CONST_127 = vdupq_n_s32(127);
172 static const float32x4_t CONST_LN2 = vdupq_n_f32(0.6931471805f);
175 int32x4_t m = vsubq_s32(vreinterpretq_s32_u32(vshrq_n_u32(vreinterpretq_u32_f32(x), 23)), CONST_127);
176 float32x4_t val = vreinterpretq_f32_s32(vsubq_s32(vreinterpretq_s32_f32(x), vshlq_n_s32(m, 23)));
182 poly = vmlaq_f32(poly, vcvtq_f32_s32(m), CONST_LN2);
187 inline float32x4_t
vtanhq_f32(float32x4_t val)
189 static const float32x4_t CONST_1 = vdupq_n_f32(1.f);
190 static const float32x4_t CONST_2 = vdupq_n_f32(2.f);
191 static const float32x4_t CONST_MIN_TANH = vdupq_n_f32(-10.f);
192 static const float32x4_t CONST_MAX_TANH = vdupq_n_f32(10.f);
194 float32x4_t x = vminq_f32(vmaxq_f32(val, CONST_MIN_TANH), CONST_MAX_TANH);
195 float32x4_t exp2x =
vexpq_f32(vmulq_f32(CONST_2, x));
196 float32x4_t num = vsubq_f32(exp2x, CONST_1);
197 float32x4_t den = vaddq_f32(exp2x, CONST_1);
198 float32x4_t tanh = vmulq_f32(num,
vinvq_f32(den));
202 inline float32x4_t
vpowq_f32(float32x4_t val, float32x4_t n)
207 inline float32x4_t
vsinq_f32(float32x4_t val)
209 const float32x4_t pi_v = vdupq_n_f32(
M_PI);
210 const float32x4_t pio2_v = vdupq_n_f32(
M_PI / 2);
211 const float32x4_t ipi_v = vdupq_n_f32(1 /
M_PI);
214 const int32x4_t c_v = vabsq_s32(vcvtq_s32_f32(vmulq_f32(val, ipi_v)));
215 const uint32x4_t sign_v = vcleq_f32(val, vdupq_n_f32(0));
216 const uint32x4_t odd_v = vandq_u32(vreinterpretq_u32_s32(c_v), vdupq_n_u32(1));
218 uint32x4_t neg_v = veorq_u32(odd_v, sign_v);
221 float32x4_t ma = vsubq_f32(vabsq_f32(val), vmulq_f32(pi_v, vcvtq_f32_s32(c_v)));
222 const uint32x4_t reb_v = vcgeq_f32(ma, pio2_v);
225 ma = vbslq_f32(reb_v, vsubq_f32(pi_v, ma), ma);
228 const float32x4_t ma2 = vmulq_f32(ma, ma);
231 float32x4_t elem = vmulq_f32(vmulq_f32(ma, ma2), vdupq_n_f32(te_sin_coeff2));
232 float32x4_t res = vsubq_f32(ma, elem);
235 elem = vmulq_f32(vmulq_f32(elem, ma2), vdupq_n_f32(te_sin_coeff3));
236 res = vaddq_f32(res, elem);
239 elem = vmulq_f32(vmulq_f32(elem, ma2), vdupq_n_f32(te_sin_coeff4));
240 res = vsubq_f32(res, elem);
243 elem = vmulq_f32(vmulq_f32(elem, ma2), vdupq_n_f32(te_sin_coeff5));
244 res = vaddq_f32(res, elem);
247 neg_v = vshlq_n_u32(neg_v, 31);
248 res = vreinterpretq_f32_u32(veorq_u32(vreinterpretq_u32_f32(res), neg_v));
252 inline float32x2_t
vsin_f32(float32x2_t val)
254 const float32x2_t pi_v = vdup_n_f32(
M_PI);
255 const float32x2_t pio2_v = vdup_n_f32(
M_PI / 2);
256 const float32x2_t ipi_v = vdup_n_f32(1 /
M_PI);
259 const int32x2_t c_v = vabs_s32(vcvt_s32_f32(vmul_f32(val, ipi_v)));
260 const uint32x2_t sign_v = vcle_f32(val, vdup_n_f32(0));
261 const uint32x2_t odd_v = vand_u32(vreinterpret_u32_s32(c_v), vdup_n_u32(1));
263 uint32x2_t neg_v = veor_u32(odd_v, sign_v);
266 float32x2_t ma = vsub_f32(vabs_f32(val), vmul_f32(pi_v, vcvt_f32_s32(c_v)));
267 const uint32x2_t reb_v = vcge_f32(ma, pio2_v);
270 ma = vbsl_f32(reb_v, vsub_f32(pi_v, ma), ma);
273 const float32x2_t ma2 = vmul_f32(ma, ma);
276 float32x2_t elem = vmul_f32(vmul_f32(ma, ma2), vdup_n_f32(te_sin_coeff2));
277 float32x2_t res = vsub_f32(ma, elem);
280 elem = vmul_f32(vmul_f32(elem, ma2), vdup_n_f32(te_sin_coeff3));
281 res = vadd_f32(res, elem);
284 elem = vmul_f32(vmul_f32(elem, ma2), vdup_n_f32(te_sin_coeff4));
285 res = vsub_f32(res, elem);
288 elem = vmul_f32(vmul_f32(elem, ma2), vdup_n_f32(te_sin_coeff5));
289 res = vadd_f32(res, elem);
292 neg_v = vshl_n_u32(neg_v, 31);
293 res = vreinterpret_f32_u32(veor_u32(vreinterpret_u32_f32(res), neg_v));
301 const int32x4_t shift_vec = vnegq_s32(exponent);
302 const int32x4_t fixup = vshrq_n_s32(vandq_s32(x, shift_vec), 31);
303 const int32x4_t fixed_up_x = vqaddq_s32(x, fixup);
304 return vrshlq_s32(fixed_up_x, shift_vec);
309 const int32x4_t shift_vec = vdupq_n_s32(-exponent);
310 const int32x4_t fixup = vshrq_n_s32(vandq_s32(x, shift_vec), 31);
311 const int32x4_t fixed_up_x = vqaddq_s32(x, fixup);
312 return vrshlq_s32(fixed_up_x, shift_vec);
317 const int32_t mask = (1 << exponent) - 1;
318 const int32_t
threshold = (mask >> 1) + (x < 0 ? 1 : 0);
319 return (x >> exponent) + ((x & mask) > threshold ? 1 : 0);
326 const auto tmp1 = vmovl_u8(vget_low_u8(in));
327 out.val[0] = vcvtq_f32_u32(vmovl_u16(vget_low_u16(tmp1)));
328 out.val[1] = vcvtq_f32_u32(vmovl_u16(vget_high_u16(tmp1)));
330 const auto tmp2 = vmovl_u8(vget_high_u8(in));
331 out.val[2] = vcvtq_f32_u32(vmovl_u16(vget_low_u16(tmp2)));
332 out.val[3] = vcvtq_f32_u32(vmovl_u16(vget_high_u16(tmp2)));
340 const auto tmp1 = vmovl_s8(vget_low_s8(in));
341 out.val[0] = vcvtq_f32_s32(vmovl_s16(vget_low_s16(tmp1)));
342 out.val[1] = vcvtq_f32_s32(vmovl_s16(vget_high_s16(tmp1)));
344 const auto tmp2 = vmovl_s8(vget_high_s8(in));
345 out.val[2] = vcvtq_f32_s32(vmovl_s16(vget_low_s16(tmp2)));
346 out.val[3] = vcvtq_f32_s32(vmovl_s16(vget_high_s16(tmp2)));
364 out.val[0] = vqmovn_u16(vcombine_u16(vqmovn_u32(vcvtq_u32_f32(in1.val[0])),
365 vqmovn_u32(vcvtq_u32_f32(in2.val[0]))));
366 out.val[1] = vqmovn_u16(vcombine_u16(vqmovn_u32(vcvtq_u32_f32(in1.val[1])),
367 vqmovn_u32(vcvtq_u32_f32(in2.val[1]))));
368 out.val[2] = vqmovn_u16(vcombine_u16(vqmovn_u32(vcvtq_u32_f32(in1.val[2])),
369 vqmovn_u32(vcvtq_u32_f32(in2.val[2]))));
374 const auto low = vcombine_u16(vqmovn_u32(vcvtq_u32_f32(in.val[0])),
375 vqmovn_u32(vcvtq_u32_f32(in.val[1])));
376 const auto high = vcombine_u16(vqmovn_u32(vcvtq_u32_f32(in.val[2])),
377 vqmovn_u32(vcvtq_u32_f32(in.val[3])));
378 out = vcombine_u8(vqmovn_u16(low), vqmovn_u16(high));
383 const auto low = vcombine_s16(vqmovn_s32(vcvtq_s32_f32(in.val[0])),
384 vqmovn_s32(vcvtq_s32_f32(in.val[1])));
385 const auto high = vcombine_s16(vqmovn_s32(vcvtq_s32_f32(in.val[2])),
386 vqmovn_s32(vcvtq_s32_f32(in.val[3])));
387 out = vcombine_s8(vqmovn_s16(low), vqmovn_s16(high));
390 #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC 393 #ifndef DOXYGEN_SKIP_THIS 394 inline float16x8_t vfloorq_f16(float16x8_t val)
396 static const float16x8_t CONST_1 = vdupq_n_f16(1.f);
404 inline float16x8_t vroundq_rte_f16(float16x8_t val)
406 return vrndnq_f16(val);
409 inline float16x4_t vinvsqrt_f16(float16x4_t x)
414 return sqrt_reciprocal;
417 inline float16x8_t vinvsqrtq_f16(float16x8_t x)
422 return sqrt_reciprocal;
425 inline float16x4_t vinv_f16(float16x4_t x)
433 inline float16x8_t vinvq_f16(float16x8_t x)
441 inline float16x8_t vtanhq_f16(float16x8_t val)
443 const float16x8_t CONST_1 = vdupq_n_f16(1.f);
444 const float16x8_t CONST_2 = vdupq_n_f16(2.f);
445 const float16x8_t CONST_MIN_TANH = vdupq_n_f16(-10.f);
446 const float16x8_t CONST_MAX_TANH = vdupq_n_f16(10.f);
449 const float16x8_t exp2x = vexpq_f16(
vmulq_f16(CONST_2, x));
450 const float16x8_t num =
vsubq_f16(exp2x, CONST_1);
451 const float16x8_t den =
vaddq_f16(exp2x, CONST_1);
452 const float16x8_t tanh =
vmulq_f16(num, vinvq_f16(den));
456 inline float16x8_t vtaylor_polyq_f16(float16x8_t x,
const std::array<float16x8_t, 8> &coeffs)
463 const float16x8_t x4 =
vmulq_f16(x2, x2);
468 inline float16x8_t vexpq_f16(float16x8_t x)
471 const float32x4_t x_high = vcvt_f32_f16(vget_high_f16(x));
472 const float32x4_t x_low = vcvt_f32_f16(vget_low_f16(x));
474 const float16x8_t res = vcombine_f16(vcvt_f16_f32(
vexpq_f32(x_low)), vcvt_f16_f32(
vexpq_f32(x_high)));
478 inline float16x8_t vlogq_f16(float16x8_t x)
481 const float32x4_t x_high = vcvt_f32_f16(vget_high_f16(x));
482 const float32x4_t x_low = vcvt_f32_f16(vget_low_f16(x));
484 const float16x8_t res = vcombine_f16(vcvt_f16_f32(
vlogq_f32(x_low)), vcvt_f16_f32(
vlogq_f32(x_high)));
488 inline float16x8_t vpowq_f16(float16x8_t val, float16x8_t n)
491 float32x4_t n0_f32 = vcvt_f32_f16(vget_low_f16(n));
492 float32x4_t n1_f32 = vcvt_f32_f16(vget_high_f16(n));
493 float32x4_t val0_f32 = vcvt_f32_f16(vget_low_f16(val));
494 float32x4_t val1_f32 = vcvt_f32_f16(vget_high_f16(val));
499 return vcombine_f16(vcvt_f16_f32(res0_f32), vcvt_f16_f32(res1_f32));
502 inline float16x8_t vsinq_f16(float16x8_t val)
504 const float32x4_t val_high = vcvt_f32_f16(vget_high_f16(val));
505 const float32x4_t val_low = vcvt_f32_f16(vget_low_f16(val));
507 const float32x4_t res_high =
vsinq_f32(val_high);
508 const float32x4_t res_low =
vsinq_f32(val_low);
510 return vcombine_f16(vcvt_f16_f32(res_low), vcvt_f16_f32(res_high));
513 inline float16x4_t vsin_f16(float16x4_t val)
515 const float32x4_t val_f32 = vcvt_f32_f16(val);
516 const float32x2_t val_high = vget_high_f32(val_f32);
517 const float32x2_t val_low = vget_low_f32(val_f32);
519 const float32x2_t res_high =
vsin_f32(val_high);
520 const float32x2_t res_low =
vsin_f32(val_low);
522 return vcvt_f16_f32(vcombine_f32(res_low, res_high));
float32x2_t vsin_f32(float32x2_t val)
Calculate sine.
float16x8_t vmaxq_f16(float16x8_t, float16x8_t)
float32x4_t vtanhq_f32(float32x4_t val)
Calculate hyperbolic tangent.
uint16x8_t vcvtq_f16_s16(float16x8_t)
float32x4_t vinvsqrtq_f32(float32x4_t x)
Calculate inverse square root.
constexpr float te_sin_coeff5
float32x2_t vinv_f32(float32x2_t x)
Calculate reciprocal.
float16x8_t vsubq_f16(float16x8_t, float16x8_t)
float16x8_t vmulq_f16(float16x8_t, float16x8_t)
float16x8_t vrsqrteq_f16(float16x8_t)
float32x4x4_t convert_to_float32x4x4(const T &in)
Converts to float32x4x4_t from the specified templated 16 elements vectors.
float32x4x4_t convert_int8x16_to_float32x4x4(const int8x16_t &in)
Converts from int8x16 to float32x4x4_t.
float32x4x4_t convert_uint8x16_to_float32x4x4(const uint8x16_t &in)
Converts from uint8x16 to float32x4x4_t.
float16x8_t vaddq_f16(float16x8_t, float16x8_t)
float16x8_t vrsqrtsq_f16(float16x8_t, float16x8_t)
float32x4_t vtaylor_polyq_f32(float32x4_t x, const std::array< float32x4_t, 8 > &coeffs)
Perform a 7th degree polynomial approximation using Estrin's method.
Copyright (c) 2017-2021 Arm Limited.
float32x4_t vfloorq_f32(float32x4_t val)
Calculate floor of a vector.
const std::array< float32x4_t, 8 > exp_tab
Exponent polynomial coefficients.
float16x8_t vrecpsq_f16(float16x8_t, float16x8_t)
float16x4_t vrsqrte_f16(float16x4_t)
float32x4_t vpowq_f32(float32x4_t val, float32x4_t n)
Calculate n power of a number.
float16x4_t vrecpe_f16(float16x4_t)
float16x8_t vrecpeq_f16(float16x8_t)
float16x4_t vrecps_f16(float16x4_t, float16x4_t)
void convert_float32x4x4_to_int8x16(const float32x4x4_t &in, int8x16_t &out)
Converts from float32x4x4_t to just one int8x16_t.
constexpr float te_sin_coeff3
float16x4_t vmul_f16(float16x4_t, float16x4_t)
float16x8_t vbslq_f16(uint16x8_t, float16x8_t, float16x8_t)
float32x4_t vlogq_f32(float32x4_t x)
Calculate logarithm.
constexpr float te_sin_coeff4
int32x4_t rounding_divide_by_pow2(int32x4_t x, int32x4_t exponent)
Round to the nearest division by a power-of-two using exponent.
float32x2_t vinvsqrt_f32(float32x2_t x)
Calculate inverse square root.
float32x4_t vsinq_f32(float32x4_t val)
Calculate sine.
float32x4_t vroundq_rte_f32(float32x4_t val)
Calculate round value of a vector to nearest with ties to even.
float16x4_t vrsqrts_f16(float16x4_t, float16x4_t)
float32x4_t vexpq_f32(float32x4_t x)
Calculate exponential.
constexpr float te_sin_coeff2
Sin polynomial coefficients.
float32x4_t vinvq_f32(float32x4_t x)
Calculate reciprocal.
int16x8_t vcvtq_s16_f16(float16x8_t)
void convert_float32x4x4_to_uint8x16(const float32x4x4_t &in, uint8x16_t &out)
Converts from two float32x4x4_t to just one uint8x16_t.
float16x8_t vminq_f16(float16x8_t, float16x8_t)
uint16x8_t vcgtq_f16(float16x8_t, float16x8_t)
SimpleTensor< T > threshold(const SimpleTensor< T > &src, T threshold, T false_value, T true_value, ThresholdType type, T upper)
void convert_float32x4x3_to_uint8x8x3(const float32x4x3_t &in1, const float32x4x3_t &in2, uint8x8x3_t &out)
Converts from two float32x4x3_t to just one uint8x8x3_t.
const std::array< float32x4_t, 8 > log_tab
Logarithm polynomial coefficients.