powl.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. /* origin: OpenBSD /usr/src/lib/libm/src/ld80/e_powl.c */
  2. /*
  3. * Copyright (c) 2008 Stephen L. Moshier <[email protected]>
  4. *
  5. * Permission to use, copy, modify, and distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  10. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  12. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  14. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  15. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. /* powl.c
  18. *
  19. * Power function, long double precision
  20. *
  21. *
  22. * SYNOPSIS:
  23. *
  24. * long double x, y, z, powl();
  25. *
  26. * z = powl( x, y );
  27. *
  28. *
  29. * DESCRIPTION:
  30. *
  31. * Computes x raised to the yth power. Analytically,
  32. *
  33. * x**y = exp( y log(x) ).
  34. *
  35. * Following Cody and Waite, this program uses a lookup table
  36. * of 2**-i/32 and pseudo extended precision arithmetic to
  37. * obtain several extra bits of accuracy in both the logarithm
  38. * and the exponential.
  39. *
  40. *
  41. * ACCURACY:
  42. *
  43. * The relative error of pow(x,y) can be estimated
  44. * by y dl ln(2), where dl is the absolute error of
  45. * the internally computed base 2 logarithm. At the ends
  46. * of the approximation interval the logarithm equal 1/32
  47. * and its relative error is about 1 lsb = 1.1e-19. Hence
  48. * the predicted relative error in the result is 2.3e-21 y .
  49. *
  50. * Relative error:
  51. * arithmetic domain # trials peak rms
  52. *
  53. * IEEE +-1000 40000 2.8e-18 3.7e-19
  54. * .001 < x < 1000, with log(x) uniformly distributed.
  55. * -1000 < y < 1000, y uniformly distributed.
  56. *
  57. * IEEE 0,8700 60000 6.5e-18 1.0e-18
  58. * 0.99 < x < 1.01, 0 < y < 8700, uniformly distributed.
  59. *
  60. *
  61. * ERROR MESSAGES:
  62. *
  63. * message condition value returned
  64. * pow overflow x**y > MAXNUM INFINITY
  65. * pow underflow x**y < 1/MAXNUM 0.0
  66. * pow domain x<0 and y noninteger 0.0
  67. *
  68. */
  69. #include "libm.h"
  70. #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024
  71. long double powl(long double x, long double y)
  72. {
  73. return pow(x, y);
  74. }
  75. #elif LDBL_MANT_DIG == 64 && LDBL_MAX_EXP == 16384
  76. /* Table size */
  77. #define NXT 32
  78. /* log(1+x) = x - .5x^2 + x^3 * P(z)/Q(z)
  79. * on the domain 2^(-1/32) - 1 <= x <= 2^(1/32) - 1
  80. */
  81. static const long double P[] = {
  82. 8.3319510773868690346226E-4L,
  83. 4.9000050881978028599627E-1L,
  84. 1.7500123722550302671919E0L,
  85. 1.4000100839971580279335E0L,
  86. };
  87. static const long double Q[] = {
  88. /* 1.0000000000000000000000E0L,*/
  89. 5.2500282295834889175431E0L,
  90. 8.4000598057587009834666E0L,
  91. 4.2000302519914740834728E0L,
  92. };
  93. /* A[i] = 2^(-i/32), rounded to IEEE long double precision.
  94. * If i is even, A[i] + B[i/2] gives additional accuracy.
  95. */
  96. static const long double A[33] = {
  97. 1.0000000000000000000000E0L,
  98. 9.7857206208770013448287E-1L,
  99. 9.5760328069857364691013E-1L,
  100. 9.3708381705514995065011E-1L,
  101. 9.1700404320467123175367E-1L,
  102. 8.9735453750155359320742E-1L,
  103. 8.7812608018664974155474E-1L,
  104. 8.5930964906123895780165E-1L,
  105. 8.4089641525371454301892E-1L,
  106. 8.2287773907698242225554E-1L,
  107. 8.0524516597462715409607E-1L,
  108. 7.8799042255394324325455E-1L,
  109. 7.7110541270397041179298E-1L,
  110. 7.5458221379671136985669E-1L,
  111. 7.3841307296974965571198E-1L,
  112. 7.2259040348852331001267E-1L,
  113. 7.0710678118654752438189E-1L,
  114. 6.9195494098191597746178E-1L,
  115. 6.7712777346844636413344E-1L,
  116. 6.6261832157987064729696E-1L,
  117. 6.4841977732550483296079E-1L,
  118. 6.3452547859586661129850E-1L,
  119. 6.2092890603674202431705E-1L,
  120. 6.0762367999023443907803E-1L,
  121. 5.9460355750136053334378E-1L,
  122. 5.8186242938878875689693E-1L,
  123. 5.6939431737834582684856E-1L,
  124. 5.5719337129794626814472E-1L,
  125. 5.4525386633262882960438E-1L,
  126. 5.3357020033841180906486E-1L,
  127. 5.2213689121370692017331E-1L,
  128. 5.1094857432705833910408E-1L,
  129. 5.0000000000000000000000E-1L,
  130. };
  131. static const long double B[17] = {
  132. 0.0000000000000000000000E0L,
  133. 2.6176170809902549338711E-20L,
  134. -1.0126791927256478897086E-20L,
  135. 1.3438228172316276937655E-21L,
  136. 1.2207982955417546912101E-20L,
  137. -6.3084814358060867200133E-21L,
  138. 1.3164426894366316434230E-20L,
  139. -1.8527916071632873716786E-20L,
  140. 1.8950325588932570796551E-20L,
  141. 1.5564775779538780478155E-20L,
  142. 6.0859793637556860974380E-21L,
  143. -2.0208749253662532228949E-20L,
  144. 1.4966292219224761844552E-20L,
  145. 3.3540909728056476875639E-21L,
  146. -8.6987564101742849540743E-22L,
  147. -1.2327176863327626135542E-20L,
  148. 0.0000000000000000000000E0L,
  149. };
  150. /* 2^x = 1 + x P(x),
  151. * on the interval -1/32 <= x <= 0
  152. */
  153. static const long double R[] = {
  154. 1.5089970579127659901157E-5L,
  155. 1.5402715328927013076125E-4L,
  156. 1.3333556028915671091390E-3L,
  157. 9.6181291046036762031786E-3L,
  158. 5.5504108664798463044015E-2L,
  159. 2.4022650695910062854352E-1L,
  160. 6.9314718055994530931447E-1L,
  161. };
  162. #define MEXP (NXT*16384.0L)
  163. /* The following if denormal numbers are supported, else -MEXP: */
  164. #define MNEXP (-NXT*(16384.0L+64.0L))
  165. /* log2(e) - 1 */
  166. #define LOG2EA 0.44269504088896340735992L
  167. #define F W
  168. #define Fa Wa
  169. #define Fb Wb
  170. #define G W
  171. #define Ga Wa
  172. #define Gb u
  173. #define H W
  174. #define Ha Wb
  175. #define Hb Wb
  176. static const long double MAXLOGL = 1.1356523406294143949492E4L;
  177. static const long double MINLOGL = -1.13994985314888605586758E4L;
  178. static const long double LOGE2L = 6.9314718055994530941723E-1L;
  179. static const long double huge = 0x1p10000L;
  180. /* XXX Prevent gcc from erroneously constant folding this. */
  181. static const volatile long double twom10000 = 0x1p-10000L;
  182. static long double reducl(long double);
  183. static long double powil(long double, int);
  184. long double powl(long double x, long double y)
  185. {
  186. /* double F, Fa, Fb, G, Ga, Gb, H, Ha, Hb */
  187. int i, nflg, iyflg, yoddint;
  188. long e;
  189. volatile long double z=0;
  190. long double w=0, W=0, Wa=0, Wb=0, ya=0, yb=0, u=0;
  191. /* make sure no invalid exception is raised by nan comparision */
  192. if (isnan(x)) {
  193. if (!isnan(y) && y == 0.0)
  194. return 1.0;
  195. return x;
  196. }
  197. if (isnan(y)) {
  198. if (x == 1.0)
  199. return 1.0;
  200. return y;
  201. }
  202. if (x == 1.0)
  203. return 1.0; /* 1**y = 1, even if y is nan */
  204. if (x == -1.0 && !isfinite(y))
  205. return 1.0; /* -1**inf = 1 */
  206. if (y == 0.0)
  207. return 1.0; /* x**0 = 1, even if x is nan */
  208. if (y == 1.0)
  209. return x;
  210. if (y >= LDBL_MAX) {
  211. if (x > 1.0 || x < -1.0)
  212. return INFINITY;
  213. if (x != 0.0)
  214. return 0.0;
  215. }
  216. if (y <= -LDBL_MAX) {
  217. if (x > 1.0 || x < -1.0)
  218. return 0.0;
  219. if (x != 0.0)
  220. return INFINITY;
  221. }
  222. if (x >= LDBL_MAX) {
  223. if (y > 0.0)
  224. return INFINITY;
  225. return 0.0;
  226. }
  227. w = floorl(y);
  228. /* Set iyflg to 1 if y is an integer. */
  229. iyflg = 0;
  230. if (w == y)
  231. iyflg = 1;
  232. /* Test for odd integer y. */
  233. yoddint = 0;
  234. if (iyflg) {
  235. ya = fabsl(y);
  236. ya = floorl(0.5 * ya);
  237. yb = 0.5 * fabsl(w);
  238. if( ya != yb )
  239. yoddint = 1;
  240. }
  241. if (x <= -LDBL_MAX) {
  242. if (y > 0.0) {
  243. if (yoddint)
  244. return -INFINITY;
  245. return INFINITY;
  246. }
  247. if (y < 0.0) {
  248. if (yoddint)
  249. return -0.0;
  250. return 0.0;
  251. }
  252. }
  253. nflg = 0; /* (x<0)**(odd int) */
  254. if (x <= 0.0) {
  255. if (x == 0.0) {
  256. if (y < 0.0) {
  257. if (signbit(x) && yoddint)
  258. /* (-0.0)**(-odd int) = -inf, divbyzero */
  259. return -1.0/0.0;
  260. /* (+-0.0)**(negative) = inf, divbyzero */
  261. return 1.0/0.0;
  262. }
  263. if (signbit(x) && yoddint)
  264. return -0.0;
  265. return 0.0;
  266. }
  267. if (iyflg == 0)
  268. return (x - x) / (x - x); /* (x<0)**(non-int) is NaN */
  269. /* (x<0)**(integer) */
  270. if (yoddint)
  271. nflg = 1; /* negate result */
  272. x = -x;
  273. }
  274. /* (+integer)**(integer) */
  275. if (iyflg && floorl(x) == x && fabsl(y) < 32768.0) {
  276. w = powil(x, (int)y);
  277. return nflg ? -w : w;
  278. }
  279. /* separate significand from exponent */
  280. x = frexpl(x, &i);
  281. e = i;
  282. /* find significand in antilog table A[] */
  283. i = 1;
  284. if (x <= A[17])
  285. i = 17;
  286. if (x <= A[i+8])
  287. i += 8;
  288. if (x <= A[i+4])
  289. i += 4;
  290. if (x <= A[i+2])
  291. i += 2;
  292. if (x >= A[1])
  293. i = -1;
  294. i += 1;
  295. /* Find (x - A[i])/A[i]
  296. * in order to compute log(x/A[i]):
  297. *
  298. * log(x) = log( a x/a ) = log(a) + log(x/a)
  299. *
  300. * log(x/a) = log(1+v), v = x/a - 1 = (x-a)/a
  301. */
  302. x -= A[i];
  303. x -= B[i/2];
  304. x /= A[i];
  305. /* rational approximation for log(1+v):
  306. *
  307. * log(1+v) = v - v**2/2 + v**3 P(v) / Q(v)
  308. */
  309. z = x*x;
  310. w = x * (z * __polevll(x, P, 3) / __p1evll(x, Q, 3));
  311. w = w - 0.5*z;
  312. /* Convert to base 2 logarithm:
  313. * multiply by log2(e) = 1 + LOG2EA
  314. */
  315. z = LOG2EA * w;
  316. z += w;
  317. z += LOG2EA * x;
  318. z += x;
  319. /* Compute exponent term of the base 2 logarithm. */
  320. w = -i;
  321. w /= NXT;
  322. w += e;
  323. /* Now base 2 log of x is w + z. */
  324. /* Multiply base 2 log by y, in extended precision. */
  325. /* separate y into large part ya
  326. * and small part yb less than 1/NXT
  327. */
  328. ya = reducl(y);
  329. yb = y - ya;
  330. /* (w+z)(ya+yb)
  331. * = w*ya + w*yb + z*y
  332. */
  333. F = z * y + w * yb;
  334. Fa = reducl(F);
  335. Fb = F - Fa;
  336. G = Fa + w * ya;
  337. Ga = reducl(G);
  338. Gb = G - Ga;
  339. H = Fb + Gb;
  340. Ha = reducl(H);
  341. w = (Ga + Ha) * NXT;
  342. /* Test the power of 2 for overflow */
  343. if (w > MEXP)
  344. return huge * huge; /* overflow */
  345. if (w < MNEXP)
  346. return twom10000 * twom10000; /* underflow */
  347. e = w;
  348. Hb = H - Ha;
  349. if (Hb > 0.0) {
  350. e += 1;
  351. Hb -= 1.0/NXT; /*0.0625L;*/
  352. }
  353. /* Now the product y * log2(x) = Hb + e/NXT.
  354. *
  355. * Compute base 2 exponential of Hb,
  356. * where -0.0625 <= Hb <= 0.
  357. */
  358. z = Hb * __polevll(Hb, R, 6); /* z = 2**Hb - 1 */
  359. /* Express e/NXT as an integer plus a negative number of (1/NXT)ths.
  360. * Find lookup table entry for the fractional power of 2.
  361. */
  362. if (e < 0)
  363. i = 0;
  364. else
  365. i = 1;
  366. i = e/NXT + i;
  367. e = NXT*i - e;
  368. w = A[e];
  369. z = w * z; /* 2**-e * ( 1 + (2**Hb-1) ) */
  370. z = z + w;
  371. z = scalbnl(z, i); /* multiply by integer power of 2 */
  372. if (nflg)
  373. z = -z;
  374. return z;
  375. }
  376. /* Find a multiple of 1/NXT that is within 1/NXT of x. */
  377. static long double reducl(long double x)
  378. {
  379. long double t;
  380. t = x * NXT;
  381. t = floorl(t);
  382. t = t / NXT;
  383. return t;
  384. }
  385. /*
  386. * Positive real raised to integer power, long double precision
  387. *
  388. *
  389. * SYNOPSIS:
  390. *
  391. * long double x, y, powil();
  392. * int n;
  393. *
  394. * y = powil( x, n );
  395. *
  396. *
  397. * DESCRIPTION:
  398. *
  399. * Returns argument x>0 raised to the nth power.
  400. * The routine efficiently decomposes n as a sum of powers of
  401. * two. The desired power is a product of two-to-the-kth
  402. * powers of x. Thus to compute the 32767 power of x requires
  403. * 28 multiplications instead of 32767 multiplications.
  404. *
  405. *
  406. * ACCURACY:
  407. *
  408. * Relative error:
  409. * arithmetic x domain n domain # trials peak rms
  410. * IEEE .001,1000 -1022,1023 50000 4.3e-17 7.8e-18
  411. * IEEE 1,2 -1022,1023 20000 3.9e-17 7.6e-18
  412. * IEEE .99,1.01 0,8700 10000 3.6e-16 7.2e-17
  413. *
  414. * Returns MAXNUM on overflow, zero on underflow.
  415. */
  416. static long double powil(long double x, int nn)
  417. {
  418. long double ww, y;
  419. long double s;
  420. int n, e, sign, lx;
  421. if (nn == 0)
  422. return 1.0;
  423. if (nn < 0) {
  424. sign = -1;
  425. n = -nn;
  426. } else {
  427. sign = 1;
  428. n = nn;
  429. }
  430. /* Overflow detection */
  431. /* Calculate approximate logarithm of answer */
  432. s = x;
  433. s = frexpl( s, &lx);
  434. e = (lx - 1)*n;
  435. if ((e == 0) || (e > 64) || (e < -64)) {
  436. s = (s - 7.0710678118654752e-1L) / (s + 7.0710678118654752e-1L);
  437. s = (2.9142135623730950L * s - 0.5 + lx) * nn * LOGE2L;
  438. } else {
  439. s = LOGE2L * e;
  440. }
  441. if (s > MAXLOGL)
  442. return huge * huge; /* overflow */
  443. if (s < MINLOGL)
  444. return twom10000 * twom10000; /* underflow */
  445. /* Handle tiny denormal answer, but with less accuracy
  446. * since roundoff error in 1.0/x will be amplified.
  447. * The precise demarcation should be the gradual underflow threshold.
  448. */
  449. if (s < -MAXLOGL+2.0) {
  450. x = 1.0/x;
  451. sign = -sign;
  452. }
  453. /* First bit of the power */
  454. if (n & 1)
  455. y = x;
  456. else
  457. y = 1.0;
  458. ww = x;
  459. n >>= 1;
  460. while (n) {
  461. ww = ww * ww; /* arg to the 2-to-the-kth power */
  462. if (n & 1) /* if that bit is set, then include in product */
  463. y *= ww;
  464. n >>= 1;
  465. }
  466. if (sign < 0)
  467. y = 1.0/y;
  468. return y;
  469. }
  470. #endif