1 1.1 martin /*- 2 1.1 martin * Copyright (c) 2005 David Schultz <das (at) FreeBSD.ORG> 3 1.1 martin * All rights reserved. 4 1.1 martin * 5 1.1 martin * Redistribution and use in source and binary forms, with or without 6 1.1 martin * modification, are permitted provided that the following conditions 7 1.1 martin * are met: 8 1.1 martin * 1. Redistributions of source code must retain the above copyright 9 1.1 martin * notice, this list of conditions and the following disclaimer. 10 1.1 martin * 2. Redistributions in binary form must reproduce the above copyright 11 1.1 martin * notice, this list of conditions and the following disclaimer in the 12 1.1 martin * documentation and/or other materials provided with the distribution. 13 1.1 martin * 14 1.1 martin * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 1.1 martin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 1.1 martin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 1.1 martin * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 1.1 martin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 1.1 martin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 1.1 martin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 1.1 martin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 1.1 martin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 1.1 martin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 1.1 martin * SUCH DAMAGE. 25 1.1 martin */ 26 1.1 martin 27 1.1 martin #include <sys/cdefs.h> 28 1.2 martin __RCSID("$NetBSD: n_exp2f.c,v 1.2 2014/03/23 15:26:47 martin Exp $"); 29 1.1 martin #ifdef __FBSDID 30 1.1 martin __FBSDID("$FreeBSD: src/lib/msun/src/s_exp2f.c,v 1.9 2008/02/22 02:27:34 das Exp $"); 31 1.1 martin #endif 32 1.1 martin 33 1.1 martin #include <stdint.h> 34 1.1 martin #include <float.h> 35 1.2 martin #include <string.h> 36 1.1 martin 37 1.1 martin #include "math.h" 38 1.1 martin 39 1.1 martin #define TBLBITS 4 40 1.1 martin #define TBLSIZE (1 << TBLBITS) 41 1.1 martin 42 1.1 martin static const float 43 1.1 martin huge = 0x1p100f, 44 1.1 martin redux = 0x1.8p23f / TBLSIZE, 45 1.1 martin P1 = 0x1.62e430p-1f, 46 1.1 martin P2 = 0x1.ebfbe0p-3f, 47 1.1 martin P3 = 0x1.c6b348p-5f, 48 1.1 martin P4 = 0x1.3b2c9cp-7f; 49 1.1 martin 50 1.1 martin static volatile float twom100 = 0x1p-100f; 51 1.1 martin 52 1.1 martin static const double exp2ft[TBLSIZE] = { 53 1.1 martin 0x1.6a09e667f3bcdp-1, 54 1.1 martin 0x1.7a11473eb0187p-1, 55 1.1 martin 0x1.8ace5422aa0dbp-1, 56 1.1 martin 0x1.9c49182a3f090p-1, 57 1.1 martin 0x1.ae89f995ad3adp-1, 58 1.1 martin 0x1.c199bdd85529cp-1, 59 1.1 martin 0x1.d5818dcfba487p-1, 60 1.1 martin 0x1.ea4afa2a490dap-1, 61 1.1 martin 0x1.0000000000000p+0, 62 1.1 martin 0x1.0b5586cf9890fp+0, 63 1.1 martin 0x1.172b83c7d517bp+0, 64 1.1 martin 0x1.2387a6e756238p+0, 65 1.1 martin 0x1.306fe0a31b715p+0, 66 1.1 martin 0x1.3dea64c123422p+0, 67 1.1 martin 0x1.4bfdad5362a27p+0, 68 1.1 martin 0x1.5ab07dd485429p+0, 69 1.1 martin }; 70 1.1 martin 71 1.1 martin /* 72 1.1 martin * exp2f(x): compute the base 2 exponential of x 73 1.1 martin * 74 1.1 martin * Accuracy: Peak error < 0.501 ulp; location of peak: -0.030110927. 75 1.1 martin * 76 1.1 martin * Method: (equally-spaced tables) 77 1.1 martin * 78 1.1 martin * Reduce x: 79 1.1 martin * x = 2**k + y, for integer k and |y| <= 1/2. 80 1.1 martin * Thus we have exp2f(x) = 2**k * exp2(y). 81 1.1 martin * 82 1.1 martin * Reduce y: 83 1.1 martin * y = i/TBLSIZE + z for integer i near y * TBLSIZE. 84 1.1 martin * Thus we have exp2(y) = exp2(i/TBLSIZE) * exp2(z), 85 1.1 martin * with |z| <= 2**-(TBLSIZE+1). 86 1.1 martin * 87 1.1 martin * We compute exp2(i/TBLSIZE) via table lookup and exp2(z) via a 88 1.1 martin * degree-4 minimax polynomial with maximum error under 1.4 * 2**-33. 89 1.1 martin * Using double precision for everything except the reduction makes 90 1.1 martin * roundoff error insignificant and simplifies the scaling step. 91 1.1 martin * 92 1.1 martin * This method is due to Tang, but I do not use his suggested parameters: 93 1.1 martin * 94 1.1 martin * Tang, P. Table-driven Implementation of the Exponential Function 95 1.1 martin * in IEEE Floating-Point Arithmetic. TOMS 15(2), 144-157 (1989). 96 1.1 martin */ 97 1.1 martin float 98 1.1 martin exp2f(float x) 99 1.1 martin { 100 1.1 martin double tv, twopk, u, z; 101 1.1 martin float t; 102 1.1 martin uint32_t hx, ix, i0; 103 1.2 martin int32_t k, temp; 104 1.1 martin 105 1.1 martin /* Filter out exceptional cases. */ 106 1.2 martin memcpy(&hx, &x, sizeof(hx)); 107 1.1 martin ix = hx & 0x7fffffff; /* high word of |x| */ 108 1.1 martin if(ix >= 0x43000000) { /* |x| >= 128 */ 109 1.1 martin if(x >= 0x1.0p7f) 110 1.1 martin return (huge * huge); /* overflow */ 111 1.1 martin if(x <= -0x1.2cp7f) 112 1.1 martin return (twom100 * twom100); /* underflow */ 113 1.1 martin } else if (ix <= 0x33000000) { /* |x| <= 0x1p-25 */ 114 1.1 martin return (1.0f + x); 115 1.1 martin } 116 1.1 martin 117 1.1 martin /* Reduce x, computing z, i0, and k. */ 118 1.2 martin i0 = x + redux; 119 1.2 martin memcpy(&t, &i0, sizeof(t)); 120 1.1 martin i0 += TBLSIZE / 2; 121 1.1 martin k = (i0 >> TBLBITS) << 20; 122 1.1 martin i0 &= TBLSIZE - 1; 123 1.1 martin t -= redux; 124 1.1 martin z = x - t; 125 1.2 martin temp = 0x3ff00000+k; 126 1.2 martin twopk = 0.0; 127 1.2 martin memcpy(&twopk, &temp, sizeof(temp)); 128 1.1 martin 129 1.1 martin /* Compute r = exp2(y) = exp2ft[i0] * p(z). */ 130 1.1 martin tv = exp2ft[i0]; 131 1.1 martin u = tv * z; 132 1.1 martin tv = tv + u * (P1 + z * P2) + u * (z * z) * (P3 + z * P4); 133 1.1 martin 134 1.1 martin /* Scale by 2**(k>>20). */ 135 1.1 martin return (tv * twopk); 136 1.1 martin } 137