powq.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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. /* Expansions and modifications for 128-bit long double are
  12. Copyright (C) 2001 Stephen L. Moshier <moshier@na-net.ornl.gov>
  13. and are incorporated herein by permission of the author. The author
  14. reserves the right to distribute this material elsewhere under different
  15. copying permissions. These modifications are distributed here under
  16. the following terms:
  17. This library is free software; you can redistribute it and/or
  18. modify it under the terms of the GNU Lesser General Public
  19. License as published by the Free Software Foundation; either
  20. version 2.1 of the License, or (at your option) any later version.
  21. This library is distributed in the hope that it will be useful,
  22. but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  24. Lesser General Public License for more details.
  25. You should have received a copy of the GNU Lesser General Public
  26. License along with this library; if not, write to the Free Software
  27. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
  28. /* powq(x,y) return x**y
  29. *
  30. * n
  31. * Method: Let x = 2 * (1+f)
  32. * 1. Compute and return log2(x) in two pieces:
  33. * log2(x) = w1 + w2,
  34. * where w1 has 113-53 = 60 bit trailing zeros.
  35. * 2. Perform y*log2(x) = n+y' by simulating muti-precision
  36. * arithmetic, where |y'|<=0.5.
  37. * 3. Return x**y = 2**n*exp(y'*log2)
  38. *
  39. * Special cases:
  40. * 1. (anything) ** 0 is 1
  41. * 2. (anything) ** 1 is itself
  42. * 3. (anything) ** NAN is NAN
  43. * 4. NAN ** (anything except 0) is NAN
  44. * 5. +-(|x| > 1) ** +INF is +INF
  45. * 6. +-(|x| > 1) ** -INF is +0
  46. * 7. +-(|x| < 1) ** +INF is +0
  47. * 8. +-(|x| < 1) ** -INF is +INF
  48. * 9. +-1 ** +-INF is NAN
  49. * 10. +0 ** (+anything except 0, NAN) is +0
  50. * 11. -0 ** (+anything except 0, NAN, odd integer) is +0
  51. * 12. +0 ** (-anything except 0, NAN) is +INF
  52. * 13. -0 ** (-anything except 0, NAN, odd integer) is +INF
  53. * 14. -0 ** (odd integer) = -( +0 ** (odd integer) )
  54. * 15. +INF ** (+anything except 0,NAN) is +INF
  55. * 16. +INF ** (-anything except 0,NAN) is +0
  56. * 17. -INF ** (anything) = -0 ** (-anything)
  57. * 18. (-anything) ** (integer) is (-1)**(integer)*(+anything**integer)
  58. * 19. (-anything except 0 and inf) ** (non-integer) is NAN
  59. *
  60. */
  61. #include "quadmath-imp.h"
  62. static const __float128 bp[] = {
  63. 1.0Q,
  64. 1.5Q,
  65. };
  66. /* log_2(1.5) */
  67. static const __float128 dp_h[] = {
  68. 0.0,
  69. 5.8496250072115607565592654282227158546448E-1Q
  70. };
  71. /* Low part of log_2(1.5) */
  72. static const __float128 dp_l[] = {
  73. 0.0,
  74. 1.0579781240112554492329533686862998106046E-16Q
  75. };
  76. static const __float128 zero = 0.0Q,
  77. one = 1.0Q,
  78. two = 2.0Q,
  79. two113 = 1.0384593717069655257060992658440192E34Q,
  80. huge = 1.0e3000Q,
  81. tiny = 1.0e-3000Q;
  82. /* 3/2 log x = 3 z + z^3 + z^3 (z^2 R(z^2))
  83. z = (x-1)/(x+1)
  84. 1 <= x <= 1.25
  85. Peak relative error 2.3e-37 */
  86. static const __float128 LN[] =
  87. {
  88. -3.0779177200290054398792536829702930623200E1Q,
  89. 6.5135778082209159921251824580292116201640E1Q,
  90. -4.6312921812152436921591152809994014413540E1Q,
  91. 1.2510208195629420304615674658258363295208E1Q,
  92. -9.9266909031921425609179910128531667336670E-1Q
  93. };
  94. static const __float128 LD[] =
  95. {
  96. -5.129862866715009066465422805058933131960E1Q,
  97. 1.452015077564081884387441590064272782044E2Q,
  98. -1.524043275549860505277434040464085593165E2Q,
  99. 7.236063513651544224319663428634139768808E1Q,
  100. -1.494198912340228235853027849917095580053E1Q
  101. /* 1.0E0 */
  102. };
  103. /* exp(x) = 1 + x - x / (1 - 2 / (x - x^2 R(x^2)))
  104. 0 <= x <= 0.5
  105. Peak relative error 5.7e-38 */
  106. static const __float128 PN[] =
  107. {
  108. 5.081801691915377692446852383385968225675E8Q,
  109. 9.360895299872484512023336636427675327355E6Q,
  110. 4.213701282274196030811629773097579432957E4Q,
  111. 5.201006511142748908655720086041570288182E1Q,
  112. 9.088368420359444263703202925095675982530E-3Q,
  113. };
  114. static const __float128 PD[] =
  115. {
  116. 3.049081015149226615468111430031590411682E9Q,
  117. 1.069833887183886839966085436512368982758E8Q,
  118. 8.259257717868875207333991924545445705394E5Q,
  119. 1.872583833284143212651746812884298360922E3Q,
  120. /* 1.0E0 */
  121. };
  122. static const __float128
  123. /* ln 2 */
  124. lg2 = 6.9314718055994530941723212145817656807550E-1Q,
  125. lg2_h = 6.9314718055994528622676398299518041312695E-1Q,
  126. lg2_l = 2.3190468138462996154948554638754786504121E-17Q,
  127. ovt = 8.0085662595372944372e-0017Q,
  128. /* 2/(3*log(2)) */
  129. cp = 9.6179669392597560490661645400126142495110E-1Q,
  130. cp_h = 9.6179669392597555432899980587535537779331E-1Q,
  131. cp_l = 5.0577616648125906047157785230014751039424E-17Q;
  132. __float128
  133. powq (__float128 x, __float128 y)
  134. {
  135. __float128 z, ax, z_h, z_l, p_h, p_l;
  136. __float128 y1, t1, t2, r, s, t, u, v, w;
  137. __float128 s2, s_h, s_l, t_h, t_l, ay;
  138. int32_t i, j, k, yisint, n;
  139. uint32_t ix, iy;
  140. int32_t hx, hy;
  141. ieee854_float128 o, p, q;
  142. p.value = x;
  143. hx = p.words32.w0;
  144. ix = hx & 0x7fffffff;
  145. q.value = y;
  146. hy = q.words32.w0;
  147. iy = hy & 0x7fffffff;
  148. /* y==zero: x**0 = 1 */
  149. if ((iy | q.words32.w1 | q.words32.w2 | q.words32.w3) == 0)
  150. return one;
  151. /* 1.0**y = 1; -1.0**+-Inf = 1 */
  152. if (x == one)
  153. return one;
  154. if (x == -1.0Q && iy == 0x7fff0000
  155. && (q.words32.w1 | q.words32.w2 | q.words32.w3) == 0)
  156. return one;
  157. /* +-NaN return x+y */
  158. if ((ix > 0x7fff0000)
  159. || ((ix == 0x7fff0000)
  160. && ((p.words32.w1 | p.words32.w2 | p.words32.w3) != 0))
  161. || (iy > 0x7fff0000)
  162. || ((iy == 0x7fff0000)
  163. && ((q.words32.w1 | q.words32.w2 | q.words32.w3) != 0)))
  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. {
  173. if (iy >= 0x40700000) /* 2^113 */
  174. yisint = 2; /* even integer y */
  175. else if (iy >= 0x3fff0000) /* 1.0 */
  176. {
  177. if (floorq (y) == y)
  178. {
  179. z = 0.5 * y;
  180. if (floorq (z) == z)
  181. yisint = 2;
  182. else
  183. yisint = 1;
  184. }
  185. }
  186. }
  187. /* special value of y */
  188. if ((q.words32.w1 | q.words32.w2 | q.words32.w3) == 0)
  189. {
  190. if (iy == 0x7fff0000) /* y is +-inf */
  191. {
  192. if (((ix - 0x3fff0000) | p.words32.w1 | p.words32.w2 | p.words32.w3)
  193. == 0)
  194. return y - y; /* +-1**inf is NaN */
  195. else if (ix >= 0x3fff0000) /* (|x|>1)**+-inf = inf,0 */
  196. return (hy >= 0) ? y : zero;
  197. else /* (|x|<1)**-,+inf = inf,0 */
  198. return (hy < 0) ? -y : zero;
  199. }
  200. if (iy == 0x3fff0000)
  201. { /* y is +-1 */
  202. if (hy < 0)
  203. return one / x;
  204. else
  205. return x;
  206. }
  207. if (hy == 0x40000000)
  208. return x * x; /* y is 2 */
  209. if (hy == 0x3ffe0000)
  210. { /* y is 0.5 */
  211. if (hx >= 0) /* x >= +0 */
  212. return sqrtq (x);
  213. }
  214. }
  215. ax = fabsq (x);
  216. /* special value of x */
  217. if ((p.words32.w1 | p.words32.w2 | p.words32.w3) == 0)
  218. {
  219. if (ix == 0x7fff0000 || ix == 0 || ix == 0x3fff0000)
  220. {
  221. z = ax; /*x is +-0,+-inf,+-1 */
  222. if (hy < 0)
  223. z = one / z; /* z = (1/|x|) */
  224. if (hx < 0)
  225. {
  226. if (((ix - 0x3fff0000) | yisint) == 0)
  227. {
  228. z = (z - z) / (z - z); /* (-1)**non-int is NaN */
  229. }
  230. else if (yisint == 1)
  231. z = -z; /* (x<0)**odd = -(|x|**odd) */
  232. }
  233. return z;
  234. }
  235. }
  236. /* (x<0)**(non-int) is NaN */
  237. if (((((uint32_t) hx >> 31) - 1) | yisint) == 0)
  238. return (x - x) / (x - x);
  239. /* |y| is huge.
  240. 2^-16495 = 1/2 of smallest representable value.
  241. If (1 - 1/131072)^y underflows, y > 1.4986e9 */
  242. if (iy > 0x401d654b)
  243. {
  244. /* if (1 - 2^-113)^y underflows, y > 1.1873e38 */
  245. if (iy > 0x407d654b)
  246. {
  247. if (ix <= 0x3ffeffff)
  248. return (hy < 0) ? huge * huge : tiny * tiny;
  249. if (ix >= 0x3fff0000)
  250. return (hy > 0) ? huge * huge : tiny * tiny;
  251. }
  252. /* over/underflow if x is not close to one */
  253. if (ix < 0x3ffeffff)
  254. return (hy < 0) ? huge * huge : tiny * tiny;
  255. if (ix > 0x3fff0000)
  256. return (hy > 0) ? huge * huge : tiny * tiny;
  257. }
  258. ay = y > 0 ? y : -y;
  259. if (ay < 0x1p-128)
  260. y = y < 0 ? -0x1p-128 : 0x1p-128;
  261. n = 0;
  262. /* take care subnormal number */
  263. if (ix < 0x00010000)
  264. {
  265. ax *= two113;
  266. n -= 113;
  267. o.value = ax;
  268. ix = o.words32.w0;
  269. }
  270. n += ((ix) >> 16) - 0x3fff;
  271. j = ix & 0x0000ffff;
  272. /* determine interval */
  273. ix = j | 0x3fff0000; /* normalize ix */
  274. if (j <= 0x3988)
  275. k = 0; /* |x|<sqrt(3/2) */
  276. else if (j < 0xbb67)
  277. k = 1; /* |x|<sqrt(3) */
  278. else
  279. {
  280. k = 0;
  281. n += 1;
  282. ix -= 0x00010000;
  283. }
  284. o.value = ax;
  285. o.words32.w0 = ix;
  286. ax = o.value;
  287. /* compute s = s_h+s_l = (x-1)/(x+1) or (x-1.5)/(x+1.5) */
  288. u = ax - bp[k]; /* bp[0]=1.0, bp[1]=1.5 */
  289. v = one / (ax + bp[k]);
  290. s = u * v;
  291. s_h = s;
  292. o.value = s_h;
  293. o.words32.w3 = 0;
  294. o.words32.w2 &= 0xf8000000;
  295. s_h = o.value;
  296. /* t_h=ax+bp[k] High */
  297. t_h = ax + bp[k];
  298. o.value = t_h;
  299. o.words32.w3 = 0;
  300. o.words32.w2 &= 0xf8000000;
  301. t_h = o.value;
  302. t_l = ax - (t_h - bp[k]);
  303. s_l = v * ((u - s_h * t_h) - s_h * t_l);
  304. /* compute log(ax) */
  305. s2 = s * s;
  306. u = LN[0] + s2 * (LN[1] + s2 * (LN[2] + s2 * (LN[3] + s2 * LN[4])));
  307. v = LD[0] + s2 * (LD[1] + s2 * (LD[2] + s2 * (LD[3] + s2 * (LD[4] + s2))));
  308. r = s2 * s2 * u / v;
  309. r += s_l * (s_h + s);
  310. s2 = s_h * s_h;
  311. t_h = 3.0 + s2 + r;
  312. o.value = t_h;
  313. o.words32.w3 = 0;
  314. o.words32.w2 &= 0xf8000000;
  315. t_h = o.value;
  316. t_l = r - ((t_h - 3.0) - s2);
  317. /* u+v = s*(1+...) */
  318. u = s_h * t_h;
  319. v = s_l * t_h + t_l * s;
  320. /* 2/(3log2)*(s+...) */
  321. p_h = u + v;
  322. o.value = p_h;
  323. o.words32.w3 = 0;
  324. o.words32.w2 &= 0xf8000000;
  325. p_h = o.value;
  326. p_l = v - (p_h - u);
  327. z_h = cp_h * p_h; /* cp_h+cp_l = 2/(3*log2) */
  328. z_l = cp_l * p_h + p_l * cp + dp_l[k];
  329. /* log2(ax) = (s+..)*2/(3*log2) = n + dp_h + z_h + z_l */
  330. t = (__float128) n;
  331. t1 = (((z_h + z_l) + dp_h[k]) + t);
  332. o.value = t1;
  333. o.words32.w3 = 0;
  334. o.words32.w2 &= 0xf8000000;
  335. t1 = o.value;
  336. t2 = z_l - (((t1 - t) - dp_h[k]) - z_h);
  337. /* s (sign of result -ve**odd) = -1 else = 1 */
  338. s = one;
  339. if (((((uint32_t) hx >> 31) - 1) | (yisint - 1)) == 0)
  340. s = -one; /* (-ve)**(odd int) */
  341. /* split up y into y1+y2 and compute (y1+y2)*(t1+t2) */
  342. y1 = y;
  343. o.value = y1;
  344. o.words32.w3 = 0;
  345. o.words32.w2 &= 0xf8000000;
  346. y1 = o.value;
  347. p_l = (y - y1) * t1 + y * t2;
  348. p_h = y1 * t1;
  349. z = p_l + p_h;
  350. o.value = z;
  351. j = o.words32.w0;
  352. if (j >= 0x400d0000) /* z >= 16384 */
  353. {
  354. /* if z > 16384 */
  355. if (((j - 0x400d0000) | o.words32.w1 | o.words32.w2 | o.words32.w3) != 0)
  356. return s * huge * huge; /* overflow */
  357. else
  358. {
  359. if (p_l + ovt > z - p_h)
  360. return s * huge * huge; /* overflow */
  361. }
  362. }
  363. else if ((j & 0x7fffffff) >= 0x400d01b9) /* z <= -16495 */
  364. {
  365. /* z < -16495 */
  366. if (((j - 0xc00d01bc) | o.words32.w1 | o.words32.w2 | o.words32.w3)
  367. != 0)
  368. return s * tiny * tiny; /* underflow */
  369. else
  370. {
  371. if (p_l <= z - p_h)
  372. return s * tiny * tiny; /* underflow */
  373. }
  374. }
  375. /* compute 2**(p_h+p_l) */
  376. i = j & 0x7fffffff;
  377. k = (i >> 16) - 0x3fff;
  378. n = 0;
  379. if (i > 0x3ffe0000)
  380. { /* if |z| > 0.5, set n = [z+0.5] */
  381. n = floorq (z + 0.5Q);
  382. t = n;
  383. p_h -= t;
  384. }
  385. t = p_l + p_h;
  386. o.value = t;
  387. o.words32.w3 = 0;
  388. o.words32.w2 &= 0xf8000000;
  389. t = o.value;
  390. u = t * lg2_h;
  391. v = (p_l - (t - p_h)) * lg2 + t * lg2_l;
  392. z = u + v;
  393. w = v - (z - u);
  394. /* exp(z) */
  395. t = z * z;
  396. u = PN[0] + t * (PN[1] + t * (PN[2] + t * (PN[3] + t * PN[4])));
  397. v = PD[0] + t * (PD[1] + t * (PD[2] + t * (PD[3] + t)));
  398. t1 = z - t * u / v;
  399. r = (z * t1) / (t1 - two) - (w + z * w);
  400. z = one - (r - z);
  401. o.value = z;
  402. j = o.words32.w0;
  403. j += (n << 16);
  404. if ((j >> 16) <= 0)
  405. z = scalbnq (z, n); /* subnormal output */
  406. else
  407. {
  408. o.words32.w0 = j;
  409. z = o.value;
  410. }
  411. return s * z;
  412. }