e_powl.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. /*
  2. * ====================================================
  3. * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  4. *
  5. * Developed at SunPro, a Sun Microsystems, Inc. business.
  6. * Permission to use, copy, modify, and distribute this
  7. * software is freely granted, provided that this notice
  8. * is preserved.
  9. * ====================================================
  10. */
  11. /*
  12. * Copyright (c) 2008 Stephen L. Moshier <[email protected]>
  13. *
  14. * Permission to use, copy, modify, and distribute this software for any
  15. * purpose with or without fee is hereby granted, provided that the above
  16. * copyright notice and this permission notice appear in all copies.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  19. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  20. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  21. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  22. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  23. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  24. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  25. */
  26. /* powl(x,y) return x**y
  27. *
  28. * n
  29. * Method: Let x = 2 * (1+f)
  30. * 1. Compute and return log2(x) in two pieces:
  31. * log2(x) = w1 + w2,
  32. * where w1 has 113-53 = 60 bit trailing zeros.
  33. * 2. Perform y*log2(x) = n+y' by simulating muti-precision
  34. * arithmetic, where |y'|<=0.5.
  35. * 3. Return x**y = 2**n*exp(y'*log2)
  36. *
  37. * Special cases:
  38. * 1. (anything) ** 0 is 1
  39. * 2. (anything) ** 1 is itself
  40. * 3. (anything) ** NAN is NAN
  41. * 4. NAN ** (anything except 0) is NAN
  42. * 5. +-(|x| > 1) ** +INF is +INF
  43. * 6. +-(|x| > 1) ** -INF is +0
  44. * 7. +-(|x| < 1) ** +INF is +0
  45. * 8. +-(|x| < 1) ** -INF is +INF
  46. * 9. +-1 ** +-INF is NAN
  47. * 10. +0 ** (+anything except 0, NAN) is +0
  48. * 11. -0 ** (+anything except 0, NAN, odd integer) is +0
  49. * 12. +0 ** (-anything except 0, NAN) is +INF
  50. * 13. -0 ** (-anything except 0, NAN, odd integer) is +INF
  51. * 14. -0 ** (odd integer) = -( +0 ** (odd integer) )
  52. * 15. +INF ** (+anything except 0,NAN) is +INF
  53. * 16. +INF ** (-anything except 0,NAN) is +0
  54. * 17. -INF ** (anything) = -0 ** (-anything)
  55. * 18. (-anything) ** (integer) is (-1)**(integer)*(+anything**integer)
  56. * 19. (-anything except 0 and inf) ** (non-integer) is NAN
  57. *
  58. */
  59. #include <openlibm_math.h>
  60. #include "math_private.h"
  61. static const long double bp[] = {
  62. 1.0L,
  63. 1.5L,
  64. };
  65. /* log_2(1.5) */
  66. static const long double dp_h[] = {
  67. 0.0,
  68. 5.8496250072115607565592654282227158546448E-1L
  69. };
  70. /* Low part of log_2(1.5) */
  71. static const long double dp_l[] = {
  72. 0.0,
  73. 1.0579781240112554492329533686862998106046E-16L
  74. };
  75. static const long double zero = 0.0L,
  76. one = 1.0L,
  77. two = 2.0L,
  78. two113 = 1.0384593717069655257060992658440192E34L,
  79. huge = 1.0e3000L,
  80. tiny = 1.0e-3000L;
  81. /* 3/2 log x = 3 z + z^3 + z^3 (z^2 R(z^2))
  82. z = (x-1)/(x+1)
  83. 1 <= x <= 1.25
  84. Peak relative error 2.3e-37 */
  85. static const long double LN[] =
  86. {
  87. -3.0779177200290054398792536829702930623200E1L,
  88. 6.5135778082209159921251824580292116201640E1L,
  89. -4.6312921812152436921591152809994014413540E1L,
  90. 1.2510208195629420304615674658258363295208E1L,
  91. -9.9266909031921425609179910128531667336670E-1L
  92. };
  93. static const long double LD[] =
  94. {
  95. -5.129862866715009066465422805058933131960E1L,
  96. 1.452015077564081884387441590064272782044E2L,
  97. -1.524043275549860505277434040464085593165E2L,
  98. 7.236063513651544224319663428634139768808E1L,
  99. -1.494198912340228235853027849917095580053E1L
  100. /* 1.0E0 */
  101. };
  102. /* exp(x) = 1 + x - x / (1 - 2 / (x - x^2 R(x^2)))
  103. 0 <= x <= 0.5
  104. Peak relative error 5.7e-38 */
  105. static const long double PN[] =
  106. {
  107. 5.081801691915377692446852383385968225675E8L,
  108. 9.360895299872484512023336636427675327355E6L,
  109. 4.213701282274196030811629773097579432957E4L,
  110. 5.201006511142748908655720086041570288182E1L,
  111. 9.088368420359444263703202925095675982530E-3L,
  112. };
  113. static const long double PD[] =
  114. {
  115. 3.049081015149226615468111430031590411682E9L,
  116. 1.069833887183886839966085436512368982758E8L,
  117. 8.259257717868875207333991924545445705394E5L,
  118. 1.872583833284143212651746812884298360922E3L,
  119. /* 1.0E0 */
  120. };
  121. static const long double
  122. /* ln 2 */
  123. lg2 = 6.9314718055994530941723212145817656807550E-1L,
  124. lg2_h = 6.9314718055994528622676398299518041312695E-1L,
  125. lg2_l = 2.3190468138462996154948554638754786504121E-17L,
  126. ovt = 8.0085662595372944372e-0017L,
  127. /* 2/(3*log(2)) */
  128. cp = 9.6179669392597560490661645400126142495110E-1L,
  129. cp_h = 9.6179669392597555432899980587535537779331E-1L,
  130. cp_l = 5.0577616648125906047157785230014751039424E-17L;
  131. long double
  132. powl(long double x, long double y)
  133. {
  134. long double z, ax, z_h, z_l, p_h, p_l;
  135. long double yy1, t1, t2, r, s, t, u, v, w;
  136. long double s2, s_h, s_l, t_h, t_l;
  137. int32_t i, j, k, yisint, n;
  138. u_int32_t ix, iy;
  139. int32_t hx, hy;
  140. ieee_quad_shape_type o, p, q;
  141. p.value = x;
  142. hx = p.parts32.mswhi;
  143. ix = hx & 0x7fffffff;
  144. q.value = y;
  145. hy = q.parts32.mswhi;
  146. iy = hy & 0x7fffffff;
  147. /* y==zero: x**0 = 1 */
  148. if ((iy | q.parts32.mswlo | q.parts32.lswhi | q.parts32.lswlo) == 0)
  149. return one;
  150. /* 1.0**y = 1; -1.0**+-Inf = 1 */
  151. if (x == one)
  152. return one;
  153. if (x == -1.0L && iy == 0x7fff0000
  154. && (q.parts32.mswlo | q.parts32.lswhi | q.parts32.lswlo) == 0)
  155. return one;
  156. /* +-NaN return x+y */
  157. if ((ix > 0x7fff0000)
  158. || ((ix == 0x7fff0000)
  159. && ((p.parts32.mswlo | p.parts32.lswhi | p.parts32.lswlo) != 0))
  160. || (iy > 0x7fff0000)
  161. || ((iy == 0x7fff0000)
  162. && ((q.parts32.mswlo | q.parts32.lswhi | q.parts32.lswlo) != 0)))
  163. return x + y;
  164. /* determine if y is an odd int when x < 0
  165. * yisint = 0 ... y is not an integer
  166. * yisint = 1 ... y is an odd int
  167. * yisint = 2 ... y is an even int
  168. */
  169. yisint = 0;
  170. if (hx < 0)
  171. {
  172. if (iy >= 0x40700000) /* 2^113 */
  173. yisint = 2; /* even integer y */
  174. else if (iy >= 0x3fff0000) /* 1.0 */
  175. {
  176. if (floorl (y) == y)
  177. {
  178. z = 0.5 * y;
  179. if (floorl (z) == z)
  180. yisint = 2;
  181. else
  182. yisint = 1;
  183. }
  184. }
  185. }
  186. /* special value of y */
  187. if ((q.parts32.mswlo | q.parts32.lswhi | q.parts32.lswlo) == 0)
  188. {
  189. if (iy == 0x7fff0000) /* y is +-inf */
  190. {
  191. if (((ix - 0x3fff0000) | p.parts32.mswlo | p.parts32.lswhi |
  192. p.parts32.lswlo) == 0)
  193. return y - y; /* +-1**inf is NaN */
  194. else if (ix >= 0x3fff0000) /* (|x|>1)**+-inf = inf,0 */
  195. return (hy >= 0) ? y : zero;
  196. else /* (|x|<1)**-,+inf = inf,0 */
  197. return (hy < 0) ? -y : zero;
  198. }
  199. if (iy == 0x3fff0000)
  200. { /* y is +-1 */
  201. if (hy < 0)
  202. return one / x;
  203. else
  204. return x;
  205. }
  206. if (hy == 0x40000000)
  207. return x * x; /* y is 2 */
  208. if (hy == 0x3ffe0000)
  209. { /* y is 0.5 */
  210. if (hx >= 0) /* x >= +0 */
  211. return sqrtl (x);
  212. }
  213. }
  214. ax = fabsl (x);
  215. /* special value of x */
  216. if ((p.parts32.mswlo | p.parts32.lswhi | p.parts32.lswlo) == 0)
  217. {
  218. if (ix == 0x7fff0000 || ix == 0 || ix == 0x3fff0000)
  219. {
  220. z = ax; /*x is +-0,+-inf,+-1 */
  221. if (hy < 0)
  222. z = one / z; /* z = (1/|x|) */
  223. if (hx < 0)
  224. {
  225. if (((ix - 0x3fff0000) | yisint) == 0)
  226. {
  227. z = (z - z) / (z - z); /* (-1)**non-int is NaN */
  228. }
  229. else if (yisint == 1)
  230. z = -z; /* (x<0)**odd = -(|x|**odd) */
  231. }
  232. return z;
  233. }
  234. }
  235. /* (x<0)**(non-int) is NaN */
  236. if (((((u_int32_t) hx >> 31) - 1) | yisint) == 0)
  237. return (x - x) / (x - x);
  238. /* |y| is huge.
  239. 2^-16495 = 1/2 of smallest representable value.
  240. If (1 - 1/131072)^y underflows, y > 1.4986e9 */
  241. if (iy > 0x401d654b)
  242. {
  243. /* if (1 - 2^-113)^y underflows, y > 1.1873e38 */
  244. if (iy > 0x407d654b)
  245. {
  246. if (ix <= 0x3ffeffff)
  247. return (hy < 0) ? huge * huge : tiny * tiny;
  248. if (ix >= 0x3fff0000)
  249. return (hy > 0) ? huge * huge : tiny * tiny;
  250. }
  251. /* over/underflow if x is not close to one */
  252. if (ix < 0x3ffeffff)
  253. return (hy < 0) ? huge * huge : tiny * tiny;
  254. if (ix > 0x3fff0000)
  255. return (hy > 0) ? huge * huge : tiny * tiny;
  256. }
  257. n = 0;
  258. /* take care subnormal number */
  259. if (ix < 0x00010000)
  260. {
  261. ax *= two113;
  262. n -= 113;
  263. o.value = ax;
  264. ix = o.parts32.mswhi;
  265. }
  266. n += ((ix) >> 16) - 0x3fff;
  267. j = ix & 0x0000ffff;
  268. /* determine interval */
  269. ix = j | 0x3fff0000; /* normalize ix */
  270. if (j <= 0x3988)
  271. k = 0; /* |x|<sqrt(3/2) */
  272. else if (j < 0xbb67)
  273. k = 1; /* |x|<sqrt(3) */
  274. else
  275. {
  276. k = 0;
  277. n += 1;
  278. ix -= 0x00010000;
  279. }
  280. o.value = ax;
  281. o.parts32.mswhi = ix;
  282. ax = o.value;
  283. /* compute s = s_h+s_l = (x-1)/(x+1) or (x-1.5)/(x+1.5) */
  284. u = ax - bp[k]; /* bp[0]=1.0, bp[1]=1.5 */
  285. v = one / (ax + bp[k]);
  286. s = u * v;
  287. s_h = s;
  288. o.value = s_h;
  289. o.parts32.lswlo = 0;
  290. o.parts32.lswhi &= 0xf8000000;
  291. s_h = o.value;
  292. /* t_h=ax+bp[k] High */
  293. t_h = ax + bp[k];
  294. o.value = t_h;
  295. o.parts32.lswlo = 0;
  296. o.parts32.lswhi &= 0xf8000000;
  297. t_h = o.value;
  298. t_l = ax - (t_h - bp[k]);
  299. s_l = v * ((u - s_h * t_h) - s_h * t_l);
  300. /* compute log(ax) */
  301. s2 = s * s;
  302. u = LN[0] + s2 * (LN[1] + s2 * (LN[2] + s2 * (LN[3] + s2 * LN[4])));
  303. v = LD[0] + s2 * (LD[1] + s2 * (LD[2] + s2 * (LD[3] + s2 * (LD[4] + s2))));
  304. r = s2 * s2 * u / v;
  305. r += s_l * (s_h + s);
  306. s2 = s_h * s_h;
  307. t_h = 3.0 + s2 + r;
  308. o.value = t_h;
  309. o.parts32.lswlo = 0;
  310. o.parts32.lswhi &= 0xf8000000;
  311. t_h = o.value;
  312. t_l = r - ((t_h - 3.0) - s2);
  313. /* u+v = s*(1+...) */
  314. u = s_h * t_h;
  315. v = s_l * t_h + t_l * s;
  316. /* 2/(3log2)*(s+...) */
  317. p_h = u + v;
  318. o.value = p_h;
  319. o.parts32.lswlo = 0;
  320. o.parts32.lswhi &= 0xf8000000;
  321. p_h = o.value;
  322. p_l = v - (p_h - u);
  323. z_h = cp_h * p_h; /* cp_h+cp_l = 2/(3*log2) */
  324. z_l = cp_l * p_h + p_l * cp + dp_l[k];
  325. /* log2(ax) = (s+..)*2/(3*log2) = n + dp_h + z_h + z_l */
  326. t = (long double) n;
  327. t1 = (((z_h + z_l) + dp_h[k]) + t);
  328. o.value = t1;
  329. o.parts32.lswlo = 0;
  330. o.parts32.lswhi &= 0xf8000000;
  331. t1 = o.value;
  332. t2 = z_l - (((t1 - t) - dp_h[k]) - z_h);
  333. /* s (sign of result -ve**odd) = -1 else = 1 */
  334. s = one;
  335. if (((((u_int32_t) hx >> 31) - 1) | (yisint - 1)) == 0)
  336. s = -one; /* (-ve)**(odd int) */
  337. /* split up y into yy1+y2 and compute (yy1+y2)*(t1+t2) */
  338. yy1 = y;
  339. o.value = yy1;
  340. o.parts32.lswlo = 0;
  341. o.parts32.lswhi &= 0xf8000000;
  342. yy1 = o.value;
  343. p_l = (y - yy1) * t1 + y * t2;
  344. p_h = yy1 * t1;
  345. z = p_l + p_h;
  346. o.value = z;
  347. j = o.parts32.mswhi;
  348. if (j >= 0x400d0000) /* z >= 16384 */
  349. {
  350. /* if z > 16384 */
  351. if (((j - 0x400d0000) | o.parts32.mswlo | o.parts32.lswhi |
  352. o.parts32.lswlo) != 0)
  353. return s * huge * huge; /* overflow */
  354. else
  355. {
  356. if (p_l + ovt > z - p_h)
  357. return s * huge * huge; /* overflow */
  358. }
  359. }
  360. else if ((j & 0x7fffffff) >= 0x400d01b9) /* z <= -16495 */
  361. {
  362. /* z < -16495 */
  363. if (((j - 0xc00d01bc) | o.parts32.mswlo | o.parts32.lswhi |
  364. o.parts32.lswlo)
  365. != 0)
  366. return s * tiny * tiny; /* underflow */
  367. else
  368. {
  369. if (p_l <= z - p_h)
  370. return s * tiny * tiny; /* underflow */
  371. }
  372. }
  373. /* compute 2**(p_h+p_l) */
  374. i = j & 0x7fffffff;
  375. k = (i >> 16) - 0x3fff;
  376. n = 0;
  377. if (i > 0x3ffe0000)
  378. { /* if |z| > 0.5, set n = [z+0.5] */
  379. n = floorl (z + 0.5L);
  380. t = n;
  381. p_h -= t;
  382. }
  383. t = p_l + p_h;
  384. o.value = t;
  385. o.parts32.lswlo = 0;
  386. o.parts32.lswhi &= 0xf8000000;
  387. t = o.value;
  388. u = t * lg2_h;
  389. v = (p_l - (t - p_h)) * lg2 + t * lg2_l;
  390. z = u + v;
  391. w = v - (z - u);
  392. /* exp(z) */
  393. t = z * z;
  394. u = PN[0] + t * (PN[1] + t * (PN[2] + t * (PN[3] + t * PN[4])));
  395. v = PD[0] + t * (PD[1] + t * (PD[2] + t * (PD[3] + t)));
  396. t1 = z - t * u / v;
  397. r = (z * t1) / (t1 - two) - (w + z * w);
  398. z = one - (r - z);
  399. o.value = z;
  400. j = o.parts32.mswhi;
  401. j += (n << 16);
  402. if ((j >> 16) <= 0)
  403. z = scalbnl (z, n); /* subnormal output */
  404. else
  405. {
  406. o.parts32.mswhi = j;
  407. z = o.value;
  408. }
  409. return s * z;
  410. }