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 15 kB

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