s_exp2f.c revision 251024
150477Speter/*- 212031Sache * Copyright (c) 2005 David Schultz <das@FreeBSD.ORG> 386071Sache * All rights reserved. 4123682Sache * 5123682Sache * Redistribution and use in source and binary forms, with or without 6123682Sache * modification, are permitted provided that the following conditions 7108428Sache * are met: 8108428Sache * 1. Redistributions of source code must retain the above copyright 986071Sache * notice, this list of conditions and the following disclaimer. 1088473Sphantom * 2. Redistributions in binary form must reproduce the above copyright 1177980Sache * notice, this list of conditions and the following disclaimer in the 1277980Sache * documentation and/or other materials provided with the distribution. 1388473Sphantom * 1477980Sache * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1577980Sache * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1687043Sache * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17193961Sedwin * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18117259Sache * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 1988473Sphantom * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2077980Sache * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21125208Sache * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2288473Sphantom * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23196788Sache * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2477980Sache * SUCH DAMAGE. 2577980Sache */ 2677980Sache 2788473Sphantom#include <sys/cdefs.h> 28180939Sdes__FBSDID("$FreeBSD: head/lib/msun/src/s_exp2f.c 251024 2013-05-27 08:50:10Z das $"); 29180939Sdes 3077980Sache#include <float.h> 3199961Sache 32115921Sache#include "math.h" 3370484Sphantom#include "math_private.h" 3477980Sache 3570484Sphantom#define TBLBITS 4 36120921Sache#define TBLSIZE (1 << TBLBITS) 37105444Sache 3888473Sphantomstatic const float 3977980Sache redux = 0x1.8p23f / TBLSIZE, 40174887Sache P1 = 0x1.62e430p-1f, 41143126Sru P2 = 0x1.ebfbe0p-3f, 4288314Sache P3 = 0x1.c6b348p-5f, 4352389Sache P4 = 0x1.3b2c9cp-7f; 4423228Swosch 45136596Srustatic volatile float 4612031Sache huge = 0x1p100f, 47136596Sru twom100 = 0x1p-100f; 4824275Sache 49136596Srustatic const double exp2ft[TBLSIZE] = { 50136596Sru 0x1.6a09e667f3bcdp-1, 51136596Sru 0x1.7a11473eb0187p-1, 52136596Sru 0x1.8ace5422aa0dbp-1, 53136596Sru 0x1.9c49182a3f090p-1, 54136596Sru 0x1.ae89f995ad3adp-1, 55136596Sru 0x1.c199bdd85529cp-1, 56136596Sru 0x1.d5818dcfba487p-1, 57136596Sru 0x1.ea4afa2a490dap-1, 58136596Sru 0x1.0000000000000p+0, 59136596Sru 0x1.0b5586cf9890fp+0, 60136596Sru 0x1.172b83c7d517bp+0, 61136596Sru 0x1.2387a6e756238p+0, 62136596Sru 0x1.306fe0a31b715p+0, 63136596Sru 0x1.3dea64c123422p+0, 64136596Sru 0x1.4bfdad5362a27p+0, 65196788Sache 0x1.5ab07dd485429p+0, 66136596Sru}; 67136596Sru 68136596Sru/* 69136596Sru * exp2f(x): compute the base 2 exponential of x 70134437Stjr * 71180939Sdes * Accuracy: Peak error < 0.501 ulp; location of peak: -0.030110927. 72180939Sdes * 73193908Sedwin * Method: (equally-spaced tables) 74180939Sdes * 75180939Sdes * Reduce x: 76180939Sdes * x = 2**k + y, for integer k and |y| <= 1/2. 7745544Sfoxfair * Thus we have exp2f(x) = 2**k * exp2(y). 78193961Sedwin * 79136596Sru * Reduce y: 8012031Sache * y = i/TBLSIZE + z for integer i near y * TBLSIZE. 81196788Sache * Thus we have exp2(y) = exp2(i/TBLSIZE) * exp2(z), 82196788Sache * with |z| <= 2**-(TBLSIZE+1). 83196788Sache * 84136596Sru * We compute exp2(i/TBLSIZE) via table lookup and exp2(z) via a 85136596Sru * degree-4 minimax polynomial with maximum error under 1.4 * 2**-33. 86136596Sru * Using double precision for everything except the reduction makes 87136596Sru * roundoff error insignificant and simplifies the scaling step. 88136596Sru * 89136596Sru * This method is due to Tang, but I do not use his suggested parameters: 90136596Sru * 91136596Sru * Tang, P. Table-driven Implementation of the Exponential Function 92136596Sru * in IEEE Floating-Point Arithmetic. TOMS 15(2), 144-157 (1989). 93136596Sru */ 94136596Srufloat 95136596Sruexp2f(float x) 96136596Sru{ 97193908Sedwin double tv, twopk, u, z; 98193908Sedwin float t; 99193908Sedwin uint32_t hx, ix, i0; 100193908Sedwin int32_t k; 101196788Sache 102164131Sdes /* Filter out exceptional cases. */ 103128526Stjr GET_FLOAT_WORD(hx, x); 104128526Stjr ix = hx & 0x7fffffff; /* high word of |x| */ 105136596Sru if(ix >= 0x43000000) { /* |x| >= 128 */ 106127474Stjr if(ix >= 0x7f800000) { 107136596Sru if ((ix & 0x7fffff) != 0 || (hx & 0x80000000) == 0) 108136596Sru return (x + x); /* x is NaN or +Inf */ 109136596Sru else 110136596Sru return (0.0); /* x is -Inf */ 111136596Sru } 112136596Sru if(x >= 0x1.0p7f) 11323228Swosch return (huge * huge); /* overflow */ 11424275Sache if(x <= -0x1.2cp7f) 11545544Sfoxfair return (twom100 * twom100); /* underflow */ 11612031Sache } else if (ix <= 0x33000000) { /* |x| <= 0x1p-25 */ 11712031Sache return (1.0f + x); 118 } 119 120 /* Reduce x, computing z, i0, and k. */ 121 STRICT_ASSIGN(float, t, x + redux); 122 GET_FLOAT_WORD(i0, t); 123 i0 += TBLSIZE / 2; 124 k = (i0 >> TBLBITS) << 20; 125 i0 &= TBLSIZE - 1; 126 t -= redux; 127 z = x - t; 128 INSERT_WORDS(twopk, 0x3ff00000 + k, 0); 129 130 /* Compute r = exp2(y) = exp2ft[i0] * p(z). */ 131 tv = exp2ft[i0]; 132 u = tv * z; 133 tv = tv + u * (P1 + z * P2) + u * (z * z) * (P3 + z * P4); 134 135 /* Scale by 2**(k>>20). */ 136 return (tv * twopk); 137} 138