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