Elffuss BraidHub

kikocis/ncnn public

master · 12.3 KiBRawPermalink
1/* NEON implementation of sin, cos, exp and log
2 *
3 * Inspired by Intel Approximate Math library, and based on the
4 * corresponding algorithms of the cephes math library
5 */
6
7/* Copyright (C) 2011 Julien Pommier
8 *
9 * This software is provided 'as-is', without any express or implied
10 * warranty. In no event will the authors be held liable for any damages
11 * arising from the use of this software.
12 *
13 * Permission is granted to anyone to use this software for any purpose,
14 * including commercial applications, and to alter it and redistribute it
15 * freely, subject to the following restrictions:
16 *
17 * 1. The origin of this software must not be misrepresented; you must not
18 * claim that you wrote the original software. If you use this software
19 * in a product, an acknowledgment in the product documentation would be
20 * appreciated but is not required.
21 * 2. Altered source versions must be plainly marked as such, and must not be
22 * misrepresented as being the original software.
23 * 3. This notice may not be removed or altered from any source distribution.
24 *
25 * (this is the zlib license)
26 */
27
28#ifndef NEON_MATHFUN_H
29#define NEON_MATHFUN_H
30
31#include <arm_neon.h>
32
33#define c_inv_mant_mask ~0x7f800000u
34#define c_cephes_SQRTHF 0.707106781186547524
35#define c_cephes_log_p0 7.0376836292E-2
36#define c_cephes_log_p1 -1.1514610310E-1
37#define c_cephes_log_p2 1.1676998740E-1
38#define c_cephes_log_p3 -1.2420140846E-1
39#define c_cephes_log_p4 +1.4249322787E-1
40#define c_cephes_log_p5 -1.6668057665E-1
41#define c_cephes_log_p6 +2.0000714765E-1
42#define c_cephes_log_p7 -2.4999993993E-1
43#define c_cephes_log_p8 +3.3333331174E-1
44#define c_cephes_log_q1 -2.12194440e-4
45#define c_cephes_log_q2 0.693359375
46
47/* natural logarithm computed for 4 simultaneous float
48 * return NaN for x <= 0
49 */
50static inline float32x4_t log_ps(float32x4_t x)
51{
52 float32x4_t one = vdupq_n_f32(1);
53
54 x = vmaxq_f32(x, vdupq_n_f32(0)); /* force flush to zero on denormal values */
55 uint32x4_t invalid_mask = vcleq_f32(x, vdupq_n_f32(0));
56
57 int32x4_t ux = vreinterpretq_s32_f32(x);
58
59 int32x4_t emm0 = vshrq_n_s32(ux, 23);
60
61 /* keep only the fractional part */
62 ux = vandq_s32(ux, vdupq_n_s32(c_inv_mant_mask));
63 ux = vorrq_s32(ux, vreinterpretq_s32_f32(vdupq_n_f32(0.5f)));
64 x = vreinterpretq_f32_s32(ux);
65
66 emm0 = vsubq_s32(emm0, vdupq_n_s32(0x7f));
67 float32x4_t e = vcvtq_f32_s32(emm0);
68
69 e = vaddq_f32(e, one);
70
71 /* part2:
72 * if( x < SQRTHF ) {
73 * e -= 1;
74 * x = x + x - 1.0;
75 * } else { x = x - 1.0; }
76 */
77 uint32x4_t mask = vcltq_f32(x, vdupq_n_f32(c_cephes_SQRTHF));
78 float32x4_t tmp = vreinterpretq_f32_u32(vandq_u32(vreinterpretq_u32_f32(x), mask));
79 x = vsubq_f32(x, one);
80 e = vsubq_f32(e, vreinterpretq_f32_u32(vandq_u32(vreinterpretq_u32_f32(one), mask)));
81 x = vaddq_f32(x, tmp);
82
83 float32x4_t z = vmulq_f32(x, x);
84
85 float32x4_t y = vdupq_n_f32(c_cephes_log_p0);
86 y = vmlaq_f32(vdupq_n_f32(c_cephes_log_p1), y, x);
87 y = vmlaq_f32(vdupq_n_f32(c_cephes_log_p2), y, x);
88 y = vmlaq_f32(vdupq_n_f32(c_cephes_log_p3), y, x);
89 y = vmlaq_f32(vdupq_n_f32(c_cephes_log_p4), y, x);
90 y = vmlaq_f32(vdupq_n_f32(c_cephes_log_p5), y, x);
91 y = vmlaq_f32(vdupq_n_f32(c_cephes_log_p6), y, x);
92 y = vmlaq_f32(vdupq_n_f32(c_cephes_log_p7), y, x);
93 y = vmlaq_f32(vdupq_n_f32(c_cephes_log_p8), y, x);
94 y = vmulq_f32(y, x);
95
96 y = vmulq_f32(y, z);
97
98 y = vmlaq_f32(y, e, vdupq_n_f32(c_cephes_log_q1));
99
100 y = vmlsq_f32(y, z, vdupq_n_f32(0.5f));
101
102 x = vaddq_f32(x, y);
103 x = vmlaq_f32(x, e, vdupq_n_f32(c_cephes_log_q2));
104 x = vreinterpretq_f32_u32(vorrq_u32(vreinterpretq_u32_f32(x), invalid_mask)); // negative arg will be NAN
105 return x;
106}
107
108#define c_exp_hi 88.3762626647949f
109#define c_exp_lo -88.3762626647949f
110
111#define c_cephes_LOG2EF 1.44269504088896341
112#define c_cephes_exp_C1 0.693359375
113#define c_cephes_exp_C2 -2.12194440e-4
114
115#define c_cephes_exp_p0 1.9875691500E-4
116#define c_cephes_exp_p1 1.3981999507E-3
117#define c_cephes_exp_p2 8.3334519073E-3
118#define c_cephes_exp_p3 4.1665795894E-2
119#define c_cephes_exp_p4 1.6666665459E-1
120#define c_cephes_exp_p5 5.0000001201E-1
121
122/* exp() computed for 4 float at once */
123static inline float32x4_t exp_ps(float32x4_t x)
124{
125 float32x4_t tmp, fx;
126
127 float32x4_t one = vdupq_n_f32(1);
128 x = vminq_f32(x, vdupq_n_f32(c_exp_hi));
129 x = vmaxq_f32(x, vdupq_n_f32(c_exp_lo));
130
131 /* express exp(x) as exp(g + n*log(2)) */
132 fx = vmlaq_f32(vdupq_n_f32(0.5f), x, vdupq_n_f32(c_cephes_LOG2EF));
133
134 /* perform a floorf */
135 tmp = vcvtq_f32_s32(vcvtq_s32_f32(fx));
136
137 /* if greater, substract 1 */
138 uint32x4_t mask = vcgtq_f32(tmp, fx);
139 mask = vandq_u32(mask, vreinterpretq_u32_f32(one));
140
141 fx = vsubq_f32(tmp, vreinterpretq_f32_u32(mask));
142
143 tmp = vmulq_f32(fx, vdupq_n_f32(c_cephes_exp_C1));
144 float32x4_t z = vmulq_f32(fx, vdupq_n_f32(c_cephes_exp_C2));
145 x = vsubq_f32(x, tmp);
146 x = vsubq_f32(x, z);
147
148 z = vmulq_f32(x, x);
149
150 float32x4_t y = vdupq_n_f32(c_cephes_exp_p0);
151 y = vmlaq_f32(vdupq_n_f32(c_cephes_exp_p1), y, x);
152 y = vmlaq_f32(vdupq_n_f32(c_cephes_exp_p2), y, x);
153 y = vmlaq_f32(vdupq_n_f32(c_cephes_exp_p3), y, x);
154 y = vmlaq_f32(vdupq_n_f32(c_cephes_exp_p4), y, x);
155 y = vmlaq_f32(vdupq_n_f32(c_cephes_exp_p5), y, x);
156
157 y = vmlaq_f32(x, y, z);
158 y = vaddq_f32(y, one);
159
160 /* build 2^n */
161 int32x4_t mm;
162 mm = vcvtq_s32_f32(fx);
163 mm = vaddq_s32(mm, vdupq_n_s32(0x7f));
164 mm = vshlq_n_s32(mm, 23);
165 float32x4_t pow2n = vreinterpretq_f32_s32(mm);
166
167 y = vmulq_f32(y, pow2n);
168 return y;
169}
170
171#define c_minus_cephes_DP1 -0.78515625
172#define c_minus_cephes_DP2 -2.4187564849853515625e-4
173#define c_minus_cephes_DP3 -3.77489497744594108e-8
174#define c_sincof_p0 -1.9515295891E-4
175#define c_sincof_p1 8.3321608736E-3
176#define c_sincof_p2 -1.6666654611E-1
177#define c_coscof_p0 2.443315711809948E-005
178#define c_coscof_p1 -1.388731625493765E-003
179#define c_coscof_p2 4.166664568298827E-002
180#define c_cephes_FOPI 1.27323954473516 // 4 / M_PI
181
182/* evaluation of 4 sines & cosines at once.
183 *
184 * The code is the exact rewriting of the cephes sinf function.
185 * Precision is excellent as long as x < 8192 (I did not bother to
186 * take into account the special handling they have for greater values
187 * -- it does not return garbage for arguments over 8192, though, but
188 * the extra precision is missing).
189 *
190 * Note that it is such that sinf((float)M_PI) = 8.74e-8, which is the
191 * surprising but correct result.
192 *
193 * Note also that when you compute sin(x), cos(x) is available at
194 * almost no extra price so both sin_ps and cos_ps make use of
195 * sincos_ps..
196 */
197static inline void sincos_ps(float32x4_t x, float32x4_t* ysin, float32x4_t* ycos)
198{
199 // any x
200 float32x4_t y;
201
202 uint32x4_t emm2;
203
204 uint32x4_t sign_mask_sin, sign_mask_cos;
205 sign_mask_sin = vcltq_f32(x, vdupq_n_f32(0));
206 x = vabsq_f32(x);
207
208 /* scale by 4/Pi */
209 y = vmulq_f32(x, vdupq_n_f32(c_cephes_FOPI));
210
211 /* store the integer part of y in mm0 */
212 emm2 = vcvtq_u32_f32(y);
213 /* j=(j+1) & (~1) (see the cephes sources) */
214 emm2 = vaddq_u32(emm2, vdupq_n_u32(1));
215 emm2 = vandq_u32(emm2, vdupq_n_u32(~1));
216 y = vcvtq_f32_u32(emm2);
217
218 /* get the polynom selection mask
219 * there is one polynom for 0 <= x <= Pi/4
220 * and another one for Pi/4<x<=Pi/2
221 *
222 * Both branches will be computed.
223 */
224 uint32x4_t poly_mask = vtstq_u32(emm2, vdupq_n_u32(2));
225
226 /* The magic pass: "Extended precision modular arithmetic"
227 * x = ((x - y * DP1) - y * DP2) - y * DP3; */
228 x = vmlaq_f32(x, y, vdupq_n_f32(c_minus_cephes_DP1));
229 x = vmlaq_f32(x, y, vdupq_n_f32(c_minus_cephes_DP2));
230 x = vmlaq_f32(x, y, vdupq_n_f32(c_minus_cephes_DP3));
231
232 sign_mask_sin = veorq_u32(sign_mask_sin, vtstq_u32(emm2, vdupq_n_u32(4)));
233 sign_mask_cos = vtstq_u32(vsubq_u32(emm2, vdupq_n_u32(2)), vdupq_n_u32(4));
234
235 /* Evaluate the first polynom (0 <= x <= Pi/4) in y1,
236 * and the second polynom (Pi/4 <= x <= 0) in y2 */
237 float32x4_t z = vmulq_f32(x, x);
238 float32x4_t y1, y2;
239
240 y1 = vmlaq_f32(vdupq_n_f32(c_coscof_p1), z, vdupq_n_f32(c_coscof_p0));
241 y2 = vmlaq_f32(vdupq_n_f32(c_sincof_p1), z, vdupq_n_f32(c_sincof_p0));
242 y1 = vmlaq_f32(vdupq_n_f32(c_coscof_p2), y1, z);
243 y2 = vmlaq_f32(vdupq_n_f32(c_sincof_p2), y2, z);
244 y1 = vmulq_f32(y1, z);
245 y2 = vmulq_f32(y2, z);
246 y1 = vmulq_f32(y1, z);
247 y1 = vmlsq_f32(y1, z, vdupq_n_f32(0.5f));
248 y2 = vmlaq_f32(x, y2, x);
249 y1 = vaddq_f32(y1, vdupq_n_f32(1));
250
251 /* select the correct result from the two polynoms */
252 float32x4_t ys = vbslq_f32(poly_mask, y1, y2);
253 float32x4_t yc = vbslq_f32(poly_mask, y2, y1);
254 *ysin = vbslq_f32(sign_mask_sin, vnegq_f32(ys), ys);
255 *ycos = vbslq_f32(sign_mask_cos, yc, vnegq_f32(yc));
256}
257
258static inline float32x4_t sin_ps(float32x4_t x)
259{
260 float32x4_t ysin, ycos;
261 sincos_ps(x, &ysin, &ycos);
262 return ysin;
263}
264
265static inline float32x4_t cos_ps(float32x4_t x)
266{
267 float32x4_t ysin, ycos;
268 sincos_ps(x, &ysin, &ycos);
269 return ycos;
270}
271
272static inline float32x4_t div_ps(float32x4_t a, float32x4_t b)
273{
274#if __aarch64__
275 return vdivq_f32(a, b);
276#else
277 float32x4_t reciprocal = vrecpeq_f32(b);
278 reciprocal = vmulq_f32(vrecpsq_f32(b, reciprocal), reciprocal);
279 reciprocal = vmulq_f32(vrecpsq_f32(b, reciprocal), reciprocal);
280 return vmulq_f32(a, reciprocal);
281#endif
282}
283
284static inline float32x4_t tan_ps(float32x4_t x)
285{
286 float32x4_t ysin, ycos;
287 sincos_ps(x, &ysin, &ycos);
288 float32x4_t ytan = div_ps(ysin, ycos);
289 return ytan;
290}
291
292static inline float32x4_t pow_ps(float32x4_t a, float32x4_t b)
293{
294 // pow(x, m) = exp(m * log(x))
295 return exp_ps(vmulq_f32(b, log_ps(a)));
296}
297
298static inline float32x4_t sigmoid_ps(float32x4_t _v)
299{
300 float32x4_t _one = vdupq_n_f32(1.f);
301 _v = vnegq_f32(_v);
302 _v = exp_ps(_v);
303 _v = vaddq_f32(_v, _one);
304 float32x4_t _outp = vrecpeq_f32(_v);
305 _outp = vmulq_f32(vrecpsq_f32(_v, _outp), _outp);
306 return vmulq_f32(vrecpsq_f32(_v, _outp), _outp);
307}
308
309static const float asinf_lut[7] = {
310 1.5707961728,
311 -0.2145852647,
312 0.0887556286,
313 -0.0488025043,
314 0.0268999482,
315 -0.0111462294,
316 0.0022959648
317};
318
319static inline void asincos_ps(float32x4_t x, float32x4_t* yasin, float32x4_t* yacos)
320{
321 int i = 0;
322 float32x4_t one = vdupq_n_f32(1);
323 float32x4_t negone = vdupq_n_f32(-1);
324 float32x4_t lut[7];
325 float32x4_t xv[5];
326 float32x4_t a0, a1, a2, a3;
327 float32x4_t phx;
328 float32x4_t arcsinx, arcnsinx;
329 float32x4_t sat = vdupq_n_f32(0.9999999f);
330 float32x4_t m_pi_2 = vdupq_n_f32(1.570796326);
331 for (i = 0; i <= 6; i++)
332 {
333 lut[i] = vdupq_n_f32(asinf_lut[i]);
334 }
335
336 uint32x4_t sign_mask_asin, saturate;
337 sign_mask_asin = vcltq_f32(x, vdupq_n_f32(0));
338 x = vabsq_f32(x);
339 saturate = vcgeq_f32(x, one);
340 x = vbslq_f32(saturate, sat, x);
341 float32x4_t y = vsubq_f32(one, x);
342
343#if __aarch64__
344 y = vsqrtq_f32(y);
345#else
346 float32x4_t _reciprocal = vrsqrteq_f32(y);
347 _reciprocal = vmulq_f32(vrsqrtsq_f32(vmulq_f32(y, _reciprocal), _reciprocal), _reciprocal);
348 y = vmulq_f32(y, _reciprocal);
349#endif
350
351 xv[0] = vmulq_f32(x, x);
352 for (i = 1; i < 5; i++)
353 {
354 xv[i] = vmulq_f32(xv[i - 1], x);
355 }
356
357 a0 = vaddq_f32(lut[0], vmulq_f32(lut[1], x));
358 a1 = vaddq_f32(vmulq_f32(lut[2], xv[0]), vmulq_f32(lut[3], xv[1]));
359 a2 = vaddq_f32(vmulq_f32(lut[4], xv[2]), vmulq_f32(lut[5], xv[3]));
360 a3 = vmulq_f32(lut[6], xv[4]);
361 phx = vaddq_f32(vaddq_f32(a0, vaddq_f32(a1, a2)), a3);
362
363 arcsinx = vmulq_f32(y, phx);
364 arcsinx = vsubq_f32(m_pi_2, arcsinx);
365 arcnsinx = vmulq_f32(negone, arcsinx);
366 arcsinx = vbslq_f32(sign_mask_asin, arcnsinx, arcsinx);
367
368 *yasin = arcsinx;
369 *yacos = vsubq_f32(m_pi_2, arcsinx);
370}
371
372static inline float32x4_t asin_ps(float32x4_t x)
373{
374 float32x4_t yasin, yacos;
375 asincos_ps(x, &yasin, &yacos);
376 return yasin;
377}
378
379static inline float32x4_t acos_ps(float32x4_t x)
380{
381 float32x4_t yasin, yacos;
382 asincos_ps(x, &yasin, &yacos);
383 return yacos;
384}
385
386static inline float32x4_t atan2_ps(float32x4_t a, float32x4_t b)
387{
388 //TODO neon optimize
389 float tmpx[4];
390 float tmpy[4];
391 vst1q_f32(tmpx, a);
392 vst1q_f32(tmpy, b);
393 for (int i = 0; i < 4; i++)
394 tmpx[i] = atan2f(tmpx[i], tmpy[i]);
395 return vld1q_f32(tmpx);
396}
397
398#include "neon_mathfun_tanh.h"
399#endif // NEON_MATHFUN_H