Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. /*                                                      powl.c
  2.  *
  3.  *      Power function, long double precision
  4.  *
  5.  *
  6.  *
  7.  * SYNOPSIS:
  8.  *
  9.  * long double x, y, z, powl();
  10.  *
  11.  * z = powl( x, y );
  12.  *
  13.  *
  14.  *
  15.  * DESCRIPTION:
  16.  *
  17.  * Computes x raised to the yth power.  Analytically,
  18.  *
  19.  *      x**y  =  exp( y log(x) ).
  20.  *
  21.  * Following Cody and Waite, this program uses a lookup table
  22.  * of 2**-i/32 and pseudo extended precision arithmetic to
  23.  * obtain several extra bits of accuracy in both the logarithm
  24.  * and the exponential.
  25.  *
  26.  *
  27.  *
  28.  * ACCURACY:
  29.  *
  30.  * The relative error of pow(x,y) can be estimated
  31.  * by   y dl ln(2),   where dl is the absolute error of
  32.  * the internally computed base 2 logarithm.  At the ends
  33.  * of the approximation interval the logarithm equal 1/32
  34.  * and its relative error is about 1 lsb = 1.1e-19.  Hence
  35.  * the predicted relative error in the result is 2.3e-21 y .
  36.  *
  37.  *                      Relative error:
  38.  * arithmetic   domain     # trials      peak         rms
  39.  *
  40.  *    IEEE     +-1000       40000      2.8e-18      3.7e-19
  41.  * .001 < x < 1000, with log(x) uniformly distributed.
  42.  * -1000 < y < 1000, y uniformly distributed.
  43.  *
  44.  *    IEEE     0,8700       60000      6.5e-18      1.0e-18
  45.  * 0.99 < x < 1.01, 0 < y < 8700, uniformly distributed.
  46.  *
  47.  *
  48.  * ERROR MESSAGES:
  49.  *
  50.  *   message         condition      value returned
  51.  * pow overflow     x**y > MAXNUM      INFINITY
  52.  * pow underflow   x**y < 1/MAXNUM       0.0
  53.  * pow domain      x<0 and y noninteger  0.0
  54.  *
  55.  */
  56. /*
  57. Cephes Math Library Release 2.7:  May, 1998
  58. Copyright 1984, 1991, 1998 by Stephen L. Moshier
  59. */
  60.  
  61. /*
  62. Modified for mingw
  63. 2002-07-22 Danny Smith <dannysmith@users.sourceforge.net>
  64. */
  65.  
  66. #ifdef __MINGW32__
  67. #include "cephes_mconf.h"
  68. #else
  69. #include "mconf.h"
  70.  
  71. static char fname[] = {"powl"};
  72. #endif
  73.  
  74. #ifndef _SET_ERRNO
  75. #define _SET_ERRNO(x)
  76. #endif
  77.  
  78.  
  79. /* Table size */
  80. #define NXT 32
  81. /* log2(Table size) */
  82. #define LNXT 5
  83.  
  84. #ifdef UNK
  85. /* log(1+x) =  x - .5x^2 + x^3 *  P(z)/Q(z)
  86.  * on the domain  2^(-1/32) - 1  <=  x  <=  2^(1/32) - 1
  87.  */
  88. static long double P[] = {
  89.  8.3319510773868690346226E-4L,
  90.  4.9000050881978028599627E-1L,
  91.  1.7500123722550302671919E0L,
  92.  1.4000100839971580279335E0L,
  93. };
  94. static long double Q[] = {
  95. /* 1.0000000000000000000000E0L,*/
  96.  5.2500282295834889175431E0L,
  97.  8.4000598057587009834666E0L,
  98.  4.2000302519914740834728E0L,
  99. };
  100. /* A[i] = 2^(-i/32), rounded to IEEE long double precision.
  101.  * If i is even, A[i] + B[i/2] gives additional accuracy.
  102.  */
  103. static long double A[33] = {
  104.  1.0000000000000000000000E0L,
  105.  9.7857206208770013448287E-1L,
  106.  9.5760328069857364691013E-1L,
  107.  9.3708381705514995065011E-1L,
  108.  9.1700404320467123175367E-1L,
  109.  8.9735453750155359320742E-1L,
  110.  8.7812608018664974155474E-1L,
  111.  8.5930964906123895780165E-1L,
  112.  8.4089641525371454301892E-1L,
  113.  8.2287773907698242225554E-1L,
  114.  8.0524516597462715409607E-1L,
  115.  7.8799042255394324325455E-1L,
  116.  7.7110541270397041179298E-1L,
  117.  7.5458221379671136985669E-1L,
  118.  7.3841307296974965571198E-1L,
  119.  7.2259040348852331001267E-1L,
  120.  7.0710678118654752438189E-1L,
  121.  6.9195494098191597746178E-1L,
  122.  6.7712777346844636413344E-1L,
  123.  6.6261832157987064729696E-1L,
  124.  6.4841977732550483296079E-1L,
  125.  6.3452547859586661129850E-1L,
  126.  6.2092890603674202431705E-1L,
  127.  6.0762367999023443907803E-1L,
  128.  5.9460355750136053334378E-1L,
  129.  5.8186242938878875689693E-1L,
  130.  5.6939431737834582684856E-1L,
  131.  5.5719337129794626814472E-1L,
  132.  5.4525386633262882960438E-1L,
  133.  5.3357020033841180906486E-1L,
  134.  5.2213689121370692017331E-1L,
  135.  5.1094857432705833910408E-1L,
  136.  5.0000000000000000000000E-1L,
  137. };
  138. static long double B[17] = {
  139.  0.0000000000000000000000E0L,
  140.  2.6176170809902549338711E-20L,
  141. -1.0126791927256478897086E-20L,
  142.  1.3438228172316276937655E-21L,
  143.  1.2207982955417546912101E-20L,
  144. -6.3084814358060867200133E-21L,
  145.  1.3164426894366316434230E-20L,
  146. -1.8527916071632873716786E-20L,
  147.  1.8950325588932570796551E-20L,
  148.  1.5564775779538780478155E-20L,
  149.  6.0859793637556860974380E-21L,
  150. -2.0208749253662532228949E-20L,
  151.  1.4966292219224761844552E-20L,
  152.  3.3540909728056476875639E-21L,
  153. -8.6987564101742849540743E-22L,
  154. -1.2327176863327626135542E-20L,
  155.  0.0000000000000000000000E0L,
  156. };
  157.  
  158. /* 2^x = 1 + x P(x),
  159.  * on the interval -1/32 <= x <= 0
  160.  */
  161. static long double R[] = {
  162.  1.5089970579127659901157E-5L,
  163.  1.5402715328927013076125E-4L,
  164.  1.3333556028915671091390E-3L,
  165.  9.6181291046036762031786E-3L,
  166.  5.5504108664798463044015E-2L,
  167.  2.4022650695910062854352E-1L,
  168.  6.9314718055994530931447E-1L,
  169. };
  170.  
  171. #define douba(k) A[k]
  172. #define doubb(k) B[k]
  173. #define MEXP (NXT*16384.0L)
  174. /* The following if denormal numbers are supported, else -MEXP: */
  175. #ifdef DENORMAL
  176. #define MNEXP (-NXT*(16384.0L+64.0L))
  177. #else
  178. #define MNEXP (-NXT*16384.0L)
  179. #endif
  180. /* log2(e) - 1 */
  181. #define LOG2EA 0.44269504088896340735992L
  182. #endif
  183.  
  184.  
  185. #ifdef IBMPC
  186. static const unsigned short P[] = {
  187. 0xb804,0xa8b7,0xc6f4,0xda6a,0x3ff4, XPD
  188. 0x7de9,0xcf02,0x58c0,0xfae1,0x3ffd, XPD
  189. 0x405a,0x3722,0x67c9,0xe000,0x3fff, XPD
  190. 0xcd99,0x6b43,0x87ca,0xb333,0x3fff, XPD
  191. };
  192. static const unsigned short Q[] = {
  193. /* 0x0000,0x0000,0x0000,0x8000,0x3fff, */
  194. 0x6307,0xa469,0x3b33,0xa800,0x4001, XPD
  195. 0xfec2,0x62d7,0xa51c,0x8666,0x4002, XPD
  196. 0xda32,0xd072,0xa5d7,0x8666,0x4001, XPD
  197. };
  198. static const unsigned short A[] = {
  199. 0x0000,0x0000,0x0000,0x8000,0x3fff, XPD
  200. 0x033a,0x722a,0xb2db,0xfa83,0x3ffe, XPD
  201. 0xcc2c,0x2486,0x7d15,0xf525,0x3ffe, XPD
  202. 0xf5cb,0xdcda,0xb99b,0xefe4,0x3ffe, XPD
  203. 0x392f,0xdd24,0xc6e7,0xeac0,0x3ffe, XPD
  204. 0x48a8,0x7c83,0x06e7,0xe5b9,0x3ffe, XPD
  205. 0xe111,0x2a94,0xdeec,0xe0cc,0x3ffe, XPD
  206. 0x3755,0xdaf2,0xb797,0xdbfb,0x3ffe, XPD
  207. 0x6af4,0xd69d,0xfcca,0xd744,0x3ffe, XPD
  208. 0xe45a,0xf12a,0x1d91,0xd2a8,0x3ffe, XPD
  209. 0x80e4,0x1f84,0x8c15,0xce24,0x3ffe, XPD
  210. 0x27a3,0x6e2f,0xbd86,0xc9b9,0x3ffe, XPD
  211. 0xdadd,0x5506,0x2a11,0xc567,0x3ffe, XPD
  212. 0x9456,0x6670,0x4cca,0xc12c,0x3ffe, XPD
  213. 0x36bf,0x580c,0xa39f,0xbd08,0x3ffe, XPD
  214. 0x9ee9,0x62fb,0xaf47,0xb8fb,0x3ffe, XPD
  215. 0x6484,0xf9de,0xf333,0xb504,0x3ffe, XPD
  216. 0x2590,0xd2ac,0xf581,0xb123,0x3ffe, XPD
  217. 0x4ac6,0x42a1,0x3eea,0xad58,0x3ffe, XPD
  218. 0x0ef8,0xea7c,0x5ab4,0xa9a1,0x3ffe, XPD
  219. 0x38ea,0xb151,0xd6a9,0xa5fe,0x3ffe, XPD
  220. 0x6819,0x0c49,0x4303,0xa270,0x3ffe, XPD
  221. 0x11ae,0x91a1,0x3260,0x9ef5,0x3ffe, XPD
  222. 0x5539,0xd54e,0x39b9,0x9b8d,0x3ffe, XPD
  223. 0xa96f,0x8db8,0xf051,0x9837,0x3ffe, XPD
  224. 0x0961,0xfef7,0xefa8,0x94f4,0x3ffe, XPD
  225. 0xc336,0xab11,0xd373,0x91c3,0x3ffe, XPD
  226. 0x53c0,0x45cd,0x398b,0x8ea4,0x3ffe, XPD
  227. 0xd6e7,0xea8b,0xc1e3,0x8b95,0x3ffe, XPD
  228. 0x8527,0x92da,0x0e80,0x8898,0x3ffe, XPD
  229. 0x7b15,0xcc48,0xc367,0x85aa,0x3ffe, XPD
  230. 0xa1d7,0xac2b,0x8698,0x82cd,0x3ffe, XPD
  231. 0x0000,0x0000,0x0000,0x8000,0x3ffe, XPD
  232. };
  233. static const unsigned short B[] = {
  234. 0x0000,0x0000,0x0000,0x0000,0x0000, XPD
  235. 0x1f87,0xdb30,0x18f5,0xf73a,0x3fbd, XPD
  236. 0xac15,0x3e46,0x2932,0xbf4a,0xbfbc, XPD
  237. 0x7944,0xba66,0xa091,0xcb12,0x3fb9, XPD
  238. 0xff78,0x40b4,0x2ee6,0xe69a,0x3fbc, XPD
  239. 0xc895,0x5069,0xe383,0xee53,0xbfbb, XPD
  240. 0x7cde,0x9376,0x4325,0xf8ab,0x3fbc, XPD
  241. 0xa10c,0x25e0,0xc093,0xaefd,0xbfbd, XPD
  242. 0x7d3e,0xea95,0x1366,0xb2fb,0x3fbd, XPD
  243. 0x5d89,0xeb34,0x5191,0x9301,0x3fbd, XPD
  244. 0x80d9,0xb883,0xfb10,0xe5eb,0x3fbb, XPD
  245. 0x045d,0x288c,0xc1ec,0xbedd,0xbfbd, XPD
  246. 0xeded,0x5c85,0x4630,0x8d5a,0x3fbd, XPD
  247. 0x9d82,0xe5ac,0x8e0a,0xfd6d,0x3fba, XPD
  248. 0x6dfd,0xeb58,0xaf14,0x8373,0xbfb9, XPD
  249. 0xf938,0x7aac,0x91cf,0xe8da,0xbfbc, XPD
  250. 0x0000,0x0000,0x0000,0x0000,0x0000, XPD
  251. };
  252. static const unsigned short R[] = {
  253. 0xa69b,0x530e,0xee1d,0xfd2a,0x3fee, XPD
  254. 0xc746,0x8e7e,0x5960,0xa182,0x3ff2, XPD
  255. 0x63b6,0xadda,0xfd6a,0xaec3,0x3ff5, XPD
  256. 0xc104,0xfd99,0x5b7c,0x9d95,0x3ff8, XPD
  257. 0xe05e,0x249d,0x46b8,0xe358,0x3ffa, XPD
  258. 0x5d1d,0x162c,0xeffc,0xf5fd,0x3ffc, XPD
  259. 0x79aa,0xd1cf,0x17f7,0xb172,0x3ffe, XPD
  260. };
  261.  
  262. /* 10 byte sizes versus 12 byte */
  263. #define douba(k) (*(long double *)(&A[(sizeof( long double )/2)*(k)]))
  264. #define doubb(k) (*(long double *)(&B[(sizeof( long double )/2)*(k)]))
  265. #define MEXP (NXT*16384.0L)
  266. #ifdef DENORMAL
  267. #define MNEXP (-NXT*(16384.0L+64.0L))
  268. #else
  269. #define MNEXP (-NXT*16384.0L)
  270. #endif
  271. static const
  272. union
  273. {
  274.   unsigned short L[6];
  275.   long double ld;
  276. } log2ea = {{0xc2ef,0x705f,0xeca5,0xe2a8,0x3ffd, XPD}};
  277.  
  278. #define LOG2EA (log2ea.ld)
  279. /*
  280. #define LOG2EA 0.44269504088896340735992L
  281. */
  282. #endif
  283.  
  284. #ifdef MIEEE
  285. static long P[] = {
  286. 0x3ff40000,0xda6ac6f4,0xa8b7b804,
  287. 0x3ffd0000,0xfae158c0,0xcf027de9,
  288. 0x3fff0000,0xe00067c9,0x3722405a,
  289. 0x3fff0000,0xb33387ca,0x6b43cd99,
  290. };
  291. static long Q[] = {
  292. /* 0x3fff0000,0x80000000,0x00000000, */
  293. 0x40010000,0xa8003b33,0xa4696307,
  294. 0x40020000,0x8666a51c,0x62d7fec2,
  295. 0x40010000,0x8666a5d7,0xd072da32,
  296. };
  297. static long A[] = {
  298. 0x3fff0000,0x80000000,0x00000000,
  299. 0x3ffe0000,0xfa83b2db,0x722a033a,
  300. 0x3ffe0000,0xf5257d15,0x2486cc2c,
  301. 0x3ffe0000,0xefe4b99b,0xdcdaf5cb,
  302. 0x3ffe0000,0xeac0c6e7,0xdd24392f,
  303. 0x3ffe0000,0xe5b906e7,0x7c8348a8,
  304. 0x3ffe0000,0xe0ccdeec,0x2a94e111,
  305. 0x3ffe0000,0xdbfbb797,0xdaf23755,
  306. 0x3ffe0000,0xd744fcca,0xd69d6af4,
  307. 0x3ffe0000,0xd2a81d91,0xf12ae45a,
  308. 0x3ffe0000,0xce248c15,0x1f8480e4,
  309. 0x3ffe0000,0xc9b9bd86,0x6e2f27a3,
  310. 0x3ffe0000,0xc5672a11,0x5506dadd,
  311. 0x3ffe0000,0xc12c4cca,0x66709456,
  312. 0x3ffe0000,0xbd08a39f,0x580c36bf,
  313. 0x3ffe0000,0xb8fbaf47,0x62fb9ee9,
  314. 0x3ffe0000,0xb504f333,0xf9de6484,
  315. 0x3ffe0000,0xb123f581,0xd2ac2590,
  316. 0x3ffe0000,0xad583eea,0x42a14ac6,
  317. 0x3ffe0000,0xa9a15ab4,0xea7c0ef8,
  318. 0x3ffe0000,0xa5fed6a9,0xb15138ea,
  319. 0x3ffe0000,0xa2704303,0x0c496819,
  320. 0x3ffe0000,0x9ef53260,0x91a111ae,
  321. 0x3ffe0000,0x9b8d39b9,0xd54e5539,
  322. 0x3ffe0000,0x9837f051,0x8db8a96f,
  323. 0x3ffe0000,0x94f4efa8,0xfef70961,
  324. 0x3ffe0000,0x91c3d373,0xab11c336,
  325. 0x3ffe0000,0x8ea4398b,0x45cd53c0,
  326. 0x3ffe0000,0x8b95c1e3,0xea8bd6e7,
  327. 0x3ffe0000,0x88980e80,0x92da8527,
  328. 0x3ffe0000,0x85aac367,0xcc487b15,
  329. 0x3ffe0000,0x82cd8698,0xac2ba1d7,
  330. 0x3ffe0000,0x80000000,0x00000000,
  331. };
  332. static long B[51] = {
  333. 0x00000000,0x00000000,0x00000000,
  334. 0x3fbd0000,0xf73a18f5,0xdb301f87,
  335. 0xbfbc0000,0xbf4a2932,0x3e46ac15,
  336. 0x3fb90000,0xcb12a091,0xba667944,
  337. 0x3fbc0000,0xe69a2ee6,0x40b4ff78,
  338. 0xbfbb0000,0xee53e383,0x5069c895,
  339. 0x3fbc0000,0xf8ab4325,0x93767cde,
  340. 0xbfbd0000,0xaefdc093,0x25e0a10c,
  341. 0x3fbd0000,0xb2fb1366,0xea957d3e,
  342. 0x3fbd0000,0x93015191,0xeb345d89,
  343. 0x3fbb0000,0xe5ebfb10,0xb88380d9,
  344. 0xbfbd0000,0xbeddc1ec,0x288c045d,
  345. 0x3fbd0000,0x8d5a4630,0x5c85eded,
  346. 0x3fba0000,0xfd6d8e0a,0xe5ac9d82,
  347. 0xbfb90000,0x8373af14,0xeb586dfd,
  348. 0xbfbc0000,0xe8da91cf,0x7aacf938,
  349. 0x00000000,0x00000000,0x00000000,
  350. };
  351. static long R[] = {
  352. 0x3fee0000,0xfd2aee1d,0x530ea69b,
  353. 0x3ff20000,0xa1825960,0x8e7ec746,
  354. 0x3ff50000,0xaec3fd6a,0xadda63b6,
  355. 0x3ff80000,0x9d955b7c,0xfd99c104,
  356. 0x3ffa0000,0xe35846b8,0x249de05e,
  357. 0x3ffc0000,0xf5fdeffc,0x162c5d1d,
  358. 0x3ffe0000,0xb17217f7,0xd1cf79aa,
  359. };
  360.  
  361. #define douba(k) (*(long double *)&A[3*(k)])
  362. #define doubb(k) (*(long double *)&B[3*(k)])
  363. #define MEXP (NXT*16384.0L)
  364. #ifdef DENORMAL
  365. #define MNEXP (-NXT*(16384.0L+64.0L))
  366. #else
  367. #define MNEXP (-NXT*16382.0L)
  368. #endif
  369. static long L[3] = {0x3ffd0000,0xe2a8eca5,0x705fc2ef};
  370. #define LOG2EA (*(long double *)(&L[0]))
  371. #endif
  372.  
  373.  
  374. #define F W
  375. #define Fa Wa
  376. #define Fb Wb
  377. #define G W
  378. #define Ga Wa
  379. #define Gb u
  380. #define H W
  381. #define Ha Wb
  382. #define Hb Wb
  383.  
  384. #ifndef __MINGW32__
  385. extern long double MAXNUML;
  386. #endif
  387.  
  388. static VOLATILE long double z;
  389. static long double w, W, Wa, Wb, ya, yb, u;
  390.  
  391. #ifdef __MINGW32__
  392. static __inline__ long double reducl( long double );
  393. extern long double __powil ( long double, int );
  394. extern long double powl ( long double x, long double y);
  395. #else
  396. #ifdef ANSIPROT
  397. extern long double floorl ( long double );
  398. extern long double fabsl ( long double );
  399. extern long double frexpl ( long double, int * );
  400. extern long double ldexpl ( long double, int );
  401. extern long double polevll ( long double, void *, int );
  402. extern long double p1evll ( long double, void *, int );
  403. extern long double __powil ( long double, int );
  404. extern int isnanl ( long double );
  405. extern int isfinitel ( long double );
  406. static long double reducl( long double );
  407. extern int signbitl ( long double );
  408. #else
  409. long double floorl(), fabsl(), frexpl(), ldexpl();
  410. long double polevll(), p1evll(), __powil();
  411. static long double reducl();
  412. int isnanl(), isfinitel(), signbitl();
  413. #endif  /* __MINGW32__ */
  414.  
  415. #ifdef INFINITIES
  416. extern long double INFINITYL;
  417. #else
  418. #define INFINITYL MAXNUML
  419. #endif
  420.  
  421. #ifdef NANS
  422. extern long double NANL;
  423. #endif
  424. #ifdef MINUSZERO
  425. extern long double NEGZEROL;
  426. #endif
  427.  
  428. #endif /* __MINGW32__ */
  429.  
  430. #ifdef __MINGW32__
  431.  
  432. /* No error checking. We handle Infs and zeros ourselves.  */
  433. static __inline__ long double
  434. __fast_ldexpl (long double x, int expn)
  435. {
  436.   long double res;
  437.   __asm__ ("fscale"
  438.             : "=t" (res)
  439.             : "0" (x), "u" ((long double) expn));
  440.   return res;
  441. }
  442.  
  443. #define ldexpl  __fast_ldexpl
  444.  
  445. #endif
  446.  
  447.  
  448. long double powl( x, y )
  449. long double x, y;
  450. {
  451. /* double F, Fa, Fb, G, Ga, Gb, H, Ha, Hb */
  452. int i, nflg, iyflg, yoddint;
  453. long e;
  454.  
  455. if( y == 0.0L )
  456.         return( 1.0L );
  457.  
  458. #ifdef NANS
  459. if( isnanl(x) )
  460.         {
  461.         _SET_ERRNO (EDOM);
  462.         return( x );
  463.         }
  464. if( isnanl(y) )
  465.         {
  466.         _SET_ERRNO (EDOM);
  467.         return( y );
  468.         }
  469. #endif
  470.  
  471. if( y == 1.0L )
  472.         return( x );
  473.  
  474. if( isinfl(y) && (x == -1.0L || x == 1.0L) )
  475.         return( y );
  476.  
  477. if( x == 1.0L )
  478.         return( 1.0L );
  479.  
  480. if( y >= MAXNUML )
  481.         {
  482.         _SET_ERRNO (ERANGE);
  483. #ifdef INFINITIES
  484.         if( x > 1.0L )
  485.                 return( INFINITYL );
  486. #else
  487.         if( x > 1.0L )
  488.                 return( MAXNUML );
  489. #endif
  490.         if( x > 0.0L && x < 1.0L )
  491.                 return( 0.0L );
  492. #ifdef INFINITIES
  493.         if( x < -1.0L )
  494.                 return( INFINITYL );
  495. #else
  496.         if( x < -1.0L )
  497.                 return( MAXNUML );
  498. #endif
  499.         if( x > -1.0L && x < 0.0L )
  500.                 return( 0.0L );
  501.         }
  502. if( y <= -MAXNUML )
  503.         {
  504.         _SET_ERRNO (ERANGE);
  505.         if( x > 1.0L )
  506.                 return( 0.0L );
  507. #ifdef INFINITIES
  508.         if( x > 0.0L && x < 1.0L )
  509.                 return( INFINITYL );
  510. #else
  511.         if( x > 0.0L && x < 1.0L )
  512.                 return( MAXNUML );
  513. #endif
  514.         if( x < -1.0L )
  515.                 return( 0.0L );
  516. #ifdef INFINITIES
  517.         if( x > -1.0L && x < 0.0L )
  518.                 return( INFINITYL );
  519. #else
  520.         if( x > -1.0L && x < 0.0L )
  521.                 return( MAXNUML );
  522. #endif
  523.         }
  524. if( x >= MAXNUML )
  525.         {
  526. #if INFINITIES
  527.         if( y > 0.0L )
  528.                 return( INFINITYL );
  529. #else
  530.         if( y > 0.0L )
  531.                 return( MAXNUML );
  532. #endif
  533.         return( 0.0L );
  534.         }
  535.  
  536. w = floorl(y);
  537. /* Set iyflg to 1 if y is an integer.  */
  538. iyflg = 0;
  539. if( w == y )
  540.         iyflg = 1;
  541.  
  542. /* Test for odd integer y.  */
  543. yoddint = 0;
  544. if( iyflg )
  545.         {
  546.         ya = fabsl(y);
  547.         ya = floorl(0.5L * ya);
  548.         yb = 0.5L * fabsl(w);
  549.         if( ya != yb )
  550.                 yoddint = 1;
  551.         }
  552.  
  553. if( x <= -MAXNUML )
  554.         {
  555.         if( y > 0.0L )
  556.                 {
  557. #ifdef INFINITIES
  558.                 if( yoddint )
  559.                         return( -INFINITYL );
  560.                 return( INFINITYL );
  561. #else
  562.                 if( yoddint )
  563.                         return( -MAXNUML );
  564.                 return( MAXNUML );
  565. #endif
  566.                 }
  567.         if( y < 0.0L )
  568.                 {
  569. #ifdef MINUSZERO
  570.                 if( yoddint )
  571.                         return( NEGZEROL );
  572. #endif
  573.                 return( 0.0 );
  574.                 }
  575.         }
  576.  
  577.  
  578. nflg = 0;       /* flag = 1 if x<0 raised to integer power */
  579. if( x <= 0.0L )
  580.         {
  581.         if( x == 0.0L )
  582.                 {
  583.                 if( y < 0.0 )
  584.                         {
  585. #ifdef MINUSZERO
  586.                         if( signbitl(x) && yoddint )
  587.                                 return( -INFINITYL );
  588. #endif
  589. #ifdef INFINITIES
  590.                         return( INFINITYL );
  591. #else
  592.                         return( MAXNUML );
  593. #endif
  594.                         }
  595.                 if( y > 0.0 )
  596.                         {
  597. #ifdef MINUSZERO
  598.                         if( signbitl(x) && yoddint )
  599.                                 return( NEGZEROL );
  600. #endif
  601.                         return( 0.0 );
  602.                         }
  603.                 if( y == 0.0L )
  604.                         return( 1.0L );  /*   0**0   */
  605.                 else  
  606.                         return( 0.0L );  /*   0**y   */
  607.                 }
  608.         else
  609.                 {
  610.                 if( iyflg == 0 )
  611.                         { /* noninteger power of negative number */
  612.                         mtherr( fname, DOMAIN );
  613.                         _SET_ERRNO (EDOM);
  614. #ifdef NANS
  615.                         return(NANL);
  616. #else
  617.                         return(0.0L);
  618. #endif
  619.                         }
  620.                 nflg = 1;
  621.                 }
  622.         }
  623.  
  624. /* Integer power of an integer.  */
  625.  
  626. if( iyflg )
  627.         {
  628.         i = w;
  629.         w = floorl(x);
  630.         if( (w == x) && (fabsl(y) < 32768.0) )
  631.                 {
  632.                 w = __powil( x, (int) y );
  633.                 return( w );
  634.                 }
  635.         }
  636.  
  637.  
  638. if( nflg )
  639.         x = fabsl(x);
  640.  
  641. /* separate significand from exponent */
  642. x = frexpl( x, &i );
  643. e = i;
  644.  
  645. /* find significand in antilog table A[] */
  646. i = 1;
  647. if( x <= douba(17) )
  648.         i = 17;
  649. if( x <= douba(i+8) )
  650.         i += 8;
  651. if( x <= douba(i+4) )
  652.         i += 4;
  653. if( x <= douba(i+2) )
  654.         i += 2;
  655. if( x >= douba(1) )
  656.         i = -1;
  657. i += 1;
  658.  
  659.  
  660. /* Find (x - A[i])/A[i]
  661.  * in order to compute log(x/A[i]):
  662.  *
  663.  * log(x) = log( a x/a ) = log(a) + log(x/a)
  664.  *
  665.  * log(x/a) = log(1+v),  v = x/a - 1 = (x-a)/a
  666.  */
  667. x -= douba(i);
  668. x -= doubb(i/2);
  669. x /= douba(i);
  670.  
  671.  
  672. /* rational approximation for log(1+v):
  673.  *
  674.  * log(1+v)  =  v  -  v**2/2  +  v**3 P(v) / Q(v)
  675.  */
  676. z = x*x;
  677. w = x * ( z * polevll( x, P, 3 ) / p1evll( x, Q, 3 ) );
  678. w = w - ldexpl( z, -1 );   /*  w - 0.5 * z  */
  679.  
  680. /* Convert to base 2 logarithm:
  681.  * multiply by log2(e) = 1 + LOG2EA
  682.  */
  683. z = LOG2EA * w;
  684. z += w;
  685. z += LOG2EA * x;
  686. z += x;
  687.  
  688. /* Compute exponent term of the base 2 logarithm. */
  689. w = -i;
  690. w = ldexpl( w, -LNXT ); /* divide by NXT */
  691. w += e;
  692. /* Now base 2 log of x is w + z. */
  693.  
  694. /* Multiply base 2 log by y, in extended precision. */
  695.  
  696. /* separate y into large part ya
  697.  * and small part yb less than 1/NXT
  698.  */
  699. ya = reducl(y);
  700. yb = y - ya;
  701.  
  702. /* (w+z)(ya+yb)
  703.  * = w*ya + w*yb + z*y
  704.  */
  705. F = z * y  +  w * yb;
  706. Fa = reducl(F);
  707. Fb = F - Fa;
  708.  
  709. G = Fa + w * ya;
  710. Ga = reducl(G);
  711. Gb = G - Ga;
  712.  
  713. H = Fb + Gb;
  714. Ha = reducl(H);
  715. w = ldexpl( Ga + Ha, LNXT );
  716.  
  717. /* Test the power of 2 for overflow */
  718. if( w > MEXP )
  719.         {
  720.         _SET_ERRNO (ERANGE);
  721.         mtherr( fname, OVERFLOW );
  722.         return( MAXNUML );
  723.         }
  724.  
  725. if( w < MNEXP )
  726.         {
  727.         _SET_ERRNO (ERANGE);
  728.         mtherr( fname, UNDERFLOW );
  729.         return( 0.0L );
  730.         }
  731.  
  732. e = w;
  733. Hb = H - Ha;
  734.  
  735. if( Hb > 0.0L )
  736.         {
  737.         e += 1;
  738.         Hb -= (1.0L/NXT);  /*0.0625L;*/
  739.         }
  740.  
  741. /* Now the product y * log2(x)  =  Hb + e/NXT.
  742.  *
  743.  * Compute base 2 exponential of Hb,
  744.  * where -0.0625 <= Hb <= 0.
  745.  */
  746. z = Hb * polevll( Hb, R, 6 );  /*    z  =  2**Hb - 1    */
  747.  
  748. /* Express e/NXT as an integer plus a negative number of (1/NXT)ths.
  749.  * Find lookup table entry for the fractional power of 2.
  750.  */
  751. if( e < 0 )
  752.         i = 0;
  753. else
  754.         i = 1;
  755. i = e/NXT + i;
  756. e = NXT*i - e;
  757. w = douba( e );
  758. z = w * z;      /*    2**-e * ( 1 + (2**Hb-1) )    */
  759. z = z + w;
  760. z = ldexpl( z, i );  /* multiply by integer power of 2 */
  761.  
  762. if( nflg )
  763.         {
  764. /* For negative x,
  765.  * find out if the integer exponent
  766.  * is odd or even.
  767.  */
  768.         w = ldexpl( y, -1 );
  769.         w = floorl(w);
  770.         w = ldexpl( w, 1 );
  771.         if( w != y )
  772.                 z = -z; /* odd exponent */
  773.         }
  774.  
  775. return( z );
  776. }
  777.  
  778. static __inline__ long double
  779. __convert_inf_to_maxnum(long double x)
  780. {
  781.   if (isinf(x))
  782.     return (x > 0.0L ? MAXNUML : -MAXNUML);
  783.   else
  784.     return x;
  785. }
  786.  
  787.  
  788. /* Find a multiple of 1/NXT that is within 1/NXT of x. */
  789. static __inline__ long double reducl(x)
  790. long double x;
  791. {
  792. long double t;
  793.  
  794. /* If the call to ldexpl overflows, set it to MAXNUML.
  795.    This avoids Inf - Inf = Nan result when calculating the 'small'
  796.    part of a reduction.  Instead, the small part becomes Inf,
  797.    causing under/overflow when adding it to the 'large' part.
  798.    There must be a cleaner way of doing this. */
  799. t =  __convert_inf_to_maxnum (ldexpl( x, LNXT ));
  800. t = floorl( t );
  801. t = ldexpl( t, -LNXT );
  802. return(t);
  803. }
  804.