trace(1): resolve all level-5 LLVM warnings
[minix3.git] / lib / libm / src / s_exp2f.c
blob17cdd197dab73dce0fecc48a2f32ad44526847da
1 /*-
2 * Copyright (c) 2005 David Schultz <das@FreeBSD.ORG>
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
27 #include <sys/cdefs.h>
28 __RCSID("$NetBSD: s_exp2f.c,v 1.2 2014/03/16 22:30:43 dsl Exp $");
29 #ifdef __FBSDID
30 __FBSDID("$FreeBSD: src/lib/msun/src/s_exp2f.c,v 1.9 2008/02/22 02:27:34 das Exp $");
31 #endif
33 #include <float.h>
35 #include "math.h"
36 #include "math_private.h"
38 #define TBLBITS 4
39 #define TBLSIZE (1 << TBLBITS)
41 static const float
42 redux = 0x1.8p23f / TBLSIZE,
43 P1 = 0x1.62e430p-1f,
44 P2 = 0x1.ebfbe0p-3f,
45 P3 = 0x1.c6b348p-5f,
46 P4 = 0x1.3b2c9cp-7f;
49 * For out of range values we need to generate the appropriate
50 * underflow or overflow trap as well as generating infinity or zero.
51 * This means we have to get the fpu to execute an instruction that
52 * will generate the trap (and not have the compiler optimise it away).
53 * This is normally done by calculating 'huge * huge' or 'tiny * tiny'.
55 * i386 is particularly problematic.
56 * The 'float' result is returned on the x87 stack, so is 'long double'.
57 * If we just multiply two 'float' values the caller will see 0x1p+/-200
58 * (not 0 or infinity).
59 * If we use 'double' the compiler does a store-load which will convert the
60 * value and generate the required exception.
62 #ifdef __i386__
63 static volatile double overflow = 0x1p+1000;
64 static volatile double underflow = 0x1p-1000;
65 #else
66 static volatile float huge = 0x1p+100;
67 static volatile float tiny = 0x1p-100;
68 #define overflow (huge * huge)
69 #define underflow (tiny * tiny)
70 #endif
72 static const double exp2ft[TBLSIZE] = {
73 0x1.6a09e667f3bcdp-1,
74 0x1.7a11473eb0187p-1,
75 0x1.8ace5422aa0dbp-1,
76 0x1.9c49182a3f090p-1,
77 0x1.ae89f995ad3adp-1,
78 0x1.c199bdd85529cp-1,
79 0x1.d5818dcfba487p-1,
80 0x1.ea4afa2a490dap-1,
81 0x1.0000000000000p+0,
82 0x1.0b5586cf9890fp+0,
83 0x1.172b83c7d517bp+0,
84 0x1.2387a6e756238p+0,
85 0x1.306fe0a31b715p+0,
86 0x1.3dea64c123422p+0,
87 0x1.4bfdad5362a27p+0,
88 0x1.5ab07dd485429p+0,
92 * exp2f(x): compute the base 2 exponential of x
94 * Accuracy: Peak error < 0.501 ulp; location of peak: -0.030110927.
96 * Method: (equally-spaced tables)
98 * Reduce x:
99 * x = 2**k + y, for integer k and |y| <= 1/2.
100 * Thus we have exp2f(x) = 2**k * exp2(y).
102 * Reduce y:
103 * y = i/TBLSIZE + z for integer i near y * TBLSIZE.
104 * Thus we have exp2(y) = exp2(i/TBLSIZE) * exp2(z),
105 * with |z| <= 2**-(TBLSIZE+1).
107 * We compute exp2(i/TBLSIZE) via table lookup and exp2(z) via a
108 * degree-4 minimax polynomial with maximum error under 1.4 * 2**-33.
109 * Using double precision for everything except the reduction makes
110 * roundoff error insignificant and simplifies the scaling step.
112 * This method is due to Tang, but I do not use his suggested parameters:
114 * Tang, P. Table-driven Implementation of the Exponential Function
115 * in IEEE Floating-Point Arithmetic. TOMS 15(2), 144-157 (1989).
117 float
118 exp2f(float x)
120 double tv, twopk, u, z;
121 float t;
122 uint32_t hx, ix, i0;
123 int32_t k;
125 /* Filter out exceptional cases. */
126 GET_FLOAT_WORD(hx, x);
127 ix = hx & 0x7fffffff; /* high word of |x| */
128 if(ix >= 0x43000000) { /* |x| >= 128 */
129 if(ix >= 0x7f800000) {
130 if ((ix & 0x7fffff) != 0 || (hx & 0x80000000) == 0)
131 return (x + x); /* x is NaN or +Inf */
132 else
133 return (0.0); /* x is -Inf */
135 if(x >= 0x1.0p7f)
136 return overflow; /* +infinity with overflow */
137 if(x <= -0x1.2cp7f)
138 return underflow; /* zero with underflow */
139 } else if (ix <= 0x33000000) { /* |x| <= 0x1p-25 */
140 return (1.0f + x);
143 /* Reduce x, computing z, i0, and k. */
144 STRICT_ASSIGN(float, t, x + redux);
145 GET_FLOAT_WORD(i0, t);
146 i0 += TBLSIZE / 2;
147 k = (i0 >> TBLBITS) << 20;
148 i0 &= TBLSIZE - 1;
149 t -= redux;
150 z = x - t;
151 INSERT_WORDS(twopk, 0x3ff00000 + k, 0);
153 /* Compute r = exp2(y) = exp2ft[i0] * p(z). */
154 tv = exp2ft[i0];
155 u = tv * z;
156 tv = tv + u * (P1 + z * P2) + u * (z * z) * (P3 + z * P4);
158 /* Scale by 2**(k>>20). */
159 return (tv * twopk);