You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

simplemath.cpp 14 kB

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