Replace FSF snail mail address with URLs.
[glibc.git] / sysdeps / ieee754 / flt-32 / e_exp2f.c
blobe728e6ec74b32c353157d632cd642ee8e6762aeb
1 /* Single-precision floating point 2^x.
2 Copyright (C) 1997,1998,2000,2001,2005,2006,2011
3 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
5 Contributed by Geoffrey Keating <geoffk@ozemail.com.au>
7 The GNU C Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
12 The GNU C Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public
18 License along with the GNU C Library; if not, see
19 <http://www.gnu.org/licenses/>. */
21 /* The basic design here is from
22 Shmuel Gal and Boris Bachelis, "An Accurate Elementary Mathematical
23 Library for the IEEE Floating Point Standard", ACM Trans. Math. Soft.,
24 17 (1), March 1991, pp. 26-45.
25 It has been slightly modified to compute 2^x instead of e^x, and for
26 single-precision.
28 #ifndef _GNU_SOURCE
29 # define _GNU_SOURCE
30 #endif
31 #include <stdlib.h>
32 #include <float.h>
33 #include <ieee754.h>
34 #include <math.h>
35 #include <fenv.h>
36 #include <inttypes.h>
37 #include <math_private.h>
39 #include "t_exp2f.h"
41 static const volatile float TWOM100 = 7.88860905e-31;
42 static const volatile float TWO127 = 1.7014118346e+38;
44 float
45 __ieee754_exp2f (float x)
47 static const float himark = (float) FLT_MAX_EXP;
48 static const float lomark = (float) (FLT_MIN_EXP - FLT_MANT_DIG - 1);
50 /* Check for usual case. */
51 if (isless (x, himark) && isgreaterequal (x, lomark))
53 static const float THREEp14 = 49152.0;
54 int tval, unsafe;
55 float rx, x22, result;
56 union ieee754_float ex2_u, scale_u;
57 fenv_t oldenv;
59 libc_feholdexcept_setroundf (&oldenv, FE_TONEAREST);
61 /* 1. Argument reduction.
62 Choose integers ex, -128 <= t < 128, and some real
63 -1/512 <= x1 <= 1/512 so that
64 x = ex + t/512 + x1.
66 First, calculate rx = ex + t/256. */
67 rx = x + THREEp14;
68 rx -= THREEp14;
69 x -= rx; /* Compute x=x1. */
70 /* Compute tval = (ex*256 + t)+128.
71 Now, t = (tval mod 256)-128 and ex=tval/256 [that's mod, NOT %; and
72 /-round-to-nearest not the usual c integer /]. */
73 tval = (int) (rx * 256.0f + 128.0f);
75 /* 2. Adjust for accurate table entry.
76 Find e so that
77 x = ex + t/256 + e + x2
78 where -7e-4 < e < 7e-4, and
79 (float)(2^(t/256+e))
80 is accurate to one part in 2^-64. */
82 /* 'tval & 255' is the same as 'tval%256' except that it's always
83 positive.
84 Compute x = x2. */
85 x -= __exp2f_deltatable[tval & 255];
87 /* 3. Compute ex2 = 2^(t/255+e+ex). */
88 ex2_u.f = __exp2f_atable[tval & 255];
89 tval >>= 8;
90 unsafe = abs(tval) >= -FLT_MIN_EXP - 1;
91 ex2_u.ieee.exponent += tval >> unsafe;
92 scale_u.f = 1.0;
93 scale_u.ieee.exponent += tval - (tval >> unsafe);
95 /* 4. Approximate 2^x2 - 1, using a second-degree polynomial,
96 with maximum error in [-2^-9 - 2^-14, 2^-9 + 2^-14]
97 less than 1.3e-10. */
99 x22 = (.24022656679f * x + .69314736128f) * ex2_u.f;
101 /* 5. Return (2^x2-1) * 2^(t/512+e+ex) + 2^(t/512+e+ex). */
102 libc_fesetenv (&oldenv);
104 result = x22 * x + ex2_u.f;
106 if (!unsafe)
107 return result;
108 else
109 return result * scale_u.f;
111 /* Exceptional cases: */
112 else if (isless (x, himark))
114 if (__isinf_nsf (x))
115 /* e^-inf == 0, with no error. */
116 return 0;
117 else
118 /* Underflow */
119 return TWOM100 * TWOM100;
121 else
122 /* Return x, if x is a NaN or Inf; or overflow, otherwise. */
123 return TWO127*x;
125 strong_alias (__ieee754_exp2f, __exp2f_finite)