alsa.audio: limit the supported frequencies to common set
[AROS.git] / compiler / stdc / math / s_exp2f.c
blob410d58023eb8c5dfcfa9d0c356a6e05475a650b7
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 __FBSDID("$FreeBSD: src/lib/msun/src/s_exp2f.c,v 1.1 2005/04/05 02:57:15 das Exp $");
29 #include "math.h"
30 #include "math_private.h"
32 #define TBLBITS 4
33 #define TBLSIZE (1 << TBLBITS)
35 static const float
36 huge = 0x1p100f,
37 twom100 = 0x1p-100f,
38 redux = 0x1.8p23f / TBLSIZE,
39 P1 = 0x1.62e430p-1f,
40 P2 = 0x1.ebfbe0p-3f,
41 P3 = 0x1.c6b348p-5f,
42 P4 = 0x1.3b2c9cp-7f;
44 static const double exp2ft[TBLSIZE] = {
45 0x1.6a09e667f3bcdp-1,
46 0x1.7a11473eb0187p-1,
47 0x1.8ace5422aa0dbp-1,
48 0x1.9c49182a3f090p-1,
49 0x1.ae89f995ad3adp-1,
50 0x1.c199bdd85529cp-1,
51 0x1.d5818dcfba487p-1,
52 0x1.ea4afa2a490dap-1,
53 0x1.0000000000000p+0,
54 0x1.0b5586cf9890fp+0,
55 0x1.172b83c7d517bp+0,
56 0x1.2387a6e756238p+0,
57 0x1.306fe0a31b715p+0,
58 0x1.3dea64c123422p+0,
59 0x1.4bfdad5362a27p+0,
60 0x1.5ab07dd485429p+0,
64 * exp2f(x): compute the base 2 exponential of x
66 * Accuracy: Peak error < 0.501 ulp; location of peak: -0.030110927.
68 * Method: (equally-spaced tables)
70 * Reduce x:
71 * x = 2**k + y, for integer k and |y| <= 1/2.
72 * Thus we have exp2f(x) = 2**k * exp2(y).
74 * Reduce y:
75 * y = i/TBLSIZE + z for integer i near y * TBLSIZE.
76 * Thus we have exp2(y) = exp2(i/TBLSIZE) * exp2(z),
77 * with |z| <= 2**-(TBLSIZE+1).
79 * We compute exp2(i/TBLSIZE) via table lookup and exp2(z) via a
80 * degree-4 minimax polynomial with maximum error under 1.4 * 2**-33.
81 * Using double precision in the final calculation avoids roundoff error.
83 * This method is due to Tang, but I do not use his suggested parameters:
85 * Tang, P. Table-driven Implementation of the Exponential Function
86 * in IEEE Floating-Point Arithmetic. TOMS 15(2), 144-157 (1989).
88 float
89 exp2f(float x)
91 double tv;
92 float r, z;
93 volatile float t; /* prevent gcc from using too much precision */
94 uint32_t hx, hr, ix, i0;
95 int32_t k;
97 /* Filter out exceptional cases. */
98 GET_FLOAT_WORD(hx,x);
99 ix = hx & 0x7fffffff; /* high word of |x| */
100 if(ix >= 0x43000000) { /* |x| >= 128 */
101 if(ix >= 0x7f800000) {
102 if ((ix & 0x7fffff) != 0 || (hx & 0x80000000) == 0)
103 return (x); /* x is NaN or +Inf */
104 else
105 return (0.0); /* x is -Inf */
107 if(x >= 0x1.0p7f)
108 return (huge * huge); /* overflow */
109 if(x <= -0x1.2cp7f)
110 return (twom100 * twom100); /* underflow */
111 } else if (ix <= 0x33000000) { /* |x| <= 0x1p-25 */
112 return (1.0f + x);
115 /* Reduce x, computing z, i0, and k. */
116 t = x + redux;
117 GET_FLOAT_WORD(i0, t);
118 i0 += TBLSIZE / 2;
119 k = (i0 >> TBLBITS) << 23;
120 i0 &= TBLSIZE - 1;
121 t -= redux;
122 z = x - t;
124 /* Compute r = exp2(y) = exp2ft[i0] * p(z). */
125 tv = exp2ft[i0];
126 r = tv + tv * (z * (P1 + z * (P2 + z * (P3 + z * P4))));
128 /* Scale by 2**(k>>23). */
129 if(k >= -125 << 23) {
130 if (k != 0) {
131 GET_FLOAT_WORD(hr, r);
132 SET_FLOAT_WORD(r, hr + k);
134 return (r);
135 } else {
136 GET_FLOAT_WORD(hr, r);
137 SET_FLOAT_WORD(r, hr + (k + (100 << 23)));
138 return (r * twom100);