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$"); 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 redux = 0x1.8p23f / TBLSIZE, 40144648Sdas P1 = 0x1.62e430p-1f, 41144648Sdas P2 = 0x1.ebfbe0p-3f, 42144648Sdas P3 = 0x1.c6b348p-5f, 43144648Sdas P4 = 0x1.3b2c9cp-7f; 44144648Sdas 45251024Sdasstatic volatile float 46251024Sdas huge = 0x1p100f, 47251024Sdas twom100 = 0x1p-100f; 48175468Sdas 49144648Sdasstatic const double exp2ft[TBLSIZE] = { 50144648Sdas 0x1.6a09e667f3bcdp-1, 51144648Sdas 0x1.7a11473eb0187p-1, 52144648Sdas 0x1.8ace5422aa0dbp-1, 53144648Sdas 0x1.9c49182a3f090p-1, 54144648Sdas 0x1.ae89f995ad3adp-1, 55144648Sdas 0x1.c199bdd85529cp-1, 56144648Sdas 0x1.d5818dcfba487p-1, 57144648Sdas 0x1.ea4afa2a490dap-1, 58144648Sdas 0x1.0000000000000p+0, 59144648Sdas 0x1.0b5586cf9890fp+0, 60144648Sdas 0x1.172b83c7d517bp+0, 61144648Sdas 0x1.2387a6e756238p+0, 62144648Sdas 0x1.306fe0a31b715p+0, 63144648Sdas 0x1.3dea64c123422p+0, 64144648Sdas 0x1.4bfdad5362a27p+0, 65144648Sdas 0x1.5ab07dd485429p+0, 66144648Sdas}; 67144648Sdas 68144648Sdas/* 69144648Sdas * exp2f(x): compute the base 2 exponential of x 70144648Sdas * 71144648Sdas * Accuracy: Peak error < 0.501 ulp; location of peak: -0.030110927. 72144648Sdas * 73144648Sdas * Method: (equally-spaced tables) 74144648Sdas * 75144648Sdas * Reduce x: 76144648Sdas * x = 2**k + y, for integer k and |y| <= 1/2. 77144648Sdas * Thus we have exp2f(x) = 2**k * exp2(y). 78144648Sdas * 79144648Sdas * Reduce y: 80144648Sdas * y = i/TBLSIZE + z for integer i near y * TBLSIZE. 81144648Sdas * Thus we have exp2(y) = exp2(i/TBLSIZE) * exp2(z), 82144648Sdas * with |z| <= 2**-(TBLSIZE+1). 83144648Sdas * 84144648Sdas * We compute exp2(i/TBLSIZE) via table lookup and exp2(z) via a 85144648Sdas * degree-4 minimax polynomial with maximum error under 1.4 * 2**-33. 86176168Sbde * Using double precision for everything except the reduction makes 87176168Sbde * roundoff error insignificant and simplifies the scaling step. 88144648Sdas * 89144648Sdas * This method is due to Tang, but I do not use his suggested parameters: 90144648Sdas * 91144648Sdas * Tang, P. Table-driven Implementation of the Exponential Function 92144648Sdas * in IEEE Floating-Point Arithmetic. TOMS 15(2), 144-157 (1989). 93144648Sdas */ 94144648Sdasfloat 95144648Sdasexp2f(float x) 96144648Sdas{ 97176229Sbde double tv, twopk, u, z; 98176168Sbde float t; 99176450Sdas uint32_t hx, ix, i0; 100144648Sdas int32_t k; 101144648Sdas 102144648Sdas /* Filter out exceptional cases. */ 103175731Sdas GET_FLOAT_WORD(hx, x); 104144648Sdas ix = hx & 0x7fffffff; /* high word of |x| */ 105144648Sdas if(ix >= 0x43000000) { /* |x| >= 128 */ 106144648Sdas if(ix >= 0x7f800000) { 107144648Sdas if ((ix & 0x7fffff) != 0 || (hx & 0x80000000) == 0) 108176231Sbde return (x + x); /* x is NaN or +Inf */ 109144648Sdas else 110144648Sdas return (0.0); /* x is -Inf */ 111144648Sdas } 112144648Sdas if(x >= 0x1.0p7f) 113144648Sdas return (huge * huge); /* overflow */ 114144648Sdas if(x <= -0x1.2cp7f) 115144648Sdas return (twom100 * twom100); /* underflow */ 116144648Sdas } else if (ix <= 0x33000000) { /* |x| <= 0x1p-25 */ 117144648Sdas return (1.0f + x); 118144648Sdas } 119144648Sdas 120144648Sdas /* Reduce x, computing z, i0, and k. */ 121175501Sbde STRICT_ASSIGN(float, t, x + redux); 122144648Sdas GET_FLOAT_WORD(i0, t); 123144648Sdas i0 += TBLSIZE / 2; 124175731Sdas k = (i0 >> TBLBITS) << 20; 125144648Sdas i0 &= TBLSIZE - 1; 126144648Sdas t -= redux; 127144648Sdas z = x - t; 128176229Sbde INSERT_WORDS(twopk, 0x3ff00000 + k, 0); 129144648Sdas 130144648Sdas /* Compute r = exp2(y) = exp2ft[i0] * p(z). */ 131144648Sdas tv = exp2ft[i0]; 132176229Sbde u = tv * z; 133176229Sbde tv = tv + u * (P1 + z * P2) + u * (z * z) * (P3 + z * P4); 134144648Sdas 135175731Sdas /* Scale by 2**(k>>20). */ 136176074Sbde return (tv * twopk); 137144648Sdas} 138