e_powl.c 12 KB

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