blob: a5aba0bf23dd1477bbbd4d88cc7ccc2b60f46fce [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 */
Viet-Hoa Doef9da002023-09-27 16:39:05 +010024
25#include "src/core/utils/Math.h"
SiCongLi410e21e2020-12-11 15:07:53 +000026#include "support/ToolchainSupport.h"
27
Manuel Bottinied753262019-05-15 15:30:47 +010028#include <cmath>
morgolock3155f772020-05-11 16:00:04 +010029#include <limits>
Manuel Bottinied753262019-05-15 15:30:47 +010030
Anthony Barbier6ff3b192017-09-04 18:44:23 +010031namespace arm_compute
32{
Alex Gildayc357c472018-03-21 13:54:09 +000033/** Logarithm polynomial coefficients */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010034const std::array<float32x4_t, 8> log_tab = {{
35 vdupq_n_f32(-2.29561495781f),
36 vdupq_n_f32(-2.47071170807f),
37 vdupq_n_f32(-5.68692588806f),
38 vdupq_n_f32(-0.165253549814f),
39 vdupq_n_f32(5.17591238022f),
40 vdupq_n_f32(0.844007015228f),
41 vdupq_n_f32(4.58445882797f),
42 vdupq_n_f32(0.0141278216615f),
43}};
Anthony Barbier6ff3b192017-09-04 18:44:23 +010044
Manuel Bottinied753262019-05-15 15:30:47 +010045/** Sin polynomial coefficients */
46constexpr float te_sin_coeff2 = 0.166666666666f; // 1/(2*3)
47constexpr float te_sin_coeff3 = 0.05f; // 1/(4*5)
48constexpr float te_sin_coeff4 = 0.023809523810f; // 1/(6*7)
49constexpr float te_sin_coeff5 = 0.013888888889f; // 1/(8*9)
50
Alex Gildayc357c472018-03-21 13:54:09 +000051#ifndef DOXYGEN_SKIP_THIS
Viet-Hoa Do86689cd2022-11-21 17:17:56 +000052inline float32x4_t prefer_vfmaq_f32(float32x4_t a, float32x4_t b, float32x4_t c)
53{
Pablo Marquez Tello3f16c572023-06-20 13:39:10 +010054#if __ARM_FEATURE_FMA
Viet-Hoa Do86689cd2022-11-21 17:17:56 +000055 return vfmaq_f32(a, b, c);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010056#else // __ARM_FEATURE_FMA
Viet-Hoa Do86689cd2022-11-21 17:17:56 +000057 return vmlaq_f32(a, b, c);
Pablo Marquez Tello3f16c572023-06-20 13:39:10 +010058#endif // __ARM_FEATURE_FMA
Viet-Hoa Do86689cd2022-11-21 17:17:56 +000059}
60
Georgios Pinitasd8e765b2017-08-02 13:44:33 +010061inline float32x4_t vfloorq_f32(float32x4_t val)
62{
63 static const float32x4_t CONST_1 = vdupq_n_f32(1.f);
64
65 const int32x4_t z = vcvtq_s32_f32(val);
66 const float32x4_t r = vcvtq_f32_s32(z);
67
68 return vbslq_f32(vcgtq_f32(r, val), vsubq_f32(r, CONST_1), r);
69}
70
Usama Arif0a5a57a2019-05-23 14:20:33 +010071inline float32x4_t vroundq_rte_f32(float32x4_t val)
72{
73#ifdef __aarch64__
74 return vrndnq_f32(val);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010075#else // __aarch64__
Usama Arif0a5a57a2019-05-23 14:20:33 +010076 static const float32x4_t CONST_HALF_FLOAT = vdupq_n_f32(0.5f);
Manuel Bottini7bb56c62019-06-26 15:17:09 +010077 static const float32x4_t CONST_1_FLOAT = vdupq_n_f32(1.f);
78 static const int32x4_t CONST_1_INT = vdupq_n_s32(1);
79 const float32x4_t floor_val = vfloorq_f32(val);
80 const float32x4_t diff = vsubq_f32(val, floor_val);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010081 const float32x4_t fp32_upper_limit =
82 vreinterpretq_f32_u32(vdupq_n_u32(0x4B000000)); // 0x4B000000 = (23U + 127U) << 23U
Usama Arif0a5a57a2019-05-23 14:20:33 +010083
84 /*
Ramy Elgammal7fefac72023-04-20 12:32:03 +010085 * 1. Select the floor value when (diff<0.5 || (diff==0.5 && floor_val%2==0).
86 * 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))))
87 *
88 * 2. In case the input value (val) is out of signed int32 range, then simple use the input value as the rounded value
89 * Because:
90 * in this case converting to int32 would saturate
91 * If the input float value is >= 2^23 * 1.00... 23 Zeros ..0 then the rounded value is exactly equal to the input value.
92 * Because:
93 * 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.
94 * Hence, rounding has no effect:
95 * 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 +010096 */
97
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010098 float32x4_t rounded_val = vbslq_f32(
99 vorrq_u32(vcltq_f32(diff, CONST_HALF_FLOAT),
100 vandq_u32(vceqq_f32(diff, CONST_HALF_FLOAT),
101 vmvnq_u32(vtstq_s32(vandq_s32(vcvtq_s32_f32(floor_val), CONST_1_INT), CONST_1_INT)))),
102 floor_val, vaddq_f32(floor_val, CONST_1_FLOAT));
Ramy Elgammal7fefac72023-04-20 12:32:03 +0100103
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100104 float32x4_t result = vbslq_f32(vcgeq_f32(vabsq_f32(val), fp32_upper_limit), val, rounded_val);
Ramy Elgammal7fefac72023-04-20 12:32:03 +0100105
106 return result;
Usama Arif0a5a57a2019-05-23 14:20:33 +0100107#endif // __aarch64__
108}
109
Georgios Pinitascdf51452017-08-31 14:21:36 +0100110inline float32x2_t vinvsqrt_f32(float32x2_t x)
111{
112 float32x2_t sqrt_reciprocal = vrsqrte_f32(x);
113 sqrt_reciprocal = vmul_f32(vrsqrts_f32(vmul_f32(x, sqrt_reciprocal), sqrt_reciprocal), sqrt_reciprocal);
114 sqrt_reciprocal = vmul_f32(vrsqrts_f32(vmul_f32(x, sqrt_reciprocal), sqrt_reciprocal), sqrt_reciprocal);
115
116 return sqrt_reciprocal;
117}
118
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100119inline float32x4_t vinvsqrtq_f32(float32x4_t x)
120{
121 float32x4_t sqrt_reciprocal = vrsqrteq_f32(x);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100122 sqrt_reciprocal = vmulq_f32(vrsqrtsq_f32(vmulq_f32(x, sqrt_reciprocal), sqrt_reciprocal), sqrt_reciprocal);
123 sqrt_reciprocal = vmulq_f32(vrsqrtsq_f32(vmulq_f32(x, sqrt_reciprocal), sqrt_reciprocal), sqrt_reciprocal);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100124
125 return sqrt_reciprocal;
126}
127
Georgios Pinitascdf51452017-08-31 14:21:36 +0100128inline float32x2_t vinv_f32(float32x2_t x)
129{
130 float32x2_t recip = vrecpe_f32(x);
131 recip = vmul_f32(vrecps_f32(x, recip), recip);
132 recip = vmul_f32(vrecps_f32(x, recip), recip);
133 return recip;
134}
135
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100136inline float32x4_t vinvq_f32(float32x4_t x)
137{
138 float32x4_t recip = vrecpeq_f32(x);
139 recip = vmulq_f32(vrecpsq_f32(x, recip), recip);
140 recip = vmulq_f32(vrecpsq_f32(x, recip), recip);
141 return recip;
142}
143
144inline float32x4_t vtaylor_polyq_f32(float32x4_t x, const std::array<float32x4_t, 8> &coeffs)
145{
146 float32x4_t A = vmlaq_f32(coeffs[0], coeffs[4], x);
147 float32x4_t B = vmlaq_f32(coeffs[2], coeffs[6], x);
148 float32x4_t C = vmlaq_f32(coeffs[1], coeffs[5], x);
149 float32x4_t D = vmlaq_f32(coeffs[3], coeffs[7], x);
150 float32x4_t x2 = vmulq_f32(x, x);
151 float32x4_t x4 = vmulq_f32(x2, x2);
152 float32x4_t res = vmlaq_f32(vmlaq_f32(A, B, x2), vmlaq_f32(C, D, x2), x4);
153 return res;
154}
155
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100156static const uint32_t exp_f32_coeff[] = {
Viet-Hoa Do86689cd2022-11-21 17:17:56 +0000157 0x3f7ffff6, // x^1: 0x1.ffffecp-1f
158 0x3efffedb, // x^2: 0x1.fffdb6p-2f
159 0x3e2aaf33, // x^3: 0x1.555e66p-3f
160 0x3d2b9f17, // x^4: 0x1.573e2ep-5f
161 0x3c072010, // x^5: 0x1.0e4020p-7f
162};
163
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100164inline float32x4_t vexpq_f32(float32x4_t x)
165{
Viet-Hoa Do86689cd2022-11-21 17:17:56 +0000166 const auto c1 = vreinterpretq_f32_u32(vdupq_n_u32(exp_f32_coeff[0]));
167 const auto c2 = vreinterpretq_f32_u32(vdupq_n_u32(exp_f32_coeff[1]));
168 const auto c3 = vreinterpretq_f32_u32(vdupq_n_u32(exp_f32_coeff[2]));
169 const auto c4 = vreinterpretq_f32_u32(vdupq_n_u32(exp_f32_coeff[3]));
170 const auto c5 = vreinterpretq_f32_u32(vdupq_n_u32(exp_f32_coeff[4]));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100171
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100172 const auto shift = vreinterpretq_f32_u32(vdupq_n_u32(0x4b00007f)); // 2^23 + 127 = 0x1.0000fep23f
173 const auto inv_ln2 = vreinterpretq_f32_u32(vdupq_n_u32(0x3fb8aa3b)); // 1 / ln(2) = 0x1.715476p+0f
174 const auto neg_ln2_hi =
175 vreinterpretq_f32_u32(vdupq_n_u32(0xbf317200)); // -ln(2) from bits -1 to -19: -0x1.62e400p-1f
176 const auto neg_ln2_lo =
177 vreinterpretq_f32_u32(vdupq_n_u32(0xb5bfbe8e)); // -ln(2) from bits -20 to -42: -0x1.7f7d1cp-20f
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100178
Viet-Hoa Do86689cd2022-11-21 17:17:56 +0000179 const auto inf = vdupq_n_f32(std::numeric_limits<float>::infinity());
Ramy Elgammal7fefac72023-04-20 12:32:03 +0100180 const auto max_input = vdupq_n_f32(88.37f); // Approximately ln(2^127.5)
Viet-Hoa Do86689cd2022-11-21 17:17:56 +0000181 const auto zero = vdupq_n_f32(0.f);
Ramy Elgammal7fefac72023-04-20 12:32:03 +0100182 const auto min_input = vdupq_n_f32(-86.64f); // Approximately ln(2^-125)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100183
Viet-Hoa Do86689cd2022-11-21 17:17:56 +0000184 // Range reduction:
185 // e^x = 2^n * e^r
186 // where:
187 // n = floor(x / ln(2))
188 // r = x - n * ln(2)
189 //
190 // By adding x / ln(2) with 2^23 + 127 (shift):
191 // * As FP32 fraction part only has 23-bits, the addition of 2^23 + 127 forces decimal part
192 // of x / ln(2) out of the result. The integer part of x / ln(2) (i.e. n) + 127 will occupy
193 // the whole fraction part of z in FP32 format.
194 // Subtracting 2^23 + 127 (shift) from z will result in the integer part of x / ln(2)
195 // (i.e. n) because the decimal part has been pushed out and lost.
196 // * The addition of 127 makes the FP32 fraction part of z ready to be used as the exponent
197 // in FP32 format. Left shifting z by 23 bits will result in 2^n.
Ramy Elgammal7fefac72023-04-20 12:32:03 +0100198 const auto z = prefer_vfmaq_f32(shift, x, inv_ln2);
199 const auto n = z - shift;
200 const auto scale = vreinterpretq_f32_u32(vreinterpretq_u32_f32(z) << 23); // 2^n
Viet-Hoa Do86689cd2022-11-21 17:17:56 +0000201
202 // The calculation of n * ln(2) is done using 2 steps to achieve accuracy beyond FP32.
203 // This outperforms longer Taylor series (3-4 tabs) both in term of accuracy and performance.
204 const auto r_hi = prefer_vfmaq_f32(x, n, neg_ln2_hi);
Ramy Elgammal7fefac72023-04-20 12:32:03 +0100205 const auto r = prefer_vfmaq_f32(r_hi, n, neg_ln2_lo);
Viet-Hoa Do86689cd2022-11-21 17:17:56 +0000206
207 // Compute the truncated Taylor series of e^r.
208 // poly = scale * (1 + c1 * r + c2 * r^2 + c3 * r^3 + c4 * r^4 + c5 * r^5)
209 const auto r2 = r * r;
210
Ramy Elgammal7fefac72023-04-20 12:32:03 +0100211 const auto p1 = c1 * r;
212 const auto p23 = prefer_vfmaq_f32(c2, c3, r);
213 const auto p45 = prefer_vfmaq_f32(c4, c5, r);
214 const auto p2345 = prefer_vfmaq_f32(p23, p45, r2);
Viet-Hoa Do86689cd2022-11-21 17:17:56 +0000215 const auto p12345 = prefer_vfmaq_f32(p1, p2345, r2);
216
217 auto poly = prefer_vfmaq_f32(scale, p12345, scale);
218
219 // Handle underflow and overflow.
220 poly = vbslq_f32(vcltq_f32(x, min_input), zero, poly);
221 poly = vbslq_f32(vcgtq_f32(x, max_input), inf, poly);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100222
223 return poly;
224}
225
Murray Kornelsen926f5022022-07-13 21:22:39 -0400226#ifdef __aarch64__
227inline float32x4_t verfq_f32(float32x4_t x)
228{
Viet-Hoa Doef9da002023-09-27 16:39:05 +0100229 const float32x4_t max_value = vdupq_n_f32(3.9375); // 4 - 8/128
230 const float32x4_t shift = vdupq_n_f32(65536); // 2^16
231 const float32x4_t third = vdupq_n_f32(0.3333333333); // 1/3
232 const float32x4_t one = vdupq_n_f32(1.f);
233 const uint32x4_t max_index = vdupq_n_u32(512);
234 const uint32x4_t sign_mask = vdupq_n_u32(0x7fffffff);
Murray Kornelsen926f5022022-07-13 21:22:39 -0400235
Viet-Hoa Doef9da002023-09-27 16:39:05 +0100236 const float32x4_t x_abs = vabsq_f32(x);
Murray Kornelsen926f5022022-07-13 21:22:39 -0400237
Viet-Hoa Doef9da002023-09-27 16:39:05 +0100238 // erf(x) for x in [0, 3.9375] is approxiated as follows:
239 //
240 // erf(x) = erf(r) + scale(r) * d * (1 - r * d - 1/3 * d^2)
241 //
242 // where:
243 // r = floor(x * 128) / 128
244 // d = x - r
245 //
246 // erf(r) and scale(r) are stored in a 513-entry lookup table.
247 // The LUT covers the range from 0 to 4 with the step of 1/128.
248 //
249 // Special cases:
250 // erf(x) = 1 for x > 3.9375
251 // erf(x) = -1 for x < -3.9375
Murray Kornelsen926f5022022-07-13 21:22:39 -0400252
Viet-Hoa Doef9da002023-09-27 16:39:05 +0100253 // Find the LUT indices by rounding the input value to the step of 1/128.
254 //
255 // `shift` is used to push out the 16 LSBs of the input value. Only 7 bits in the fraction part
256 // of the input value is preserved.
257 const float32x4_t z = x_abs + shift;
258 const float32x4_t r = z - shift;
Murray Kornelsen926f5022022-07-13 21:22:39 -0400259
Viet-Hoa Doef9da002023-09-27 16:39:05 +0100260 uint32x4_t index = vreinterpretq_u32_f32(z) - vreinterpretq_u32_f32(shift);
261 index = vminq_u32(index, max_index);
Murray Kornelsen926f5022022-07-13 21:22:39 -0400262
Viet-Hoa Doef9da002023-09-27 16:39:05 +0100263 // Lookup erf(r) and scale(r).
264 const float64_t entry_0 = *reinterpret_cast<const float64_t *>(&erf_f32_lut[index[0]]);
265 const float64_t entry_1 = *reinterpret_cast<const float64_t *>(&erf_f32_lut[index[1]]);
266 const float64_t entry_2 = *reinterpret_cast<const float64_t *>(&erf_f32_lut[index[2]]);
267 const float64_t entry_3 = *reinterpret_cast<const float64_t *>(&erf_f32_lut[index[3]]);
Murray Kornelsen926f5022022-07-13 21:22:39 -0400268
Viet-Hoa Doef9da002023-09-27 16:39:05 +0100269 const float32x4_t entry_01 = vreinterpretq_f32_f64(float64x2_t{entry_0, entry_1});
270 const float32x4_t entry_23 = vreinterpretq_f32_f64(float64x2_t{entry_2, entry_3});
Murray Kornelsen926f5022022-07-13 21:22:39 -0400271
Viet-Hoa Doef9da002023-09-27 16:39:05 +0100272 const float32x4_t erf_r = vuzp1q_f32(entry_01, entry_23);
273 const float32x4_t scale_r = vuzp2q_f32(entry_01, entry_23);
Murray Kornelsen926f5022022-07-13 21:22:39 -0400274
Viet-Hoa Doef9da002023-09-27 16:39:05 +0100275 // Approximate erf(x) = erf(r) + scale(r) * d * (1 - r * d - 1/3 * d^2).
276 const float32x4_t d = x_abs - r;
277 const float32x4_t d2 = d * d;
278
279 const float32x4_t t0 = vfmaq_f32(r, third, d); // t0 = r + 1/3 * d.
280 const float32x4_t t1 = vfmsq_f32(d, d2, t0); // t1 = d - d2 * t0 = d * (1 - r * d - 1/3 * d^2).
281 const float32x4_t erf_x = vfmaq_f32(erf_r, scale_r, t1);
282
283 const float32x4_t clamped = vbslq_f32(x_abs > max_value, one, erf_x);
284 const float32x4_t result = vbslq_f32(sign_mask, clamped, x);
Murray Kornelsen926f5022022-07-13 21:22:39 -0400285
286 return result;
287}
288#endif // #ifdef __aarch64__
289
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100290inline float32x4_t vlogq_f32(float32x4_t x)
291{
292 static const int32x4_t CONST_127 = vdupq_n_s32(127); // 127
293 static const float32x4_t CONST_LN2 = vdupq_n_f32(0.6931471805f); // ln(2)
294
295 // Extract exponent
296 int32x4_t m = vsubq_s32(vreinterpretq_s32_u32(vshrq_n_u32(vreinterpretq_u32_f32(x), 23)), CONST_127);
297 float32x4_t val = vreinterpretq_f32_s32(vsubq_s32(vreinterpretq_s32_f32(x), vshlq_n_s32(m, 23)));
298
299 // Polynomial Approximation
300 float32x4_t poly = vtaylor_polyq_f32(val, log_tab);
301
302 // Reconstruct
303 poly = vmlaq_f32(poly, vcvtq_f32_s32(m), CONST_LN2);
304
305 return poly;
306}
307
308inline float32x4_t vtanhq_f32(float32x4_t val)
309{
310 static const float32x4_t CONST_1 = vdupq_n_f32(1.f);
311 static const float32x4_t CONST_2 = vdupq_n_f32(2.f);
312 static const float32x4_t CONST_MIN_TANH = vdupq_n_f32(-10.f);
313 static const float32x4_t CONST_MAX_TANH = vdupq_n_f32(10.f);
Aleksandr Nikolaev7e9f34d2021-05-04 16:46:27 +0100314 static const float32x4_t CONST_THR = vdupq_n_f32(5.e-3);
315 static const float32x4_t CONST_1_3 = vdupq_n_f32(0.3333333f);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100316
Sheri Zhang5dda2172021-10-15 19:54:17 +0100317 float32x4_t x = vminq_f32(vmaxq_f32(val, CONST_MIN_TANH), CONST_MAX_TANH);
Aleksandr Nikolaev7e9f34d2021-05-04 16:46:27 +0100318 // x * (1 - x^2/3) if |x| < 5.e-3 or (exp2x - 1) / (exp2x + 1) otherwise
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100319 float32x4_t exp2x =
320 vbslq_f32(vcgtq_f32(vabsq_f32(x), CONST_THR), vexpq_f32(vmulq_f32(CONST_2, x)), vmulq_f32(x, x));
321 float32x4_t num =
322 vbslq_f32(vcgtq_f32(vabsq_f32(x), CONST_THR), vsubq_f32(exp2x, CONST_1), vmulq_f32(CONST_1_3, exp2x));
323 float32x4_t den = vbslq_f32(vcgtq_f32(vabsq_f32(x), CONST_THR), vaddq_f32(exp2x, CONST_1), vsubq_f32(CONST_1, num));
324 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 +0100325 return tanh;
326}
327
328inline float32x4_t vpowq_f32(float32x4_t val, float32x4_t n)
329{
330 return vexpq_f32(vmulq_f32(n, vlogq_f32(val)));
331}
Manuel Bottinied753262019-05-15 15:30:47 +0100332
333inline float32x4_t vsinq_f32(float32x4_t val)
334{
335 const float32x4_t pi_v = vdupq_n_f32(M_PI);
336 const float32x4_t pio2_v = vdupq_n_f32(M_PI / 2);
337 const float32x4_t ipi_v = vdupq_n_f32(1 / M_PI);
338
339 //Find positive or negative
340 const int32x4_t c_v = vabsq_s32(vcvtq_s32_f32(vmulq_f32(val, ipi_v)));
341 const uint32x4_t sign_v = vcleq_f32(val, vdupq_n_f32(0));
342 const uint32x4_t odd_v = vandq_u32(vreinterpretq_u32_s32(c_v), vdupq_n_u32(1));
343
344 uint32x4_t neg_v = veorq_u32(odd_v, sign_v);
345
346 //Modulus a - (n * int(a*(1/n)))
347 float32x4_t ma = vsubq_f32(vabsq_f32(val), vmulq_f32(pi_v, vcvtq_f32_s32(c_v)));
348 const uint32x4_t reb_v = vcgeq_f32(ma, pio2_v);
349
350 //Rebase a between 0 and pi/2
351 ma = vbslq_f32(reb_v, vsubq_f32(pi_v, ma), ma);
352
353 //Taylor series
354 const float32x4_t ma2 = vmulq_f32(ma, ma);
355
356 //2nd elem: x^3 / 3!
357 float32x4_t elem = vmulq_f32(vmulq_f32(ma, ma2), vdupq_n_f32(te_sin_coeff2));
358 float32x4_t res = vsubq_f32(ma, elem);
359
360 //3rd elem: x^5 / 5!
361 elem = vmulq_f32(vmulq_f32(elem, ma2), vdupq_n_f32(te_sin_coeff3));
362 res = vaddq_f32(res, elem);
363
364 //4th elem: x^7 / 7!float32x2_t vsin_f32(float32x2_t val)
365 elem = vmulq_f32(vmulq_f32(elem, ma2), vdupq_n_f32(te_sin_coeff4));
366 res = vsubq_f32(res, elem);
367
368 //5th elem: x^9 / 9!
369 elem = vmulq_f32(vmulq_f32(elem, ma2), vdupq_n_f32(te_sin_coeff5));
370 res = vaddq_f32(res, elem);
371
372 //Change of sign
373 neg_v = vshlq_n_u32(neg_v, 31);
374 res = vreinterpretq_f32_u32(veorq_u32(vreinterpretq_u32_f32(res), neg_v));
375 return res;
376}
377
378inline float32x2_t vsin_f32(float32x2_t val)
379{
380 const float32x2_t pi_v = vdup_n_f32(M_PI);
381 const float32x2_t pio2_v = vdup_n_f32(M_PI / 2);
382 const float32x2_t ipi_v = vdup_n_f32(1 / M_PI);
383
384 //Find positive or negative
385 const int32x2_t c_v = vabs_s32(vcvt_s32_f32(vmul_f32(val, ipi_v)));
386 const uint32x2_t sign_v = vcle_f32(val, vdup_n_f32(0));
387 const uint32x2_t odd_v = vand_u32(vreinterpret_u32_s32(c_v), vdup_n_u32(1));
388
389 uint32x2_t neg_v = veor_u32(odd_v, sign_v);
390
391 //Modulus a - (n * int(a*(1/n)))
392 float32x2_t ma = vsub_f32(vabs_f32(val), vmul_f32(pi_v, vcvt_f32_s32(c_v)));
393 const uint32x2_t reb_v = vcge_f32(ma, pio2_v);
394
395 //Rebase a between 0 and pi/2
396 ma = vbsl_f32(reb_v, vsub_f32(pi_v, ma), ma);
397
398 //Taylor series
399 const float32x2_t ma2 = vmul_f32(ma, ma);
400
401 //2nd elem: x^3 / 3!
402 float32x2_t elem = vmul_f32(vmul_f32(ma, ma2), vdup_n_f32(te_sin_coeff2));
403 float32x2_t res = vsub_f32(ma, elem);
404
405 //3rd elem: x^5 / 5!
406 elem = vmul_f32(vmul_f32(elem, ma2), vdup_n_f32(te_sin_coeff3));
407 res = vadd_f32(res, elem);
408
409 //4th elem: x^7 / 7!float32x2_t vsin_f32(float32x2_t val)
410 elem = vmul_f32(vmul_f32(elem, ma2), vdup_n_f32(te_sin_coeff4));
411 res = vsub_f32(res, elem);
412
413 //5th elem: x^9 / 9!
414 elem = vmul_f32(vmul_f32(elem, ma2), vdup_n_f32(te_sin_coeff5));
415 res = vadd_f32(res, elem);
416
417 //Change of sign
418 neg_v = vshl_n_u32(neg_v, 31);
419 res = vreinterpret_f32_u32(veor_u32(vreinterpret_u32_f32(res), neg_v));
420 return res;
421}
422
Alex Gildayc357c472018-03-21 13:54:09 +0000423#endif /* DOXYGEN_SKIP_THIS */
424
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100425inline int32x4_t rounding_divide_by_pow2(int32x4_t x, int32x4_t exponent)
426{
427 const int32x4_t shift_vec = vnegq_s32(exponent);
428 const int32x4_t fixup = vshrq_n_s32(vandq_s32(x, shift_vec), 31);
429 const int32x4_t fixed_up_x = vqaddq_s32(x, fixup);
430 return vrshlq_s32(fixed_up_x, shift_vec);
431}
432
Manuel Bottini7bb56c62019-06-26 15:17:09 +0100433inline int32x4_t rounding_divide_by_pow2(int32x4_t x, int exponent)
434{
435 const int32x4_t shift_vec = vdupq_n_s32(-exponent);
436 const int32x4_t fixup = vshrq_n_s32(vandq_s32(x, shift_vec), 31);
437 const int32x4_t fixed_up_x = vqaddq_s32(x, fixup);
438 return vrshlq_s32(fixed_up_x, shift_vec);
439}
440
441inline int32_t rounding_divide_by_pow2(int32_t x, int exponent)
442{
443 const int32_t mask = (1 << exponent) - 1;
444 const int32_t threshold = (mask >> 1) + (x < 0 ? 1 : 0);
445 return (x >> exponent) + ((x & mask) > threshold ? 1 : 0);
446}
447
Manuel Bottini21079dd2019-10-29 17:20:09 +0000448inline float32x4x4_t convert_uint8x16_to_float32x4x4(const uint8x16_t &in)
449{
450 float32x4x4_t out;
451
452 const auto tmp1 = vmovl_u8(vget_low_u8(in));
453 out.val[0] = vcvtq_f32_u32(vmovl_u16(vget_low_u16(tmp1)));
454 out.val[1] = vcvtq_f32_u32(vmovl_u16(vget_high_u16(tmp1)));
455
456 const auto tmp2 = vmovl_u8(vget_high_u8(in));
457 out.val[2] = vcvtq_f32_u32(vmovl_u16(vget_low_u16(tmp2)));
458 out.val[3] = vcvtq_f32_u32(vmovl_u16(vget_high_u16(tmp2)));
459 return out;
460}
461
Sang-Hoon Parkc3a74202019-11-22 16:05:46 +0000462inline float32x4x4_t convert_int8x16_to_float32x4x4(const int8x16_t &in)
463{
464 float32x4x4_t out;
465
466 const auto tmp1 = vmovl_s8(vget_low_s8(in));
467 out.val[0] = vcvtq_f32_s32(vmovl_s16(vget_low_s16(tmp1)));
468 out.val[1] = vcvtq_f32_s32(vmovl_s16(vget_high_s16(tmp1)));
469
470 const auto tmp2 = vmovl_s8(vget_high_s8(in));
471 out.val[2] = vcvtq_f32_s32(vmovl_s16(vget_low_s16(tmp2)));
472 out.val[3] = vcvtq_f32_s32(vmovl_s16(vget_high_s16(tmp2)));
473 return out;
474}
475
Manuel Bottini4370cff2020-02-07 16:31:59 +0000476template <>
477inline float32x4x4_t convert_to_float32x4x4(const uint8x16_t &in)
478{
479 return convert_uint8x16_to_float32x4x4(in);
480}
481
482template <>
483inline float32x4x4_t convert_to_float32x4x4(const int8x16_t &in)
484{
485 return convert_int8x16_to_float32x4x4(in);
486}
487
Manuel Bottini21079dd2019-10-29 17:20:09 +0000488inline void convert_float32x4x3_to_uint8x8x3(const float32x4x3_t &in1, const float32x4x3_t &in2, uint8x8x3_t &out)
489{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100490 out.val[0] = vqmovn_u16(vcombine_u16(vqmovn_u32(vcvtq_u32_f32(in1.val[0])), vqmovn_u32(vcvtq_u32_f32(in2.val[0]))));
491 out.val[1] = vqmovn_u16(vcombine_u16(vqmovn_u32(vcvtq_u32_f32(in1.val[1])), vqmovn_u32(vcvtq_u32_f32(in2.val[1]))));
492 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 +0000493}
494
Sang-Hoon Parkc3a74202019-11-22 16:05:46 +0000495inline void convert_float32x4x4_to_uint8x16(const float32x4x4_t &in, uint8x16_t &out)
Manuel Bottini21079dd2019-10-29 17:20:09 +0000496{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100497 const auto low = vcombine_u16(vqmovn_u32(vcvtq_u32_f32(in.val[0])), vqmovn_u32(vcvtq_u32_f32(in.val[1])));
498 const auto high = vcombine_u16(vqmovn_u32(vcvtq_u32_f32(in.val[2])), vqmovn_u32(vcvtq_u32_f32(in.val[3])));
499 out = vcombine_u8(vqmovn_u16(low), vqmovn_u16(high));
Manuel Bottini21079dd2019-10-29 17:20:09 +0000500}
501
Sang-Hoon Parkc3a74202019-11-22 16:05:46 +0000502inline void convert_float32x4x4_to_int8x16(const float32x4x4_t &in, int8x16_t &out)
503{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100504 const auto low = vcombine_s16(vqmovn_s32(vcvtq_s32_f32(in.val[0])), vqmovn_s32(vcvtq_s32_f32(in.val[1])));
505 const auto high = vcombine_s16(vqmovn_s32(vcvtq_s32_f32(in.val[2])), vqmovn_s32(vcvtq_s32_f32(in.val[3])));
506 out = vcombine_s8(vqmovn_s16(low), vqmovn_s16(high));
Sang-Hoon Parkc3a74202019-11-22 16:05:46 +0000507}
508
Sang-Hoon Parkdcf3c7e2021-03-04 17:03:46 +0000509template <>
510inline uint8x16_t convert_float_to_int<float32x4x4_t, uint8x16_t>(const float32x4x4_t &in)
511{
512 uint8x16_t out;
513 convert_float32x4x4_to_uint8x16(in, out);
514 return out;
515}
516
517template <>
518inline float32x4x4_t convert_int_to_float<float32x4x4_t, uint8x16_t>(const uint8x16_t &in)
519{
520 return convert_uint8x16_to_float32x4x4(in);
521}
522
523template <>
524inline int8x16_t convert_float_to_int<float32x4x4_t, int8x16_t>(const float32x4x4_t &in)
525{
526 int8x16_t out;
527 convert_float32x4x4_to_int8x16(in, out);
528 return out;
529}
530
531template <>
532inline float32x4x4_t convert_int_to_float<float32x4x4_t, int8x16_t>(const int8x16_t &in)
533{
534 return convert_int8x16_to_float32x4x4(in);
535}
536
Sheri Zhang5dda2172021-10-15 19:54:17 +0100537inline float vreduce(const float32x4_t &v)
538{
539 const float32x2_t v0 = vget_high_f32(v);
540 const float32x2_t v1 = vget_low_f32(v);
541 const float32x2_t v_out = vadd_f32(v0, v1);
542
543 const float a = vget_lane_f32(v_out, 0);
544 const float b = vget_lane_f32(v_out, 1);
545
546 return a + b;
547}
548
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000549#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Alex Gildayc357c472018-03-21 13:54:09 +0000550/** Exponent polynomial coefficients */
Alex Gildayc357c472018-03-21 13:54:09 +0000551/** Logarithm polynomial coefficients */
Alex Gildayc357c472018-03-21 13:54:09 +0000552#ifndef DOXYGEN_SKIP_THIS
Georgios Pinitas565bf2d2018-08-31 11:46:49 +0100553inline float16x8_t vfloorq_f16(float16x8_t val)
554{
555 static const float16x8_t CONST_1 = vdupq_n_f16(1.f);
556
557 const int16x8_t z = vcvtq_s16_f16(val);
558 const float16x8_t r = vcvtq_f16_s16(z);
559
560 return vbslq_f16(vcgtq_f16(r, val), vsubq_f16(r, CONST_1), r);
561}
Usama Arif0a5a57a2019-05-23 14:20:33 +0100562
563inline float16x8_t vroundq_rte_f16(float16x8_t val)
564{
565 return vrndnq_f16(val);
566}
567
Georgios Pinitascdf51452017-08-31 14:21:36 +0100568inline float16x4_t vinvsqrt_f16(float16x4_t x)
569{
570 float16x4_t sqrt_reciprocal = vrsqrte_f16(x);
571 sqrt_reciprocal = vmul_f16(vrsqrts_f16(vmul_f16(x, sqrt_reciprocal), sqrt_reciprocal), sqrt_reciprocal);
572 sqrt_reciprocal = vmul_f16(vrsqrts_f16(vmul_f16(x, sqrt_reciprocal), sqrt_reciprocal), sqrt_reciprocal);
573 return sqrt_reciprocal;
574}
575
Pablo Tello91654c42017-07-05 11:32:17 +0100576inline float16x8_t vinvsqrtq_f16(float16x8_t x)
577{
578 float16x8_t sqrt_reciprocal = vrsqrteq_f16(x);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100579 sqrt_reciprocal = vmulq_f16(vrsqrtsq_f16(vmulq_f16(x, sqrt_reciprocal), sqrt_reciprocal), sqrt_reciprocal);
580 sqrt_reciprocal = vmulq_f16(vrsqrtsq_f16(vmulq_f16(x, sqrt_reciprocal), sqrt_reciprocal), sqrt_reciprocal);
Pablo Tello91654c42017-07-05 11:32:17 +0100581 return sqrt_reciprocal;
582}
Pablo Tellodf246182017-07-03 16:25:09 +0100583
Georgios Pinitascdf51452017-08-31 14:21:36 +0100584inline float16x4_t vinv_f16(float16x4_t x)
585{
586 float16x4_t recip = vrecpe_f16(x);
587 recip = vmul_f16(vrecps_f16(x, recip), recip);
588 recip = vmul_f16(vrecps_f16(x, recip), recip);
589 return recip;
590}
591
Pablo Tellodf246182017-07-03 16:25:09 +0100592inline float16x8_t vinvq_f16(float16x8_t x)
593{
594 float16x8_t recip = vrecpeq_f16(x);
595 recip = vmulq_f16(vrecpsq_f16(x, recip), recip);
596 recip = vmulq_f16(vrecpsq_f16(x, recip), recip);
597 return recip;
598}
599
Jonathan Deakin2bc8cfe2022-10-13 10:50:25 +0000600inline float16x4_t vtanh_rational_approx_f16(float16x4_t x16)
Pablo Tello91654c42017-07-05 11:32:17 +0100601{
Jonathan Deakin2bc8cfe2022-10-13 10:50:25 +0000602 // Calculate rational approximation part of tanh exactly on a half-register of F16 by using F32s
603 // Note: doesn't handle overflows, needs truncating at |x| = 4.508
604 const float32x4_t x = vcvt_f32_f16(x16);
605
606 const float32x4_t ONE = vdupq_n_f32(1.0f);
Ramy Elgammal7fefac72023-04-20 12:32:03 +0100607 const float32x4_t C1 = vdupq_n_f32(0.43760237f);
608 const float32x4_t C2 = vdupq_n_f32(0.104402f);
609 const float32x4_t C3 = vdupq_n_f32(0.013442706f);
610 const float32x4_t C4 = vdupq_n_f32(0.00073561433f);
Jonathan Deakin2bc8cfe2022-10-13 10:50:25 +0000611
Ramy Elgammal7fefac72023-04-20 12:32:03 +0100612 const float32x4_t x2 = vmulq_f32(x, x);
Jonathan Deakin2bc8cfe2022-10-13 10:50:25 +0000613
614 // Denominator polynomial 1 + C1*x^2 + C3*x^4
615 float32x4_t denom = vfmaq_f32(C1, C3, x2);
Ramy Elgammal7fefac72023-04-20 12:32:03 +0100616 denom = vfmaq_f32(ONE, x2, denom);
Jonathan Deakin2bc8cfe2022-10-13 10:50:25 +0000617
618 // Numerator polynomial x*(1 + C2*x^2 + C4*x^4)
619 float32x4_t numer = vfmaq_f32(C2, C4, x2);
Ramy Elgammal7fefac72023-04-20 12:32:03 +0100620 numer = vfmaq_f32(ONE, x2, numer);
621 numer = vmulq_f32(numer, x);
Jonathan Deakin2bc8cfe2022-10-13 10:50:25 +0000622
623 return vcvt_f16_f32(vdivq_f32(numer, denom));
624}
625
626inline float16x8_t vtanhq_f16(float16x8_t x)
627{
628 // Split into high/low and use rational approximation on both parts exactly
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100629 const float16x8_t tanh =
630 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 +0000631
632 // tanh(x) == sign(x) to F16 precision for |x| >= 4.508, use sign after this
Ramy Elgammal7fefac72023-04-20 12:32:03 +0100633 const float16x8_t ONE = vdupq_n_f16(1.0f);
634 const float16x8_t MAX_X = vdupq_n_f16(4.508f);
635 const auto at_limit = vcageq_f16(x, MAX_X); // |x| >= 4.508
636 const float16x8_t sign_x = vbslq_f16(vclezq_f16(x), -ONE, ONE);
Jonathan Deakin2bc8cfe2022-10-13 10:50:25 +0000637 return vbslq_f16(at_limit, sign_x, tanh);
Pablo Tello91654c42017-07-05 11:32:17 +0100638}
639
Pablo Tellodf246182017-07-03 16:25:09 +0100640inline float16x8_t vtaylor_polyq_f16(float16x8_t x, const std::array<float16x8_t, 8> &coeffs)
641{
642 const float16x8_t A = vaddq_f16(coeffs[0], vmulq_f16(coeffs[4], x));
643 const float16x8_t B = vaddq_f16(coeffs[2], vmulq_f16(coeffs[6], x));
644 const float16x8_t C = vaddq_f16(coeffs[1], vmulq_f16(coeffs[5], x));
645 const float16x8_t D = vaddq_f16(coeffs[3], vmulq_f16(coeffs[7], x));
646 const float16x8_t x2 = vmulq_f16(x, x);
647 const float16x8_t x4 = vmulq_f16(x2, x2);
648 const float16x8_t res = vaddq_f16(vaddq_f16(A, vmulq_f16(B, x2)), vmulq_f16(vaddq_f16(C, vmulq_f16(D, x2)), x4));
649 return res;
650}
651
652inline float16x8_t vexpq_f16(float16x8_t x)
653{
Michele Di Giorgio1c948d42018-11-20 16:03:01 +0000654 const float32x4_t x_high = vcvt_f32_f16(vget_high_f16(x));
655 const float32x4_t x_low = vcvt_f32_f16(vget_low_f16(x));
Anthony Barbier3a6163e2018-08-10 17:36:36 +0100656
Georgios Pinitasf2cdce32019-12-09 18:35:57 +0000657 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 +0000658 return res;
Pablo Tellodf246182017-07-03 16:25:09 +0100659}
660
Murray Kornelsen926f5022022-07-13 21:22:39 -0400661#ifdef __aarch64__
662inline float16x8_t verfq_f16(float16x8_t x)
663{
664 const float32x4_t x_high = vcvt_f32_f16(vget_high_f16(x));
665 const float32x4_t x_low = vcvt_f32_f16(vget_low_f16(x));
666
667 const float16x8_t res = vcombine_f16(vcvt_f16_f32(verfq_f32(x_low)), vcvt_f16_f32(verfq_f32(x_high)));
668 return res;
669}
670#endif // #ifdef __aarch64__
671
Pablo Tellodf246182017-07-03 16:25:09 +0100672inline float16x8_t vlogq_f16(float16x8_t x)
673{
Georgios Pinitas5a594532018-12-03 14:30:05 +0000674 const float32x4_t x_high = vcvt_f32_f16(vget_high_f16(x));
675 const float32x4_t x_low = vcvt_f32_f16(vget_low_f16(x));
Anthony Barbier3a6163e2018-08-10 17:36:36 +0100676
Georgios Pinitasf2cdce32019-12-09 18:35:57 +0000677 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 +0000678 return res;
Pablo Tellodf246182017-07-03 16:25:09 +0100679}
680
681inline float16x8_t vpowq_f16(float16x8_t val, float16x8_t n)
682{
Gian Marco Iodicef2cde9b2018-08-23 15:29:16 +0100683 float32x4_t n0_f32 = vcvt_f32_f16(vget_low_f16(n));
684 float32x4_t n1_f32 = vcvt_f32_f16(vget_high_f16(n));
685 float32x4_t val0_f32 = vcvt_f32_f16(vget_low_f16(val));
686 float32x4_t val1_f32 = vcvt_f32_f16(vget_high_f16(val));
687
688 float32x4_t res0_f32 = vexpq_f32(vmulq_f32(n0_f32, vlogq_f32(val0_f32)));
689 float32x4_t res1_f32 = vexpq_f32(vmulq_f32(n1_f32, vlogq_f32(val1_f32)));
690
691 return vcombine_f16(vcvt_f16_f32(res0_f32), vcvt_f16_f32(res1_f32));
Pablo Tellodf246182017-07-03 16:25:09 +0100692}
Manuel Bottinied753262019-05-15 15:30:47 +0100693
694inline float16x8_t vsinq_f16(float16x8_t val)
695{
696 const float32x4_t val_high = vcvt_f32_f16(vget_high_f16(val));
697 const float32x4_t val_low = vcvt_f32_f16(vget_low_f16(val));
698
699 const float32x4_t res_high = vsinq_f32(val_high);
700 const float32x4_t res_low = vsinq_f32(val_low);
701
702 return vcombine_f16(vcvt_f16_f32(res_low), vcvt_f16_f32(res_high));
703}
704
705inline float16x4_t vsin_f16(float16x4_t val)
706{
707 const float32x4_t val_f32 = vcvt_f32_f16(val);
708 const float32x2_t val_high = vget_high_f32(val_f32);
709 const float32x2_t val_low = vget_low_f32(val_f32);
710
711 const float32x2_t res_high = vsin_f32(val_high);
712 const float32x2_t res_low = vsin_f32(val_low);
713
714 return vcvt_f16_f32(vcombine_f32(res_low, res_high));
715}
716
Sheri Zhang5dda2172021-10-15 19:54:17 +0100717inline float16_t vreduce(const float16x8_t &v)
718{
719 const float16x4_t v0 = vget_high_f16(v);
720 const float16x4_t v1 = vget_low_f16(v);
721 const float16x4_t v_out = vadd_f16(v0, v1);
722
723 const float16_t a = vget_lane_f16(v_out, 0);
724 const float16_t b = vget_lane_f16(v_out, 1);
725 const float16_t c = vget_lane_f16(v_out, 2);
726 const float16_t d = vget_lane_f16(v_out, 3);
727
728 return a + b + c + d;
729}
Alex Gildayc357c472018-03-21 13:54:09 +0000730#endif /* DOXYGEN_SKIP_THIS */
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000731#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Gian Marco Iodice356f6432017-09-22 11:32:21 +0100732} // namespace arm_compute