blob: acac36e1a58568f5643d5504aa98921cbe601a07 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Murray Kornelsen926f5022022-07-13 21:22:39 -04002 * Copyright (c) 2016-2022 Arm Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
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 */
SiCongLi410e21e2020-12-11 15:07:53 +000024#include "support/ToolchainSupport.h"
25
Manuel Bottinied753262019-05-15 15:30:47 +010026#include <cmath>
morgolock3155f772020-05-11 16:00:04 +010027#include <limits>
Manuel Bottinied753262019-05-15 15:30:47 +010028
Anthony Barbier6ff3b192017-09-04 18:44:23 +010029namespace arm_compute
30{
Alex Gildayc357c472018-03-21 13:54:09 +000031/** Exponent polynomial coefficients */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010032const std::array<float32x4_t, 8> exp_tab =
33{
34 {
35 vdupq_n_f32(1.f),
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),
43 }
44};
45
Alex Gildayc357c472018-03-21 13:54:09 +000046/** Logarithm polynomial coefficients */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010047const std::array<float32x4_t, 8> log_tab =
48{
49 {
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),
58 }
59};
60
Manuel Bottinied753262019-05-15 15:30:47 +010061/** Sin polynomial coefficients */
62constexpr float te_sin_coeff2 = 0.166666666666f; // 1/(2*3)
63constexpr float te_sin_coeff3 = 0.05f; // 1/(4*5)
64constexpr float te_sin_coeff4 = 0.023809523810f; // 1/(6*7)
65constexpr float te_sin_coeff5 = 0.013888888889f; // 1/(8*9)
66
Alex Gildayc357c472018-03-21 13:54:09 +000067#ifndef DOXYGEN_SKIP_THIS
Georgios Pinitasd8e765b2017-08-02 13:44:33 +010068inline float32x4_t vfloorq_f32(float32x4_t val)
69{
70 static const float32x4_t CONST_1 = vdupq_n_f32(1.f);
71
72 const int32x4_t z = vcvtq_s32_f32(val);
73 const float32x4_t r = vcvtq_f32_s32(z);
74
75 return vbslq_f32(vcgtq_f32(r, val), vsubq_f32(r, CONST_1), r);
76}
77
Usama Arif0a5a57a2019-05-23 14:20:33 +010078inline float32x4_t vroundq_rte_f32(float32x4_t val)
79{
80#ifdef __aarch64__
81 return vrndnq_f32(val);
Manuel Bottini7bb56c62019-06-26 15:17:09 +010082#else // __aarch64__
Usama Arif0a5a57a2019-05-23 14:20:33 +010083 static const float32x4_t CONST_HALF_FLOAT = vdupq_n_f32(0.5f);
Manuel Bottini7bb56c62019-06-26 15:17:09 +010084 static const float32x4_t CONST_1_FLOAT = vdupq_n_f32(1.f);
85 static const int32x4_t CONST_1_INT = vdupq_n_s32(1);
86 const float32x4_t floor_val = vfloorq_f32(val);
87 const float32x4_t diff = vsubq_f32(val, floor_val);
Usama Arif0a5a57a2019-05-23 14:20:33 +010088
89 /*
90 * Select the floor value when (diff<0.5 || (diff==0.5 && floor_val%2==0).
91 * This condition is checked by 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))))
92 */
93
Manuel Bottini7bb56c62019-06-26 15:17:09 +010094 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));
Usama Arif0a5a57a2019-05-23 14:20:33 +010096#endif // __aarch64__
97}
98
Georgios Pinitascdf51452017-08-31 14:21:36 +010099inline float32x2_t vinvsqrt_f32(float32x2_t x)
100{
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);
104
105 return sqrt_reciprocal;
106}
107
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100108inline float32x4_t vinvsqrtq_f32(float32x4_t x)
109{
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);
113
114 return sqrt_reciprocal;
115}
116
Georgios Pinitascdf51452017-08-31 14:21:36 +0100117inline float32x2_t vinv_f32(float32x2_t x)
118{
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);
122 return recip;
123}
124
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100125inline float32x4_t vinvq_f32(float32x4_t x)
126{
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);
130 return recip;
131}
132
133inline float32x4_t vtaylor_polyq_f32(float32x4_t x, const std::array<float32x4_t, 8> &coeffs)
134{
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);
142 return res;
143}
144
145inline float32x4_t vexpq_f32(float32x4_t x)
146{
Georgios Pinitasee122542017-06-26 15:54:06 +0100147 static const float32x4_t CONST_LN2 = vdupq_n_f32(0.6931471805f); // ln(2)
148 static const float32x4_t CONST_INV_LN2 = vdupq_n_f32(1.4426950408f); // 1/ln(2)
morgolock3155f772020-05-11 16:00:04 +0100149 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);
Georgios Pinitasee122542017-06-26 15:54:06 +0100151 static const float32x4_t CONST_0 = vdupq_n_f32(0.f);
152 static const int32x4_t CONST_NEGATIVE_126 = vdupq_n_s32(-126);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100153
154 // Perform range reduction [-log(2),log(2)]
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);
157
158 // Polynomial Approximation
159 float32x4_t poly = vtaylor_polyq_f32(val, exp_tab);
160
161 // Reconstruct
Georgios Pinitasee122542017-06-26 15:54:06 +0100162 poly = vreinterpretq_f32_s32(vqaddq_s32(vreinterpretq_s32_f32(poly), vqshlq_n_s32(m, 23)));
morgolock3155f772020-05-11 16:00:04 +0100163 poly = vbslq_f32(vcltq_s32(m, CONST_NEGATIVE_126), CONST_0, poly); // Handle underflow
164 poly = vbslq_f32(vcgtq_f32(x, CONST_MAX_INPUT), CONST_INF, poly); // Handle overflow
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100165
166 return poly;
167}
168
Murray Kornelsen926f5022022-07-13 21:22:39 -0400169#ifdef __aarch64__
170inline float32x4_t verfq_f32(float32x4_t x)
171{
172 static const float erffdata[4] = { 0.278393f, 0.230389f, 0.000972f, 0.078108f };
173 static const float32x4_t coeffdata = vld1q_f32(erffdata);
174 static const float32x4_t onev{ vdupq_n_f32(1.0f) };
175
176 uint32x4_t selector = vcltzq_f32(x);
177
178 float32x4_t absx = vabsq_f32(x);
179 float32x4_t absx2 = vmulq_f32(x, x);
180 float32x4_t absx3 = vmulq_f32(absx2, absx);
181 float32x4_t absx4 = vmulq_f32(absx2, absx2);
182
183 float32x4_t denom = onev;
184 denom = vfmaq_laneq_f32(denom, absx, coeffdata, 0);
185 denom = vfmaq_laneq_f32(denom, absx2, coeffdata, 1);
186 denom = vfmaq_laneq_f32(denom, absx3, coeffdata, 2);
187 denom = vfmaq_laneq_f32(denom, absx4, coeffdata, 3);
188
189 denom = vmulq_f32(denom, denom);
190 denom = vmulq_f32(denom, denom);
191
192 float32x4_t fract = onev;
193 fract = vdivq_f32(fract, denom);
194
195 float32x4_t result = onev;
196 result = vsubq_f32(result, fract);
197
198 float32x4_t inverse = vnegq_f32(result);
199
200 result = vbslq_f32(selector, inverse, result);
201
202 return result;
203}
204#endif // #ifdef __aarch64__
205
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100206inline float32x4_t vlogq_f32(float32x4_t x)
207{
208 static const int32x4_t CONST_127 = vdupq_n_s32(127); // 127
209 static const float32x4_t CONST_LN2 = vdupq_n_f32(0.6931471805f); // ln(2)
210
211 // Extract exponent
212 int32x4_t m = vsubq_s32(vreinterpretq_s32_u32(vshrq_n_u32(vreinterpretq_u32_f32(x), 23)), CONST_127);
213 float32x4_t val = vreinterpretq_f32_s32(vsubq_s32(vreinterpretq_s32_f32(x), vshlq_n_s32(m, 23)));
214
215 // Polynomial Approximation
216 float32x4_t poly = vtaylor_polyq_f32(val, log_tab);
217
218 // Reconstruct
219 poly = vmlaq_f32(poly, vcvtq_f32_s32(m), CONST_LN2);
220
221 return poly;
222}
223
224inline float32x4_t vtanhq_f32(float32x4_t val)
225{
226 static const float32x4_t CONST_1 = vdupq_n_f32(1.f);
227 static const float32x4_t CONST_2 = vdupq_n_f32(2.f);
228 static const float32x4_t CONST_MIN_TANH = vdupq_n_f32(-10.f);
229 static const float32x4_t CONST_MAX_TANH = vdupq_n_f32(10.f);
Aleksandr Nikolaev7e9f34d2021-05-04 16:46:27 +0100230 static const float32x4_t CONST_THR = vdupq_n_f32(5.e-3);
231 static const float32x4_t CONST_1_3 = vdupq_n_f32(0.3333333f);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100232
Sheri Zhang5dda2172021-10-15 19:54:17 +0100233 float32x4_t x = vminq_f32(vmaxq_f32(val, CONST_MIN_TANH), CONST_MAX_TANH);
Aleksandr Nikolaev7e9f34d2021-05-04 16:46:27 +0100234 // x * (1 - x^2/3) if |x| < 5.e-3 or (exp2x - 1) / (exp2x + 1) otherwise
235 float32x4_t exp2x = vbslq_f32(vcgtq_f32(vabsq_f32(x), CONST_THR), vexpq_f32(vmulq_f32(CONST_2, x)), vmulq_f32(x, x));
236 float32x4_t num = vbslq_f32(vcgtq_f32(vabsq_f32(x), CONST_THR), vsubq_f32(exp2x, CONST_1), vmulq_f32(CONST_1_3, exp2x));
237 float32x4_t den = vbslq_f32(vcgtq_f32(vabsq_f32(x), CONST_THR), vaddq_f32(exp2x, CONST_1), vsubq_f32(CONST_1, num));
238 float32x4_t tanh = vbslq_f32(vcgtq_f32(vabsq_f32(x), CONST_THR), vmulq_f32(num, vinvq_f32(den)), vmulq_f32(x, den));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100239 return tanh;
240}
241
242inline float32x4_t vpowq_f32(float32x4_t val, float32x4_t n)
243{
244 return vexpq_f32(vmulq_f32(n, vlogq_f32(val)));
245}
Manuel Bottinied753262019-05-15 15:30:47 +0100246
247inline float32x4_t vsinq_f32(float32x4_t val)
248{
249 const float32x4_t pi_v = vdupq_n_f32(M_PI);
250 const float32x4_t pio2_v = vdupq_n_f32(M_PI / 2);
251 const float32x4_t ipi_v = vdupq_n_f32(1 / M_PI);
252
253 //Find positive or negative
254 const int32x4_t c_v = vabsq_s32(vcvtq_s32_f32(vmulq_f32(val, ipi_v)));
255 const uint32x4_t sign_v = vcleq_f32(val, vdupq_n_f32(0));
256 const uint32x4_t odd_v = vandq_u32(vreinterpretq_u32_s32(c_v), vdupq_n_u32(1));
257
258 uint32x4_t neg_v = veorq_u32(odd_v, sign_v);
259
260 //Modulus a - (n * int(a*(1/n)))
261 float32x4_t ma = vsubq_f32(vabsq_f32(val), vmulq_f32(pi_v, vcvtq_f32_s32(c_v)));
262 const uint32x4_t reb_v = vcgeq_f32(ma, pio2_v);
263
264 //Rebase a between 0 and pi/2
265 ma = vbslq_f32(reb_v, vsubq_f32(pi_v, ma), ma);
266
267 //Taylor series
268 const float32x4_t ma2 = vmulq_f32(ma, ma);
269
270 //2nd elem: x^3 / 3!
271 float32x4_t elem = vmulq_f32(vmulq_f32(ma, ma2), vdupq_n_f32(te_sin_coeff2));
272 float32x4_t res = vsubq_f32(ma, elem);
273
274 //3rd elem: x^5 / 5!
275 elem = vmulq_f32(vmulq_f32(elem, ma2), vdupq_n_f32(te_sin_coeff3));
276 res = vaddq_f32(res, elem);
277
278 //4th elem: x^7 / 7!float32x2_t vsin_f32(float32x2_t val)
279 elem = vmulq_f32(vmulq_f32(elem, ma2), vdupq_n_f32(te_sin_coeff4));
280 res = vsubq_f32(res, elem);
281
282 //5th elem: x^9 / 9!
283 elem = vmulq_f32(vmulq_f32(elem, ma2), vdupq_n_f32(te_sin_coeff5));
284 res = vaddq_f32(res, elem);
285
286 //Change of sign
287 neg_v = vshlq_n_u32(neg_v, 31);
288 res = vreinterpretq_f32_u32(veorq_u32(vreinterpretq_u32_f32(res), neg_v));
289 return res;
290}
291
292inline float32x2_t vsin_f32(float32x2_t val)
293{
294 const float32x2_t pi_v = vdup_n_f32(M_PI);
295 const float32x2_t pio2_v = vdup_n_f32(M_PI / 2);
296 const float32x2_t ipi_v = vdup_n_f32(1 / M_PI);
297
298 //Find positive or negative
299 const int32x2_t c_v = vabs_s32(vcvt_s32_f32(vmul_f32(val, ipi_v)));
300 const uint32x2_t sign_v = vcle_f32(val, vdup_n_f32(0));
301 const uint32x2_t odd_v = vand_u32(vreinterpret_u32_s32(c_v), vdup_n_u32(1));
302
303 uint32x2_t neg_v = veor_u32(odd_v, sign_v);
304
305 //Modulus a - (n * int(a*(1/n)))
306 float32x2_t ma = vsub_f32(vabs_f32(val), vmul_f32(pi_v, vcvt_f32_s32(c_v)));
307 const uint32x2_t reb_v = vcge_f32(ma, pio2_v);
308
309 //Rebase a between 0 and pi/2
310 ma = vbsl_f32(reb_v, vsub_f32(pi_v, ma), ma);
311
312 //Taylor series
313 const float32x2_t ma2 = vmul_f32(ma, ma);
314
315 //2nd elem: x^3 / 3!
316 float32x2_t elem = vmul_f32(vmul_f32(ma, ma2), vdup_n_f32(te_sin_coeff2));
317 float32x2_t res = vsub_f32(ma, elem);
318
319 //3rd elem: x^5 / 5!
320 elem = vmul_f32(vmul_f32(elem, ma2), vdup_n_f32(te_sin_coeff3));
321 res = vadd_f32(res, elem);
322
323 //4th elem: x^7 / 7!float32x2_t vsin_f32(float32x2_t val)
324 elem = vmul_f32(vmul_f32(elem, ma2), vdup_n_f32(te_sin_coeff4));
325 res = vsub_f32(res, elem);
326
327 //5th elem: x^9 / 9!
328 elem = vmul_f32(vmul_f32(elem, ma2), vdup_n_f32(te_sin_coeff5));
329 res = vadd_f32(res, elem);
330
331 //Change of sign
332 neg_v = vshl_n_u32(neg_v, 31);
333 res = vreinterpret_f32_u32(veor_u32(vreinterpret_u32_f32(res), neg_v));
334 return res;
335}
336
Alex Gildayc357c472018-03-21 13:54:09 +0000337#endif /* DOXYGEN_SKIP_THIS */
338
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100339inline int32x4_t rounding_divide_by_pow2(int32x4_t x, int32x4_t exponent)
340{
341 const int32x4_t shift_vec = vnegq_s32(exponent);
342 const int32x4_t fixup = vshrq_n_s32(vandq_s32(x, shift_vec), 31);
343 const int32x4_t fixed_up_x = vqaddq_s32(x, fixup);
344 return vrshlq_s32(fixed_up_x, shift_vec);
345}
346
Manuel Bottini7bb56c62019-06-26 15:17:09 +0100347inline int32x4_t rounding_divide_by_pow2(int32x4_t x, int exponent)
348{
349 const int32x4_t shift_vec = vdupq_n_s32(-exponent);
350 const int32x4_t fixup = vshrq_n_s32(vandq_s32(x, shift_vec), 31);
351 const int32x4_t fixed_up_x = vqaddq_s32(x, fixup);
352 return vrshlq_s32(fixed_up_x, shift_vec);
353}
354
355inline int32_t rounding_divide_by_pow2(int32_t x, int exponent)
356{
357 const int32_t mask = (1 << exponent) - 1;
358 const int32_t threshold = (mask >> 1) + (x < 0 ? 1 : 0);
359 return (x >> exponent) + ((x & mask) > threshold ? 1 : 0);
360}
361
Manuel Bottini21079dd2019-10-29 17:20:09 +0000362inline float32x4x4_t convert_uint8x16_to_float32x4x4(const uint8x16_t &in)
363{
364 float32x4x4_t out;
365
366 const auto tmp1 = vmovl_u8(vget_low_u8(in));
367 out.val[0] = vcvtq_f32_u32(vmovl_u16(vget_low_u16(tmp1)));
368 out.val[1] = vcvtq_f32_u32(vmovl_u16(vget_high_u16(tmp1)));
369
370 const auto tmp2 = vmovl_u8(vget_high_u8(in));
371 out.val[2] = vcvtq_f32_u32(vmovl_u16(vget_low_u16(tmp2)));
372 out.val[3] = vcvtq_f32_u32(vmovl_u16(vget_high_u16(tmp2)));
373 return out;
374}
375
Sang-Hoon Parkc3a74202019-11-22 16:05:46 +0000376inline float32x4x4_t convert_int8x16_to_float32x4x4(const int8x16_t &in)
377{
378 float32x4x4_t out;
379
380 const auto tmp1 = vmovl_s8(vget_low_s8(in));
381 out.val[0] = vcvtq_f32_s32(vmovl_s16(vget_low_s16(tmp1)));
382 out.val[1] = vcvtq_f32_s32(vmovl_s16(vget_high_s16(tmp1)));
383
384 const auto tmp2 = vmovl_s8(vget_high_s8(in));
385 out.val[2] = vcvtq_f32_s32(vmovl_s16(vget_low_s16(tmp2)));
386 out.val[3] = vcvtq_f32_s32(vmovl_s16(vget_high_s16(tmp2)));
387 return out;
388}
389
Manuel Bottini4370cff2020-02-07 16:31:59 +0000390template <>
391inline float32x4x4_t convert_to_float32x4x4(const uint8x16_t &in)
392{
393 return convert_uint8x16_to_float32x4x4(in);
394}
395
396template <>
397inline float32x4x4_t convert_to_float32x4x4(const int8x16_t &in)
398{
399 return convert_int8x16_to_float32x4x4(in);
400}
401
Manuel Bottini21079dd2019-10-29 17:20:09 +0000402inline void convert_float32x4x3_to_uint8x8x3(const float32x4x3_t &in1, const float32x4x3_t &in2, uint8x8x3_t &out)
403{
404 out.val[0] = vqmovn_u16(vcombine_u16(vqmovn_u32(vcvtq_u32_f32(in1.val[0])),
405 vqmovn_u32(vcvtq_u32_f32(in2.val[0]))));
406 out.val[1] = vqmovn_u16(vcombine_u16(vqmovn_u32(vcvtq_u32_f32(in1.val[1])),
407 vqmovn_u32(vcvtq_u32_f32(in2.val[1]))));
408 out.val[2] = vqmovn_u16(vcombine_u16(vqmovn_u32(vcvtq_u32_f32(in1.val[2])),
409 vqmovn_u32(vcvtq_u32_f32(in2.val[2]))));
410}
411
Sang-Hoon Parkc3a74202019-11-22 16:05:46 +0000412inline void convert_float32x4x4_to_uint8x16(const float32x4x4_t &in, uint8x16_t &out)
Manuel Bottini21079dd2019-10-29 17:20:09 +0000413{
414 const auto low = vcombine_u16(vqmovn_u32(vcvtq_u32_f32(in.val[0])),
415 vqmovn_u32(vcvtq_u32_f32(in.val[1])));
416 const auto high = vcombine_u16(vqmovn_u32(vcvtq_u32_f32(in.val[2])),
417 vqmovn_u32(vcvtq_u32_f32(in.val[3])));
418 out = vcombine_u8(vqmovn_u16(low), vqmovn_u16(high));
419}
420
Sang-Hoon Parkc3a74202019-11-22 16:05:46 +0000421inline void convert_float32x4x4_to_int8x16(const float32x4x4_t &in, int8x16_t &out)
422{
423 const auto low = vcombine_s16(vqmovn_s32(vcvtq_s32_f32(in.val[0])),
424 vqmovn_s32(vcvtq_s32_f32(in.val[1])));
425 const auto high = vcombine_s16(vqmovn_s32(vcvtq_s32_f32(in.val[2])),
426 vqmovn_s32(vcvtq_s32_f32(in.val[3])));
427 out = vcombine_s8(vqmovn_s16(low), vqmovn_s16(high));
428}
429
Sang-Hoon Parkdcf3c7e2021-03-04 17:03:46 +0000430template <>
431inline uint8x16_t convert_float_to_int<float32x4x4_t, uint8x16_t>(const float32x4x4_t &in)
432{
433 uint8x16_t out;
434 convert_float32x4x4_to_uint8x16(in, out);
435 return out;
436}
437
438template <>
439inline float32x4x4_t convert_int_to_float<float32x4x4_t, uint8x16_t>(const uint8x16_t &in)
440{
441 return convert_uint8x16_to_float32x4x4(in);
442}
443
444template <>
445inline int8x16_t convert_float_to_int<float32x4x4_t, int8x16_t>(const float32x4x4_t &in)
446{
447 int8x16_t out;
448 convert_float32x4x4_to_int8x16(in, out);
449 return out;
450}
451
452template <>
453inline float32x4x4_t convert_int_to_float<float32x4x4_t, int8x16_t>(const int8x16_t &in)
454{
455 return convert_int8x16_to_float32x4x4(in);
456}
457
Sheri Zhang5dda2172021-10-15 19:54:17 +0100458inline float vreduce(const float32x4_t &v)
459{
460 const float32x2_t v0 = vget_high_f32(v);
461 const float32x2_t v1 = vget_low_f32(v);
462 const float32x2_t v_out = vadd_f32(v0, v1);
463
464 const float a = vget_lane_f32(v_out, 0);
465 const float b = vget_lane_f32(v_out, 1);
466
467 return a + b;
468}
469
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000470#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Alex Gildayc357c472018-03-21 13:54:09 +0000471/** Exponent polynomial coefficients */
Alex Gildayc357c472018-03-21 13:54:09 +0000472/** Logarithm polynomial coefficients */
Alex Gildayc357c472018-03-21 13:54:09 +0000473#ifndef DOXYGEN_SKIP_THIS
Georgios Pinitas565bf2d2018-08-31 11:46:49 +0100474inline float16x8_t vfloorq_f16(float16x8_t val)
475{
476 static const float16x8_t CONST_1 = vdupq_n_f16(1.f);
477
478 const int16x8_t z = vcvtq_s16_f16(val);
479 const float16x8_t r = vcvtq_f16_s16(z);
480
481 return vbslq_f16(vcgtq_f16(r, val), vsubq_f16(r, CONST_1), r);
482}
Usama Arif0a5a57a2019-05-23 14:20:33 +0100483
484inline float16x8_t vroundq_rte_f16(float16x8_t val)
485{
486 return vrndnq_f16(val);
487}
488
Georgios Pinitascdf51452017-08-31 14:21:36 +0100489inline float16x4_t vinvsqrt_f16(float16x4_t x)
490{
491 float16x4_t sqrt_reciprocal = vrsqrte_f16(x);
492 sqrt_reciprocal = vmul_f16(vrsqrts_f16(vmul_f16(x, sqrt_reciprocal), sqrt_reciprocal), sqrt_reciprocal);
493 sqrt_reciprocal = vmul_f16(vrsqrts_f16(vmul_f16(x, sqrt_reciprocal), sqrt_reciprocal), sqrt_reciprocal);
494 return sqrt_reciprocal;
495}
496
Pablo Tello91654c42017-07-05 11:32:17 +0100497inline float16x8_t vinvsqrtq_f16(float16x8_t x)
498{
499 float16x8_t sqrt_reciprocal = vrsqrteq_f16(x);
500 sqrt_reciprocal = vmulq_f16(vrsqrtsq_f16(vmulq_f16(x, sqrt_reciprocal), sqrt_reciprocal), sqrt_reciprocal);
501 sqrt_reciprocal = vmulq_f16(vrsqrtsq_f16(vmulq_f16(x, sqrt_reciprocal), sqrt_reciprocal), sqrt_reciprocal);
Pablo Tello91654c42017-07-05 11:32:17 +0100502 return sqrt_reciprocal;
503}
Pablo Tellodf246182017-07-03 16:25:09 +0100504
Georgios Pinitascdf51452017-08-31 14:21:36 +0100505inline float16x4_t vinv_f16(float16x4_t x)
506{
507 float16x4_t recip = vrecpe_f16(x);
508 recip = vmul_f16(vrecps_f16(x, recip), recip);
509 recip = vmul_f16(vrecps_f16(x, recip), recip);
510 return recip;
511}
512
Pablo Tellodf246182017-07-03 16:25:09 +0100513inline float16x8_t vinvq_f16(float16x8_t x)
514{
515 float16x8_t recip = vrecpeq_f16(x);
516 recip = vmulq_f16(vrecpsq_f16(x, recip), recip);
517 recip = vmulq_f16(vrecpsq_f16(x, recip), recip);
518 return recip;
519}
520
Jonathan Deakin2bc8cfe2022-10-13 10:50:25 +0000521inline float16x4_t vtanh_rational_approx_f16(float16x4_t x16)
Pablo Tello91654c42017-07-05 11:32:17 +0100522{
Jonathan Deakin2bc8cfe2022-10-13 10:50:25 +0000523 // Calculate rational approximation part of tanh exactly on a half-register of F16 by using F32s
524 // Note: doesn't handle overflows, needs truncating at |x| = 4.508
525 const float32x4_t x = vcvt_f32_f16(x16);
526
527 const float32x4_t ONE = vdupq_n_f32(1.0f);
528 const float32x4_t C1 = vdupq_n_f32(0.43760237f);
529 const float32x4_t C2 = vdupq_n_f32(0.104402f);
530 const float32x4_t C3 = vdupq_n_f32(0.013442706f);
531 const float32x4_t C4 = vdupq_n_f32(0.00073561433f);
532
533 const float32x4_t x2 = vmulq_f32(x,x);
534
535 // Denominator polynomial 1 + C1*x^2 + C3*x^4
536 float32x4_t denom = vfmaq_f32(C1, C3, x2);
537 denom = vfmaq_f32(ONE, x2, denom);
538
539 // Numerator polynomial x*(1 + C2*x^2 + C4*x^4)
540 float32x4_t numer = vfmaq_f32(C2, C4, x2);
541 numer = vfmaq_f32(ONE, x2, numer);
542 numer = vmulq_f32(numer, x);
543
544 return vcvt_f16_f32(vdivq_f32(numer, denom));
545}
546
547inline float16x8_t vtanhq_f16(float16x8_t x)
548{
549 // Split into high/low and use rational approximation on both parts exactly
550 const float16x8_t tanh = vcombine_f16(vtanh_rational_approx_f16( vget_low_f16(x)),
551 vtanh_rational_approx_f16(vget_high_f16(x)));
552
553 // tanh(x) == sign(x) to F16 precision for |x| >= 4.508, use sign after this
554 const float16x8_t ONE = vdupq_n_f16(1.0f);
555 const float16x8_t MAX_X = vdupq_n_f16(4.508f);
556 const auto at_limit = vcageq_f16(x, MAX_X); // |x| >= 4.508
557 const float16x8_t sign_x = vbslq_f16(vclezq_f16(x), -ONE, ONE);
558 return vbslq_f16(at_limit, sign_x, tanh);
Pablo Tello91654c42017-07-05 11:32:17 +0100559}
560
Pablo Tellodf246182017-07-03 16:25:09 +0100561inline float16x8_t vtaylor_polyq_f16(float16x8_t x, const std::array<float16x8_t, 8> &coeffs)
562{
563 const float16x8_t A = vaddq_f16(coeffs[0], vmulq_f16(coeffs[4], x));
564 const float16x8_t B = vaddq_f16(coeffs[2], vmulq_f16(coeffs[6], x));
565 const float16x8_t C = vaddq_f16(coeffs[1], vmulq_f16(coeffs[5], x));
566 const float16x8_t D = vaddq_f16(coeffs[3], vmulq_f16(coeffs[7], x));
567 const float16x8_t x2 = vmulq_f16(x, x);
568 const float16x8_t x4 = vmulq_f16(x2, x2);
569 const float16x8_t res = vaddq_f16(vaddq_f16(A, vmulq_f16(B, x2)), vmulq_f16(vaddq_f16(C, vmulq_f16(D, x2)), x4));
570 return res;
571}
572
573inline float16x8_t vexpq_f16(float16x8_t x)
574{
Michele Di Giorgio1c948d42018-11-20 16:03:01 +0000575 const float32x4_t x_high = vcvt_f32_f16(vget_high_f16(x));
576 const float32x4_t x_low = vcvt_f32_f16(vget_low_f16(x));
Anthony Barbier3a6163e2018-08-10 17:36:36 +0100577
Georgios Pinitasf2cdce32019-12-09 18:35:57 +0000578 const float16x8_t res = vcombine_f16(vcvt_f16_f32(vexpq_f32(x_low)), vcvt_f16_f32(vexpq_f32(x_high)));
Michele Di Giorgio1c948d42018-11-20 16:03:01 +0000579 return res;
Pablo Tellodf246182017-07-03 16:25:09 +0100580}
581
Murray Kornelsen926f5022022-07-13 21:22:39 -0400582#ifdef __aarch64__
583inline float16x8_t verfq_f16(float16x8_t x)
584{
585 const float32x4_t x_high = vcvt_f32_f16(vget_high_f16(x));
586 const float32x4_t x_low = vcvt_f32_f16(vget_low_f16(x));
587
588 const float16x8_t res = vcombine_f16(vcvt_f16_f32(verfq_f32(x_low)), vcvt_f16_f32(verfq_f32(x_high)));
589 return res;
590}
591#endif // #ifdef __aarch64__
592
Pablo Tellodf246182017-07-03 16:25:09 +0100593inline float16x8_t vlogq_f16(float16x8_t x)
594{
Georgios Pinitas5a594532018-12-03 14:30:05 +0000595 const float32x4_t x_high = vcvt_f32_f16(vget_high_f16(x));
596 const float32x4_t x_low = vcvt_f32_f16(vget_low_f16(x));
Anthony Barbier3a6163e2018-08-10 17:36:36 +0100597
Georgios Pinitasf2cdce32019-12-09 18:35:57 +0000598 const float16x8_t res = vcombine_f16(vcvt_f16_f32(vlogq_f32(x_low)), vcvt_f16_f32(vlogq_f32(x_high)));
Georgios Pinitas5a594532018-12-03 14:30:05 +0000599 return res;
Pablo Tellodf246182017-07-03 16:25:09 +0100600}
601
602inline float16x8_t vpowq_f16(float16x8_t val, float16x8_t n)
603{
Gian Marco Iodicef2cde9b2018-08-23 15:29:16 +0100604 float32x4_t n0_f32 = vcvt_f32_f16(vget_low_f16(n));
605 float32x4_t n1_f32 = vcvt_f32_f16(vget_high_f16(n));
606 float32x4_t val0_f32 = vcvt_f32_f16(vget_low_f16(val));
607 float32x4_t val1_f32 = vcvt_f32_f16(vget_high_f16(val));
608
609 float32x4_t res0_f32 = vexpq_f32(vmulq_f32(n0_f32, vlogq_f32(val0_f32)));
610 float32x4_t res1_f32 = vexpq_f32(vmulq_f32(n1_f32, vlogq_f32(val1_f32)));
611
612 return vcombine_f16(vcvt_f16_f32(res0_f32), vcvt_f16_f32(res1_f32));
Pablo Tellodf246182017-07-03 16:25:09 +0100613}
Manuel Bottinied753262019-05-15 15:30:47 +0100614
615inline float16x8_t vsinq_f16(float16x8_t val)
616{
617 const float32x4_t val_high = vcvt_f32_f16(vget_high_f16(val));
618 const float32x4_t val_low = vcvt_f32_f16(vget_low_f16(val));
619
620 const float32x4_t res_high = vsinq_f32(val_high);
621 const float32x4_t res_low = vsinq_f32(val_low);
622
623 return vcombine_f16(vcvt_f16_f32(res_low), vcvt_f16_f32(res_high));
624}
625
626inline float16x4_t vsin_f16(float16x4_t val)
627{
628 const float32x4_t val_f32 = vcvt_f32_f16(val);
629 const float32x2_t val_high = vget_high_f32(val_f32);
630 const float32x2_t val_low = vget_low_f32(val_f32);
631
632 const float32x2_t res_high = vsin_f32(val_high);
633 const float32x2_t res_low = vsin_f32(val_low);
634
635 return vcvt_f16_f32(vcombine_f32(res_low, res_high));
636}
637
Sheri Zhang5dda2172021-10-15 19:54:17 +0100638inline float16_t vreduce(const float16x8_t &v)
639{
640 const float16x4_t v0 = vget_high_f16(v);
641 const float16x4_t v1 = vget_low_f16(v);
642 const float16x4_t v_out = vadd_f16(v0, v1);
643
644 const float16_t a = vget_lane_f16(v_out, 0);
645 const float16_t b = vget_lane_f16(v_out, 1);
646 const float16_t c = vget_lane_f16(v_out, 2);
647 const float16_t d = vget_lane_f16(v_out, 3);
648
649 return a + b + c + d;
650}
Alex Gildayc357c472018-03-21 13:54:09 +0000651#endif /* DOXYGEN_SKIP_THIS */
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000652#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Gian Marco Iodice356f6432017-09-22 11:32:21 +0100653} // namespace arm_compute