]>
git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/ieee754/dbl-64/e_exp2.c
22cade8b297985695960215ee9bca8262dc98ef0
1 /* Double-precision 2^x function.
2 Copyright (C) 2018-2019 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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/>. */
21 #include <math-barriers.h>
22 #include <math-narrow-eval.h>
23 #include <math-svid-compat.h>
24 #include <shlib-compat.h>
25 #include <libm-alias-double.h>
26 #include "math_config.h"
28 #define N (1 << EXP_TABLE_BITS)
29 #define Shift __exp_data.exp2_shift
30 #define T __exp_data.tab
31 #define C1 __exp_data.exp2_poly[0]
32 #define C2 __exp_data.exp2_poly[1]
33 #define C3 __exp_data.exp2_poly[2]
34 #define C4 __exp_data.exp2_poly[3]
35 #define C5 __exp_data.exp2_poly[4]
37 /* Handle cases that may overflow or underflow when computing the result that
38 is scale*(1+TMP) without intermediate rounding. The bit representation of
39 scale is in SBITS, however it has a computed exponent that may have
40 overflown into the sign bit so that needs to be adjusted before using it as
41 a double. (int32_t)KI is the k used in the argument reduction and exponent
42 adjustment of scale, positive k here means the result may overflow and
43 negative k means the result may underflow. */
45 specialcase (double_t tmp
, uint64_t sbits
, uint64_t ki
)
49 if ((ki
& 0x80000000) == 0)
51 /* k > 0, the exponent of scale might have overflowed by 1. */
53 scale
= asdouble (sbits
);
54 y
= 2 * (scale
+ scale
* tmp
);
55 return check_oflow (y
);
57 /* k < 0, need special care in the subnormal range. */
58 sbits
+= 1022ull << 52;
59 scale
= asdouble (sbits
);
60 y
= scale
+ scale
* tmp
;
63 /* Round y to the right precision before scaling it into the subnormal
64 range to avoid double rounding that can cause 0.5+E/2 ulp error where
65 E is the worst-case ulp error outside the subnormal range. So this
66 is only useful if the goal is better than 1 ulp worst-case error. */
68 lo
= scale
- y
+ scale
* tmp
;
70 lo
= 1.0 - hi
+ y
+ lo
;
71 y
= math_narrow_eval (hi
+ lo
) - 1.0;
72 /* Avoid -0.0 with downward rounding. */
73 if (WANT_ROUNDING
&& y
== 0.0)
75 /* The underflow exception needs to be signaled explicitly. */
76 math_force_eval (math_opt_barrier (0x1p
-1022) * 0x1p
-1022);
79 return check_uflow (y
);
82 /* Top 12 bits of a double (sign and exponent bits). */
83 static inline uint32_t
86 return asuint64 (x
) >> 52;
93 uint64_t ki
, idx
, top
, sbits
;
94 /* double_t for better performance on targets with FLT_EVAL_METHOD==2. */
95 double_t kd
, r
, r2
, scale
, tail
, tmp
;
97 abstop
= top12 (x
) & 0x7ff;
98 if (__glibc_unlikely (abstop
- top12 (0x1p
-54)
99 >= top12 (512.0) - top12 (0x1p
-54)))
101 if (abstop
- top12 (0x1p
-54) >= 0x80000000)
102 /* Avoid spurious underflow for tiny x. */
103 /* Note: 0 is common input. */
104 return WANT_ROUNDING
? 1.0 + x
: 1.0;
105 if (abstop
>= top12 (1024.0))
107 if (asuint64 (x
) == asuint64 (-INFINITY
))
109 if (abstop
>= top12 (INFINITY
))
111 if (!(asuint64 (x
) >> 63))
112 return __math_oflow (0);
113 else if (asuint64 (x
) >= asuint64 (-1075.0))
114 return __math_uflow (0);
116 if (2 * asuint64 (x
) > 2 * asuint64 (928.0))
117 /* Large x is special cased below. */
121 /* exp2(x) = 2^(k/N) * 2^r, with 2^r in [2^(-1/2N),2^(1/2N)]. */
122 /* x = k/N + r, with int k and r in [-1/2N, 1/2N]. */
123 kd
= math_narrow_eval (x
+ Shift
);
124 ki
= asuint64 (kd
); /* k. */
125 kd
-= Shift
; /* k/N for int k. */
127 /* 2^(k/N) ~= scale * (1 + tail). */
129 top
= ki
<< (52 - EXP_TABLE_BITS
);
130 tail
= asdouble (T
[idx
]);
131 /* This is only a valid scale when -1023*N < k < 1024*N. */
132 sbits
= T
[idx
+ 1] + top
;
133 /* exp2(x) = 2^(k/N) * 2^r ~= scale + scale * (tail + 2^r - 1). */
134 /* Evaluation is optimized assuming superscalar pipelined execution. */
136 /* Without fma the worst case error is 0.5/N ulp larger. */
137 /* Worst case error is less than 0.5+0.86/N+(abs poly error * 2^53) ulp. */
138 tmp
= tail
+ r
* C1
+ r2
* (C2
+ r
* C3
) + r2
* r2
* (C4
+ r
* C5
);
139 if (__glibc_unlikely (abstop
== 0))
140 return specialcase (tmp
, sbits
, ki
);
141 scale
= asdouble (sbits
);
142 /* Note: tmp == 0 or |tmp| > 2^-65 and scale > 2^-928, so there
143 is no spurious underflow here even without fma. */
144 return scale
+ scale
* tmp
;
147 strong_alias (__exp2
, __ieee754_exp2
)
148 strong_alias (__exp2
, __exp2_finite
)
149 # if LIBM_SVID_COMPAT
150 versioned_symbol (libm
, __exp2
, exp2
, GLIBC_2_29
);
151 libm_alias_double_other (__exp2
, exp2
)
153 libm_alias_double (__exp2
, exp2
)