1*25c28e83SPiotr Jasiukajtis /* 2*25c28e83SPiotr Jasiukajtis * CDDL HEADER START 3*25c28e83SPiotr Jasiukajtis * 4*25c28e83SPiotr Jasiukajtis * The contents of this file are subject to the terms of the 5*25c28e83SPiotr Jasiukajtis * Common Development and Distribution License (the "License"). 6*25c28e83SPiotr Jasiukajtis * You may not use this file except in compliance with the License. 7*25c28e83SPiotr Jasiukajtis * 8*25c28e83SPiotr Jasiukajtis * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9*25c28e83SPiotr Jasiukajtis * or http://www.opensolaris.org/os/licensing. 10*25c28e83SPiotr Jasiukajtis * See the License for the specific language governing permissions 11*25c28e83SPiotr Jasiukajtis * and limitations under the License. 12*25c28e83SPiotr Jasiukajtis * 13*25c28e83SPiotr Jasiukajtis * When distributing Covered Code, include this CDDL HEADER in each 14*25c28e83SPiotr Jasiukajtis * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15*25c28e83SPiotr Jasiukajtis * If applicable, add the following below this CDDL HEADER, with the 16*25c28e83SPiotr Jasiukajtis * fields enclosed by brackets "[]" replaced with your own identifying 17*25c28e83SPiotr Jasiukajtis * information: Portions Copyright [yyyy] [name of copyright owner] 18*25c28e83SPiotr Jasiukajtis * 19*25c28e83SPiotr Jasiukajtis * CDDL HEADER END 20*25c28e83SPiotr Jasiukajtis */ 21*25c28e83SPiotr Jasiukajtis 22*25c28e83SPiotr Jasiukajtis /* 23*25c28e83SPiotr Jasiukajtis * Copyright 2011 Nexenta Systems, Inc. All rights reserved. 24*25c28e83SPiotr Jasiukajtis */ 25*25c28e83SPiotr Jasiukajtis /* 26*25c28e83SPiotr Jasiukajtis * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 27*25c28e83SPiotr Jasiukajtis * Use is subject to license terms. 28*25c28e83SPiotr Jasiukajtis */ 29*25c28e83SPiotr Jasiukajtis 30*25c28e83SPiotr Jasiukajtis /* INDENT OFF */ 31*25c28e83SPiotr Jasiukajtis /* 32*25c28e83SPiotr Jasiukajtis * double __k_cexp(double x, int *n); 33*25c28e83SPiotr Jasiukajtis * Returns the exponential of x in the form of 2**n * y, y=__k_cexp(x,&n). 34*25c28e83SPiotr Jasiukajtis * 35*25c28e83SPiotr Jasiukajtis * Method 36*25c28e83SPiotr Jasiukajtis * 1. Argument reduction: 37*25c28e83SPiotr Jasiukajtis * Reduce x to an r so that |r| <= 0.5*ln2 ~ 0.34658. 38*25c28e83SPiotr Jasiukajtis * Given x, find r and integer k such that 39*25c28e83SPiotr Jasiukajtis * 40*25c28e83SPiotr Jasiukajtis * x = k*ln2 + r, |r| <= 0.5*ln2. 41*25c28e83SPiotr Jasiukajtis * 42*25c28e83SPiotr Jasiukajtis * Here r will be represented as r = hi-lo for better 43*25c28e83SPiotr Jasiukajtis * accuracy. 44*25c28e83SPiotr Jasiukajtis * 45*25c28e83SPiotr Jasiukajtis * 2. Approximation of exp(r) by a special rational function on 46*25c28e83SPiotr Jasiukajtis * the interval [0,0.34658]: 47*25c28e83SPiotr Jasiukajtis * Write 48*25c28e83SPiotr Jasiukajtis * R(r**2) = r*(exp(r)+1)/(exp(r)-1) = 2 + r*r/6 - r**4/360 + ... 49*25c28e83SPiotr Jasiukajtis * We use a special Remez algorithm on [0,0.34658] to generate 50*25c28e83SPiotr Jasiukajtis * a polynomial of degree 5 to approximate R. The maximum error 51*25c28e83SPiotr Jasiukajtis * of this polynomial approximation is bounded by 2**-59. In 52*25c28e83SPiotr Jasiukajtis * other words, 53*25c28e83SPiotr Jasiukajtis * R(z) ~ 2.0 + P1*z + P2*z**2 + P3*z**3 + P4*z**4 + P5*z**5 54*25c28e83SPiotr Jasiukajtis * (where z=r*r, and the values of P1 to P5 are listed below) 55*25c28e83SPiotr Jasiukajtis * and 56*25c28e83SPiotr Jasiukajtis * | 5 | -59 57*25c28e83SPiotr Jasiukajtis * | 2.0+P1*z+...+P5*z - R(z) | <= 2 58*25c28e83SPiotr Jasiukajtis * | | 59*25c28e83SPiotr Jasiukajtis * The computation of exp(r) thus becomes 60*25c28e83SPiotr Jasiukajtis * 2*r 61*25c28e83SPiotr Jasiukajtis * exp(r) = 1 + ------- 62*25c28e83SPiotr Jasiukajtis * R - r 63*25c28e83SPiotr Jasiukajtis * r*R1(r) 64*25c28e83SPiotr Jasiukajtis * = 1 + r + ----------- (for better accuracy) 65*25c28e83SPiotr Jasiukajtis * 2 - R1(r) 66*25c28e83SPiotr Jasiukajtis * where 67*25c28e83SPiotr Jasiukajtis * 2 4 10 68*25c28e83SPiotr Jasiukajtis * R1(r) = r - (P1*r + P2*r + ... + P5*r ). 69*25c28e83SPiotr Jasiukajtis * 70*25c28e83SPiotr Jasiukajtis * 3. Return n = k and __k_cexp = exp(r). 71*25c28e83SPiotr Jasiukajtis * 72*25c28e83SPiotr Jasiukajtis * Special cases: 73*25c28e83SPiotr Jasiukajtis * exp(INF) is INF, exp(NaN) is NaN; 74*25c28e83SPiotr Jasiukajtis * exp(-INF) is 0, and 75*25c28e83SPiotr Jasiukajtis * for finite argument, only exp(0)=1 is exact. 76*25c28e83SPiotr Jasiukajtis * 77*25c28e83SPiotr Jasiukajtis * Range and Accuracy: 78*25c28e83SPiotr Jasiukajtis * When |x| is really big, say |x| > 50000, the accuracy 79*25c28e83SPiotr Jasiukajtis * is not important because the ultimate result will over or under 80*25c28e83SPiotr Jasiukajtis * flow. So we will simply replace n = 50000 and r = 0.0. For 81*25c28e83SPiotr Jasiukajtis * moderate size x, according to an error analysis, the error is 82*25c28e83SPiotr Jasiukajtis * always less than 1 ulp (unit in the last place). 83*25c28e83SPiotr Jasiukajtis * 84*25c28e83SPiotr Jasiukajtis * Constants: 85*25c28e83SPiotr Jasiukajtis * The hexadecimal values are the intended ones for the following 86*25c28e83SPiotr Jasiukajtis * constants. The decimal values may be used, provided that the 87*25c28e83SPiotr Jasiukajtis * compiler will convert from decimal to binary accurately enough 88*25c28e83SPiotr Jasiukajtis * to produce the hexadecimal values shown. 89*25c28e83SPiotr Jasiukajtis */ 90*25c28e83SPiotr Jasiukajtis /* INDENT ON */ 91*25c28e83SPiotr Jasiukajtis 92*25c28e83SPiotr Jasiukajtis #include "libm.h" /* __k_cexp */ 93*25c28e83SPiotr Jasiukajtis #include "complex_wrapper.h" /* HI_WORD/LO_WORD */ 94*25c28e83SPiotr Jasiukajtis 95*25c28e83SPiotr Jasiukajtis /* INDENT OFF */ 96*25c28e83SPiotr Jasiukajtis static const double 97*25c28e83SPiotr Jasiukajtis one = 1.0, 98*25c28e83SPiotr Jasiukajtis two128 = 3.40282366920938463463e+38, 99*25c28e83SPiotr Jasiukajtis halF[2] = { 100*25c28e83SPiotr Jasiukajtis 0.5, -0.5, 101*25c28e83SPiotr Jasiukajtis }, 102*25c28e83SPiotr Jasiukajtis ln2HI[2] = { 103*25c28e83SPiotr Jasiukajtis 6.93147180369123816490e-01, /* 0x3fe62e42, 0xfee00000 */ 104*25c28e83SPiotr Jasiukajtis -6.93147180369123816490e-01, /* 0xbfe62e42, 0xfee00000 */ 105*25c28e83SPiotr Jasiukajtis }, 106*25c28e83SPiotr Jasiukajtis ln2LO[2] = { 107*25c28e83SPiotr Jasiukajtis 1.90821492927058770002e-10, /* 0x3dea39ef, 0x35793c76 */ 108*25c28e83SPiotr Jasiukajtis -1.90821492927058770002e-10, /* 0xbdea39ef, 0x35793c76 */ 109*25c28e83SPiotr Jasiukajtis }, 110*25c28e83SPiotr Jasiukajtis invln2 = 1.44269504088896338700e+00, /* 0x3ff71547, 0x652b82fe */ 111*25c28e83SPiotr Jasiukajtis P1 = 1.66666666666666019037e-01, /* 0x3FC55555, 0x5555553E */ 112*25c28e83SPiotr Jasiukajtis P2 = -2.77777777770155933842e-03, /* 0xBF66C16C, 0x16BEBD93 */ 113*25c28e83SPiotr Jasiukajtis P3 = 6.61375632143793436117e-05, /* 0x3F11566A, 0xAF25DE2C */ 114*25c28e83SPiotr Jasiukajtis P4 = -1.65339022054652515390e-06, /* 0xBEBBBD41, 0xC5D26BF1 */ 115*25c28e83SPiotr Jasiukajtis P5 = 4.13813679705723846039e-08; /* 0x3E663769, 0x72BEA4D0 */ 116*25c28e83SPiotr Jasiukajtis /* INDENT ON */ 117*25c28e83SPiotr Jasiukajtis 118*25c28e83SPiotr Jasiukajtis double 119*25c28e83SPiotr Jasiukajtis __k_cexp(double x, int *n) { 120*25c28e83SPiotr Jasiukajtis double hi = 0.0L, lo = 0.0L, c, t; 121*25c28e83SPiotr Jasiukajtis int k, xsb; 122*25c28e83SPiotr Jasiukajtis unsigned hx, lx; 123*25c28e83SPiotr Jasiukajtis 124*25c28e83SPiotr Jasiukajtis hx = HI_WORD(x); /* high word of x */ 125*25c28e83SPiotr Jasiukajtis lx = LO_WORD(x); /* low word of x */ 126*25c28e83SPiotr Jasiukajtis xsb = (hx >> 31) & 1; /* sign bit of x */ 127*25c28e83SPiotr Jasiukajtis hx &= 0x7fffffff; /* high word of |x| */ 128*25c28e83SPiotr Jasiukajtis 129*25c28e83SPiotr Jasiukajtis /* filter out non-finite argument */ 130*25c28e83SPiotr Jasiukajtis if (hx >= 0x40e86a00) { /* if |x| > 50000 */ 131*25c28e83SPiotr Jasiukajtis if (hx >= 0x7ff00000) { 132*25c28e83SPiotr Jasiukajtis *n = 1; 133*25c28e83SPiotr Jasiukajtis if (((hx & 0xfffff) | lx) != 0) 134*25c28e83SPiotr Jasiukajtis return (x + x); /* NaN */ 135*25c28e83SPiotr Jasiukajtis else 136*25c28e83SPiotr Jasiukajtis return ((xsb == 0) ? x : 0.0); 137*25c28e83SPiotr Jasiukajtis /* exp(+-inf)={inf,0} */ 138*25c28e83SPiotr Jasiukajtis } 139*25c28e83SPiotr Jasiukajtis *n = (xsb == 0) ? 50000 : -50000; 140*25c28e83SPiotr Jasiukajtis return (one + ln2LO[1] * ln2LO[1]); /* generate inexact */ 141*25c28e83SPiotr Jasiukajtis } 142*25c28e83SPiotr Jasiukajtis 143*25c28e83SPiotr Jasiukajtis *n = 0; 144*25c28e83SPiotr Jasiukajtis /* argument reduction */ 145*25c28e83SPiotr Jasiukajtis if (hx > 0x3fd62e42) { /* if |x| > 0.5 ln2 */ 146*25c28e83SPiotr Jasiukajtis if (hx < 0x3FF0A2B2) { /* and |x| < 1.5 ln2 */ 147*25c28e83SPiotr Jasiukajtis hi = x - ln2HI[xsb]; 148*25c28e83SPiotr Jasiukajtis lo = ln2LO[xsb]; 149*25c28e83SPiotr Jasiukajtis k = 1 - xsb - xsb; 150*25c28e83SPiotr Jasiukajtis } else { 151*25c28e83SPiotr Jasiukajtis k = (int) (invln2 * x + halF[xsb]); 152*25c28e83SPiotr Jasiukajtis t = k; 153*25c28e83SPiotr Jasiukajtis hi = x - t * ln2HI[0]; 154*25c28e83SPiotr Jasiukajtis /* t*ln2HI is exact for t<2**20 */ 155*25c28e83SPiotr Jasiukajtis lo = t * ln2LO[0]; 156*25c28e83SPiotr Jasiukajtis } 157*25c28e83SPiotr Jasiukajtis x = hi - lo; 158*25c28e83SPiotr Jasiukajtis *n = k; 159*25c28e83SPiotr Jasiukajtis } else if (hx < 0x3e300000) { /* when |x|<2**-28 */ 160*25c28e83SPiotr Jasiukajtis return (one + x); 161*25c28e83SPiotr Jasiukajtis } else 162*25c28e83SPiotr Jasiukajtis k = 0; 163*25c28e83SPiotr Jasiukajtis 164*25c28e83SPiotr Jasiukajtis /* x is now in primary range */ 165*25c28e83SPiotr Jasiukajtis t = x * x; 166*25c28e83SPiotr Jasiukajtis c = x - t * (P1 + t * (P2 + t * (P3 + t * (P4 + t * P5)))); 167*25c28e83SPiotr Jasiukajtis if (k == 0) 168*25c28e83SPiotr Jasiukajtis return (one - ((x * c) / (c - 2.0) - x)); 169*25c28e83SPiotr Jasiukajtis else { 170*25c28e83SPiotr Jasiukajtis t = one - ((lo - (x * c) / (2.0 - c)) - hi); 171*25c28e83SPiotr Jasiukajtis if (k > 128) { 172*25c28e83SPiotr Jasiukajtis t *= two128; 173*25c28e83SPiotr Jasiukajtis *n = k - 128; 174*25c28e83SPiotr Jasiukajtis } else if (k > 0) { 175*25c28e83SPiotr Jasiukajtis HI_WORD(t) += (k << 20); 176*25c28e83SPiotr Jasiukajtis *n = 0; 177*25c28e83SPiotr Jasiukajtis } 178*25c28e83SPiotr Jasiukajtis return (t); 179*25c28e83SPiotr Jasiukajtis } 180*25c28e83SPiotr Jasiukajtis } 181