blob: f875917988fe6696d6c67d84d960c165991d4670 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Viet-Hoa Dofd472f02023-03-15 14:05:06 +00002 * Copyright (c) 2016-2023 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/** Logarithm polynomial coefficients */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010032const std::array<float32x4_t, 8> log_tab = {{
33 vdupq_n_f32(-2.29561495781f),
34 vdupq_n_f32(-2.47071170807f),
35 vdupq_n_f32(-5.68692588806f),
36 vdupq_n_f32(-0.165253549814f),
37 vdupq_n_f32(5.17591238022f),
38 vdupq_n_f32(0.844007015228f),
39 vdupq_n_f32(4.58445882797f),
40 vdupq_n_f32(0.0141278216615f),
41}};
Anthony Barbier6ff3b192017-09-04 18:44:23 +010042
Manuel Bottinied753262019-05-15 15:30:47 +010043/** Sin polynomial coefficients */
44constexpr float te_sin_coeff2 = 0.166666666666f; // 1/(2*3)
45constexpr float te_sin_coeff3 = 0.05f; // 1/(4*5)
46constexpr float te_sin_coeff4 = 0.023809523810f; // 1/(6*7)
47constexpr float te_sin_coeff5 = 0.013888888889f; // 1/(8*9)
48
Alex Gildayc357c472018-03-21 13:54:09 +000049#ifndef DOXYGEN_SKIP_THIS
Viet-Hoa Do86689cd2022-11-21 17:17:56 +000050inline float32x4_t prefer_vfmaq_f32(float32x4_t a, float32x4_t b, float32x4_t c)
51{
Pablo Marquez Tello3f16c572023-06-20 13:39:10 +010052#if __ARM_FEATURE_FMA
Viet-Hoa Do86689cd2022-11-21 17:17:56 +000053 return vfmaq_f32(a, b, c);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010054#else // __ARM_FEATURE_FMA
Viet-Hoa Do86689cd2022-11-21 17:17:56 +000055 return vmlaq_f32(a, b, c);
Pablo Marquez Tello3f16c572023-06-20 13:39:10 +010056#endif // __ARM_FEATURE_FMA
Viet-Hoa Do86689cd2022-11-21 17:17:56 +000057}
58
Georgios Pinitasd8e765b2017-08-02 13:44:33 +010059inline float32x4_t vfloorq_f32(float32x4_t val)
60{
61 static const float32x4_t CONST_1 = vdupq_n_f32(1.f);
62
63 const int32x4_t z = vcvtq_s32_f32(val);
64 const float32x4_t r = vcvtq_f32_s32(z);
65
66 return vbslq_f32(vcgtq_f32(r, val), vsubq_f32(r, CONST_1), r);
67}
68
Usama Arif0a5a57a2019-05-23 14:20:33 +010069inline float32x4_t vroundq_rte_f32(float32x4_t val)
70{
71#ifdef __aarch64__
72 return vrndnq_f32(val);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010073#else // __aarch64__
Usama Arif0a5a57a2019-05-23 14:20:33 +010074 static const float32x4_t CONST_HALF_FLOAT = vdupq_n_f32(0.5f);
Manuel Bottini7bb56c62019-06-26 15:17:09 +010075 static const float32x4_t CONST_1_FLOAT = vdupq_n_f32(1.f);
76 static const int32x4_t CONST_1_INT = vdupq_n_s32(1);
77 const float32x4_t floor_val = vfloorq_f32(val);
78 const float32x4_t diff = vsubq_f32(val, floor_val);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010079 const float32x4_t fp32_upper_limit =
80 vreinterpretq_f32_u32(vdupq_n_u32(0x4B000000)); // 0x4B000000 = (23U + 127U) << 23U
Usama Arif0a5a57a2019-05-23 14:20:33 +010081
82 /*
Ramy Elgammal7fefac72023-04-20 12:32:03 +010083 * 1. Select the floor value when (diff<0.5 || (diff==0.5 && floor_val%2==0).
84 * 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))))
85 *
86 * 2. In case the input value (val) is out of signed int32 range, then simple use the input value as the rounded value
87 * Because:
88 * in this case converting to int32 would saturate
89 * If the input float value is >= 2^23 * 1.00... 23 Zeros ..0 then the rounded value is exactly equal to the input value.
90 * Because:
91 * in IEEE single precision floating point representation the fraction part is 23 bit, so if exponent is 23 it means the fraction part = 0 as any digits after decimal point are truncated.
92 * Hence, rounding has no effect:
93 * Threshold upper limit with format |S|E(8bits)| Fraction(23bits) | = (23 + 127) << 23 (assuming positive sign): Adding 127, because 127 represents the actual zero in this format.
Usama Arif0a5a57a2019-05-23 14:20:33 +010094 */
95
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010096 float32x4_t rounded_val = vbslq_f32(
97 vorrq_u32(vcltq_f32(diff, CONST_HALF_FLOAT),
98 vandq_u32(vceqq_f32(diff, CONST_HALF_FLOAT),
99 vmvnq_u32(vtstq_s32(vandq_s32(vcvtq_s32_f32(floor_val), CONST_1_INT), CONST_1_INT)))),
100 floor_val, vaddq_f32(floor_val, CONST_1_FLOAT));
Ramy Elgammal7fefac72023-04-20 12:32:03 +0100101
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100102 float32x4_t result = vbslq_f32(vcgeq_f32(vabsq_f32(val), fp32_upper_limit), val, rounded_val);
Ramy Elgammal7fefac72023-04-20 12:32:03 +0100103
104 return result;
Usama Arif0a5a57a2019-05-23 14:20:33 +0100105#endif // __aarch64__
106}
107
Georgios Pinitascdf51452017-08-31 14:21:36 +0100108inline float32x2_t vinvsqrt_f32(float32x2_t x)
109{
110 float32x2_t sqrt_reciprocal = vrsqrte_f32(x);
111 sqrt_reciprocal = vmul_f32(vrsqrts_f32(vmul_f32(x, sqrt_reciprocal), sqrt_reciprocal), sqrt_reciprocal);
112 sqrt_reciprocal = vmul_f32(vrsqrts_f32(vmul_f32(x, sqrt_reciprocal), sqrt_reciprocal), sqrt_reciprocal);
113
114 return sqrt_reciprocal;
115}
116
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100117inline float32x4_t vinvsqrtq_f32(float32x4_t x)
118{
119 float32x4_t sqrt_reciprocal = vrsqrteq_f32(x);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100120 sqrt_reciprocal = vmulq_f32(vrsqrtsq_f32(vmulq_f32(x, sqrt_reciprocal), sqrt_reciprocal), sqrt_reciprocal);
121 sqrt_reciprocal = vmulq_f32(vrsqrtsq_f32(vmulq_f32(x, sqrt_reciprocal), sqrt_reciprocal), sqrt_reciprocal);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100122
123 return sqrt_reciprocal;
124}
125
Georgios Pinitascdf51452017-08-31 14:21:36 +0100126inline float32x2_t vinv_f32(float32x2_t x)
127{
128 float32x2_t recip = vrecpe_f32(x);
129 recip = vmul_f32(vrecps_f32(x, recip), recip);
130 recip = vmul_f32(vrecps_f32(x, recip), recip);
131 return recip;
132}
133
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100134inline float32x4_t vinvq_f32(float32x4_t x)
135{
136 float32x4_t recip = vrecpeq_f32(x);
137 recip = vmulq_f32(vrecpsq_f32(x, recip), recip);
138 recip = vmulq_f32(vrecpsq_f32(x, recip), recip);
139 return recip;
140}
141
142inline float32x4_t vtaylor_polyq_f32(float32x4_t x, const std::array<float32x4_t, 8> &coeffs)
143{
144 float32x4_t A = vmlaq_f32(coeffs[0], coeffs[4], x);
145 float32x4_t B = vmlaq_f32(coeffs[2], coeffs[6], x);
146 float32x4_t C = vmlaq_f32(coeffs[1], coeffs[5], x);
147 float32x4_t D = vmlaq_f32(coeffs[3], coeffs[7], x);
148 float32x4_t x2 = vmulq_f32(x, x);
149 float32x4_t x4 = vmulq_f32(x2, x2);
150 float32x4_t res = vmlaq_f32(vmlaq_f32(A, B, x2), vmlaq_f32(C, D, x2), x4);
151 return res;
152}
153
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100154static const uint32_t exp_f32_coeff[] = {
Viet-Hoa Do86689cd2022-11-21 17:17:56 +0000155 0x3f7ffff6, // x^1: 0x1.ffffecp-1f
156 0x3efffedb, // x^2: 0x1.fffdb6p-2f
157 0x3e2aaf33, // x^3: 0x1.555e66p-3f
158 0x3d2b9f17, // x^4: 0x1.573e2ep-5f
159 0x3c072010, // x^5: 0x1.0e4020p-7f
160};
161
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100162inline float32x4_t vexpq_f32(float32x4_t x)
163{
Viet-Hoa Do86689cd2022-11-21 17:17:56 +0000164 const auto c1 = vreinterpretq_f32_u32(vdupq_n_u32(exp_f32_coeff[0]));
165 const auto c2 = vreinterpretq_f32_u32(vdupq_n_u32(exp_f32_coeff[1]));
166 const auto c3 = vreinterpretq_f32_u32(vdupq_n_u32(exp_f32_coeff[2]));
167 const auto c4 = vreinterpretq_f32_u32(vdupq_n_u32(exp_f32_coeff[3]));
168 const auto c5 = vreinterpretq_f32_u32(vdupq_n_u32(exp_f32_coeff[4]));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100169
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100170 const auto shift = vreinterpretq_f32_u32(vdupq_n_u32(0x4b00007f)); // 2^23 + 127 = 0x1.0000fep23f
171 const auto inv_ln2 = vreinterpretq_f32_u32(vdupq_n_u32(0x3fb8aa3b)); // 1 / ln(2) = 0x1.715476p+0f
172 const auto neg_ln2_hi =
173 vreinterpretq_f32_u32(vdupq_n_u32(0xbf317200)); // -ln(2) from bits -1 to -19: -0x1.62e400p-1f
174 const auto neg_ln2_lo =
175 vreinterpretq_f32_u32(vdupq_n_u32(0xb5bfbe8e)); // -ln(2) from bits -20 to -42: -0x1.7f7d1cp-20f
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100176
Viet-Hoa Do86689cd2022-11-21 17:17:56 +0000177 const auto inf = vdupq_n_f32(std::numeric_limits<float>::infinity());
Ramy Elgammal7fefac72023-04-20 12:32:03 +0100178 const auto max_input = vdupq_n_f32(88.37f); // Approximately ln(2^127.5)
Viet-Hoa Do86689cd2022-11-21 17:17:56 +0000179 const auto zero = vdupq_n_f32(0.f);
Ramy Elgammal7fefac72023-04-20 12:32:03 +0100180 const auto min_input = vdupq_n_f32(-86.64f); // Approximately ln(2^-125)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100181
Viet-Hoa Do86689cd2022-11-21 17:17:56 +0000182 // Range reduction:
183 // e^x = 2^n * e^r
184 // where:
185 // n = floor(x / ln(2))
186 // r = x - n * ln(2)
187 //
188 // By adding x / ln(2) with 2^23 + 127 (shift):
189 // * As FP32 fraction part only has 23-bits, the addition of 2^23 + 127 forces decimal part
190 // of x / ln(2) out of the result. The integer part of x / ln(2) (i.e. n) + 127 will occupy
191 // the whole fraction part of z in FP32 format.
192 // Subtracting 2^23 + 127 (shift) from z will result in the integer part of x / ln(2)
193 // (i.e. n) because the decimal part has been pushed out and lost.
194 // * The addition of 127 makes the FP32 fraction part of z ready to be used as the exponent
195 // in FP32 format. Left shifting z by 23 bits will result in 2^n.
Ramy Elgammal7fefac72023-04-20 12:32:03 +0100196 const auto z = prefer_vfmaq_f32(shift, x, inv_ln2);
197 const auto n = z - shift;
198 const auto scale = vreinterpretq_f32_u32(vreinterpretq_u32_f32(z) << 23); // 2^n
Viet-Hoa Do86689cd2022-11-21 17:17:56 +0000199
200 // The calculation of n * ln(2) is done using 2 steps to achieve accuracy beyond FP32.
201 // This outperforms longer Taylor series (3-4 tabs) both in term of accuracy and performance.
202 const auto r_hi = prefer_vfmaq_f32(x, n, neg_ln2_hi);
Ramy Elgammal7fefac72023-04-20 12:32:03 +0100203 const auto r = prefer_vfmaq_f32(r_hi, n, neg_ln2_lo);
Viet-Hoa Do86689cd2022-11-21 17:17:56 +0000204
205 // Compute the truncated Taylor series of e^r.
206 // poly = scale * (1 + c1 * r + c2 * r^2 + c3 * r^3 + c4 * r^4 + c5 * r^5)
207 const auto r2 = r * r;
208
Ramy Elgammal7fefac72023-04-20 12:32:03 +0100209 const auto p1 = c1 * r;
210 const auto p23 = prefer_vfmaq_f32(c2, c3, r);
211 const auto p45 = prefer_vfmaq_f32(c4, c5, r);
212 const auto p2345 = prefer_vfmaq_f32(p23, p45, r2);
Viet-Hoa Do86689cd2022-11-21 17:17:56 +0000213 const auto p12345 = prefer_vfmaq_f32(p1, p2345, r2);
214
215 auto poly = prefer_vfmaq_f32(scale, p12345, scale);
216
217 // Handle underflow and overflow.
218 poly = vbslq_f32(vcltq_f32(x, min_input), zero, poly);
219 poly = vbslq_f32(vcgtq_f32(x, max_input), inf, poly);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100220
221 return poly;
222}
223
Murray Kornelsen926f5022022-07-13 21:22:39 -0400224#ifdef __aarch64__
225inline float32x4_t verfq_f32(float32x4_t x)
226{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100227 static const float erffdata[4] = {0.278393f, 0.230389f, 0.000972f, 0.078108f};
Murray Kornelsen926f5022022-07-13 21:22:39 -0400228 static const float32x4_t coeffdata = vld1q_f32(erffdata);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100229 static const float32x4_t onev{vdupq_n_f32(1.0f)};
Murray Kornelsen926f5022022-07-13 21:22:39 -0400230
231 uint32x4_t selector = vcltzq_f32(x);
232
233 float32x4_t absx = vabsq_f32(x);
234 float32x4_t absx2 = vmulq_f32(x, x);
235 float32x4_t absx3 = vmulq_f32(absx2, absx);
236 float32x4_t absx4 = vmulq_f32(absx2, absx2);
237
238 float32x4_t denom = onev;
239 denom = vfmaq_laneq_f32(denom, absx, coeffdata, 0);
240 denom = vfmaq_laneq_f32(denom, absx2, coeffdata, 1);
241 denom = vfmaq_laneq_f32(denom, absx3, coeffdata, 2);
242 denom = vfmaq_laneq_f32(denom, absx4, coeffdata, 3);
243
244 denom = vmulq_f32(denom, denom);
245 denom = vmulq_f32(denom, denom);
246
247 float32x4_t fract = onev;
248 fract = vdivq_f32(fract, denom);
249
250 float32x4_t result = onev;
251 result = vsubq_f32(result, fract);
252
253 float32x4_t inverse = vnegq_f32(result);
254
255 result = vbslq_f32(selector, inverse, result);
256
257 return result;
258}
259#endif // #ifdef __aarch64__
260
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100261inline float32x4_t vlogq_f32(float32x4_t x)
262{
263 static const int32x4_t CONST_127 = vdupq_n_s32(127); // 127
264 static const float32x4_t CONST_LN2 = vdupq_n_f32(0.6931471805f); // ln(2)
265
266 // Extract exponent
267 int32x4_t m = vsubq_s32(vreinterpretq_s32_u32(vshrq_n_u32(vreinterpretq_u32_f32(x), 23)), CONST_127);
268 float32x4_t val = vreinterpretq_f32_s32(vsubq_s32(vreinterpretq_s32_f32(x), vshlq_n_s32(m, 23)));
269
270 // Polynomial Approximation
271 float32x4_t poly = vtaylor_polyq_f32(val, log_tab);
272
273 // Reconstruct
274 poly = vmlaq_f32(poly, vcvtq_f32_s32(m), CONST_LN2);
275
276 return poly;
277}
278
279inline float32x4_t vtanhq_f32(float32x4_t val)
280{
281 static const float32x4_t CONST_1 = vdupq_n_f32(1.f);
282 static const float32x4_t CONST_2 = vdupq_n_f32(2.f);
283 static const float32x4_t CONST_MIN_TANH = vdupq_n_f32(-10.f);
284 static const float32x4_t CONST_MAX_TANH = vdupq_n_f32(10.f);
Aleksandr Nikolaev7e9f34d2021-05-04 16:46:27 +0100285 static const float32x4_t CONST_THR = vdupq_n_f32(5.e-3);
286 static const float32x4_t CONST_1_3 = vdupq_n_f32(0.3333333f);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100287
Sheri Zhang5dda2172021-10-15 19:54:17 +0100288 float32x4_t x = vminq_f32(vmaxq_f32(val, CONST_MIN_TANH), CONST_MAX_TANH);
Aleksandr Nikolaev7e9f34d2021-05-04 16:46:27 +0100289 // x * (1 - x^2/3) if |x| < 5.e-3 or (exp2x - 1) / (exp2x + 1) otherwise
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100290 float32x4_t exp2x =
291 vbslq_f32(vcgtq_f32(vabsq_f32(x), CONST_THR), vexpq_f32(vmulq_f32(CONST_2, x)), vmulq_f32(x, x));
292 float32x4_t num =
293 vbslq_f32(vcgtq_f32(vabsq_f32(x), CONST_THR), vsubq_f32(exp2x, CONST_1), vmulq_f32(CONST_1_3, exp2x));
294 float32x4_t den = vbslq_f32(vcgtq_f32(vabsq_f32(x), CONST_THR), vaddq_f32(exp2x, CONST_1), vsubq_f32(CONST_1, num));
295 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 +0100296 return tanh;
297}
298
299inline float32x4_t vpowq_f32(float32x4_t val, float32x4_t n)
300{
301 return vexpq_f32(vmulq_f32(n, vlogq_f32(val)));
302}
Manuel Bottinied753262019-05-15 15:30:47 +0100303
304inline float32x4_t vsinq_f32(float32x4_t val)
305{
306 const float32x4_t pi_v = vdupq_n_f32(M_PI);
307 const float32x4_t pio2_v = vdupq_n_f32(M_PI / 2);
308 const float32x4_t ipi_v = vdupq_n_f32(1 / M_PI);
309
310 //Find positive or negative
311 const int32x4_t c_v = vabsq_s32(vcvtq_s32_f32(vmulq_f32(val, ipi_v)));
312 const uint32x4_t sign_v = vcleq_f32(val, vdupq_n_f32(0));
313 const uint32x4_t odd_v = vandq_u32(vreinterpretq_u32_s32(c_v), vdupq_n_u32(1));
314
315 uint32x4_t neg_v = veorq_u32(odd_v, sign_v);
316
317 //Modulus a - (n * int(a*(1/n)))
318 float32x4_t ma = vsubq_f32(vabsq_f32(val), vmulq_f32(pi_v, vcvtq_f32_s32(c_v)));
319 const uint32x4_t reb_v = vcgeq_f32(ma, pio2_v);
320
321 //Rebase a between 0 and pi/2
322 ma = vbslq_f32(reb_v, vsubq_f32(pi_v, ma), ma);
323
324 //Taylor series
325 const float32x4_t ma2 = vmulq_f32(ma, ma);
326
327 //2nd elem: x^3 / 3!
328 float32x4_t elem = vmulq_f32(vmulq_f32(ma, ma2), vdupq_n_f32(te_sin_coeff2));
329 float32x4_t res = vsubq_f32(ma, elem);
330
331 //3rd elem: x^5 / 5!
332 elem = vmulq_f32(vmulq_f32(elem, ma2), vdupq_n_f32(te_sin_coeff3));
333 res = vaddq_f32(res, elem);
334
335 //4th elem: x^7 / 7!float32x2_t vsin_f32(float32x2_t val)
336 elem = vmulq_f32(vmulq_f32(elem, ma2), vdupq_n_f32(te_sin_coeff4));
337 res = vsubq_f32(res, elem);
338
339 //5th elem: x^9 / 9!
340 elem = vmulq_f32(vmulq_f32(elem, ma2), vdupq_n_f32(te_sin_coeff5));
341 res = vaddq_f32(res, elem);
342
343 //Change of sign
344 neg_v = vshlq_n_u32(neg_v, 31);
345 res = vreinterpretq_f32_u32(veorq_u32(vreinterpretq_u32_f32(res), neg_v));
346 return res;
347}
348
349inline float32x2_t vsin_f32(float32x2_t val)
350{
351 const float32x2_t pi_v = vdup_n_f32(M_PI);
352 const float32x2_t pio2_v = vdup_n_f32(M_PI / 2);
353 const float32x2_t ipi_v = vdup_n_f32(1 / M_PI);
354
355 //Find positive or negative
356 const int32x2_t c_v = vabs_s32(vcvt_s32_f32(vmul_f32(val, ipi_v)));
357 const uint32x2_t sign_v = vcle_f32(val, vdup_n_f32(0));
358 const uint32x2_t odd_v = vand_u32(vreinterpret_u32_s32(c_v), vdup_n_u32(1));
359
360 uint32x2_t neg_v = veor_u32(odd_v, sign_v);
361
362 //Modulus a - (n * int(a*(1/n)))
363 float32x2_t ma = vsub_f32(vabs_f32(val), vmul_f32(pi_v, vcvt_f32_s32(c_v)));
364 const uint32x2_t reb_v = vcge_f32(ma, pio2_v);
365
366 //Rebase a between 0 and pi/2
367 ma = vbsl_f32(reb_v, vsub_f32(pi_v, ma), ma);
368
369 //Taylor series
370 const float32x2_t ma2 = vmul_f32(ma, ma);
371
372 //2nd elem: x^3 / 3!
373 float32x2_t elem = vmul_f32(vmul_f32(ma, ma2), vdup_n_f32(te_sin_coeff2));
374 float32x2_t res = vsub_f32(ma, elem);
375
376 //3rd elem: x^5 / 5!
377 elem = vmul_f32(vmul_f32(elem, ma2), vdup_n_f32(te_sin_coeff3));
378 res = vadd_f32(res, elem);
379
380 //4th elem: x^7 / 7!float32x2_t vsin_f32(float32x2_t val)
381 elem = vmul_f32(vmul_f32(elem, ma2), vdup_n_f32(te_sin_coeff4));
382 res = vsub_f32(res, elem);
383
384 //5th elem: x^9 / 9!
385 elem = vmul_f32(vmul_f32(elem, ma2), vdup_n_f32(te_sin_coeff5));
386 res = vadd_f32(res, elem);
387
388 //Change of sign
389 neg_v = vshl_n_u32(neg_v, 31);
390 res = vreinterpret_f32_u32(veor_u32(vreinterpret_u32_f32(res), neg_v));
391 return res;
392}
393
Alex Gildayc357c472018-03-21 13:54:09 +0000394#endif /* DOXYGEN_SKIP_THIS */
395
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100396inline int32x4_t rounding_divide_by_pow2(int32x4_t x, int32x4_t exponent)
397{
398 const int32x4_t shift_vec = vnegq_s32(exponent);
399 const int32x4_t fixup = vshrq_n_s32(vandq_s32(x, shift_vec), 31);
400 const int32x4_t fixed_up_x = vqaddq_s32(x, fixup);
401 return vrshlq_s32(fixed_up_x, shift_vec);
402}
403
Manuel Bottini7bb56c62019-06-26 15:17:09 +0100404inline int32x4_t rounding_divide_by_pow2(int32x4_t x, int exponent)
405{
406 const int32x4_t shift_vec = vdupq_n_s32(-exponent);
407 const int32x4_t fixup = vshrq_n_s32(vandq_s32(x, shift_vec), 31);
408 const int32x4_t fixed_up_x = vqaddq_s32(x, fixup);
409 return vrshlq_s32(fixed_up_x, shift_vec);
410}
411
412inline int32_t rounding_divide_by_pow2(int32_t x, int exponent)
413{
414 const int32_t mask = (1 << exponent) - 1;
415 const int32_t threshold = (mask >> 1) + (x < 0 ? 1 : 0);
416 return (x >> exponent) + ((x & mask) > threshold ? 1 : 0);
417}
418
Manuel Bottini21079dd2019-10-29 17:20:09 +0000419inline float32x4x4_t convert_uint8x16_to_float32x4x4(const uint8x16_t &in)
420{
421 float32x4x4_t out;
422
423 const auto tmp1 = vmovl_u8(vget_low_u8(in));
424 out.val[0] = vcvtq_f32_u32(vmovl_u16(vget_low_u16(tmp1)));
425 out.val[1] = vcvtq_f32_u32(vmovl_u16(vget_high_u16(tmp1)));
426
427 const auto tmp2 = vmovl_u8(vget_high_u8(in));
428 out.val[2] = vcvtq_f32_u32(vmovl_u16(vget_low_u16(tmp2)));
429 out.val[3] = vcvtq_f32_u32(vmovl_u16(vget_high_u16(tmp2)));
430 return out;
431}
432
Sang-Hoon Parkc3a74202019-11-22 16:05:46 +0000433inline float32x4x4_t convert_int8x16_to_float32x4x4(const int8x16_t &in)
434{
435 float32x4x4_t out;
436
437 const auto tmp1 = vmovl_s8(vget_low_s8(in));
438 out.val[0] = vcvtq_f32_s32(vmovl_s16(vget_low_s16(tmp1)));
439 out.val[1] = vcvtq_f32_s32(vmovl_s16(vget_high_s16(tmp1)));
440
441 const auto tmp2 = vmovl_s8(vget_high_s8(in));
442 out.val[2] = vcvtq_f32_s32(vmovl_s16(vget_low_s16(tmp2)));
443 out.val[3] = vcvtq_f32_s32(vmovl_s16(vget_high_s16(tmp2)));
444 return out;
445}
446
Manuel Bottini4370cff2020-02-07 16:31:59 +0000447template <>
448inline float32x4x4_t convert_to_float32x4x4(const uint8x16_t &in)
449{
450 return convert_uint8x16_to_float32x4x4(in);
451}
452
453template <>
454inline float32x4x4_t convert_to_float32x4x4(const int8x16_t &in)
455{
456 return convert_int8x16_to_float32x4x4(in);
457}
458
Manuel Bottini21079dd2019-10-29 17:20:09 +0000459inline void convert_float32x4x3_to_uint8x8x3(const float32x4x3_t &in1, const float32x4x3_t &in2, uint8x8x3_t &out)
460{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100461 out.val[0] = vqmovn_u16(vcombine_u16(vqmovn_u32(vcvtq_u32_f32(in1.val[0])), vqmovn_u32(vcvtq_u32_f32(in2.val[0]))));
462 out.val[1] = vqmovn_u16(vcombine_u16(vqmovn_u32(vcvtq_u32_f32(in1.val[1])), vqmovn_u32(vcvtq_u32_f32(in2.val[1]))));
463 out.val[2] = vqmovn_u16(vcombine_u16(vqmovn_u32(vcvtq_u32_f32(in1.val[2])), vqmovn_u32(vcvtq_u32_f32(in2.val[2]))));
Manuel Bottini21079dd2019-10-29 17:20:09 +0000464}
465
Sang-Hoon Parkc3a74202019-11-22 16:05:46 +0000466inline void convert_float32x4x4_to_uint8x16(const float32x4x4_t &in, uint8x16_t &out)
Manuel Bottini21079dd2019-10-29 17:20:09 +0000467{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100468 const auto low = vcombine_u16(vqmovn_u32(vcvtq_u32_f32(in.val[0])), vqmovn_u32(vcvtq_u32_f32(in.val[1])));
469 const auto high = vcombine_u16(vqmovn_u32(vcvtq_u32_f32(in.val[2])), vqmovn_u32(vcvtq_u32_f32(in.val[3])));
470 out = vcombine_u8(vqmovn_u16(low), vqmovn_u16(high));
Manuel Bottini21079dd2019-10-29 17:20:09 +0000471}
472
Sang-Hoon Parkc3a74202019-11-22 16:05:46 +0000473inline void convert_float32x4x4_to_int8x16(const float32x4x4_t &in, int8x16_t &out)
474{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100475 const auto low = vcombine_s16(vqmovn_s32(vcvtq_s32_f32(in.val[0])), vqmovn_s32(vcvtq_s32_f32(in.val[1])));
476 const auto high = vcombine_s16(vqmovn_s32(vcvtq_s32_f32(in.val[2])), vqmovn_s32(vcvtq_s32_f32(in.val[3])));
477 out = vcombine_s8(vqmovn_s16(low), vqmovn_s16(high));
Sang-Hoon Parkc3a74202019-11-22 16:05:46 +0000478}
479
Sang-Hoon Parkdcf3c7e2021-03-04 17:03:46 +0000480template <>
481inline uint8x16_t convert_float_to_int<float32x4x4_t, uint8x16_t>(const float32x4x4_t &in)
482{
483 uint8x16_t out;
484 convert_float32x4x4_to_uint8x16(in, out);
485 return out;
486}
487
488template <>
489inline float32x4x4_t convert_int_to_float<float32x4x4_t, uint8x16_t>(const uint8x16_t &in)
490{
491 return convert_uint8x16_to_float32x4x4(in);
492}
493
494template <>
495inline int8x16_t convert_float_to_int<float32x4x4_t, int8x16_t>(const float32x4x4_t &in)
496{
497 int8x16_t out;
498 convert_float32x4x4_to_int8x16(in, out);
499 return out;
500}
501
502template <>
503inline float32x4x4_t convert_int_to_float<float32x4x4_t, int8x16_t>(const int8x16_t &in)
504{
505 return convert_int8x16_to_float32x4x4(in);
506}
507
Sheri Zhang5dda2172021-10-15 19:54:17 +0100508inline float vreduce(const float32x4_t &v)
509{
510 const float32x2_t v0 = vget_high_f32(v);
511 const float32x2_t v1 = vget_low_f32(v);
512 const float32x2_t v_out = vadd_f32(v0, v1);
513
514 const float a = vget_lane_f32(v_out, 0);
515 const float b = vget_lane_f32(v_out, 1);
516
517 return a + b;
518}
519
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000520#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Alex Gildayc357c472018-03-21 13:54:09 +0000521/** Exponent polynomial coefficients */
Alex Gildayc357c472018-03-21 13:54:09 +0000522/** Logarithm polynomial coefficients */
Alex Gildayc357c472018-03-21 13:54:09 +0000523#ifndef DOXYGEN_SKIP_THIS
Georgios Pinitas565bf2d2018-08-31 11:46:49 +0100524inline float16x8_t vfloorq_f16(float16x8_t val)
525{
526 static const float16x8_t CONST_1 = vdupq_n_f16(1.f);
527
528 const int16x8_t z = vcvtq_s16_f16(val);
529 const float16x8_t r = vcvtq_f16_s16(z);
530
531 return vbslq_f16(vcgtq_f16(r, val), vsubq_f16(r, CONST_1), r);
532}
Usama Arif0a5a57a2019-05-23 14:20:33 +0100533
534inline float16x8_t vroundq_rte_f16(float16x8_t val)
535{
536 return vrndnq_f16(val);
537}
538
Georgios Pinitascdf51452017-08-31 14:21:36 +0100539inline float16x4_t vinvsqrt_f16(float16x4_t x)
540{
541 float16x4_t sqrt_reciprocal = vrsqrte_f16(x);
542 sqrt_reciprocal = vmul_f16(vrsqrts_f16(vmul_f16(x, sqrt_reciprocal), sqrt_reciprocal), sqrt_reciprocal);
543 sqrt_reciprocal = vmul_f16(vrsqrts_f16(vmul_f16(x, sqrt_reciprocal), sqrt_reciprocal), sqrt_reciprocal);
544 return sqrt_reciprocal;
545}
546
Pablo Tello91654c42017-07-05 11:32:17 +0100547inline float16x8_t vinvsqrtq_f16(float16x8_t x)
548{
549 float16x8_t sqrt_reciprocal = vrsqrteq_f16(x);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100550 sqrt_reciprocal = vmulq_f16(vrsqrtsq_f16(vmulq_f16(x, sqrt_reciprocal), sqrt_reciprocal), sqrt_reciprocal);
551 sqrt_reciprocal = vmulq_f16(vrsqrtsq_f16(vmulq_f16(x, sqrt_reciprocal), sqrt_reciprocal), sqrt_reciprocal);
Pablo Tello91654c42017-07-05 11:32:17 +0100552 return sqrt_reciprocal;
553}
Pablo Tellodf246182017-07-03 16:25:09 +0100554
Georgios Pinitascdf51452017-08-31 14:21:36 +0100555inline float16x4_t vinv_f16(float16x4_t x)
556{
557 float16x4_t recip = vrecpe_f16(x);
558 recip = vmul_f16(vrecps_f16(x, recip), recip);
559 recip = vmul_f16(vrecps_f16(x, recip), recip);
560 return recip;
561}
562
Pablo Tellodf246182017-07-03 16:25:09 +0100563inline float16x8_t vinvq_f16(float16x8_t x)
564{
565 float16x8_t recip = vrecpeq_f16(x);
566 recip = vmulq_f16(vrecpsq_f16(x, recip), recip);
567 recip = vmulq_f16(vrecpsq_f16(x, recip), recip);
568 return recip;
569}
570
Jonathan Deakin2bc8cfe2022-10-13 10:50:25 +0000571inline float16x4_t vtanh_rational_approx_f16(float16x4_t x16)
Pablo Tello91654c42017-07-05 11:32:17 +0100572{
Jonathan Deakin2bc8cfe2022-10-13 10:50:25 +0000573 // Calculate rational approximation part of tanh exactly on a half-register of F16 by using F32s
574 // Note: doesn't handle overflows, needs truncating at |x| = 4.508
575 const float32x4_t x = vcvt_f32_f16(x16);
576
577 const float32x4_t ONE = vdupq_n_f32(1.0f);
Ramy Elgammal7fefac72023-04-20 12:32:03 +0100578 const float32x4_t C1 = vdupq_n_f32(0.43760237f);
579 const float32x4_t C2 = vdupq_n_f32(0.104402f);
580 const float32x4_t C3 = vdupq_n_f32(0.013442706f);
581 const float32x4_t C4 = vdupq_n_f32(0.00073561433f);
Jonathan Deakin2bc8cfe2022-10-13 10:50:25 +0000582
Ramy Elgammal7fefac72023-04-20 12:32:03 +0100583 const float32x4_t x2 = vmulq_f32(x, x);
Jonathan Deakin2bc8cfe2022-10-13 10:50:25 +0000584
585 // Denominator polynomial 1 + C1*x^2 + C3*x^4
586 float32x4_t denom = vfmaq_f32(C1, C3, x2);
Ramy Elgammal7fefac72023-04-20 12:32:03 +0100587 denom = vfmaq_f32(ONE, x2, denom);
Jonathan Deakin2bc8cfe2022-10-13 10:50:25 +0000588
589 // Numerator polynomial x*(1 + C2*x^2 + C4*x^4)
590 float32x4_t numer = vfmaq_f32(C2, C4, x2);
Ramy Elgammal7fefac72023-04-20 12:32:03 +0100591 numer = vfmaq_f32(ONE, x2, numer);
592 numer = vmulq_f32(numer, x);
Jonathan Deakin2bc8cfe2022-10-13 10:50:25 +0000593
594 return vcvt_f16_f32(vdivq_f32(numer, denom));
595}
596
597inline float16x8_t vtanhq_f16(float16x8_t x)
598{
599 // Split into high/low and use rational approximation on both parts exactly
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100600 const float16x8_t tanh =
601 vcombine_f16(vtanh_rational_approx_f16(vget_low_f16(x)), vtanh_rational_approx_f16(vget_high_f16(x)));
Jonathan Deakin2bc8cfe2022-10-13 10:50:25 +0000602
603 // tanh(x) == sign(x) to F16 precision for |x| >= 4.508, use sign after this
Ramy Elgammal7fefac72023-04-20 12:32:03 +0100604 const float16x8_t ONE = vdupq_n_f16(1.0f);
605 const float16x8_t MAX_X = vdupq_n_f16(4.508f);
606 const auto at_limit = vcageq_f16(x, MAX_X); // |x| >= 4.508
607 const float16x8_t sign_x = vbslq_f16(vclezq_f16(x), -ONE, ONE);
Jonathan Deakin2bc8cfe2022-10-13 10:50:25 +0000608 return vbslq_f16(at_limit, sign_x, tanh);
Pablo Tello91654c42017-07-05 11:32:17 +0100609}
610
Pablo Tellodf246182017-07-03 16:25:09 +0100611inline float16x8_t vtaylor_polyq_f16(float16x8_t x, const std::array<float16x8_t, 8> &coeffs)
612{
613 const float16x8_t A = vaddq_f16(coeffs[0], vmulq_f16(coeffs[4], x));
614 const float16x8_t B = vaddq_f16(coeffs[2], vmulq_f16(coeffs[6], x));
615 const float16x8_t C = vaddq_f16(coeffs[1], vmulq_f16(coeffs[5], x));
616 const float16x8_t D = vaddq_f16(coeffs[3], vmulq_f16(coeffs[7], x));
617 const float16x8_t x2 = vmulq_f16(x, x);
618 const float16x8_t x4 = vmulq_f16(x2, x2);
619 const float16x8_t res = vaddq_f16(vaddq_f16(A, vmulq_f16(B, x2)), vmulq_f16(vaddq_f16(C, vmulq_f16(D, x2)), x4));
620 return res;
621}
622
623inline float16x8_t vexpq_f16(float16x8_t x)
624{
Michele Di Giorgio1c948d42018-11-20 16:03:01 +0000625 const float32x4_t x_high = vcvt_f32_f16(vget_high_f16(x));
626 const float32x4_t x_low = vcvt_f32_f16(vget_low_f16(x));
Anthony Barbier3a6163e2018-08-10 17:36:36 +0100627
Georgios Pinitasf2cdce32019-12-09 18:35:57 +0000628 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 +0000629 return res;
Pablo Tellodf246182017-07-03 16:25:09 +0100630}
631
Murray Kornelsen926f5022022-07-13 21:22:39 -0400632#ifdef __aarch64__
633inline float16x8_t verfq_f16(float16x8_t x)
634{
635 const float32x4_t x_high = vcvt_f32_f16(vget_high_f16(x));
636 const float32x4_t x_low = vcvt_f32_f16(vget_low_f16(x));
637
638 const float16x8_t res = vcombine_f16(vcvt_f16_f32(verfq_f32(x_low)), vcvt_f16_f32(verfq_f32(x_high)));
639 return res;
640}
641#endif // #ifdef __aarch64__
642
Pablo Tellodf246182017-07-03 16:25:09 +0100643inline float16x8_t vlogq_f16(float16x8_t x)
644{
Georgios Pinitas5a594532018-12-03 14:30:05 +0000645 const float32x4_t x_high = vcvt_f32_f16(vget_high_f16(x));
646 const float32x4_t x_low = vcvt_f32_f16(vget_low_f16(x));
Anthony Barbier3a6163e2018-08-10 17:36:36 +0100647
Georgios Pinitasf2cdce32019-12-09 18:35:57 +0000648 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 +0000649 return res;
Pablo Tellodf246182017-07-03 16:25:09 +0100650}
651
652inline float16x8_t vpowq_f16(float16x8_t val, float16x8_t n)
653{
Gian Marco Iodicef2cde9b2018-08-23 15:29:16 +0100654 float32x4_t n0_f32 = vcvt_f32_f16(vget_low_f16(n));
655 float32x4_t n1_f32 = vcvt_f32_f16(vget_high_f16(n));
656 float32x4_t val0_f32 = vcvt_f32_f16(vget_low_f16(val));
657 float32x4_t val1_f32 = vcvt_f32_f16(vget_high_f16(val));
658
659 float32x4_t res0_f32 = vexpq_f32(vmulq_f32(n0_f32, vlogq_f32(val0_f32)));
660 float32x4_t res1_f32 = vexpq_f32(vmulq_f32(n1_f32, vlogq_f32(val1_f32)));
661
662 return vcombine_f16(vcvt_f16_f32(res0_f32), vcvt_f16_f32(res1_f32));
Pablo Tellodf246182017-07-03 16:25:09 +0100663}
Manuel Bottinied753262019-05-15 15:30:47 +0100664
665inline float16x8_t vsinq_f16(float16x8_t val)
666{
667 const float32x4_t val_high = vcvt_f32_f16(vget_high_f16(val));
668 const float32x4_t val_low = vcvt_f32_f16(vget_low_f16(val));
669
670 const float32x4_t res_high = vsinq_f32(val_high);
671 const float32x4_t res_low = vsinq_f32(val_low);
672
673 return vcombine_f16(vcvt_f16_f32(res_low), vcvt_f16_f32(res_high));
674}
675
676inline float16x4_t vsin_f16(float16x4_t val)
677{
678 const float32x4_t val_f32 = vcvt_f32_f16(val);
679 const float32x2_t val_high = vget_high_f32(val_f32);
680 const float32x2_t val_low = vget_low_f32(val_f32);
681
682 const float32x2_t res_high = vsin_f32(val_high);
683 const float32x2_t res_low = vsin_f32(val_low);
684
685 return vcvt_f16_f32(vcombine_f32(res_low, res_high));
686}
687
Sheri Zhang5dda2172021-10-15 19:54:17 +0100688inline float16_t vreduce(const float16x8_t &v)
689{
690 const float16x4_t v0 = vget_high_f16(v);
691 const float16x4_t v1 = vget_low_f16(v);
692 const float16x4_t v_out = vadd_f16(v0, v1);
693
694 const float16_t a = vget_lane_f16(v_out, 0);
695 const float16_t b = vget_lane_f16(v_out, 1);
696 const float16_t c = vget_lane_f16(v_out, 2);
697 const float16_t d = vget_lane_f16(v_out, 3);
698
699 return a + b + c + d;
700}
Alex Gildayc357c472018-03-21 13:54:09 +0000701#endif /* DOXYGEN_SKIP_THIS */
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000702#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Gian Marco Iodice356f6432017-09-22 11:32:21 +0100703} // namespace arm_compute