s_exp2f.c revision 175501
1144648Sdas/*- 2144648Sdas * Copyright (c) 2005 David Schultz <das@FreeBSD.ORG> 3144648Sdas * All rights reserved. 4144648Sdas * 5144648Sdas * Redistribution and use in source and binary forms, with or without 6144648Sdas * modification, are permitted provided that the following conditions 7144648Sdas * are met: 8144648Sdas * 1. Redistributions of source code must retain the above copyright 9144648Sdas * notice, this list of conditions and the following disclaimer. 10144648Sdas * 2. Redistributions in binary form must reproduce the above copyright 11144648Sdas * notice, this list of conditions and the following disclaimer in the 12144648Sdas * documentation and/or other materials provided with the distribution. 13144648Sdas * 14144648Sdas * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15144648Sdas * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16144648Sdas * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17144648Sdas * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18144648Sdas * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19144648Sdas * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20144648Sdas * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21144648Sdas * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22144648Sdas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23144648Sdas * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24144648Sdas * SUCH DAMAGE. 25144648Sdas */ 26144648Sdas 27144648Sdas#include <sys/cdefs.h> 28144648Sdas__FBSDID("$FreeBSD: head/lib/msun/src/s_exp2f.c 175501 2008-01-19 21:37:14Z bde $"); 29144648Sdas 30175501Sbde#include <float.h> 31175501Sbde 32144648Sdas#include "math.h" 33144648Sdas#include "math_private.h" 34144648Sdas 35144648Sdas#define TBLBITS 4 36144648Sdas#define TBLSIZE (1 << TBLBITS) 37144648Sdas 38144648Sdasstatic const float 39144648Sdas huge = 0x1p100f, 40144648Sdas redux = 0x1.8p23f / TBLSIZE, 41144648Sdas P1 = 0x1.62e430p-1f, 42144648Sdas P2 = 0x1.ebfbe0p-3f, 43144648Sdas P3 = 0x1.c6b348p-5f, 44144648Sdas P4 = 0x1.3b2c9cp-7f; 45144648Sdas 46175468Sdasstatic volatile float twom100 = 0x1p-100f; 47175468Sdas 48144648Sdasstatic const double exp2ft[TBLSIZE] = { 49144648Sdas 0x1.6a09e667f3bcdp-1, 50144648Sdas 0x1.7a11473eb0187p-1, 51144648Sdas 0x1.8ace5422aa0dbp-1, 52144648Sdas 0x1.9c49182a3f090p-1, 53144648Sdas 0x1.ae89f995ad3adp-1, 54144648Sdas 0x1.c199bdd85529cp-1, 55144648Sdas 0x1.d5818dcfba487p-1, 56144648Sdas 0x1.ea4afa2a490dap-1, 57144648Sdas 0x1.0000000000000p+0, 58144648Sdas 0x1.0b5586cf9890fp+0, 59144648Sdas 0x1.172b83c7d517bp+0, 60144648Sdas 0x1.2387a6e756238p+0, 61144648Sdas 0x1.306fe0a31b715p+0, 62144648Sdas 0x1.3dea64c123422p+0, 63144648Sdas 0x1.4bfdad5362a27p+0, 64144648Sdas 0x1.5ab07dd485429p+0, 65144648Sdas}; 66144648Sdas 67144648Sdas/* 68144648Sdas * exp2f(x): compute the base 2 exponential of x 69144648Sdas * 70144648Sdas * Accuracy: Peak error < 0.501 ulp; location of peak: -0.030110927. 71144648Sdas * 72144648Sdas * Method: (equally-spaced tables) 73144648Sdas * 74144648Sdas * Reduce x: 75144648Sdas * x = 2**k + y, for integer k and |y| <= 1/2. 76144648Sdas * Thus we have exp2f(x) = 2**k * exp2(y). 77144648Sdas * 78144648Sdas * Reduce y: 79144648Sdas * y = i/TBLSIZE + z for integer i near y * TBLSIZE. 80144648Sdas * Thus we have exp2(y) = exp2(i/TBLSIZE) * exp2(z), 81144648Sdas * with |z| <= 2**-(TBLSIZE+1). 82144648Sdas * 83144648Sdas * We compute exp2(i/TBLSIZE) via table lookup and exp2(z) via a 84144648Sdas * degree-4 minimax polynomial with maximum error under 1.4 * 2**-33. 85144648Sdas * Using double precision in the final calculation avoids roundoff error. 86144648Sdas * 87144648Sdas * This method is due to Tang, but I do not use his suggested parameters: 88144648Sdas * 89144648Sdas * Tang, P. Table-driven Implementation of the Exponential Function 90144648Sdas * in IEEE Floating-Point Arithmetic. TOMS 15(2), 144-157 (1989). 91144648Sdas */ 92144648Sdasfloat 93144648Sdasexp2f(float x) 94144648Sdas{ 95144648Sdas double tv; 96175501Sbde float r, t, z; 97144648Sdas uint32_t hx, hr, ix, i0; 98144648Sdas int32_t k; 99144648Sdas 100144648Sdas /* Filter out exceptional cases. */ 101144648Sdas GET_FLOAT_WORD(hx,x); 102144648Sdas ix = hx & 0x7fffffff; /* high word of |x| */ 103144648Sdas if(ix >= 0x43000000) { /* |x| >= 128 */ 104144648Sdas if(ix >= 0x7f800000) { 105144648Sdas if ((ix & 0x7fffff) != 0 || (hx & 0x80000000) == 0) 106144648Sdas return (x); /* x is NaN or +Inf */ 107144648Sdas else 108144648Sdas return (0.0); /* x is -Inf */ 109144648Sdas } 110144648Sdas if(x >= 0x1.0p7f) 111144648Sdas return (huge * huge); /* overflow */ 112144648Sdas if(x <= -0x1.2cp7f) 113144648Sdas return (twom100 * twom100); /* underflow */ 114144648Sdas } else if (ix <= 0x33000000) { /* |x| <= 0x1p-25 */ 115144648Sdas return (1.0f + x); 116144648Sdas } 117144648Sdas 118144648Sdas /* Reduce x, computing z, i0, and k. */ 119175501Sbde STRICT_ASSIGN(float, t, x + redux); 120144648Sdas GET_FLOAT_WORD(i0, t); 121144648Sdas i0 += TBLSIZE / 2; 122144648Sdas k = (i0 >> TBLBITS) << 23; 123144648Sdas i0 &= TBLSIZE - 1; 124144648Sdas t -= redux; 125144648Sdas z = x - t; 126144648Sdas 127144648Sdas /* Compute r = exp2(y) = exp2ft[i0] * p(z). */ 128144648Sdas tv = exp2ft[i0]; 129144648Sdas r = tv + tv * (z * (P1 + z * (P2 + z * (P3 + z * P4)))); 130144648Sdas 131144648Sdas /* Scale by 2**(k>>23). */ 132144648Sdas if(k >= -125 << 23) { 133144648Sdas if (k != 0) { 134144648Sdas GET_FLOAT_WORD(hr, r); 135144648Sdas SET_FLOAT_WORD(r, hr + k); 136144648Sdas } 137144648Sdas return (r); 138144648Sdas } else { 139144648Sdas GET_FLOAT_WORD(hr, r); 140144648Sdas SET_FLOAT_WORD(r, hr + (k + (100 << 23))); 141144648Sdas return (r * twom100); 142144648Sdas } 143144648Sdas} 144