LCOV - code coverage report
Current view: top level - ld80 - e_powl.c (source / functions) Coverage Total Hit
Test: app.info Lines: 0.0 % 197 0
Test Date: 2024-01-11 15:52:50 Functions: 0.0 % 3 0
Branches: 0.0 % 146 0

             Branch data     Line data    Source code
       1                 :             : /*      $OpenBSD: e_powl.c,v 1.5 2013/11/12 20:35:19 martynas Exp $     */
       2                 :             : 
       3                 :             : /*
       4                 :             :  * Copyright (c) 2008 Stephen L. Moshier <steve@moshier.net>
       5                 :             :  *
       6                 :             :  * Permission to use, copy, modify, and distribute this software for any
       7                 :             :  * purpose with or without fee is hereby granted, provided that the above
       8                 :             :  * copyright notice and this permission notice appear in all copies.
       9                 :             :  *
      10                 :             :  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
      11                 :             :  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
      12                 :             :  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
      13                 :             :  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
      14                 :             :  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
      15                 :             :  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
      16                 :             :  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
      17                 :             :  */
      18                 :             : 
      19                 :             : /*                                                      powl.c
      20                 :             :  *
      21                 :             :  *      Power function, long double precision
      22                 :             :  *
      23                 :             :  *
      24                 :             :  *
      25                 :             :  * SYNOPSIS:
      26                 :             :  *
      27                 :             :  * long double x, y, z, powl();
      28                 :             :  *
      29                 :             :  * z = powl( x, y );
      30                 :             :  *
      31                 :             :  *
      32                 :             :  *
      33                 :             :  * DESCRIPTION:
      34                 :             :  *
      35                 :             :  * Computes x raised to the yth power.  Analytically,
      36                 :             :  *
      37                 :             :  *      x**y  =  exp( y log(x) ).
      38                 :             :  *
      39                 :             :  * Following Cody and Waite, this program uses a lookup table
      40                 :             :  * of 2**-i/32 and pseudo extended precision arithmetic to
      41                 :             :  * obtain several extra bits of accuracy in both the logarithm
      42                 :             :  * and the exponential.
      43                 :             :  *
      44                 :             :  *
      45                 :             :  *
      46                 :             :  * ACCURACY:
      47                 :             :  *
      48                 :             :  * The relative error of pow(x,y) can be estimated
      49                 :             :  * by   y dl ln(2),   where dl is the absolute error of
      50                 :             :  * the internally computed base 2 logarithm.  At the ends
      51                 :             :  * of the approximation interval the logarithm equal 1/32
      52                 :             :  * and its relative error is about 1 lsb = 1.1e-19.  Hence
      53                 :             :  * the predicted relative error in the result is 2.3e-21 y .
      54                 :             :  *
      55                 :             :  *                      Relative error:
      56                 :             :  * arithmetic   domain     # trials      peak         rms
      57                 :             :  *
      58                 :             :  *    IEEE     +-1000       40000      2.8e-18      3.7e-19
      59                 :             :  * .001 < x < 1000, with log(x) uniformly distributed.
      60                 :             :  * -1000 < y < 1000, y uniformly distributed.
      61                 :             :  *
      62                 :             :  *    IEEE     0,8700       60000      6.5e-18      1.0e-18
      63                 :             :  * 0.99 < x < 1.01, 0 < y < 8700, uniformly distributed.
      64                 :             :  *
      65                 :             :  *
      66                 :             :  * ERROR MESSAGES:
      67                 :             :  *
      68                 :             :  *   message         condition      value returned
      69                 :             :  * pow overflow     x**y > MAXNUM      INFINITY
      70                 :             :  * pow underflow   x**y < 1/MAXNUM       0.0
      71                 :             :  * pow domain      x<0 and y noninteger  0.0
      72                 :             :  *
      73                 :             :  */
      74                 :             : 
      75                 :             : #include <float.h>
      76                 :             : #include <openlibm_math.h>
      77                 :             : 
      78                 :             : #include "math_private.h"
      79                 :             : 
      80                 :             : /* Table size */
      81                 :             : #define NXT 32
      82                 :             : /* log2(Table size) */
      83                 :             : #define LNXT 5
      84                 :             : 
      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                 :             : #define MNEXP (-NXT*(16384.0L+64.0L))
     176                 :             : /* log2(e) - 1 */
     177                 :             : #define LOG2EA 0.44269504088896340735992L
     178                 :             : 
     179                 :             : #define F W
     180                 :             : #define Fa Wa
     181                 :             : #define Fb Wb
     182                 :             : #define G W
     183                 :             : #define Ga Wa
     184                 :             : #define Gb u
     185                 :             : #define H W
     186                 :             : #define Ha Wb
     187                 :             : #define Hb Wb
     188                 :             : 
     189                 :             : static const long double MAXLOGL = 1.1356523406294143949492E4L;
     190                 :             : static const long double MINLOGL = -1.13994985314888605586758E4L;
     191                 :             : static const long double LOGE2L = 6.9314718055994530941723E-1L;
     192                 :             : static volatile long double z;
     193                 :             : static long double w, W, Wa, Wb, ya, yb, u;
     194                 :             : static const long double huge = 0x1p10000L;
     195                 :             : #if 0 /* XXX Prevent gcc from erroneously constant folding this. */
     196                 :             : static const long double twom10000 = 0x1p-10000L;
     197                 :             : #else
     198                 :             : static volatile long double twom10000 = 0x1p-10000L;
     199                 :             : #endif
     200                 :             : 
     201                 :             : static long double reducl( long double );
     202                 :             : static long double powil ( long double, int );
     203                 :             : 
     204                 :             : long double
     205                 :           0 : powl(long double x, long double y)
     206                 :             : {
     207                 :             : /* double F, Fa, Fb, G, Ga, Gb, H, Ha, Hb */
     208                 :             : int i, nflg, iyflg, yoddint;
     209                 :             : long e;
     210                 :             : 
     211         [ #  # ]:           0 : if( y == 0.0L )
     212                 :           0 :         return( 1.0L );
     213                 :             : 
     214         [ #  # ]:           0 : if( x == 1.0L )
     215                 :           0 :         return( 1.0L );
     216                 :             : 
     217         [ #  # ]:           0 : if( isnan(x) )
     218                 :           0 :         return( x );
     219         [ #  # ]:           0 : if( isnan(y) )
     220                 :           0 :         return( y );
     221                 :             : 
     222         [ #  # ]:           0 : if( y == 1.0L )
     223                 :           0 :         return( x );
     224                 :             : 
     225   [ #  #  #  # ]:           0 : if( !isfinite(y) && x == -1.0L )
     226                 :           0 :         return( 1.0L );
     227                 :             : 
     228         [ #  # ]:           0 : if( y >= LDBL_MAX )
     229                 :             :         {
     230         [ #  # ]:           0 :         if( x > 1.0L )
     231                 :           0 :                 return( INFINITY );
     232   [ #  #  #  # ]:           0 :         if( x > 0.0L && x < 1.0L )
     233                 :           0 :                 return( 0.0L );
     234         [ #  # ]:           0 :         if( x < -1.0L )
     235                 :           0 :                 return( INFINITY );
     236   [ #  #  #  # ]:           0 :         if( x > -1.0L && x < 0.0L )
     237                 :           0 :                 return( 0.0L );
     238                 :             :         }
     239         [ #  # ]:           0 : if( y <= -LDBL_MAX )
     240                 :             :         {
     241         [ #  # ]:           0 :         if( x > 1.0L )
     242                 :           0 :                 return( 0.0L );
     243   [ #  #  #  # ]:           0 :         if( x > 0.0L && x < 1.0L )
     244                 :           0 :                 return( INFINITY );
     245         [ #  # ]:           0 :         if( x < -1.0L )
     246                 :           0 :                 return( 0.0L );
     247   [ #  #  #  # ]:           0 :         if( x > -1.0L && x < 0.0L )
     248                 :           0 :                 return( INFINITY );
     249                 :             :         }
     250         [ #  # ]:           0 : if( x >= LDBL_MAX )
     251                 :             :         {
     252         [ #  # ]:           0 :         if( y > 0.0L )
     253                 :           0 :                 return( INFINITY );
     254                 :           0 :         return( 0.0L );
     255                 :             :         }
     256                 :             : 
     257                 :           0 : w = floorl(y);
     258                 :             : /* Set iyflg to 1 if y is an integer.  */
     259                 :           0 : iyflg = 0;
     260         [ #  # ]:           0 : if( w == y )
     261                 :           0 :         iyflg = 1;
     262                 :             : 
     263                 :             : /* Test for odd integer y.  */
     264                 :           0 : yoddint = 0;
     265         [ #  # ]:           0 : if( iyflg )
     266                 :             :         {
     267                 :           0 :         ya = fabsl(y);
     268                 :           0 :         ya = floorl(0.5L * ya);
     269                 :           0 :         yb = 0.5L * fabsl(w);
     270         [ #  # ]:           0 :         if( ya != yb )
     271                 :           0 :                 yoddint = 1;
     272                 :             :         }
     273                 :             : 
     274         [ #  # ]:           0 : if( x <= -LDBL_MAX )
     275                 :             :         {
     276         [ #  # ]:           0 :         if( y > 0.0L )
     277                 :             :                 {
     278         [ #  # ]:           0 :                 if( yoddint )
     279                 :           0 :                         return( -INFINITY );
     280                 :           0 :                 return( INFINITY );
     281                 :             :                 }
     282         [ #  # ]:           0 :         if( y < 0.0L )
     283                 :             :                 {
     284         [ #  # ]:           0 :                 if( yoddint )
     285                 :           0 :                         return( -0.0L );
     286                 :           0 :                 return( 0.0 );
     287                 :             :                 }
     288                 :             :         }
     289                 :             : 
     290                 :             : 
     291                 :           0 : nflg = 0;       /* flag = 1 if x<0 raised to integer power */
     292         [ #  # ]:           0 : if( x <= 0.0L )
     293                 :             :         {
     294         [ #  # ]:           0 :         if( x == 0.0L )
     295                 :             :                 {
     296         [ #  # ]:           0 :                 if( y < 0.0 )
     297                 :             :                         {
     298   [ #  #  #  # ]:           0 :                         if( signbit(x) && yoddint )
     299                 :           0 :                                 return( -INFINITY );
     300                 :           0 :                         return( INFINITY );
     301                 :             :                         }
     302         [ #  # ]:           0 :                 if( y > 0.0 )
     303                 :             :                         {
     304   [ #  #  #  # ]:           0 :                         if( signbit(x) && yoddint )
     305                 :           0 :                                 return( -0.0L );
     306                 :           0 :                         return( 0.0 );
     307                 :             :                         }
     308         [ #  # ]:           0 :                 if( y == 0.0L )
     309                 :           0 :                         return( 1.0L );  /*   0**0   */
     310                 :             :                 else
     311                 :           0 :                         return( 0.0L );  /*   0**y   */
     312                 :             :                 }
     313                 :             :         else
     314                 :             :                 {
     315         [ #  # ]:           0 :                 if( iyflg == 0 )
     316                 :           0 :                         return (x - x) / (x - x); /* (x<0)**(non-int) is NaN */
     317                 :           0 :                 nflg = 1;
     318                 :             :                 }
     319                 :             :         }
     320                 :             : 
     321                 :             : /* Integer power of an integer.  */
     322                 :             : 
     323         [ #  # ]:           0 : if( iyflg )
     324                 :             :         {
     325                 :           0 :         i = w;
     326                 :           0 :         w = floorl(x);
     327   [ #  #  #  # ]:           0 :         if( (w == x) && (fabsl(y) < 32768.0) )
     328                 :             :                 {
     329                 :           0 :                 w = powil( x, (int) y );
     330                 :           0 :                 return( w );
     331                 :             :                 }
     332                 :             :         }
     333                 :             : 
     334                 :             : 
     335         [ #  # ]:           0 : if( nflg )
     336                 :           0 :         x = fabsl(x);
     337                 :             : 
     338                 :             : /* separate significand from exponent */
     339                 :           0 : x = frexpl( x, &i );
     340                 :           0 : e = i;
     341                 :             : 
     342                 :             : /* find significand in antilog table A[] */
     343                 :           0 : i = 1;
     344         [ #  # ]:           0 : if( x <= douba(17) )
     345                 :           0 :         i = 17;
     346         [ #  # ]:           0 : if( x <= douba(i+8) )
     347                 :           0 :         i += 8;
     348         [ #  # ]:           0 : if( x <= douba(i+4) )
     349                 :           0 :         i += 4;
     350         [ #  # ]:           0 : if( x <= douba(i+2) )
     351                 :           0 :         i += 2;
     352         [ #  # ]:           0 : if( x >= douba(1) )
     353                 :           0 :         i = -1;
     354                 :           0 : i += 1;
     355                 :             : 
     356                 :             : 
     357                 :             : /* Find (x - A[i])/A[i]
     358                 :             :  * in order to compute log(x/A[i]):
     359                 :             :  *
     360                 :             :  * log(x) = log( a x/a ) = log(a) + log(x/a)
     361                 :             :  *
     362                 :             :  * log(x/a) = log(1+v),  v = x/a - 1 = (x-a)/a
     363                 :             :  */
     364                 :           0 : x -= douba(i);
     365                 :           0 : x -= doubb(i/2);
     366                 :           0 : x /= douba(i);
     367                 :             : 
     368                 :             : 
     369                 :             : /* rational approximation for log(1+v):
     370                 :             :  *
     371                 :             :  * log(1+v)  =  v  -  v**2/2  +  v**3 P(v) / Q(v)
     372                 :             :  */
     373                 :           0 : z = x*x;
     374                 :           0 : w = x * ( z * __polevll( x, P, 3 ) / __p1evll( x, Q, 3 ) );
     375                 :           0 : w = w - ldexpl( z, -1 );   /*  w - 0.5 * z  */
     376                 :             : 
     377                 :             : /* Convert to base 2 logarithm:
     378                 :             :  * multiply by log2(e) = 1 + LOG2EA
     379                 :             :  */
     380                 :           0 : z = LOG2EA * w;
     381                 :           0 : z += w;
     382                 :           0 : z += LOG2EA * x;
     383                 :           0 : z += x;
     384                 :             : 
     385                 :             : /* Compute exponent term of the base 2 logarithm. */
     386                 :           0 : w = -i;
     387                 :           0 : w = ldexpl( w, -LNXT ); /* divide by NXT */
     388                 :           0 : w += e;
     389                 :             : /* Now base 2 log of x is w + z. */
     390                 :             : 
     391                 :             : /* Multiply base 2 log by y, in extended precision. */
     392                 :             : 
     393                 :             : /* separate y into large part ya
     394                 :             :  * and small part yb less than 1/NXT
     395                 :             :  */
     396                 :           0 : ya = reducl(y);
     397                 :           0 : yb = y - ya;
     398                 :             : 
     399                 :             : /* (w+z)(ya+yb)
     400                 :             :  * = w*ya + w*yb + z*y
     401                 :             :  */
     402                 :           0 : F = z * y  +  w * yb;
     403                 :           0 : Fa = reducl(F);
     404                 :           0 : Fb = F - Fa;
     405                 :             : 
     406                 :           0 : G = Fa + w * ya;
     407                 :           0 : Ga = reducl(G);
     408                 :           0 : Gb = G - Ga;
     409                 :             : 
     410                 :           0 : H = Fb + Gb;
     411                 :           0 : Ha = reducl(H);
     412                 :           0 : w = ldexpl( Ga+Ha, LNXT );
     413                 :             : 
     414                 :             : /* Test the power of 2 for overflow */
     415         [ #  # ]:           0 : if( w > MEXP )
     416                 :           0 :         return (huge * huge);           /* overflow */
     417                 :             : 
     418         [ #  # ]:           0 : if( w < MNEXP )
     419                 :           0 :         return (twom10000 * twom10000); /* underflow */
     420                 :             : 
     421                 :           0 : e = w;
     422                 :           0 : Hb = H - Ha;
     423                 :             : 
     424         [ #  # ]:           0 : if( Hb > 0.0L )
     425                 :             :         {
     426                 :           0 :         e += 1;
     427                 :           0 :         Hb -= (1.0L/NXT);  /*0.0625L;*/
     428                 :             :         }
     429                 :             : 
     430                 :             : /* Now the product y * log2(x)  =  Hb + e/NXT.
     431                 :             :  *
     432                 :             :  * Compute base 2 exponential of Hb,
     433                 :             :  * where -0.0625 <= Hb <= 0.
     434                 :             :  */
     435                 :           0 : z = Hb * __polevll( Hb, R, 6 );  /*    z  =  2**Hb - 1    */
     436                 :             : 
     437                 :             : /* Express e/NXT as an integer plus a negative number of (1/NXT)ths.
     438                 :             :  * Find lookup table entry for the fractional power of 2.
     439                 :             :  */
     440         [ #  # ]:           0 : if( e < 0 )
     441                 :           0 :         i = 0;
     442                 :             : else
     443                 :           0 :         i = 1;
     444                 :           0 : i = e/NXT + i;
     445                 :           0 : e = NXT*i - e;
     446                 :           0 : w = douba( e );
     447                 :           0 : z = w * z;      /*    2**-e * ( 1 + (2**Hb-1) )    */
     448                 :           0 : z = z + w;
     449                 :           0 : z = ldexpl( z, i );  /* multiply by integer power of 2 */
     450                 :             : 
     451         [ #  # ]:           0 : if( nflg )
     452                 :             :         {
     453                 :             : /* For negative x,
     454                 :             :  * find out if the integer exponent
     455                 :             :  * is odd or even.
     456                 :             :  */
     457                 :           0 :         w = ldexpl( y, -1 );
     458                 :           0 :         w = floorl(w);
     459                 :           0 :         w = ldexpl( w, 1 );
     460         [ #  # ]:           0 :         if( w != y )
     461                 :           0 :                 z = -z; /* odd exponent */
     462                 :             :         }
     463                 :             : 
     464                 :           0 : return( z );
     465                 :             : }
     466                 :             : 
     467                 :             : 
     468                 :             : /* Find a multiple of 1/NXT that is within 1/NXT of x. */
     469                 :             : static long double
     470                 :           0 : reducl(long double x)
     471                 :             : {
     472                 :             : long double t;
     473                 :             : 
     474                 :           0 : t = ldexpl( x, LNXT );
     475                 :           0 : t = floorl( t );
     476                 :           0 : t = ldexpl( t, -LNXT );
     477                 :           0 : return(t);
     478                 :             : }
     479                 :             : 
     480                 :             : /*                                                      powil.c
     481                 :             :  *
     482                 :             :  *      Real raised to integer power, long double precision
     483                 :             :  *
     484                 :             :  *
     485                 :             :  *
     486                 :             :  * SYNOPSIS:
     487                 :             :  *
     488                 :             :  * long double x, y, powil();
     489                 :             :  * int n;
     490                 :             :  *
     491                 :             :  * y = powil( x, n );
     492                 :             :  *
     493                 :             :  *
     494                 :             :  *
     495                 :             :  * DESCRIPTION:
     496                 :             :  *
     497                 :             :  * Returns argument x raised to the nth power.
     498                 :             :  * The routine efficiently decomposes n as a sum of powers of
     499                 :             :  * two. The desired power is a product of two-to-the-kth
     500                 :             :  * powers of x.  Thus to compute the 32767 power of x requires
     501                 :             :  * 28 multiplications instead of 32767 multiplications.
     502                 :             :  *
     503                 :             :  *
     504                 :             :  *
     505                 :             :  * ACCURACY:
     506                 :             :  *
     507                 :             :  *
     508                 :             :  *                      Relative error:
     509                 :             :  * arithmetic   x domain   n domain  # trials      peak         rms
     510                 :             :  *    IEEE     .001,1000  -1022,1023  50000       4.3e-17     7.8e-18
     511                 :             :  *    IEEE        1,2     -1022,1023  20000       3.9e-17     7.6e-18
     512                 :             :  *    IEEE     .99,1.01     0,8700    10000       3.6e-16     7.2e-17
     513                 :             :  *
     514                 :             :  * Returns MAXNUM on overflow, zero on underflow.
     515                 :             :  *
     516                 :             :  */
     517                 :             : 
     518                 :             : static long double
     519                 :           0 : powil(long double x, int nn)
     520                 :             : {
     521                 :             : long double ww, y;
     522                 :             : long double s;
     523                 :             : int n, e, sign, asign, lx;
     524                 :             : 
     525         [ #  # ]:           0 : if( x == 0.0L )
     526                 :             :         {
     527         [ #  # ]:           0 :         if( nn == 0 )
     528                 :           0 :                 return( 1.0L );
     529         [ #  # ]:           0 :         else if( nn < 0 )
     530                 :           0 :                 return( LDBL_MAX );
     531                 :             :         else
     532                 :           0 :                 return( 0.0L );
     533                 :             :         }
     534                 :             : 
     535         [ #  # ]:           0 : if( nn == 0 )
     536                 :           0 :         return( 1.0L );
     537                 :             : 
     538                 :             : 
     539         [ #  # ]:           0 : if( x < 0.0L )
     540                 :             :         {
     541                 :           0 :         asign = -1;
     542                 :           0 :         x = -x;
     543                 :             :         }
     544                 :             : else
     545                 :           0 :         asign = 0;
     546                 :             : 
     547                 :             : 
     548         [ #  # ]:           0 : if( nn < 0 )
     549                 :             :         {
     550                 :           0 :         sign = -1;
     551                 :           0 :         n = -nn;
     552                 :             :         }
     553                 :             : else
     554                 :             :         {
     555                 :           0 :         sign = 1;
     556                 :           0 :         n = nn;
     557                 :             :         }
     558                 :             : 
     559                 :             : /* Overflow detection */
     560                 :             : 
     561                 :             : /* Calculate approximate logarithm of answer */
     562                 :           0 : s = x;
     563                 :           0 : s = frexpl( s, &lx );
     564                 :           0 : e = (lx - 1)*n;
     565   [ #  #  #  #  :           0 : if( (e == 0) || (e > 64) || (e < -64) )
                   #  # ]
     566                 :             :         {
     567                 :           0 :         s = (s - 7.0710678118654752e-1L) / (s +  7.0710678118654752e-1L);
     568                 :           0 :         s = (2.9142135623730950L * s - 0.5L + lx) * nn * LOGE2L;
     569                 :             :         }
     570                 :             : else
     571                 :             :         {
     572                 :           0 :         s = LOGE2L * e;
     573                 :             :         }
     574                 :             : 
     575         [ #  # ]:           0 : if( s > MAXLOGL )
     576                 :           0 :         return (huge * huge);           /* overflow */
     577                 :             : 
     578         [ #  # ]:           0 : if( s < MINLOGL )
     579                 :           0 :         return (twom10000 * twom10000); /* underflow */
     580                 :             : /* Handle tiny denormal answer, but with less accuracy
     581                 :             :  * since roundoff error in 1.0/x will be amplified.
     582                 :             :  * The precise demarcation should be the gradual underflow threshold.
     583                 :             :  */
     584         [ #  # ]:           0 : if( s < (-MAXLOGL+2.0L) )
     585                 :             :         {
     586                 :           0 :         x = 1.0L/x;
     587                 :           0 :         sign = -sign;
     588                 :             :         }
     589                 :             : 
     590                 :             : /* First bit of the power */
     591         [ #  # ]:           0 : if( n & 1 )
     592                 :           0 :         y = x;
     593                 :             :                 
     594                 :             : else
     595                 :             :         {
     596                 :           0 :         y = 1.0L;
     597                 :           0 :         asign = 0;
     598                 :             :         }
     599                 :             : 
     600                 :           0 : ww = x;
     601                 :           0 : n >>= 1;
     602         [ #  # ]:           0 : while( n )
     603                 :             :         {
     604                 :           0 :         ww = ww * ww;   /* arg to the 2-to-the-kth power */
     605         [ #  # ]:           0 :         if( n & 1 ) /* if that bit is set, then include in product */
     606                 :           0 :                 y *= ww;
     607                 :           0 :         n >>= 1;
     608                 :             :         }
     609                 :             : 
     610         [ #  # ]:           0 : if( asign )
     611                 :           0 :         y = -y; /* odd power of negative number */
     612         [ #  # ]:           0 : if( sign < 0 )
     613                 :           0 :         y = 1.0L/y;
     614                 :           0 : return(y);
     615                 :             : }
        

Generated by: LCOV version 2.0-115.g950771e