Elffuss BraidHub

kikocis/ncnn public

d7a5f98c0f60 · 14.3 KiBRawPermalink
1// Tencent is pleased to support the open source community by making ncnn available.
2//
3// Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved.
4//
5// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
6// in compliance with the License. You may obtain a copy of the License at
7//
8// https://opensource.org/licenses/BSD-3-Clause
9//
10// Unless required by applicable law or agreed to in writing, software distributed
11// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
12// CONDITIONS OF ANY KIND, either express or implied. See the License for the
13// specific language governing permissions and limitations under the License.
14
15#include "platform.h"
16
17#if NCNN_SIMPLEMATH
18
19#include "simplemath.h"
20#define __HI(X) *(1 + (short*)&x)
21#define __LO(X) *(short*)&x
22#define INFINITY (1.0 / 0)
23#define FE_TONEAREST 0
24#define FE_DOWNWARD 1024
25#define FE_UPWARD 2048
26#define FE_TOWARDZERO 3072
27
28/*
29* ====================================================
30* some useful constants
31* ====================================================
32*/
33static const float PI = 3.14159265358979323846;
34static const float PI_2 = 1.57079632679489661923; /* PI/2 */
35static const float E = 2.71828182845904523536;
36
37/* re-interpret the bit pattern of a uint32 as an IEEE-754 float */
38static float uint32_as_float(uint32_t a)
39{
40 float r;
41 float* rp = &r;
42 uint32_t* ap = &a;
43
44 *rp = *(float*)ap;
45
46 return r;
47}
48
49#ifdef __cplusplus
50extern "C" {
51#endif
52/*
53* ====================================================
54* Discontinuous function
55* ====================================================
56*/
57float fabs(float x)
58{
59 return x > 0 ? x : -x;
60}
61
62float fabsf(float x)
63{
64 return fabs(x);
65}
66
67float fmod(float numer, float denom)
68{
69 if (denom == 0.0)
70 {
71 return numer;
72 }
73 if (numer <= denom)
74 {
75 return numer;
76 }
77
78 int quotient = static_cast<int>(numer / denom);
79 return numer - quotient * denom;
80}
81
82float floor(float x)
83{
84 int intValue = static_cast<int>(x);
85 if (x < 0 && x != intValue)
86 {
87 intValue -= 1;
88 }
89 return intValue;
90}
91
92float floorf(float x)
93{
94 return floor(x);
95}
96
97float round(float x)
98{
99 float ret = x > 0 ? floor(x + 0.5) : ceil(x - 0.5);
100 return ret;
101}
102
103float roundf(float x)
104{
105 return round(x);
106}
107
108float ceilf(float x)
109{
110 return ceil(x);
111}
112
113float ceil(float x)
114{
115 int intValue = static_cast<int>(x);
116 if (x == intValue)
117 {
118 return x;
119 }
120 return floor(x + 1);
121}
122
123float fmaxf(float x, float y)
124{
125 return x > y ? x : y;
126}
127
128float truncf(float x)
129{
130 int intValue = static_cast<int>(x);
131 return static_cast<float>(intValue);
132}
133
134float frac(float x)
135{
136 return x - floor(x);
137}
138
139/*
140* ====================================================
141* trigonometric functions
142* ====================================================
143*/
144
145/*
146 modify from https://developer.download.nvidia.cn/cg/sin.html
147*/
148float sinf(float a)
149{
150 const int x = 0;
151 const int y = 1;
152 const int z = 2;
153 const int w = 3;
154
155 float c0[4] = {0.0, 0.5, 1.0, 0.0};
156 float c1[4] = {0.25, -9.0, 0.75, 0.159154943091};
157 float c2[4] = {24.9808039603, -24.9808039603, -60.1458091736, 60.1458091736};
158 float c3[4] = {85.4537887573, -85.4537887573, -64.9393539429, 64.9393539429};
159 float c4[4] = {19.7392082214, -19.7392082214, -1.0, 1.0};
160 float r0[3], r1[3], r2[3];
161
162 // r1.x = c1.w * a - c1.x
163 r1[x] = c1[w] * a - c1[x];
164 // r1.y = frac( r1.x );
165 r1[y] = frac(r1[x]);
166 // r2.x = (float) ( r1.y < c1.x );
167 r2[x] = (float)(r1[y] < c1[x]);
168 // r2.yz = (float2) ( r1.yy >= c1.yz );
169 r2[y] = (float)(r1[y] >= c1[y]);
170 r2[z] = (float)(r1[y] >= c1[z]);
171 // r2.y = dot( r2, c4.zwz );
172 r2[y] = r2[x] * c4[z] + r2[y] * c4[w] + r2[z] * c4[z];
173
174 // r0 = c0.xyz - r1.yyy
175 r0[x] = c0[x] - r1[y];
176 r0[y] = c0[y] - r1[y];
177 r0[z] = c0[z] - r1[y];
178
179 // r0 = r0 * r0
180 r0[x] = r0[x] * r0[x];
181 r0[y] = r0[y] * r0[y];
182 r0[z] = r0[z] * r0[z];
183
184 // r1 = c2.xyx * r0 + c2.zwz
185 r1[x] = c2[x] * r0[x] + c2[z];
186 r1[y] = c2[y] * r0[y] + c2[w];
187 r1[z] = c2[x] * r0[z] + c2[z];
188
189 // r1 = r1 * r0 + c3.xyx
190 r1[x] = r1[x] * r0[x] + c3[x];
191 r1[y] = r1[y] * r0[y] + c3[y];
192 r1[z] = r1[z] * r0[z] + c3[x];
193
194 // r1 = r1 * r0 + c3.zwz
195 r1[x] = r1[x] * r0[x] + c3[z];
196 r1[y] = r1[y] * r0[y] + c3[w];
197 r1[z] = r1[z] * r0[z] + c3[z];
198
199 // r1 = r1 * r0 + c4.xyx
200 r1[x] = r1[x] * r0[x] + c4[x];
201 r1[y] = r1[y] * r0[y] + c4[y];
202 r1[z] = r1[z] * r0[z] + c4[x];
203
204 // r1 = r1 * r0 + c4.zwz
205 r1[x] = r1[x] * r0[x] + c4[z];
206 r1[y] = r1[y] * r0[y] + c4[w];
207 r1[z] = r1[z] * r0[z] + c4[z];
208
209 //r0.x = dot(r1, -r2)
210 r0[x] = -(r1[x] * r2[x] + r1[y] * r2[y] + r1[z] * r2[z]);
211
212 return r0[x];
213}
214
215float cosf(float x)
216{
217 return sinf(PI_2 + x);
218}
219
220float tanf(float x)
221{
222 return sinf(x) / cosf(x);
223}
224
225/* copy from https://developer.download.nvidia.cn/cg/asin.html */
226float asinf(float x)
227{
228 float negate = float(x < 0);
229 x = fabs(x);
230 float ret = -0.0187293;
231 ret *= x;
232 ret += 0.0742610;
233 ret *= x;
234 ret -= 0.2121144;
235 ret *= x;
236 ret += 1.5707288;
237 ret = PI * 0.5 - sqrt(1.0 - x) * ret;
238 return ret - 2 * negate * ret;
239}
240
241/* copy from https://developer.download.nvidia.cn/cg/acos.html */
242float acosf(float x)
243{
244 float negate = float(x < 0);
245 x = fabs(x);
246 float ret = -0.0187293;
247 ret = ret * x;
248 ret = ret + 0.0742610;
249 ret = ret * x;
250 ret = ret - 0.2121144;
251 ret = ret * x;
252 ret = ret + 1.5707288;
253 ret = ret * sqrt(1.0 - x);
254 ret = ret - 2 * negate * ret;
255 return negate * PI + ret;
256}
257
258/* copy from https://developer.download.nvidia.cn/cg/atan.html */
259float atanf(float a)
260{
261 if (a < 0)
262 {
263 return -atanf(-a);
264 }
265 if (a > 1)
266 {
267 return PI_2 - atanf(1 / a);
268 }
269 float s = a * a;
270 float r = 0.0027856871020048857;
271
272 r = r * s - 0.015866000205278397;
273 r = r * s + 0.042472220957279205;
274 r = r * s - 0.07497530430555344f;
275 r = r * s + 0.10644879937171936;
276 r = r * s - 0.14207030832767487;
277 r = r * s + 0.19993454217910767f;
278 r = r * s - 0.33333146572113037f;
279 r = r * s;
280 return r * a + a;
281}
282
283float atan2f(float y, float x)
284{
285 if (x == 0 && y == 0)
286 {
287 // error
288 return 0;
289 }
290 if (y == 0)
291 {
292 return x > 0 ? 0 : PI;
293 }
294 if (x == 0)
295 {
296 return copysignf(PI_2, y);
297 }
298
299 if (x > 0 && y > 0)
300 {
301 return atanf(y / x);
302 }
303 else if (x < 0 && y > 0)
304 {
305 return PI - atanf(y / -x);
306 }
307 else if (x > 0 && y < 0)
308 {
309 return -atanf(-y / x);
310 }
311 else
312 {
313 return -PI + atanf(-y / -x);
314 }
315}
316
317float tanhf(float v)
318{
319 if (v >= 8 || v <= -8)
320 {
321 return copysignf(1, v);
322 }
323 float exp2v = expf(2 * v);
324 return (exp2v - 1) / (exp2v + 1);
325}
326
327/*
328* ====================================================
329* power functions
330* ====================================================
331*/
332
333float sqrtf(float x)
334{
335 return powf(x, 0.5);
336}
337
338float sqrt(float x)
339{
340 return sqrtf(x);
341}
342
343float powf(float x, float y)
344{
345 return expf(y * logf(x));
346}
347
348/*
349* ====================================================
350* exponential and logarithm functions
351* ====================================================
352*/
353
354/* copy and modify from https://zhuanlan.zhihu.com/p/541466411 */
355float logf(float x)
356{
357 static const float
358 ln2_hi
359 = 6.93147180369123816490e-01, /* 3fe62e42 fee00000 */
360 ln2_lo = 1.90821492927058770002e-10, /* 3dea39ef 35793c76 */
361 two25 = 3.3554432e+07,
362 Lg1 = 6.666666666666735130e-01, /* 3FE55555 55555593 */
363 Lg2 = 3.999999999940941908e-01, /* 3FD99999 9997FA04 */
364 Lg3 = 2.857142874366239149e-01, /* 3FD24924 94229359 */
365 Lg4 = 2.222219843214978396e-01, /* 3FCC71C5 1D8E78AF */
366 Lg5 = 1.818357216161805012e-01, /* 3FC74664 96CB03DE */
367 Lg6 = 1.531383769920937332e-01, /* 3FC39A09 D078C69F */
368 Lg7 = 1.479819860511658591e-01; /* 3FC2F112 DF3E5244 */
369
370 static float zero = 0.0;
371 float f, s, z, R, w, t1, t2, dk;
372 short k, hx, i;
373 unsigned short lx;
374
375 hx = __HI(x); /* high word of x */
376 lx = __LO(x); /* low word of x */
377
378 k = 0;
379 if (hx < 0x0080)
380 { /* x < 2**-126 */
381 if (((hx & 0x7fff) | lx) == 0)
382 return -two25 / zero; /* log(+-0)=-inf */
383 if (hx < 0) return (x - x) / zero; /* log(-#) = NaN */
384 k -= 25;
385 x *= two25; /* subnormal number, scale up x */
386 hx = __HI(x); /* high word of x */
387 }
388
389 if (hx >= 0x7f80) return x + x;
390 k += (hx >> 7) - 127;
391 hx &= 0x007f;
392 i = (hx + 0x4b) & 0x0080;
393 __HI(x) = hx | (i ^ 0x3f80); /* normalize x or x/2 */
394 k += (i >> 7);
395 f = x - 1.0f;
396
397 s = f / (2.0f + f);
398 dk = (float)k;
399 z = s * s;
400 w = z * z;
401 t1 = w * (Lg2 + w * (Lg4 + w * Lg6));
402 t2 = z * (Lg1 + w * (Lg3 + w * (Lg5 + w * Lg7)));
403 R = t2 + t1;
404 if (k == 0)
405 return f - s * (f - R);
406 else
407 return dk * ln2_hi - ((s * (f - R) - dk * ln2_lo) - f);
408}
409
410/* copy from https://stackoverflow.com/questions/35148198/efficient-faithfully-rounded-implementation-of-error-function-erff */
411float expf(float a)
412{
413 if (a < 0)
414 {
415 float tmp = expf(-a);
416
417 float ret = 1 / tmp;
418
419 return ret;
420 }
421 float f, r, j;
422 int i;
423
424 // exp(a) = 2**i * exp(f); i = rintf (a / log(2))
425 j = 1.442695f * a;
426 j = round(j) + 12582912.f; // There is a bug, and the program lives on it.
427 j = j - 12582912.f;
428 // j = fmaf(1.442695f, a, 12582912.f) - 12582912.f; // 0x1.715476p0, 0x1.8p23
429 f = fmaf(j, -6.93145752e-1f, a); // -0x1.62e400p-1 // log_2_hi
430 f = fmaf(j, -1.42860677e-6f, f); // -0x1.7f7d1cp-20 // log_2_lo
431 i = (int)j;
432 // approximate r = exp(f) on interval [-log(2)/2, +log(2)/2]
433 r = 1.37805939e-3f; // 0x1.694000p-10
434 r = fmaf(r, f, 8.37312452e-3f); // 0x1.125edcp-7
435 r = fmaf(r, f, 4.16695364e-2f); // 0x1.555b5ap-5
436 r = fmaf(r, f, 1.66664720e-1f); // 0x1.555450p-3
437 r = fmaf(r, f, 4.99999851e-1f); // 0x1.fffff6p-2
438 r = fmaf(r, f, 1.00000000e+0f); // 0x1.000000p+0
439 r = fmaf(r, f, 1.00000000e+0f); // 0x1.000000p+0
440
441 float s, t;
442 uint32_t ia;
443 // exp(a) = 2**i * r
444 ia = (i > 0) ? 0 : 0x83000000u;
445 s = uint32_as_float(0x7f000000u + ia);
446 t = uint32_as_float(((uint32_t)i << 23) - ia);
447 r = r * s;
448 r = r * t;
449
450 // handle special cases: severe overflow / underflow
451 if (fabsf(a) >= 104.0f) r = (a > 0) ? INFINITY : 0.0f;
452
453 return r;
454}
455
456float frexp(float x, int* y)
457{
458 int hx, k;
459 hx = __HI(x);
460 k = (hx >> 7) & 0x00ff;
461 k = k - 127;
462 __HI(x) = hx & 0x807f;
463 __HI(x) = __HI(x) | 0x3f80;
464
465 *y = k + 1; // y in [1/2, 1)
466 return x / 2;
467}
468
469float log(float x)
470{
471 return logf(x);
472}
473
474float log10f(float x)
475{
476 static const float ln10 = 2.3025850929940456840179914546844;
477 return logf(x) / ln10;
478}
479
480/*
481* ====================================================
482* probability functions
483* ====================================================
484*/
485
486/* copy from https://stackoverflow.com/questions/35148198/efficient-faithfully-rounded-implementation-of-error-function-erff */
487float erf(float a)
488{
489 float r, s, t, u;
490
491 t = fabsf(a);
492 s = a * a;
493 if (t > 0.927734375f)
494 { // 475/512
495 // maximum error 0.99527 ulp
496 r = fmaf(-1.72853470e-5f, t, 3.83197126e-4f); // -0x1.220000p-16,0x1.91cfb2p-12
497 u = fmaf(-3.88396438e-3f, t, 2.42546219e-2f); // -0x1.fd1438p-9, 0x1.8d6342p-6
498 r = fmaf(r, s, u);
499 r = fmaf(r, t, -1.06777877e-1f); // -0x1.b55cb8p-4
500 r = fmaf(r, t, -6.34846687e-1f); // -0x1.450aa0p-1
501 r = fmaf(r, t, -1.28717512e-1f); // -0x1.079d0cp-3
502 r = fmaf(r, t, -t);
503 r = 1.0f - expf(r);
504 r = copysignf(r, a);
505 }
506 else
507 {
508 // maximum error 0.98929 ulp
509 r = -5.96761703e-4f; // -0x1.38e000p-11
510 r = fmaf(r, s, 4.99119423e-3f); // 0x1.471a58p-8
511 r = fmaf(r, s, -2.67681349e-2f); // -0x1.b691b2p-6
512 r = fmaf(r, s, 1.12819925e-1f); // 0x1.ce1c44p-4
513 r = fmaf(r, s, -3.76125336e-1f); // -0x1.812700p-2
514 r = fmaf(r, s, 1.28379166e-1f); // 0x1.06eba8p-3
515 r = fmaf(r, a, a);
516 }
517 return r;
518}
519
520float erff(float x)
521{
522 return erf(x);
523}
524
525float erfcf(float x)
526{
527 return 1.0 - erf(x);
528}
529
530/*
531* ====================================================
532* other functions
533* ====================================================
534*/
535
536int msb(unsigned int v)
537{
538 static const int pos[32] = {0, 1, 28, 2, 29, 14, 24, 3,
539 30, 22, 20, 15, 25, 17, 4, 8, 31, 27, 13, 23, 21, 19,
540 16, 7, 26, 12, 18, 6, 11, 5, 10, 9
541 };
542 v |= v >> 1;
543 v |= v >> 2;
544 v |= v >> 4;
545 v |= v >> 8;
546 v |= v >> 16;
547 v = (v >> 1) + 1;
548 return pos[(v * 0x077CB531UL) >> 27];
549}
550
551float fmaf(float x, float y, float z)
552{
553 float tmp = x * y;
554 float ret = tmp + z;
555 return ret;
556}
557
558float copysignf(float x, float y)
559{
560 return fabsf(x) * (y > 0 ? 1 : -1);
561}
562
563int round_mode = 0;
564void fesetround(int mode)
565{
566 round_mode = mode;
567}
568
569int fegetround()
570{
571 return round_mode;
572}
573
574float nearbyintf(float x)
575{
576 int intPart = static_cast<int>(x);
577 float floatPart = fabs(x - intPart);
578 if (floatPart == 0)
579 {
580 return x;
581 }
582
583 if (x > 0)
584 {
585 if (round_mode == FE_DOWNWARD || round_mode == FE_TOWARDZERO)
586 {
587 return static_cast<float>(intPart);
588 }
589 if (round_mode == FE_UPWARD)
590 {
591 return static_cast<float>(intPart) + 1.0;
592 }
593 if (round_mode == FE_TONEAREST)
594 {
595 if (floatPart == 0.5)
596 {
597 return intPart % 2 == 0 ? static_cast<float>(intPart) : static_cast<float>(intPart) + 1;
598 }
599 return round(x);
600 }
601 }
602 if (x < 0)
603 {
604 if (round_mode == FE_UPWARD || round_mode == FE_TOWARDZERO)
605 {
606 return static_cast<float>(intPart);
607 }
608 if (round_mode == FE_DOWNWARD)
609 {
610 return static_cast<float>(intPart) - 1.0;
611 }
612 if (round_mode == FE_TONEAREST)
613 {
614 if (floatPart == 0.5)
615 {
616 return intPart % 2 == 0 ? static_cast<float>(intPart) : static_cast<float>(intPart) - 1;
617 }
618 return round(x);
619 }
620 }
621}
622
623#ifdef __cplusplus
624} // extern "C"
625#endif
626
627#endif // NCNN_SIMPLEMATH