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
37 #include <math_private.h>
41 static const volatile float TWOM100
= 7.88860905e-31;
42 static const volatile float TWO127
= 1.7014118346e+38;
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;
55 float rx
, x22
, result
;
56 union ieee754_float ex2_u
, scale_u
;
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
66 First, calculate rx = ex + t/256. */
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.
77 x = ex + t/256 + e + x2
78 where -7e-4 < e < 7e-4, and
80 is accurate to one part in 2^-64. */
82 /* 'tval & 255' is the same as 'tval%256' except that it's always
85 x
-= __exp2f_deltatable
[tval
& 255];
87 /* 3. Compute ex2 = 2^(t/255+e+ex). */
88 ex2_u
.f
= __exp2f_atable
[tval
& 255];
90 unsafe
= abs(tval
) >= -FLT_MIN_EXP
- 1;
91 ex2_u
.ieee
.exponent
+= tval
>> unsafe
;
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]
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
;
109 return result
* scale_u
.f
;
111 /* Exceptional cases: */
112 else if (isless (x
, himark
))
115 /* e^-inf == 0, with no error. */
119 return TWOM100
* TWOM100
;
122 /* Return x, if x is a NaN or Inf; or overflow, otherwise. */
125 strong_alias (__ieee754_exp2f
, __exp2f_finite
)