math.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2013, 2014 Damien P. George
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #include "libm.h"
  27. typedef float float_t;
  28. typedef union {
  29. float f;
  30. struct {
  31. uint64_t m : 23;
  32. uint64_t e : 8;
  33. uint64_t s : 1;
  34. };
  35. } float_s_t;
  36. #ifndef NDEBUG
  37. float copysignf(float x, float y) {
  38. float_s_t fx={.f = x};
  39. float_s_t fy={.f = y};
  40. // copy sign bit;
  41. fx.s = fy.s;
  42. return fx.f;
  43. }
  44. #endif
  45. static const float _M_LN10 = 2.30258509299404; // 0x40135d8e
  46. float log10f(float x) { return logf(x) / (float)_M_LN10; }
  47. float tanhf(float x) {
  48. if (isinf(x)) {
  49. return copysignf(1, x);
  50. }
  51. return sinhf(x) / coshf(x);
  52. }
  53. /*****************************************************************************/
  54. /*****************************************************************************/
  55. // __fpclassifyf from musl-0.9.15
  56. /*****************************************************************************/
  57. /*****************************************************************************/
  58. int __fpclassifyf(float x)
  59. {
  60. union {float f; uint32_t i;} u = {x};
  61. int e = u.i>>23 & 0xff;
  62. if (!e) return u.i<<1 ? FP_SUBNORMAL : FP_ZERO;
  63. if (e==0xff) return u.i<<9 ? FP_NAN : FP_INFINITE;
  64. return FP_NORMAL;
  65. }
  66. /*****************************************************************************/
  67. /*****************************************************************************/
  68. // scalbnf from musl-0.9.15
  69. /*****************************************************************************/
  70. /*****************************************************************************/
  71. float scalbnf(float x, int n)
  72. {
  73. union {float f; uint32_t i;} u;
  74. float_t y = x;
  75. if (n > 127) {
  76. y *= 0x1p127f;
  77. n -= 127;
  78. if (n > 127) {
  79. y *= 0x1p127f;
  80. n -= 127;
  81. if (n > 127)
  82. n = 127;
  83. }
  84. } else if (n < -126) {
  85. y *= 0x1p-126f;
  86. n += 126;
  87. if (n < -126) {
  88. y *= 0x1p-126f;
  89. n += 126;
  90. if (n < -126)
  91. n = -126;
  92. }
  93. }
  94. u.i = (uint32_t)(0x7f+n)<<23;
  95. x = y * u.f;
  96. return x;
  97. }
  98. /*****************************************************************************/
  99. /*****************************************************************************/
  100. // powf from musl-0.9.15
  101. /*****************************************************************************/
  102. /*****************************************************************************/
  103. /* origin: FreeBSD /usr/src/lib/msun/src/e_powf.c */
  104. /*
  105. * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
  106. */
  107. /*
  108. * ====================================================
  109. * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  110. *
  111. * Developed at SunPro, a Sun Microsystems, Inc. business.
  112. * Permission to use, copy, modify, and distribute this
  113. * software is freely granted, provided that this notice
  114. * is preserved.
  115. * ====================================================
  116. */
  117. static const float
  118. bp[] = {1.0, 1.5,},
  119. dp_h[] = { 0.0, 5.84960938e-01,}, /* 0x3f15c000 */
  120. dp_l[] = { 0.0, 1.56322085e-06,}, /* 0x35d1cfdc */
  121. two24 = 16777216.0, /* 0x4b800000 */
  122. huge = 1.0e30,
  123. tiny = 1.0e-30,
  124. /* poly coefs for (3/2)*(log(x)-2s-2/3*s**3 */
  125. L1 = 6.0000002384e-01, /* 0x3f19999a */
  126. L2 = 4.2857143283e-01, /* 0x3edb6db7 */
  127. L3 = 3.3333334327e-01, /* 0x3eaaaaab */
  128. L4 = 2.7272811532e-01, /* 0x3e8ba305 */
  129. L5 = 2.3066075146e-01, /* 0x3e6c3255 */
  130. L6 = 2.0697501302e-01, /* 0x3e53f142 */
  131. P1 = 1.6666667163e-01, /* 0x3e2aaaab */
  132. P2 = -2.7777778450e-03, /* 0xbb360b61 */
  133. P3 = 6.6137559770e-05, /* 0x388ab355 */
  134. P4 = -1.6533901999e-06, /* 0xb5ddea0e */
  135. P5 = 4.1381369442e-08, /* 0x3331bb4c */
  136. lg2 = 6.9314718246e-01, /* 0x3f317218 */
  137. lg2_h = 6.93145752e-01, /* 0x3f317200 */
  138. lg2_l = 1.42860654e-06, /* 0x35bfbe8c */
  139. ovt = 4.2995665694e-08, /* -(128-log2(ovfl+.5ulp)) */
  140. cp = 9.6179670095e-01, /* 0x3f76384f =2/(3ln2) */
  141. cp_h = 9.6191406250e-01, /* 0x3f764000 =12b cp */
  142. cp_l = -1.1736857402e-04, /* 0xb8f623c6 =tail of cp_h */
  143. ivln2 = 1.4426950216e+00, /* 0x3fb8aa3b =1/ln2 */
  144. ivln2_h = 1.4426879883e+00, /* 0x3fb8aa00 =16b 1/ln2*/
  145. ivln2_l = 7.0526075433e-06; /* 0x36eca570 =1/ln2 tail*/
  146. float powf(float x, float y)
  147. {
  148. float z,ax,z_h,z_l,p_h,p_l;
  149. float y1,t1,t2,r,s,sn,t,u,v,w;
  150. int32_t i,j,k,yisint,n;
  151. int32_t hx,hy,ix,iy,is;
  152. GET_FLOAT_WORD(hx, x);
  153. GET_FLOAT_WORD(hy, y);
  154. ix = hx & 0x7fffffff;
  155. iy = hy & 0x7fffffff;
  156. /* x**0 = 1, even if x is NaN */
  157. if (iy == 0)
  158. return 1.0f;
  159. /* 1**y = 1, even if y is NaN */
  160. if (hx == 0x3f800000)
  161. return 1.0f;
  162. /* NaN if either arg is NaN */
  163. if (ix > 0x7f800000 || iy > 0x7f800000)
  164. return x + y;
  165. /* determine if y is an odd int when x < 0
  166. * yisint = 0 ... y is not an integer
  167. * yisint = 1 ... y is an odd int
  168. * yisint = 2 ... y is an even int
  169. */
  170. yisint = 0;
  171. if (hx < 0) {
  172. if (iy >= 0x4b800000)
  173. yisint = 2; /* even integer y */
  174. else if (iy >= 0x3f800000) {
  175. k = (iy>>23) - 0x7f; /* exponent */
  176. j = iy>>(23-k);
  177. if ((j<<(23-k)) == iy)
  178. yisint = 2 - (j & 1);
  179. }
  180. }
  181. /* special value of y */
  182. if (iy == 0x7f800000) { /* y is +-inf */
  183. if (ix == 0x3f800000) /* (-1)**+-inf is 1 */
  184. return 1.0f;
  185. else if (ix > 0x3f800000) /* (|x|>1)**+-inf = inf,0 */
  186. return hy >= 0 ? y : 0.0f;
  187. else if (ix != 0) /* (|x|<1)**+-inf = 0,inf if x!=0 */
  188. return hy >= 0 ? 0.0f: -y;
  189. }
  190. if (iy == 0x3f800000) /* y is +-1 */
  191. return hy >= 0 ? x : 1.0f/x;
  192. if (hy == 0x40000000) /* y is 2 */
  193. return x*x;
  194. if (hy == 0x3f000000) { /* y is 0.5 */
  195. if (hx >= 0) /* x >= +0 */
  196. return sqrtf(x);
  197. }
  198. ax = fabsf(x);
  199. /* special value of x */
  200. if (ix == 0x7f800000 || ix == 0 || ix == 0x3f800000) { /* x is +-0,+-inf,+-1 */
  201. z = ax;
  202. if (hy < 0) /* z = (1/|x|) */
  203. z = 1.0f/z;
  204. if (hx < 0) {
  205. if (((ix-0x3f800000)|yisint) == 0) {
  206. z = (z-z)/(z-z); /* (-1)**non-int is NaN */
  207. } else if (yisint == 1)
  208. z = -z; /* (x<0)**odd = -(|x|**odd) */
  209. }
  210. return z;
  211. }
  212. sn = 1.0f; /* sign of result */
  213. if (hx < 0) {
  214. if (yisint == 0) /* (x<0)**(non-int) is NaN */
  215. return (x-x)/(x-x);
  216. if (yisint == 1) /* (x<0)**(odd int) */
  217. sn = -1.0f;
  218. }
  219. /* |y| is huge */
  220. if (iy > 0x4d000000) { /* if |y| > 2**27 */
  221. /* over/underflow if x is not close to one */
  222. if (ix < 0x3f7ffff8)
  223. return hy < 0 ? sn*huge*huge : sn*tiny*tiny;
  224. if (ix > 0x3f800007)
  225. return hy > 0 ? sn*huge*huge : sn*tiny*tiny;
  226. /* now |1-x| is tiny <= 2**-20, suffice to compute
  227. log(x) by x-x^2/2+x^3/3-x^4/4 */
  228. t = ax - 1; /* t has 20 trailing zeros */
  229. w = (t*t)*(0.5f - t*(0.333333333333f - t*0.25f));
  230. u = ivln2_h*t; /* ivln2_h has 16 sig. bits */
  231. v = t*ivln2_l - w*ivln2;
  232. t1 = u + v;
  233. GET_FLOAT_WORD(is, t1);
  234. SET_FLOAT_WORD(t1, is & 0xfffff000);
  235. t2 = v - (t1-u);
  236. } else {
  237. float s2,s_h,s_l,t_h,t_l;
  238. n = 0;
  239. /* take care subnormal number */
  240. if (ix < 0x00800000) {
  241. ax *= two24;
  242. n -= 24;
  243. GET_FLOAT_WORD(ix, ax);
  244. }
  245. n += ((ix)>>23) - 0x7f;
  246. j = ix & 0x007fffff;
  247. /* determine interval */
  248. ix = j | 0x3f800000; /* normalize ix */
  249. if (j <= 0x1cc471) /* |x|<sqrt(3/2) */
  250. k = 0;
  251. else if (j < 0x5db3d7) /* |x|<sqrt(3) */
  252. k = 1;
  253. else {
  254. k = 0;
  255. n += 1;
  256. ix -= 0x00800000;
  257. }
  258. SET_FLOAT_WORD(ax, ix);
  259. /* compute s = s_h+s_l = (x-1)/(x+1) or (x-1.5)/(x+1.5) */
  260. u = ax - bp[k]; /* bp[0]=1.0, bp[1]=1.5 */
  261. v = 1.0f/(ax+bp[k]);
  262. s = u*v;
  263. s_h = s;
  264. GET_FLOAT_WORD(is, s_h);
  265. SET_FLOAT_WORD(s_h, is & 0xfffff000);
  266. /* t_h=ax+bp[k] High */
  267. is = ((ix>>1) & 0xfffff000) | 0x20000000;
  268. SET_FLOAT_WORD(t_h, is + 0x00400000 + (k<<21));
  269. t_l = ax - (t_h - bp[k]);
  270. s_l = v*((u - s_h*t_h) - s_h*t_l);
  271. /* compute log(ax) */
  272. s2 = s*s;
  273. r = s2*s2*(L1+s2*(L2+s2*(L3+s2*(L4+s2*(L5+s2*L6)))));
  274. r += s_l*(s_h+s);
  275. s2 = s_h*s_h;
  276. t_h = 3.0f + s2 + r;
  277. GET_FLOAT_WORD(is, t_h);
  278. SET_FLOAT_WORD(t_h, is & 0xfffff000);
  279. t_l = r - ((t_h - 3.0f) - s2);
  280. /* u+v = s*(1+...) */
  281. u = s_h*t_h;
  282. v = s_l*t_h + t_l*s;
  283. /* 2/(3log2)*(s+...) */
  284. p_h = u + v;
  285. GET_FLOAT_WORD(is, p_h);
  286. SET_FLOAT_WORD(p_h, is & 0xfffff000);
  287. p_l = v - (p_h - u);
  288. z_h = cp_h*p_h; /* cp_h+cp_l = 2/(3*log2) */
  289. z_l = cp_l*p_h + p_l*cp+dp_l[k];
  290. /* log2(ax) = (s+..)*2/(3*log2) = n + dp_h + z_h + z_l */
  291. t = (float)n;
  292. t1 = (((z_h + z_l) + dp_h[k]) + t);
  293. GET_FLOAT_WORD(is, t1);
  294. SET_FLOAT_WORD(t1, is & 0xfffff000);
  295. t2 = z_l - (((t1 - t) - dp_h[k]) - z_h);
  296. }
  297. /* split up y into y1+y2 and compute (y1+y2)*(t1+t2) */
  298. GET_FLOAT_WORD(is, y);
  299. SET_FLOAT_WORD(y1, is & 0xfffff000);
  300. p_l = (y-y1)*t1 + y*t2;
  301. p_h = y1*t1;
  302. z = p_l + p_h;
  303. GET_FLOAT_WORD(j, z);
  304. if (j > 0x43000000) /* if z > 128 */
  305. return sn*huge*huge; /* overflow */
  306. else if (j == 0x43000000) { /* if z == 128 */
  307. if (p_l + ovt > z - p_h)
  308. return sn*huge*huge; /* overflow */
  309. } else if ((j&0x7fffffff) > 0x43160000) /* z < -150 */ // FIXME: check should be (uint32_t)j > 0xc3160000
  310. return sn*tiny*tiny; /* underflow */
  311. else if (j == 0xc3160000) { /* z == -150 */
  312. if (p_l <= z-p_h)
  313. return sn*tiny*tiny; /* underflow */
  314. }
  315. /*
  316. * compute 2**(p_h+p_l)
  317. */
  318. i = j & 0x7fffffff;
  319. k = (i>>23) - 0x7f;
  320. n = 0;
  321. if (i > 0x3f000000) { /* if |z| > 0.5, set n = [z+0.5] */
  322. n = j + (0x00800000>>(k+1));
  323. k = ((n&0x7fffffff)>>23) - 0x7f; /* new k for n */
  324. SET_FLOAT_WORD(t, n & ~(0x007fffff>>k));
  325. n = ((n&0x007fffff)|0x00800000)>>(23-k);
  326. if (j < 0)
  327. n = -n;
  328. p_h -= t;
  329. }
  330. t = p_l + p_h;
  331. GET_FLOAT_WORD(is, t);
  332. SET_FLOAT_WORD(t, is & 0xffff8000);
  333. u = t*lg2_h;
  334. v = (p_l-(t-p_h))*lg2 + t*lg2_l;
  335. z = u + v;
  336. w = v - (z - u);
  337. t = z*z;
  338. t1 = z - t*(P1+t*(P2+t*(P3+t*(P4+t*P5))));
  339. r = (z*t1)/(t1-2.0f) - (w+z*w);
  340. z = 1.0f - (r - z);
  341. GET_FLOAT_WORD(j, z);
  342. j += n<<23;
  343. if ((j>>23) <= 0) /* subnormal output */
  344. z = scalbnf(z, n);
  345. else
  346. SET_FLOAT_WORD(z, j);
  347. return sn*z;
  348. }
  349. /*****************************************************************************/
  350. /*****************************************************************************/
  351. // expf from musl-0.9.15
  352. /*****************************************************************************/
  353. /*****************************************************************************/
  354. /* origin: FreeBSD /usr/src/lib/msun/src/e_expf.c */
  355. /*
  356. * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
  357. */
  358. /*
  359. * ====================================================
  360. * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  361. *
  362. * Developed at SunPro, a Sun Microsystems, Inc. business.
  363. * Permission to use, copy, modify, and distribute this
  364. * software is freely granted, provided that this notice
  365. * is preserved.
  366. * ====================================================
  367. */
  368. static const float
  369. half[2] = {0.5,-0.5},
  370. ln2hi = 6.9314575195e-1f, /* 0x3f317200 */
  371. ln2lo = 1.4286067653e-6f, /* 0x35bfbe8e */
  372. invln2 = 1.4426950216e+0f, /* 0x3fb8aa3b */
  373. /*
  374. * Domain [-0.34568, 0.34568], range ~[-4.278e-9, 4.447e-9]:
  375. * |x*(exp(x)+1)/(exp(x)-1) - p(x)| < 2**-27.74
  376. */
  377. expf_P1 = 1.6666625440e-1f, /* 0xaaaa8f.0p-26 */
  378. expf_P2 = -2.7667332906e-3f; /* -0xb55215.0p-32 */
  379. float expf(float x)
  380. {
  381. float_t hi, lo, c, xx, y;
  382. int k, sign;
  383. uint32_t hx;
  384. GET_FLOAT_WORD(hx, x);
  385. sign = hx >> 31; /* sign bit of x */
  386. hx &= 0x7fffffff; /* high word of |x| */
  387. /* special cases */
  388. if (hx >= 0x42aeac50) { /* if |x| >= -87.33655f or NaN */
  389. if (hx >= 0x42b17218 && !sign) { /* x >= 88.722839f */
  390. /* overflow */
  391. x *= 0x1p127f;
  392. return x;
  393. }
  394. if (sign) {
  395. /* underflow */
  396. FORCE_EVAL(-0x1p-149f/x);
  397. if (hx >= 0x42cff1b5) /* x <= -103.972084f */
  398. return 0;
  399. }
  400. }
  401. /* argument reduction */
  402. if (hx > 0x3eb17218) { /* if |x| > 0.5 ln2 */
  403. if (hx > 0x3f851592) /* if |x| > 1.5 ln2 */
  404. k = invln2*x + half[sign];
  405. else
  406. k = 1 - sign - sign;
  407. hi = x - k*ln2hi; /* k*ln2hi is exact here */
  408. lo = k*ln2lo;
  409. x = hi - lo;
  410. } else if (hx > 0x39000000) { /* |x| > 2**-14 */
  411. k = 0;
  412. hi = x;
  413. lo = 0;
  414. } else {
  415. /* raise inexact */
  416. FORCE_EVAL(0x1p127f + x);
  417. return 1 + x;
  418. }
  419. /* x is now in primary range */
  420. xx = x*x;
  421. c = x - xx*(expf_P1+xx*expf_P2);
  422. y = 1 + (x*c/(2-c) - lo + hi);
  423. if (k == 0)
  424. return y;
  425. return scalbnf(y, k);
  426. }
  427. /*****************************************************************************/
  428. /*****************************************************************************/
  429. // expm1f from musl-0.9.15
  430. /*****************************************************************************/
  431. /*****************************************************************************/
  432. /* origin: FreeBSD /usr/src/lib/msun/src/s_expm1f.c */
  433. /*
  434. * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
  435. */
  436. /*
  437. * ====================================================
  438. * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  439. *
  440. * Developed at SunPro, a Sun Microsystems, Inc. business.
  441. * Permission to use, copy, modify, and distribute this
  442. * software is freely granted, provided that this notice
  443. * is preserved.
  444. * ====================================================
  445. */
  446. static const float
  447. o_threshold = 8.8721679688e+01, /* 0x42b17180 */
  448. ln2_hi = 6.9313812256e-01, /* 0x3f317180 */
  449. ln2_lo = 9.0580006145e-06, /* 0x3717f7d1 */
  450. //invln2 = 1.4426950216e+00, /* 0x3fb8aa3b */
  451. /*
  452. * Domain [-0.34568, 0.34568], range ~[-6.694e-10, 6.696e-10]:
  453. * |6 / x * (1 + 2 * (1 / (exp(x) - 1) - 1 / x)) - q(x)| < 2**-30.04
  454. * Scaled coefficients: Qn_here = 2**n * Qn_for_q (see s_expm1.c):
  455. */
  456. Q1 = -3.3333212137e-2, /* -0x888868.0p-28 */
  457. Q2 = 1.5807170421e-3; /* 0xcf3010.0p-33 */
  458. float expm1f(float x)
  459. {
  460. float_t y,hi,lo,c,t,e,hxs,hfx,r1,twopk;
  461. union {float f; uint32_t i;} u = {x};
  462. uint32_t hx = u.i & 0x7fffffff;
  463. int k, sign = u.i >> 31;
  464. /* filter out huge and non-finite argument */
  465. if (hx >= 0x4195b844) { /* if |x|>=27*ln2 */
  466. if (hx > 0x7f800000) /* NaN */
  467. return x;
  468. if (sign)
  469. return -1;
  470. if (x > o_threshold) {
  471. x *= 0x1p127f;
  472. return x;
  473. }
  474. }
  475. /* argument reduction */
  476. if (hx > 0x3eb17218) { /* if |x| > 0.5 ln2 */
  477. if (hx < 0x3F851592) { /* and |x| < 1.5 ln2 */
  478. if (!sign) {
  479. hi = x - ln2_hi;
  480. lo = ln2_lo;
  481. k = 1;
  482. } else {
  483. hi = x + ln2_hi;
  484. lo = -ln2_lo;
  485. k = -1;
  486. }
  487. } else {
  488. k = invln2*x + (sign ? -0.5f : 0.5f);
  489. t = k;
  490. hi = x - t*ln2_hi; /* t*ln2_hi is exact here */
  491. lo = t*ln2_lo;
  492. }
  493. x = hi-lo;
  494. c = (hi-x)-lo;
  495. } else if (hx < 0x33000000) { /* when |x|<2**-25, return x */
  496. if (hx < 0x00800000)
  497. FORCE_EVAL(x*x);
  498. return x;
  499. } else
  500. k = 0;
  501. /* x is now in primary range */
  502. hfx = 0.5f*x;
  503. hxs = x*hfx;
  504. r1 = 1.0f+hxs*(Q1+hxs*Q2);
  505. t = 3.0f - r1*hfx;
  506. e = hxs*((r1-t)/(6.0f - x*t));
  507. if (k == 0) /* c is 0 */
  508. return x - (x*e-hxs);
  509. e = x*(e-c) - c;
  510. e -= hxs;
  511. /* exp(x) ~ 2^k (x_reduced - e + 1) */
  512. if (k == -1)
  513. return 0.5f*(x-e) - 0.5f;
  514. if (k == 1) {
  515. if (x < -0.25f)
  516. return -2.0f*(e-(x+0.5f));
  517. return 1.0f + 2.0f*(x-e);
  518. }
  519. u.i = (0x7f+k)<<23; /* 2^k */
  520. twopk = u.f;
  521. if (k < 0 || k > 56) { /* suffice to return exp(x)-1 */
  522. y = x - e + 1.0f;
  523. if (k == 128)
  524. y = y*2.0f*0x1p127f;
  525. else
  526. y = y*twopk;
  527. return y - 1.0f;
  528. }
  529. u.i = (0x7f-k)<<23; /* 2^-k */
  530. if (k < 23)
  531. y = (x-e+(1-u.f))*twopk;
  532. else
  533. y = (x-(e+u.f)+1)*twopk;
  534. return y;
  535. }
  536. /*****************************************************************************/
  537. /*****************************************************************************/
  538. // __expo2f from musl-0.9.15
  539. /*****************************************************************************/
  540. /*****************************************************************************/
  541. /* k is such that k*ln2 has minimal relative error and x - kln2 > log(FLT_MIN) */
  542. static const int k = 235;
  543. static const float kln2 = 0x1.45c778p+7f;
  544. /* expf(x)/2 for x >= log(FLT_MAX), slightly better than 0.5f*expf(x/2)*expf(x/2) */
  545. float __expo2f(float x)
  546. {
  547. float scale;
  548. /* note that k is odd and scale*scale overflows */
  549. SET_FLOAT_WORD(scale, (uint32_t)(0x7f + k/2) << 23);
  550. /* exp(x - k ln2) * 2**(k-1) */
  551. return expf(x - kln2) * scale * scale;
  552. }
  553. /*****************************************************************************/
  554. /*****************************************************************************/
  555. // logf from musl-0.9.15
  556. /*****************************************************************************/
  557. /*****************************************************************************/
  558. /* origin: FreeBSD /usr/src/lib/msun/src/e_logf.c */
  559. /*
  560. * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
  561. */
  562. /*
  563. * ====================================================
  564. * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  565. *
  566. * Developed at SunPro, a Sun Microsystems, Inc. business.
  567. * Permission to use, copy, modify, and distribute this
  568. * software is freely granted, provided that this notice
  569. * is preserved.
  570. * ====================================================
  571. */
  572. static const float
  573. /* |(log(1+s)-log(1-s))/s - Lg(s)| < 2**-34.24 (~[-4.95e-11, 4.97e-11]). */
  574. Lg1 = 0xaaaaaa.0p-24, /* 0.66666662693 */
  575. Lg2 = 0xccce13.0p-25, /* 0.40000972152 */
  576. Lg3 = 0x91e9ee.0p-25, /* 0.28498786688 */
  577. Lg4 = 0xf89e26.0p-26; /* 0.24279078841 */
  578. float logf(float x)
  579. {
  580. union {float f; uint32_t i;} u = {x};
  581. float_t hfsq,f,s,z,R,w,t1,t2,dk;
  582. uint32_t ix;
  583. int k;
  584. ix = u.i;
  585. k = 0;
  586. if (ix < 0x00800000 || ix>>31) { /* x < 2**-126 */
  587. if (ix<<1 == 0)
  588. return -1/(x*x); /* log(+-0)=-inf */
  589. if (ix>>31)
  590. return (x-x)/0.0f; /* log(-#) = NaN */
  591. /* subnormal number, scale up x */
  592. k -= 25;
  593. x *= 0x1p25f;
  594. u.f = x;
  595. ix = u.i;
  596. } else if (ix >= 0x7f800000) {
  597. return x;
  598. } else if (ix == 0x3f800000)
  599. return 0;
  600. /* reduce x into [sqrt(2)/2, sqrt(2)] */
  601. ix += 0x3f800000 - 0x3f3504f3;
  602. k += (int)(ix>>23) - 0x7f;
  603. ix = (ix&0x007fffff) + 0x3f3504f3;
  604. u.i = ix;
  605. x = u.f;
  606. f = x - 1.0f;
  607. s = f/(2.0f + f);
  608. z = s*s;
  609. w = z*z;
  610. t1= w*(Lg2+w*Lg4);
  611. t2= z*(Lg1+w*Lg3);
  612. R = t2 + t1;
  613. hfsq = 0.5f*f*f;
  614. dk = k;
  615. return s*(hfsq+R) + dk*ln2_lo - hfsq + f + dk*ln2_hi;
  616. }
  617. /*****************************************************************************/
  618. /*****************************************************************************/
  619. // coshf from musl-0.9.15
  620. /*****************************************************************************/
  621. /*****************************************************************************/
  622. float coshf(float x)
  623. {
  624. union {float f; uint32_t i;} u = {.f = x};
  625. uint32_t w;
  626. float t;
  627. /* |x| */
  628. u.i &= 0x7fffffff;
  629. x = u.f;
  630. w = u.i;
  631. /* |x| < log(2) */
  632. if (w < 0x3f317217) {
  633. if (w < 0x3f800000 - (12<<23)) {
  634. FORCE_EVAL(x + 0x1p120f);
  635. return 1;
  636. }
  637. t = expm1f(x);
  638. return 1 + t*t/(2*(1+t));
  639. }
  640. /* |x| < log(FLT_MAX) */
  641. if (w < 0x42b17217) {
  642. t = expf(x);
  643. return 0.5f*(t + 1/t);
  644. }
  645. /* |x| > log(FLT_MAX) or nan */
  646. t = __expo2f(x);
  647. return t;
  648. }
  649. /*****************************************************************************/
  650. /*****************************************************************************/
  651. // sinhf from musl-0.9.15
  652. /*****************************************************************************/
  653. /*****************************************************************************/
  654. float sinhf(float x)
  655. {
  656. union {float f; uint32_t i;} u = {.f = x};
  657. uint32_t w;
  658. float t, h, absx;
  659. h = 0.5;
  660. if (u.i >> 31)
  661. h = -h;
  662. /* |x| */
  663. u.i &= 0x7fffffff;
  664. absx = u.f;
  665. w = u.i;
  666. /* |x| < log(FLT_MAX) */
  667. if (w < 0x42b17217) {
  668. t = expm1f(absx);
  669. if (w < 0x3f800000) {
  670. if (w < 0x3f800000 - (12<<23))
  671. return x;
  672. return h*(2*t - t*t/(t+1));
  673. }
  674. return h*(t + t/(t+1));
  675. }
  676. /* |x| > logf(FLT_MAX) or nan */
  677. t = 2*h*__expo2f(absx);
  678. return t;
  679. }
  680. /*****************************************************************************/
  681. /*****************************************************************************/
  682. // ceilf, floorf and truncf from musl-0.9.15
  683. /*****************************************************************************/
  684. /*****************************************************************************/
  685. float ceilf(float x)
  686. {
  687. union {float f; uint32_t i;} u = {x};
  688. int e = (int)(u.i >> 23 & 0xff) - 0x7f;
  689. uint32_t m;
  690. if (e >= 23)
  691. return x;
  692. if (e >= 0) {
  693. m = 0x007fffff >> e;
  694. if ((u.i & m) == 0)
  695. return x;
  696. FORCE_EVAL(x + 0x1p120f);
  697. if (u.i >> 31 == 0)
  698. u.i += m;
  699. u.i &= ~m;
  700. } else {
  701. FORCE_EVAL(x + 0x1p120f);
  702. if (u.i >> 31)
  703. u.f = -0.0;
  704. else if (u.i << 1)
  705. u.f = 1.0;
  706. }
  707. return u.f;
  708. }
  709. float floorf(float x)
  710. {
  711. union {float f; uint32_t i;} u = {x};
  712. int e = (int)(u.i >> 23 & 0xff) - 0x7f;
  713. uint32_t m;
  714. if (e >= 23)
  715. return x;
  716. if (e >= 0) {
  717. m = 0x007fffff >> e;
  718. if ((u.i & m) == 0)
  719. return x;
  720. FORCE_EVAL(x + 0x1p120f);
  721. if (u.i >> 31)
  722. u.i += m;
  723. u.i &= ~m;
  724. } else {
  725. FORCE_EVAL(x + 0x1p120f);
  726. if (u.i >> 31 == 0)
  727. u.i = 0;
  728. else if (u.i << 1)
  729. u.f = -1.0;
  730. }
  731. return u.f;
  732. }
  733. float truncf(float x)
  734. {
  735. union {float f; uint32_t i;} u = {x};
  736. int e = (int)(u.i >> 23 & 0xff) - 0x7f + 9;
  737. uint32_t m;
  738. if (e >= 23 + 9)
  739. return x;
  740. if (e < 9)
  741. e = 1;
  742. m = -1U >> e;
  743. if ((u.i & m) == 0)
  744. return x;
  745. FORCE_EVAL(x + 0x1p120f);
  746. u.i &= ~m;
  747. return u.f;
  748. }