blob: 1ebc9c10aff7b31f5e5cd5b25a2edda04b7c2dbd [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Alex Gildayc357c472018-03-21 13:54:09 +00002 * Copyright (c) 2016-2018 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 */
24
25namespace arm_compute
26{
Alex Gildayc357c472018-03-21 13:54:09 +000027/** Exponent polynomial coefficients */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010028const std::array<float32x4_t, 8> exp_tab =
29{
30 {
31 vdupq_n_f32(1.f),
32 vdupq_n_f32(0.0416598916054f),
33 vdupq_n_f32(0.500000596046f),
34 vdupq_n_f32(0.0014122662833f),
35 vdupq_n_f32(1.00000011921f),
36 vdupq_n_f32(0.00833693705499f),
37 vdupq_n_f32(0.166665703058f),
38 vdupq_n_f32(0.000195780929062f),
39 }
40};
41
Alex Gildayc357c472018-03-21 13:54:09 +000042/** Logarithm polynomial coefficients */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010043const std::array<float32x4_t, 8> log_tab =
44{
45 {
46 vdupq_n_f32(-2.29561495781f),
47 vdupq_n_f32(-2.47071170807f),
48 vdupq_n_f32(-5.68692588806f),
49 vdupq_n_f32(-0.165253549814f),
50 vdupq_n_f32(5.17591238022f),
51 vdupq_n_f32(0.844007015228f),
52 vdupq_n_f32(4.58445882797f),
53 vdupq_n_f32(0.0141278216615f),
54 }
55};
56
Alex Gildayc357c472018-03-21 13:54:09 +000057#ifndef DOXYGEN_SKIP_THIS
Georgios Pinitasd8e765b2017-08-02 13:44:33 +010058inline float32x4_t vfloorq_f32(float32x4_t val)
59{
60 static const float32x4_t CONST_1 = vdupq_n_f32(1.f);
61
62 const int32x4_t z = vcvtq_s32_f32(val);
63 const float32x4_t r = vcvtq_f32_s32(z);
64
65 return vbslq_f32(vcgtq_f32(r, val), vsubq_f32(r, CONST_1), r);
66}
67
Georgios Pinitascdf51452017-08-31 14:21:36 +010068inline float32x2_t vinvsqrt_f32(float32x2_t x)
69{
70 float32x2_t sqrt_reciprocal = vrsqrte_f32(x);
71 sqrt_reciprocal = vmul_f32(vrsqrts_f32(vmul_f32(x, sqrt_reciprocal), sqrt_reciprocal), sqrt_reciprocal);
72 sqrt_reciprocal = vmul_f32(vrsqrts_f32(vmul_f32(x, sqrt_reciprocal), sqrt_reciprocal), sqrt_reciprocal);
73
74 return sqrt_reciprocal;
75}
76
Anthony Barbier6ff3b192017-09-04 18:44:23 +010077inline float32x4_t vinvsqrtq_f32(float32x4_t x)
78{
79 float32x4_t sqrt_reciprocal = vrsqrteq_f32(x);
80 sqrt_reciprocal = vmulq_f32(vrsqrtsq_f32(vmulq_f32(x, sqrt_reciprocal), sqrt_reciprocal), sqrt_reciprocal);
81 sqrt_reciprocal = vmulq_f32(vrsqrtsq_f32(vmulq_f32(x, sqrt_reciprocal), sqrt_reciprocal), sqrt_reciprocal);
82
83 return sqrt_reciprocal;
84}
85
Georgios Pinitascdf51452017-08-31 14:21:36 +010086inline float32x2_t vinv_f32(float32x2_t x)
87{
88 float32x2_t recip = vrecpe_f32(x);
89 recip = vmul_f32(vrecps_f32(x, recip), recip);
90 recip = vmul_f32(vrecps_f32(x, recip), recip);
91 return recip;
92}
93
Anthony Barbier6ff3b192017-09-04 18:44:23 +010094inline float32x4_t vinvq_f32(float32x4_t x)
95{
96 float32x4_t recip = vrecpeq_f32(x);
97 recip = vmulq_f32(vrecpsq_f32(x, recip), recip);
98 recip = vmulq_f32(vrecpsq_f32(x, recip), recip);
99 return recip;
100}
101
102inline float32x4_t vtaylor_polyq_f32(float32x4_t x, const std::array<float32x4_t, 8> &coeffs)
103{
104 float32x4_t A = vmlaq_f32(coeffs[0], coeffs[4], x);
105 float32x4_t B = vmlaq_f32(coeffs[2], coeffs[6], x);
106 float32x4_t C = vmlaq_f32(coeffs[1], coeffs[5], x);
107 float32x4_t D = vmlaq_f32(coeffs[3], coeffs[7], x);
108 float32x4_t x2 = vmulq_f32(x, x);
109 float32x4_t x4 = vmulq_f32(x2, x2);
110 float32x4_t res = vmlaq_f32(vmlaq_f32(A, B, x2), vmlaq_f32(C, D, x2), x4);
111 return res;
112}
113
114inline float32x4_t vexpq_f32(float32x4_t x)
115{
Georgios Pinitasee122542017-06-26 15:54:06 +0100116 static const float32x4_t CONST_LN2 = vdupq_n_f32(0.6931471805f); // ln(2)
117 static const float32x4_t CONST_INV_LN2 = vdupq_n_f32(1.4426950408f); // 1/ln(2)
118 static const float32x4_t CONST_0 = vdupq_n_f32(0.f);
119 static const int32x4_t CONST_NEGATIVE_126 = vdupq_n_s32(-126);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100120
121 // Perform range reduction [-log(2),log(2)]
122 int32x4_t m = vcvtq_s32_f32(vmulq_f32(x, CONST_INV_LN2));
123 float32x4_t val = vmlsq_f32(x, vcvtq_f32_s32(m), CONST_LN2);
124
125 // Polynomial Approximation
126 float32x4_t poly = vtaylor_polyq_f32(val, exp_tab);
127
128 // Reconstruct
Georgios Pinitasee122542017-06-26 15:54:06 +0100129 poly = vreinterpretq_f32_s32(vqaddq_s32(vreinterpretq_s32_f32(poly), vqshlq_n_s32(m, 23)));
130 poly = vbslq_f32(vcltq_s32(m, CONST_NEGATIVE_126), CONST_0, poly);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100131
132 return poly;
133}
134
135inline float32x4_t vlogq_f32(float32x4_t x)
136{
137 static const int32x4_t CONST_127 = vdupq_n_s32(127); // 127
138 static const float32x4_t CONST_LN2 = vdupq_n_f32(0.6931471805f); // ln(2)
139
140 // Extract exponent
141 int32x4_t m = vsubq_s32(vreinterpretq_s32_u32(vshrq_n_u32(vreinterpretq_u32_f32(x), 23)), CONST_127);
142 float32x4_t val = vreinterpretq_f32_s32(vsubq_s32(vreinterpretq_s32_f32(x), vshlq_n_s32(m, 23)));
143
144 // Polynomial Approximation
145 float32x4_t poly = vtaylor_polyq_f32(val, log_tab);
146
147 // Reconstruct
148 poly = vmlaq_f32(poly, vcvtq_f32_s32(m), CONST_LN2);
149
150 return poly;
151}
152
153inline float32x4_t vtanhq_f32(float32x4_t val)
154{
155 static const float32x4_t CONST_1 = vdupq_n_f32(1.f);
156 static const float32x4_t CONST_2 = vdupq_n_f32(2.f);
157 static const float32x4_t CONST_MIN_TANH = vdupq_n_f32(-10.f);
158 static const float32x4_t CONST_MAX_TANH = vdupq_n_f32(10.f);
159
160 float32x4_t x = vminq_f32(vmaxq_f32(val, CONST_MIN_TANH), CONST_MAX_TANH);
161 float32x4_t exp2x = vexpq_f32(vmulq_f32(CONST_2, x));
162 float32x4_t num = vsubq_f32(exp2x, CONST_1);
163 float32x4_t den = vaddq_f32(exp2x, CONST_1);
164 float32x4_t tanh = vmulq_f32(num, vinvq_f32(den));
165 return tanh;
166}
167
168inline float32x4_t vpowq_f32(float32x4_t val, float32x4_t n)
169{
170 return vexpq_f32(vmulq_f32(n, vlogq_f32(val)));
171}
Alex Gildayc357c472018-03-21 13:54:09 +0000172#endif /* DOXYGEN_SKIP_THIS */
173
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000174#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Alex Gildayc357c472018-03-21 13:54:09 +0000175/** Exponent polynomial coefficients */
Alex Gildayc357c472018-03-21 13:54:09 +0000176/** Logarithm polynomial coefficients */
Alex Gildayc357c472018-03-21 13:54:09 +0000177#ifndef DOXYGEN_SKIP_THIS
Georgios Pinitascdf51452017-08-31 14:21:36 +0100178inline float16x4_t vinvsqrt_f16(float16x4_t x)
179{
180 float16x4_t sqrt_reciprocal = vrsqrte_f16(x);
181 sqrt_reciprocal = vmul_f16(vrsqrts_f16(vmul_f16(x, sqrt_reciprocal), sqrt_reciprocal), sqrt_reciprocal);
182 sqrt_reciprocal = vmul_f16(vrsqrts_f16(vmul_f16(x, sqrt_reciprocal), sqrt_reciprocal), sqrt_reciprocal);
183 return sqrt_reciprocal;
184}
185
Pablo Tello91654c42017-07-05 11:32:17 +0100186inline float16x8_t vinvsqrtq_f16(float16x8_t x)
187{
188 float16x8_t sqrt_reciprocal = vrsqrteq_f16(x);
189 sqrt_reciprocal = vmulq_f16(vrsqrtsq_f16(vmulq_f16(x, sqrt_reciprocal), sqrt_reciprocal), sqrt_reciprocal);
190 sqrt_reciprocal = vmulq_f16(vrsqrtsq_f16(vmulq_f16(x, sqrt_reciprocal), sqrt_reciprocal), sqrt_reciprocal);
Pablo Tello91654c42017-07-05 11:32:17 +0100191 return sqrt_reciprocal;
192}
Pablo Tellodf246182017-07-03 16:25:09 +0100193
Georgios Pinitascdf51452017-08-31 14:21:36 +0100194inline float16x4_t vinv_f16(float16x4_t x)
195{
196 float16x4_t recip = vrecpe_f16(x);
197 recip = vmul_f16(vrecps_f16(x, recip), recip);
198 recip = vmul_f16(vrecps_f16(x, recip), recip);
199 return recip;
200}
201
Pablo Tellodf246182017-07-03 16:25:09 +0100202inline float16x8_t vinvq_f16(float16x8_t x)
203{
204 float16x8_t recip = vrecpeq_f16(x);
205 recip = vmulq_f16(vrecpsq_f16(x, recip), recip);
206 recip = vmulq_f16(vrecpsq_f16(x, recip), recip);
207 return recip;
208}
209
Pablo Tello91654c42017-07-05 11:32:17 +0100210inline float16x8_t vtanhq_f16(float16x8_t val)
211{
212 const float16x8_t CONST_1 = vdupq_n_f16(1.f);
213 const float16x8_t CONST_2 = vdupq_n_f16(2.f);
214 const float16x8_t CONST_MIN_TANH = vdupq_n_f16(-10.f);
215 const float16x8_t CONST_MAX_TANH = vdupq_n_f16(10.f);
216
217 const float16x8_t x = vminq_f16(vmaxq_f16(val, CONST_MIN_TANH), CONST_MAX_TANH);
218 const float16x8_t exp2x = vexpq_f16(vmulq_f16(CONST_2, x));
219 const float16x8_t num = vsubq_f16(exp2x, CONST_1);
220 const float16x8_t den = vaddq_f16(exp2x, CONST_1);
221 const float16x8_t tanh = vmulq_f16(num, vinvq_f16(den));
222 return tanh;
223}
224
Pablo Tellodf246182017-07-03 16:25:09 +0100225inline float16x8_t vtaylor_polyq_f16(float16x8_t x, const std::array<float16x8_t, 8> &coeffs)
226{
227 const float16x8_t A = vaddq_f16(coeffs[0], vmulq_f16(coeffs[4], x));
228 const float16x8_t B = vaddq_f16(coeffs[2], vmulq_f16(coeffs[6], x));
229 const float16x8_t C = vaddq_f16(coeffs[1], vmulq_f16(coeffs[5], x));
230 const float16x8_t D = vaddq_f16(coeffs[3], vmulq_f16(coeffs[7], x));
231 const float16x8_t x2 = vmulq_f16(x, x);
232 const float16x8_t x4 = vmulq_f16(x2, x2);
233 const float16x8_t res = vaddq_f16(vaddq_f16(A, vmulq_f16(B, x2)), vmulq_f16(vaddq_f16(C, vmulq_f16(D, x2)), x4));
234 return res;
235}
236
237inline float16x8_t vexpq_f16(float16x8_t x)
238{
Anthony Barbier3a6163e2018-08-10 17:36:36 +0100239 static const std::array<float16x8_t, 8> exp_tab_f16 =
240 {
241 {
242 vdupq_n_f16(1.f),
243 vdupq_n_f16(0.0416598916054f),
244 vdupq_n_f16(0.500000596046f),
245 vdupq_n_f16(0.0014122662833f),
246 vdupq_n_f16(1.00000011921f),
247 vdupq_n_f16(0.00833693705499f),
248 vdupq_n_f16(0.166665703058f),
249 vdupq_n_f16(0.000195780929062f),
250 }
251 };
252
Pablo Tellodf246182017-07-03 16:25:09 +0100253 static const float16x8_t CONST_LN2 = vdupq_n_f16(0.6931471805f); // ln(2)
254 static const float16x8_t CONST_INV_LN2 = vdupq_n_f16(1.4426950408f); // 1/ln(2)
255 static const float16x8_t CONST_0 = vdupq_n_f16(0.f);
256 static const int16x8_t CONST_NEGATIVE_126 = vdupq_n_s16(-126);
257
258 // Perform range reduction [-log(2),log(2)]
259 const int16x8_t m = vcvtq_s16_f16(vmulq_f16(x, CONST_INV_LN2));
260 const float16x8_t val = vsubq_f16(x, vmulq_f16(vcvtq_f16_s16(m), CONST_LN2));
261
262 // Polynomial Approximation
263 float16x8_t poly = vtaylor_polyq_f16(val, exp_tab_f16);
264
265 // Reconstruct
266 poly = vreinterpretq_f16_s16(vqaddq_s16(vreinterpretq_s16_f16(poly), vqshlq_n_s16(m, 9)));
267 poly = vbslq_f16(vcltq_s16(m, CONST_NEGATIVE_126), CONST_0, poly);
268
269 return poly;
270}
271
272inline float16x8_t vlogq_f16(float16x8_t x)
273{
Anthony Barbier3a6163e2018-08-10 17:36:36 +0100274 static const std::array<float16x8_t, 8> log_tab_f16 =
275 {
276 {
277 vdupq_n_f16(-2.29561495781f),
278 vdupq_n_f16(-2.47071170807f),
279 vdupq_n_f16(-5.68692588806f),
280 vdupq_n_f16(-0.165253549814f),
281 vdupq_n_f16(5.17591238022f),
282 vdupq_n_f16(0.844007015228f),
283 vdupq_n_f16(4.58445882797f),
284 vdupq_n_f16(0.0141278216615f),
285 }
286 };
287
Pablo Tellodf246182017-07-03 16:25:09 +0100288 static const int16x8_t CONST_127 = vdupq_n_s16(127); // 127
289 static const float16x8_t CONST_LN2 = vdupq_n_f16(0.6931471805f); // ln(2)
290
291 // Extract exponent
292 const int16x8_t m = vsubq_s16(vreinterpretq_s16_u16(vshrq_n_u16(vreinterpretq_u16_f16(x), 9)), CONST_127);
293 const float16x8_t val = vreinterpretq_f16_s16(vsubq_s16(vreinterpretq_s16_f16(x), vshlq_n_s16(m, 9)));
294
295 // Polynomial Approximation
296 float16x8_t poly = vtaylor_polyq_f16(val, log_tab_f16);
297
298 // Reconstruct
299 poly = vaddq_f16(poly, vmulq_f16(vcvtq_f16_s16(m), CONST_LN2));
300
301 return poly;
302}
303
304inline float16x8_t vpowq_f16(float16x8_t val, float16x8_t n)
305{
Gian Marco Iodicef2cde9b2018-08-23 15:29:16 +0100306 // TODO (giaiod01) - COMPMID-1535
307 float32x4_t n0_f32 = vcvt_f32_f16(vget_low_f16(n));
308 float32x4_t n1_f32 = vcvt_f32_f16(vget_high_f16(n));
309 float32x4_t val0_f32 = vcvt_f32_f16(vget_low_f16(val));
310 float32x4_t val1_f32 = vcvt_f32_f16(vget_high_f16(val));
311
312 float32x4_t res0_f32 = vexpq_f32(vmulq_f32(n0_f32, vlogq_f32(val0_f32)));
313 float32x4_t res1_f32 = vexpq_f32(vmulq_f32(n1_f32, vlogq_f32(val1_f32)));
314
315 return vcombine_f16(vcvt_f16_f32(res0_f32), vcvt_f16_f32(res1_f32));
Pablo Tellodf246182017-07-03 16:25:09 +0100316}
Alex Gildayc357c472018-03-21 13:54:09 +0000317#endif /* DOXYGEN_SKIP_THIS */
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000318#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Gian Marco Iodice356f6432017-09-22 11:32:21 +0100319} // namespace arm_compute