jdk/src/java.base/share/native/libfdlibm/e_exp.c
author chegar
Sun, 17 Aug 2014 15:54:13 +0100
changeset 25859 3317bb8137f4
parent 5506 jdk/src/share/native/java/lang/fdlibm/src/e_exp.c@202f599c92aa
child 39759 427916042881
permissions -rw-r--r--
8054834: Modular Source Code Reviewed-by: alanb, chegar, ihse, mduigou Contributed-by: alan.bateman@oracle.com, alex.buckley@oracle.com, chris.hegarty@oracle.com, erik.joelsson@oracle.com, jonathan.gibbons@oracle.com, karen.kinnear@oracle.com, magnus.ihse.bursie@oracle.com, mandy.chung@oracle.com, mark.reinhold@oracle.com, paul.sandoz@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     3
 * Copyright (c) 1998, 2001, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     8
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    10
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    24
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
/* __ieee754_exp(x)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * Returns the exponential of x.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * Method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 *   1. Argument reduction:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 *      Reduce x to an r so that |r| <= 0.5*ln2 ~ 0.34658.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 *      Given x, find r and integer k such that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 *               x = k*ln2 + r,  |r| <= 0.5*ln2.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *      Here r will be represented as r = hi-lo for better
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *      accuracy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *   2. Approximation of exp(r) by a special rational function on
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *      the interval [0,0.34658]:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *      Write
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *          R(r**2) = r*(exp(r)+1)/(exp(r)-1) = 2 + r*r/6 - r**4/360 + ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *      We use a special Reme algorithm on [0,0.34658] to generate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *      a polynomial of degree 5 to approximate R. The maximum error
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *      of this polynomial approximation is bounded by 2**-59. In
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *      other words,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *          R(z) ~ 2.0 + P1*z + P2*z**2 + P3*z**3 + P4*z**4 + P5*z**5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *      (where z=r*r, and the values of P1 to P5 are listed below)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *      and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *          |                  5          |     -59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *          | 2.0+P1*z+...+P5*z   -  R(z) | <= 2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *          |                             |
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *      The computation of exp(r) thus becomes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *                             2*r
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *              exp(r) = 1 + -------
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *                            R - r
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *                                 r*R1(r)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *                     = 1 + r + ----------- (for better accuracy)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *                                2 - R1(r)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *      where
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *                               2       4             10
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *              R1(r) = r - (P1*r  + P2*r  + ... + P5*r   ).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *   3. Scale back to obtain exp(x):
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *      From step 1, we have
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *         exp(x) = 2^k * exp(r)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * Special cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *      exp(INF) is INF, exp(NaN) is NaN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *      exp(-INF) is 0, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *      for finite argument, only exp(0)=1 is exact.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * Accuracy:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 *      according to an error analysis, the error is always less than
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 *      1 ulp (unit in the last place).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * Misc. info.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 *      For IEEE double
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 *          if x >  7.09782712893383973096e+02 then exp(x) overflow
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 *          if x < -7.45133219101941108420e+02 then exp(x) underflow
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * Constants:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * The hexadecimal values are the intended ones for the following
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * constants. The decimal values may be used, provided that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * compiler will convert from decimal to binary accurately enough
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * to produce the hexadecimal values shown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
#include "fdlibm.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
#ifdef __STDC__
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
static const double
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
static double
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
one     = 1.0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
halF[2] = {0.5,-0.5,},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
huge    = 1.0e+300,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
twom1000= 9.33263618503218878990e-302,     /* 2**-1000=0x01700000,0*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
o_threshold=  7.09782712893383973096e+02,  /* 0x40862E42, 0xFEFA39EF */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
u_threshold= -7.45133219101941108420e+02,  /* 0xc0874910, 0xD52D3051 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
ln2HI[2]   ={ 6.93147180369123816490e-01,  /* 0x3fe62e42, 0xfee00000 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
             -6.93147180369123816490e-01,},/* 0xbfe62e42, 0xfee00000 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
ln2LO[2]   ={ 1.90821492927058770002e-10,  /* 0x3dea39ef, 0x35793c76 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
             -1.90821492927058770002e-10,},/* 0xbdea39ef, 0x35793c76 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
invln2 =  1.44269504088896338700e+00, /* 0x3ff71547, 0x652b82fe */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
P1   =  1.66666666666666019037e-01, /* 0x3FC55555, 0x5555553E */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
P2   = -2.77777777770155933842e-03, /* 0xBF66C16C, 0x16BEBD93 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
P3   =  6.61375632143793436117e-05, /* 0x3F11566A, 0xAF25DE2C */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
P4   = -1.65339022054652515390e-06, /* 0xBEBBBD41, 0xC5D26BF1 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
P5   =  4.13813679705723846039e-08; /* 0x3E663769, 0x72BEA4D0 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
#ifdef __STDC__
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        double __ieee754_exp(double x)  /* default IEEE double exp */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        double __ieee754_exp(x) /* default IEEE double exp */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        double x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        double y,hi=0,lo=0,c,t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        int k=0,xsb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        unsigned hx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        hx  = __HI(x);  /* high word of x */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        xsb = (hx>>31)&1;               /* sign bit of x */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        hx &= 0x7fffffff;               /* high word of |x| */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    /* filter out non-finite argument */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        if(hx >= 0x40862E42) {                  /* if |x|>=709.78... */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            if(hx>=0x7ff00000) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                if(((hx&0xfffff)|__LO(x))!=0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                     return x+x;                /* NaN */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                else return (xsb==0)? x:0.0;    /* exp(+-inf)={inf,0} */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            if(x > o_threshold) return huge*huge; /* overflow */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            if(x < u_threshold) return twom1000*twom1000; /* underflow */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    /* argument reduction */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        if(hx > 0x3fd62e42) {           /* if  |x| > 0.5 ln2 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            if(hx < 0x3FF0A2B2) {       /* and |x| < 1.5 ln2 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                hi = x-ln2HI[xsb]; lo=ln2LO[xsb]; k = 1-xsb-xsb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                k  = invln2*x+halF[xsb];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                t  = k;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                hi = x - t*ln2HI[0];    /* t*ln2HI is exact here */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                lo = t*ln2LO[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            x  = hi - lo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        else if(hx < 0x3e300000)  {     /* when |x|<2**-28 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            if(huge+x>one) return one+x;/* trigger inexact */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        else k = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    /* x is now in primary range */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        t  = x*x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        c  = x - t*(P1+t*(P2+t*(P3+t*(P4+t*P5))));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        if(k==0)        return one-((x*c)/(c-2.0)-x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        else            y = one-((lo-(x*c)/(2.0-c))-hi);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        if(k >= -1021) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            __HI(y) += (k<<20); /* add k to y's exponent */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            return y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            __HI(y) += ((k+1000)<<20);/* add k to y's exponent */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            return y*twom1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
}