1 | /* Single-precision 2^x function. |
2 | Copyright (C) 2017-2018 Free Software Foundation, Inc. |
3 | This file is part of the GNU C Library. |
4 | |
5 | The GNU C Library is free software; you can redistribute it and/or |
6 | modify it under the terms of the GNU Lesser General Public |
7 | License as published by the Free Software Foundation; either |
8 | version 2.1 of the License, or (at your option) any later version. |
9 | |
10 | The GNU C Library is distributed in the hope that it will be useful, |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | Lesser General Public License for more details. |
14 | |
15 | You should have received a copy of the GNU Lesser General Public |
16 | License along with the GNU C Library; if not, see |
17 | <http://www.gnu.org/licenses/>. */ |
18 | |
19 | #include <math.h> |
20 | #include <stdint.h> |
21 | #include <shlib-compat.h> |
22 | #include <libm-alias-float.h> |
23 | #include "math_config.h" |
24 | |
25 | /* |
26 | EXP2F_TABLE_BITS = 5 |
27 | EXP2F_POLY_ORDER = 3 |
28 | |
29 | ULP error: 0.502 (nearest rounding.) |
30 | Relative error: 1.69 * 2^-34 in [-1/64, 1/64] (before rounding.) |
31 | Wrong count: 168353 (all nearest rounding wrong results with fma.) |
32 | Non-nearest ULP error: 1 (rounded ULP error) |
33 | */ |
34 | |
35 | #define N (1 << EXP2F_TABLE_BITS) |
36 | #define T __exp2f_data.tab |
37 | #define C __exp2f_data.poly |
38 | #define SHIFT __exp2f_data.shift_scaled |
39 | |
40 | static inline uint32_t |
41 | top12 (float x) |
42 | { |
43 | return asuint (x) >> 20; |
44 | } |
45 | |
46 | float |
47 | __exp2f (float x) |
48 | { |
49 | uint32_t abstop; |
50 | uint64_t ki, t; |
51 | /* double_t for better performance on targets with FLT_EVAL_METHOD==2. */ |
52 | double_t kd, xd, z, r, r2, y, s; |
53 | |
54 | xd = (double_t) x; |
55 | abstop = top12 (x) & 0x7ff; |
56 | if (__glibc_unlikely (abstop >= top12 (128.0f))) |
57 | { |
58 | /* |x| >= 128 or x is nan. */ |
59 | if (asuint (x) == asuint (-INFINITY)) |
60 | return 0.0f; |
61 | if (abstop >= top12 (INFINITY)) |
62 | return x + x; |
63 | if (x > 0.0f) |
64 | return __math_oflowf (0); |
65 | if (x <= -150.0f) |
66 | return __math_uflowf (0); |
67 | #if WANT_ERRNO_UFLOW |
68 | if (x < -149.0f) |
69 | return __math_may_uflowf (0); |
70 | #endif |
71 | } |
72 | |
73 | /* x = k/N + r with r in [-1/(2N), 1/(2N)] and int k. */ |
74 | kd = math_narrow_eval ((double) (xd + SHIFT)); /* Needs to be double. */ |
75 | ki = asuint64 (kd); |
76 | kd -= SHIFT; /* k/N for int k. */ |
77 | r = xd - kd; |
78 | |
79 | /* exp2(x) = 2^(k/N) * 2^r ~= s * (C0*r^3 + C1*r^2 + C2*r + 1) */ |
80 | t = T[ki % N]; |
81 | t += ki << (52 - EXP2F_TABLE_BITS); |
82 | s = asdouble (t); |
83 | z = C[0] * r + C[1]; |
84 | r2 = r * r; |
85 | y = C[2] * r + 1; |
86 | y = z * r2 + y; |
87 | y = y * s; |
88 | return (float) y; |
89 | } |
90 | #ifndef __exp2f |
91 | strong_alias (__exp2f, __ieee754_exp2f) |
92 | strong_alias (__exp2f, __exp2f_finite) |
93 | versioned_symbol (libm, __exp2f, exp2f, GLIBC_2_27); |
94 | libm_alias_float_other (__exp2, exp2) |
95 | #endif |
96 | |