blob: a1c3d4188096fc06319ba457c519b87b93d78bd2 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2016-2020 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 */
Manuel Bottinied753262019-05-15 15:30:47 +010024#include <cmath>
morgolock3155f772020-05-11 16:00:04 +010025#include <limits>
Manuel Bottinied753262019-05-15 15:30:47 +010026
Georgios Pinitase874ef92019-09-09 17:40:33 +010027#ifndef M_PI
28#define M_PI (3.14159265358979323846)
29#endif // M_PI
30
Anthony Barbier6ff3b192017-09-04 18:44:23 +010031namespace arm_compute
32{
Alex Gildayc357c472018-03-21 13:54:09 +000033/** Exponent polynomial coefficients */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010034const std::array<float32x4_t, 8> exp_tab =
35{
36 {
37 vdupq_n_f32(1.f),
38 vdupq_n_f32(0.0416598916054f),
39 vdupq_n_f32(0.500000596046f),
40 vdupq_n_f32(0.0014122662833f),
41 vdupq_n_f32(1.00000011921f),
42 vdupq_n_f32(0.00833693705499f),
43 vdupq_n_f32(0.166665703058f),
44 vdupq_n_f32(0.000195780929062f),
45 }
46};
47
Alex Gildayc357c472018-03-21 13:54:09 +000048/** Logarithm polynomial coefficients */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010049const std::array<float32x4_t, 8> log_tab =
50{
51 {
52 vdupq_n_f32(-2.29561495781f),
53 vdupq_n_f32(-2.47071170807f),
54 vdupq_n_f32(-5.68692588806f),
55 vdupq_n_f32(-0.165253549814f),
56 vdupq_n_f32(5.17591238022f),
57 vdupq_n_f32(0.844007015228f),
58 vdupq_n_f32(4.58445882797f),
59 vdupq_n_f32(0.0141278216615f),
60 }
61};
62
Manuel Bottinied753262019-05-15 15:30:47 +010063/** Sin polynomial coefficients */
64constexpr float te_sin_coeff2 = 0.166666666666f; // 1/(2*3)
65constexpr float te_sin_coeff3 = 0.05f; // 1/(4*5)
66constexpr float te_sin_coeff4 = 0.023809523810f; // 1/(6*7)
67constexpr float te_sin_coeff5 = 0.013888888889f; // 1/(8*9)
68
Alex Gildayc357c472018-03-21 13:54:09 +000069#ifndef DOXYGEN_SKIP_THIS
Georgios Pinitasd8e765b2017-08-02 13:44:33 +010070inline float32x4_t vfloorq_f32(float32x4_t val)
71{
72 static const float32x4_t CONST_1 = vdupq_n_f32(1.f);
73
74 const int32x4_t z = vcvtq_s32_f32(val);
75 const float32x4_t r = vcvtq_f32_s32(z);
76
77 return vbslq_f32(vcgtq_f32(r, val), vsubq_f32(r, CONST_1), r);
78}
79
Usama Arif0a5a57a2019-05-23 14:20:33 +010080inline float32x4_t vroundq_rte_f32(float32x4_t val)
81{
82#ifdef __aarch64__
83 return vrndnq_f32(val);
Manuel Bottini7bb56c62019-06-26 15:17:09 +010084#else // __aarch64__
Usama Arif0a5a57a2019-05-23 14:20:33 +010085 static const float32x4_t CONST_HALF_FLOAT = vdupq_n_f32(0.5f);
Manuel Bottini7bb56c62019-06-26 15:17:09 +010086 static const float32x4_t CONST_1_FLOAT = vdupq_n_f32(1.f);
87 static const int32x4_t CONST_1_INT = vdupq_n_s32(1);
88 const float32x4_t floor_val = vfloorq_f32(val);
89 const float32x4_t diff = vsubq_f32(val, floor_val);
Usama Arif0a5a57a2019-05-23 14:20:33 +010090
91 /*
92 * Select the floor value when (diff<0.5 || (diff==0.5 && floor_val%2==0).
93 * 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))))
94 */
95
Manuel Bottini7bb56c62019-06-26 15:17:09 +010096 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)))),
97 floor_val, vaddq_f32(floor_val, CONST_1_FLOAT));
Usama Arif0a5a57a2019-05-23 14:20:33 +010098#endif // __aarch64__
99}
100
Georgios Pinitascdf51452017-08-31 14:21:36 +0100101inline float32x2_t vinvsqrt_f32(float32x2_t x)
102{
103 float32x2_t sqrt_reciprocal = vrsqrte_f32(x);
104 sqrt_reciprocal = vmul_f32(vrsqrts_f32(vmul_f32(x, sqrt_reciprocal), sqrt_reciprocal), sqrt_reciprocal);
105 sqrt_reciprocal = vmul_f32(vrsqrts_f32(vmul_f32(x, sqrt_reciprocal), sqrt_reciprocal), sqrt_reciprocal);
106
107 return sqrt_reciprocal;
108}
109
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100110inline float32x4_t vinvsqrtq_f32(float32x4_t x)
111{
112 float32x4_t sqrt_reciprocal = vrsqrteq_f32(x);
113 sqrt_reciprocal = vmulq_f32(vrsqrtsq_f32(vmulq_f32(x, sqrt_reciprocal), sqrt_reciprocal), sqrt_reciprocal);
114 sqrt_reciprocal = vmulq_f32(vrsqrtsq_f32(vmulq_f32(x, sqrt_reciprocal), sqrt_reciprocal), sqrt_reciprocal);
115
116 return sqrt_reciprocal;
117}
118
Georgios Pinitascdf51452017-08-31 14:21:36 +0100119inline float32x2_t vinv_f32(float32x2_t x)
120{
121 float32x2_t recip = vrecpe_f32(x);
122 recip = vmul_f32(vrecps_f32(x, recip), recip);
123 recip = vmul_f32(vrecps_f32(x, recip), recip);
124 return recip;
125}
126
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100127inline float32x4_t vinvq_f32(float32x4_t x)
128{
129 float32x4_t recip = vrecpeq_f32(x);
130 recip = vmulq_f32(vrecpsq_f32(x, recip), recip);
131 recip = vmulq_f32(vrecpsq_f32(x, recip), recip);
132 return recip;
133}
134
135inline float32x4_t vtaylor_polyq_f32(float32x4_t x, const std::array<float32x4_t, 8> &coeffs)
136{
137 float32x4_t A = vmlaq_f32(coeffs[0], coeffs[4], x);
138 float32x4_t B = vmlaq_f32(coeffs[2], coeffs[6], x);
139 float32x4_t C = vmlaq_f32(coeffs[1], coeffs[5], x);
140 float32x4_t D = vmlaq_f32(coeffs[3], coeffs[7], x);
141 float32x4_t x2 = vmulq_f32(x, x);
142 float32x4_t x4 = vmulq_f32(x2, x2);
143 float32x4_t res = vmlaq_f32(vmlaq_f32(A, B, x2), vmlaq_f32(C, D, x2), x4);
144 return res;
145}
146
147inline float32x4_t vexpq_f32(float32x4_t x)
148{
Georgios Pinitasee122542017-06-26 15:54:06 +0100149 static const float32x4_t CONST_LN2 = vdupq_n_f32(0.6931471805f); // ln(2)
150 static const float32x4_t CONST_INV_LN2 = vdupq_n_f32(1.4426950408f); // 1/ln(2)
morgolock3155f772020-05-11 16:00:04 +0100151 static const float32x4_t CONST_INF = vdupq_n_f32(std::numeric_limits<float>::infinity());
152 static const float32x4_t CONST_MAX_INPUT = vdupq_n_f32(88.7f);
Georgios Pinitasee122542017-06-26 15:54:06 +0100153 static const float32x4_t CONST_0 = vdupq_n_f32(0.f);
154 static const int32x4_t CONST_NEGATIVE_126 = vdupq_n_s32(-126);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100155
156 // Perform range reduction [-log(2),log(2)]
157 int32x4_t m = vcvtq_s32_f32(vmulq_f32(x, CONST_INV_LN2));
158 float32x4_t val = vmlsq_f32(x, vcvtq_f32_s32(m), CONST_LN2);
159
160 // Polynomial Approximation
161 float32x4_t poly = vtaylor_polyq_f32(val, exp_tab);
162
163 // Reconstruct
Georgios Pinitasee122542017-06-26 15:54:06 +0100164 poly = vreinterpretq_f32_s32(vqaddq_s32(vreinterpretq_s32_f32(poly), vqshlq_n_s32(m, 23)));
morgolock3155f772020-05-11 16:00:04 +0100165 poly = vbslq_f32(vcltq_s32(m, CONST_NEGATIVE_126), CONST_0, poly); // Handle underflow
166 poly = vbslq_f32(vcgtq_f32(x, CONST_MAX_INPUT), CONST_INF, poly); // Handle overflow
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100167
168 return poly;
169}
170
171inline float32x4_t vlogq_f32(float32x4_t x)
172{
173 static const int32x4_t CONST_127 = vdupq_n_s32(127); // 127
174 static const float32x4_t CONST_LN2 = vdupq_n_f32(0.6931471805f); // ln(2)
175
176 // Extract exponent
177 int32x4_t m = vsubq_s32(vreinterpretq_s32_u32(vshrq_n_u32(vreinterpretq_u32_f32(x), 23)), CONST_127);
178 float32x4_t val = vreinterpretq_f32_s32(vsubq_s32(vreinterpretq_s32_f32(x), vshlq_n_s32(m, 23)));
179
180 // Polynomial Approximation
181 float32x4_t poly = vtaylor_polyq_f32(val, log_tab);
182
183 // Reconstruct
184 poly = vmlaq_f32(poly, vcvtq_f32_s32(m), CONST_LN2);
185
186 return poly;
187}
188
189inline float32x4_t vtanhq_f32(float32x4_t val)
190{
191 static const float32x4_t CONST_1 = vdupq_n_f32(1.f);
192 static const float32x4_t CONST_2 = vdupq_n_f32(2.f);
193 static const float32x4_t CONST_MIN_TANH = vdupq_n_f32(-10.f);
194 static const float32x4_t CONST_MAX_TANH = vdupq_n_f32(10.f);
195
196 float32x4_t x = vminq_f32(vmaxq_f32(val, CONST_MIN_TANH), CONST_MAX_TANH);
197 float32x4_t exp2x = vexpq_f32(vmulq_f32(CONST_2, x));
198 float32x4_t num = vsubq_f32(exp2x, CONST_1);
199 float32x4_t den = vaddq_f32(exp2x, CONST_1);
200 float32x4_t tanh = vmulq_f32(num, vinvq_f32(den));
201 return tanh;
202}
203
204inline float32x4_t vpowq_f32(float32x4_t val, float32x4_t n)
205{
206 return vexpq_f32(vmulq_f32(n, vlogq_f32(val)));
207}
Manuel Bottinied753262019-05-15 15:30:47 +0100208
209inline float32x4_t vsinq_f32(float32x4_t val)
210{
211 const float32x4_t pi_v = vdupq_n_f32(M_PI);
212 const float32x4_t pio2_v = vdupq_n_f32(M_PI / 2);
213 const float32x4_t ipi_v = vdupq_n_f32(1 / M_PI);
214
215 //Find positive or negative
216 const int32x4_t c_v = vabsq_s32(vcvtq_s32_f32(vmulq_f32(val, ipi_v)));
217 const uint32x4_t sign_v = vcleq_f32(val, vdupq_n_f32(0));
218 const uint32x4_t odd_v = vandq_u32(vreinterpretq_u32_s32(c_v), vdupq_n_u32(1));
219
220 uint32x4_t neg_v = veorq_u32(odd_v, sign_v);
221
222 //Modulus a - (n * int(a*(1/n)))
223 float32x4_t ma = vsubq_f32(vabsq_f32(val), vmulq_f32(pi_v, vcvtq_f32_s32(c_v)));
224 const uint32x4_t reb_v = vcgeq_f32(ma, pio2_v);
225
226 //Rebase a between 0 and pi/2
227 ma = vbslq_f32(reb_v, vsubq_f32(pi_v, ma), ma);
228
229 //Taylor series
230 const float32x4_t ma2 = vmulq_f32(ma, ma);
231
232 //2nd elem: x^3 / 3!
233 float32x4_t elem = vmulq_f32(vmulq_f32(ma, ma2), vdupq_n_f32(te_sin_coeff2));
234 float32x4_t res = vsubq_f32(ma, elem);
235
236 //3rd elem: x^5 / 5!
237 elem = vmulq_f32(vmulq_f32(elem, ma2), vdupq_n_f32(te_sin_coeff3));
238 res = vaddq_f32(res, elem);
239
240 //4th elem: x^7 / 7!float32x2_t vsin_f32(float32x2_t val)
241 elem = vmulq_f32(vmulq_f32(elem, ma2), vdupq_n_f32(te_sin_coeff4));
242 res = vsubq_f32(res, elem);
243
244 //5th elem: x^9 / 9!
245 elem = vmulq_f32(vmulq_f32(elem, ma2), vdupq_n_f32(te_sin_coeff5));
246 res = vaddq_f32(res, elem);
247
248 //Change of sign
249 neg_v = vshlq_n_u32(neg_v, 31);
250 res = vreinterpretq_f32_u32(veorq_u32(vreinterpretq_u32_f32(res), neg_v));
251 return res;
252}
253
254inline float32x2_t vsin_f32(float32x2_t val)
255{
256 const float32x2_t pi_v = vdup_n_f32(M_PI);
257 const float32x2_t pio2_v = vdup_n_f32(M_PI / 2);
258 const float32x2_t ipi_v = vdup_n_f32(1 / M_PI);
259
260 //Find positive or negative
261 const int32x2_t c_v = vabs_s32(vcvt_s32_f32(vmul_f32(val, ipi_v)));
262 const uint32x2_t sign_v = vcle_f32(val, vdup_n_f32(0));
263 const uint32x2_t odd_v = vand_u32(vreinterpret_u32_s32(c_v), vdup_n_u32(1));
264
265 uint32x2_t neg_v = veor_u32(odd_v, sign_v);
266
267 //Modulus a - (n * int(a*(1/n)))
268 float32x2_t ma = vsub_f32(vabs_f32(val), vmul_f32(pi_v, vcvt_f32_s32(c_v)));
269 const uint32x2_t reb_v = vcge_f32(ma, pio2_v);
270
271 //Rebase a between 0 and pi/2
272 ma = vbsl_f32(reb_v, vsub_f32(pi_v, ma), ma);
273
274 //Taylor series
275 const float32x2_t ma2 = vmul_f32(ma, ma);
276
277 //2nd elem: x^3 / 3!
278 float32x2_t elem = vmul_f32(vmul_f32(ma, ma2), vdup_n_f32(te_sin_coeff2));
279 float32x2_t res = vsub_f32(ma, elem);
280
281 //3rd elem: x^5 / 5!
282 elem = vmul_f32(vmul_f32(elem, ma2), vdup_n_f32(te_sin_coeff3));
283 res = vadd_f32(res, elem);
284
285 //4th elem: x^7 / 7!float32x2_t vsin_f32(float32x2_t val)
286 elem = vmul_f32(vmul_f32(elem, ma2), vdup_n_f32(te_sin_coeff4));
287 res = vsub_f32(res, elem);
288
289 //5th elem: x^9 / 9!
290 elem = vmul_f32(vmul_f32(elem, ma2), vdup_n_f32(te_sin_coeff5));
291 res = vadd_f32(res, elem);
292
293 //Change of sign
294 neg_v = vshl_n_u32(neg_v, 31);
295 res = vreinterpret_f32_u32(veor_u32(vreinterpret_u32_f32(res), neg_v));
296 return res;
297}
298
Alex Gildayc357c472018-03-21 13:54:09 +0000299#endif /* DOXYGEN_SKIP_THIS */
300
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100301inline int32x4_t rounding_divide_by_pow2(int32x4_t x, int32x4_t exponent)
302{
303 const int32x4_t shift_vec = vnegq_s32(exponent);
304 const int32x4_t fixup = vshrq_n_s32(vandq_s32(x, shift_vec), 31);
305 const int32x4_t fixed_up_x = vqaddq_s32(x, fixup);
306 return vrshlq_s32(fixed_up_x, shift_vec);
307}
308
Manuel Bottini7bb56c62019-06-26 15:17:09 +0100309inline int32x4_t rounding_divide_by_pow2(int32x4_t x, int exponent)
310{
311 const int32x4_t shift_vec = vdupq_n_s32(-exponent);
312 const int32x4_t fixup = vshrq_n_s32(vandq_s32(x, shift_vec), 31);
313 const int32x4_t fixed_up_x = vqaddq_s32(x, fixup);
314 return vrshlq_s32(fixed_up_x, shift_vec);
315}
316
317inline int32_t rounding_divide_by_pow2(int32_t x, int exponent)
318{
319 const int32_t mask = (1 << exponent) - 1;
320 const int32_t threshold = (mask >> 1) + (x < 0 ? 1 : 0);
321 return (x >> exponent) + ((x & mask) > threshold ? 1 : 0);
322}
323
Manuel Bottini21079dd2019-10-29 17:20:09 +0000324inline float32x4x4_t convert_uint8x16_to_float32x4x4(const uint8x16_t &in)
325{
326 float32x4x4_t out;
327
328 const auto tmp1 = vmovl_u8(vget_low_u8(in));
329 out.val[0] = vcvtq_f32_u32(vmovl_u16(vget_low_u16(tmp1)));
330 out.val[1] = vcvtq_f32_u32(vmovl_u16(vget_high_u16(tmp1)));
331
332 const auto tmp2 = vmovl_u8(vget_high_u8(in));
333 out.val[2] = vcvtq_f32_u32(vmovl_u16(vget_low_u16(tmp2)));
334 out.val[3] = vcvtq_f32_u32(vmovl_u16(vget_high_u16(tmp2)));
335 return out;
336}
337
Sang-Hoon Parkc3a74202019-11-22 16:05:46 +0000338inline float32x4x4_t convert_int8x16_to_float32x4x4(const int8x16_t &in)
339{
340 float32x4x4_t out;
341
342 const auto tmp1 = vmovl_s8(vget_low_s8(in));
343 out.val[0] = vcvtq_f32_s32(vmovl_s16(vget_low_s16(tmp1)));
344 out.val[1] = vcvtq_f32_s32(vmovl_s16(vget_high_s16(tmp1)));
345
346 const auto tmp2 = vmovl_s8(vget_high_s8(in));
347 out.val[2] = vcvtq_f32_s32(vmovl_s16(vget_low_s16(tmp2)));
348 out.val[3] = vcvtq_f32_s32(vmovl_s16(vget_high_s16(tmp2)));
349 return out;
350}
351
Manuel Bottini4370cff2020-02-07 16:31:59 +0000352template <>
353inline float32x4x4_t convert_to_float32x4x4(const uint8x16_t &in)
354{
355 return convert_uint8x16_to_float32x4x4(in);
356}
357
358template <>
359inline float32x4x4_t convert_to_float32x4x4(const int8x16_t &in)
360{
361 return convert_int8x16_to_float32x4x4(in);
362}
363
Manuel Bottini21079dd2019-10-29 17:20:09 +0000364inline void convert_float32x4x3_to_uint8x8x3(const float32x4x3_t &in1, const float32x4x3_t &in2, uint8x8x3_t &out)
365{
366 out.val[0] = vqmovn_u16(vcombine_u16(vqmovn_u32(vcvtq_u32_f32(in1.val[0])),
367 vqmovn_u32(vcvtq_u32_f32(in2.val[0]))));
368 out.val[1] = vqmovn_u16(vcombine_u16(vqmovn_u32(vcvtq_u32_f32(in1.val[1])),
369 vqmovn_u32(vcvtq_u32_f32(in2.val[1]))));
370 out.val[2] = vqmovn_u16(vcombine_u16(vqmovn_u32(vcvtq_u32_f32(in1.val[2])),
371 vqmovn_u32(vcvtq_u32_f32(in2.val[2]))));
372}
373
Sang-Hoon Parkc3a74202019-11-22 16:05:46 +0000374inline void convert_float32x4x4_to_uint8x16(const float32x4x4_t &in, uint8x16_t &out)
Manuel Bottini21079dd2019-10-29 17:20:09 +0000375{
376 const auto low = vcombine_u16(vqmovn_u32(vcvtq_u32_f32(in.val[0])),
377 vqmovn_u32(vcvtq_u32_f32(in.val[1])));
378 const auto high = vcombine_u16(vqmovn_u32(vcvtq_u32_f32(in.val[2])),
379 vqmovn_u32(vcvtq_u32_f32(in.val[3])));
380 out = vcombine_u8(vqmovn_u16(low), vqmovn_u16(high));
381}
382
Sang-Hoon Parkc3a74202019-11-22 16:05:46 +0000383inline void convert_float32x4x4_to_int8x16(const float32x4x4_t &in, int8x16_t &out)
384{
385 const auto low = vcombine_s16(vqmovn_s32(vcvtq_s32_f32(in.val[0])),
386 vqmovn_s32(vcvtq_s32_f32(in.val[1])));
387 const auto high = vcombine_s16(vqmovn_s32(vcvtq_s32_f32(in.val[2])),
388 vqmovn_s32(vcvtq_s32_f32(in.val[3])));
389 out = vcombine_s8(vqmovn_s16(low), vqmovn_s16(high));
390}
391
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000392#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Alex Gildayc357c472018-03-21 13:54:09 +0000393/** Exponent polynomial coefficients */
Alex Gildayc357c472018-03-21 13:54:09 +0000394/** Logarithm polynomial coefficients */
Alex Gildayc357c472018-03-21 13:54:09 +0000395#ifndef DOXYGEN_SKIP_THIS
Georgios Pinitas565bf2d2018-08-31 11:46:49 +0100396inline float16x8_t vfloorq_f16(float16x8_t val)
397{
398 static const float16x8_t CONST_1 = vdupq_n_f16(1.f);
399
400 const int16x8_t z = vcvtq_s16_f16(val);
401 const float16x8_t r = vcvtq_f16_s16(z);
402
403 return vbslq_f16(vcgtq_f16(r, val), vsubq_f16(r, CONST_1), r);
404}
Usama Arif0a5a57a2019-05-23 14:20:33 +0100405
406inline float16x8_t vroundq_rte_f16(float16x8_t val)
407{
408 return vrndnq_f16(val);
409}
410
Georgios Pinitascdf51452017-08-31 14:21:36 +0100411inline float16x4_t vinvsqrt_f16(float16x4_t x)
412{
413 float16x4_t sqrt_reciprocal = vrsqrte_f16(x);
414 sqrt_reciprocal = vmul_f16(vrsqrts_f16(vmul_f16(x, sqrt_reciprocal), sqrt_reciprocal), sqrt_reciprocal);
415 sqrt_reciprocal = vmul_f16(vrsqrts_f16(vmul_f16(x, sqrt_reciprocal), sqrt_reciprocal), sqrt_reciprocal);
416 return sqrt_reciprocal;
417}
418
Pablo Tello91654c42017-07-05 11:32:17 +0100419inline float16x8_t vinvsqrtq_f16(float16x8_t x)
420{
421 float16x8_t sqrt_reciprocal = vrsqrteq_f16(x);
422 sqrt_reciprocal = vmulq_f16(vrsqrtsq_f16(vmulq_f16(x, sqrt_reciprocal), sqrt_reciprocal), sqrt_reciprocal);
423 sqrt_reciprocal = vmulq_f16(vrsqrtsq_f16(vmulq_f16(x, sqrt_reciprocal), sqrt_reciprocal), sqrt_reciprocal);
Pablo Tello91654c42017-07-05 11:32:17 +0100424 return sqrt_reciprocal;
425}
Pablo Tellodf246182017-07-03 16:25:09 +0100426
Georgios Pinitascdf51452017-08-31 14:21:36 +0100427inline float16x4_t vinv_f16(float16x4_t x)
428{
429 float16x4_t recip = vrecpe_f16(x);
430 recip = vmul_f16(vrecps_f16(x, recip), recip);
431 recip = vmul_f16(vrecps_f16(x, recip), recip);
432 return recip;
433}
434
Pablo Tellodf246182017-07-03 16:25:09 +0100435inline float16x8_t vinvq_f16(float16x8_t x)
436{
437 float16x8_t recip = vrecpeq_f16(x);
438 recip = vmulq_f16(vrecpsq_f16(x, recip), recip);
439 recip = vmulq_f16(vrecpsq_f16(x, recip), recip);
440 return recip;
441}
442
Pablo Tello91654c42017-07-05 11:32:17 +0100443inline float16x8_t vtanhq_f16(float16x8_t val)
444{
445 const float16x8_t CONST_1 = vdupq_n_f16(1.f);
446 const float16x8_t CONST_2 = vdupq_n_f16(2.f);
447 const float16x8_t CONST_MIN_TANH = vdupq_n_f16(-10.f);
448 const float16x8_t CONST_MAX_TANH = vdupq_n_f16(10.f);
449
450 const float16x8_t x = vminq_f16(vmaxq_f16(val, CONST_MIN_TANH), CONST_MAX_TANH);
451 const float16x8_t exp2x = vexpq_f16(vmulq_f16(CONST_2, x));
452 const float16x8_t num = vsubq_f16(exp2x, CONST_1);
453 const float16x8_t den = vaddq_f16(exp2x, CONST_1);
454 const float16x8_t tanh = vmulq_f16(num, vinvq_f16(den));
455 return tanh;
456}
457
Pablo Tellodf246182017-07-03 16:25:09 +0100458inline float16x8_t vtaylor_polyq_f16(float16x8_t x, const std::array<float16x8_t, 8> &coeffs)
459{
460 const float16x8_t A = vaddq_f16(coeffs[0], vmulq_f16(coeffs[4], x));
461 const float16x8_t B = vaddq_f16(coeffs[2], vmulq_f16(coeffs[6], x));
462 const float16x8_t C = vaddq_f16(coeffs[1], vmulq_f16(coeffs[5], x));
463 const float16x8_t D = vaddq_f16(coeffs[3], vmulq_f16(coeffs[7], x));
464 const float16x8_t x2 = vmulq_f16(x, x);
465 const float16x8_t x4 = vmulq_f16(x2, x2);
466 const float16x8_t res = vaddq_f16(vaddq_f16(A, vmulq_f16(B, x2)), vmulq_f16(vaddq_f16(C, vmulq_f16(D, x2)), x4));
467 return res;
468}
469
470inline float16x8_t vexpq_f16(float16x8_t x)
471{
Michele Di Giorgio1c948d42018-11-20 16:03:01 +0000472 // TODO (COMPMID-1535) : Revisit FP16 approximations
473 const float32x4_t x_high = vcvt_f32_f16(vget_high_f16(x));
474 const float32x4_t x_low = vcvt_f32_f16(vget_low_f16(x));
Anthony Barbier3a6163e2018-08-10 17:36:36 +0100475
Georgios Pinitasf2cdce32019-12-09 18:35:57 +0000476 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 +0000477 return res;
Pablo Tellodf246182017-07-03 16:25:09 +0100478}
479
480inline float16x8_t vlogq_f16(float16x8_t x)
481{
Georgios Pinitas5a594532018-12-03 14:30:05 +0000482 // TODO (COMPMID-1535) : Revisit FP16 approximations
483 const float32x4_t x_high = vcvt_f32_f16(vget_high_f16(x));
484 const float32x4_t x_low = vcvt_f32_f16(vget_low_f16(x));
Anthony Barbier3a6163e2018-08-10 17:36:36 +0100485
Georgios Pinitasf2cdce32019-12-09 18:35:57 +0000486 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 +0000487 return res;
Pablo Tellodf246182017-07-03 16:25:09 +0100488}
489
490inline float16x8_t vpowq_f16(float16x8_t val, float16x8_t n)
491{
Gian Marco Iodicef2cde9b2018-08-23 15:29:16 +0100492 // TODO (giaiod01) - COMPMID-1535
493 float32x4_t n0_f32 = vcvt_f32_f16(vget_low_f16(n));
494 float32x4_t n1_f32 = vcvt_f32_f16(vget_high_f16(n));
495 float32x4_t val0_f32 = vcvt_f32_f16(vget_low_f16(val));
496 float32x4_t val1_f32 = vcvt_f32_f16(vget_high_f16(val));
497
498 float32x4_t res0_f32 = vexpq_f32(vmulq_f32(n0_f32, vlogq_f32(val0_f32)));
499 float32x4_t res1_f32 = vexpq_f32(vmulq_f32(n1_f32, vlogq_f32(val1_f32)));
500
501 return vcombine_f16(vcvt_f16_f32(res0_f32), vcvt_f16_f32(res1_f32));
Pablo Tellodf246182017-07-03 16:25:09 +0100502}
Manuel Bottinied753262019-05-15 15:30:47 +0100503
504inline float16x8_t vsinq_f16(float16x8_t val)
505{
506 const float32x4_t val_high = vcvt_f32_f16(vget_high_f16(val));
507 const float32x4_t val_low = vcvt_f32_f16(vget_low_f16(val));
508
509 const float32x4_t res_high = vsinq_f32(val_high);
510 const float32x4_t res_low = vsinq_f32(val_low);
511
512 return vcombine_f16(vcvt_f16_f32(res_low), vcvt_f16_f32(res_high));
513}
514
515inline float16x4_t vsin_f16(float16x4_t val)
516{
517 const float32x4_t val_f32 = vcvt_f32_f16(val);
518 const float32x2_t val_high = vget_high_f32(val_f32);
519 const float32x2_t val_low = vget_low_f32(val_f32);
520
521 const float32x2_t res_high = vsin_f32(val_high);
522 const float32x2_t res_low = vsin_f32(val_low);
523
524 return vcvt_f16_f32(vcombine_f32(res_low, res_high));
525}
526
Alex Gildayc357c472018-03-21 13:54:09 +0000527#endif /* DOXYGEN_SKIP_THIS */
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000528#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Gian Marco Iodice356f6432017-09-22 11:32:21 +0100529} // namespace arm_compute