blob: 47aa47902eca6b5ed2cc0e346ff6d4b9aab917d4 [file] [log] [blame]
Szabolcs Nagy872aa772018-04-27 16:17:24 +01001/*
2 * Double-precision 2^x function.
3 *
4 * Copyright (c) 2018, Arm Limited.
Szabolcs Nagy11253b02018-11-12 11:10:57 +00005 * SPDX-License-Identifier: MIT
Szabolcs Nagy872aa772018-04-27 16:17:24 +01006 */
7
Szabolcs Nagyf934c862019-08-28 15:15:15 +01008#include <float.h>
Szabolcs Nagy872aa772018-04-27 16:17:24 +01009#include <math.h>
10#include <stdint.h>
11#include "math_config.h"
12
13#define N (1 << EXP_TABLE_BITS)
14#define Shift __exp_data.exp2_shift
15#define T __exp_data.tab
16#define C1 __exp_data.exp2_poly[0]
17#define C2 __exp_data.exp2_poly[1]
18#define C3 __exp_data.exp2_poly[2]
19#define C4 __exp_data.exp2_poly[3]
20#define C5 __exp_data.exp2_poly[4]
21#define C6 __exp_data.exp2_poly[5]
22
Szabolcs Nagybc4b9012018-07-03 18:54:51 +010023/* Handle cases that may overflow or underflow when computing the result that
24 is scale*(1+TMP) without intermediate rounding. The bit representation of
25 scale is in SBITS, however it has a computed exponent that may have
26 overflown into the sign bit so that needs to be adjusted before using it as
27 a double. (int32_t)KI is the k used in the argument reduction and exponent
28 adjustment of scale, positive k here means the result may overflow and
29 negative k means the result may underflow. */
Szabolcs Nagy872aa772018-04-27 16:17:24 +010030static inline double
31specialcase (double_t tmp, uint64_t sbits, uint64_t ki)
32{
33 double_t scale, y;
34
35 if ((ki & 0x80000000) == 0)
36 {
Szabolcs Nagy2117b832018-06-14 10:54:06 +010037 /* k > 0, the exponent of scale might have overflowed by 1. */
38 sbits -= 1ull << 52;
Szabolcs Nagy872aa772018-04-27 16:17:24 +010039 scale = asdouble (sbits);
Szabolcs Nagy2117b832018-06-14 10:54:06 +010040 y = 2 * (scale + scale * tmp);
Szabolcs Nagy04884bd2018-12-07 14:58:51 +000041 return check_oflow (eval_as_double (y));
Szabolcs Nagy872aa772018-04-27 16:17:24 +010042 }
43 /* k < 0, need special care in the subnormal range. */
44 sbits += 1022ull << 52;
45 scale = asdouble (sbits);
46 y = scale + scale * tmp;
47 if (y < 1.0)
48 {
49 /* Round y to the right precision before scaling it into the subnormal
50 range to avoid double rounding that can cause 0.5+E/2 ulp error where
51 E is the worst-case ulp error outside the subnormal range. So this
52 is only useful if the goal is better than 1 ulp worst-case error. */
53 double_t hi, lo;
54 lo = scale - y + scale * tmp;
55 hi = 1.0 + y;
56 lo = 1.0 - hi + y + lo;
57 y = eval_as_double (hi + lo) - 1.0;
58 /* Avoid -0.0 with downward rounding. */
59 if (WANT_ROUNDING && y == 0.0)
60 y = 0.0;
61 /* The underflow exception needs to be signaled explicitly. */
Szabolcs Nagy5fa69e12018-06-12 17:18:24 +010062 force_eval_double (opt_barrier_double (0x1p-1022) * 0x1p-1022);
Szabolcs Nagy872aa772018-04-27 16:17:24 +010063 }
64 y = 0x1p-1022 * y;
Szabolcs Nagy04884bd2018-12-07 14:58:51 +000065 return check_uflow (eval_as_double (y));
Szabolcs Nagy872aa772018-04-27 16:17:24 +010066}
67
Szabolcs Nagy58ce45c2018-06-29 11:06:13 +010068/* Top 12 bits of a double (sign and exponent bits). */
Szabolcs Nagy872aa772018-04-27 16:17:24 +010069static inline uint32_t
Szabolcs Nagy2117b832018-06-14 10:54:06 +010070top12 (double x)
Szabolcs Nagy872aa772018-04-27 16:17:24 +010071{
Szabolcs Nagy2117b832018-06-14 10:54:06 +010072 return asuint64 (x) >> 52;
Szabolcs Nagy872aa772018-04-27 16:17:24 +010073}
74
75double
76exp2 (double x)
77{
78 uint32_t abstop;
79 uint64_t ki, idx, top, sbits;
80 /* double_t for better performance on targets with FLT_EVAL_METHOD==2. */
81 double_t kd, r, r2, scale, tail, tmp;
82
Szabolcs Nagy2117b832018-06-14 10:54:06 +010083 abstop = top12 (x) & 0x7ff;
84 if (unlikely (abstop - top12 (0x1p-54) >= top12 (512.0) - top12 (0x1p-54)))
Szabolcs Nagy872aa772018-04-27 16:17:24 +010085 {
Szabolcs Nagy2117b832018-06-14 10:54:06 +010086 if (abstop - top12 (0x1p-54) >= 0x80000000)
Szabolcs Nagy872aa772018-04-27 16:17:24 +010087 /* Avoid spurious underflow for tiny x. */
88 /* Note: 0 is common input. */
89 return WANT_ROUNDING ? 1.0 + x : 1.0;
Szabolcs Nagy2117b832018-06-14 10:54:06 +010090 if (abstop >= top12 (1024.0))
Szabolcs Nagy872aa772018-04-27 16:17:24 +010091 {
92 if (asuint64 (x) == asuint64 (-INFINITY))
93 return 0.0;
Szabolcs Nagy2117b832018-06-14 10:54:06 +010094 if (abstop >= top12 (INFINITY))
Szabolcs Nagy872aa772018-04-27 16:17:24 +010095 return 1.0 + x;
Szabolcs Nagy2117b832018-06-14 10:54:06 +010096 if (!(asuint64 (x) >> 63))
Szabolcs Nagy872aa772018-04-27 16:17:24 +010097 return __math_oflow (0);
Szabolcs Nagy2117b832018-06-14 10:54:06 +010098 else if (asuint64 (x) >= asuint64 (-1075.0))
99 return __math_uflow (0);
Szabolcs Nagy872aa772018-04-27 16:17:24 +0100100 }
Szabolcs Nagy2117b832018-06-14 10:54:06 +0100101 if (2 * asuint64 (x) > 2 * asuint64 (928.0))
102 /* Large x is special cased below. */
103 abstop = 0;
Szabolcs Nagy872aa772018-04-27 16:17:24 +0100104 }
105
106 /* exp2(x) = 2^(k/N) * 2^r, with 2^r in [2^(-1/2N),2^(1/2N)]. */
107 /* x = k/N + r, with int k and r in [-1/2N, 1/2N]. */
108 kd = eval_as_double (x + Shift);
109 ki = asuint64 (kd); /* k. */
110 kd -= Shift; /* k/N for int k. */
111 r = x - kd;
112 /* 2^(k/N) ~= scale * (1 + tail). */
Szabolcs Nagy5e838912018-06-29 09:56:54 +0100113 idx = 2 * (ki % N);
Szabolcs Nagy872aa772018-04-27 16:17:24 +0100114 top = ki << (52 - EXP_TABLE_BITS);
115 tail = asdouble (T[idx]);
116 /* This is only a valid scale when -1023*N < k < 1024*N. */
117 sbits = T[idx + 1] + top;
118 /* exp2(x) = 2^(k/N) * 2^r ~= scale + scale * (tail + 2^r - 1). */
119 /* Evaluation is optimized assuming superscalar pipelined execution. */
Szabolcs Nagy5e838912018-06-29 09:56:54 +0100120 r2 = r * r;
Szabolcs Nagy872aa772018-04-27 16:17:24 +0100121 /* Without fma the worst case error is 0.5/N ulp larger. */
122 /* Worst case error is less than 0.5+0.86/N+(abs poly error * 2^53) ulp. */
123#if EXP2_POLY_ORDER == 4
Szabolcs Nagy5e838912018-06-29 09:56:54 +0100124 tmp = tail + r * C1 + r2 * C2 + r * r2 * (C3 + r * C4);
Szabolcs Nagy872aa772018-04-27 16:17:24 +0100125#elif EXP2_POLY_ORDER == 5
Szabolcs Nagy5e838912018-06-29 09:56:54 +0100126 tmp = tail + r * C1 + r2 * (C2 + r * C3) + r2 * r2 * (C4 + r * C5);
Szabolcs Nagy872aa772018-04-27 16:17:24 +0100127#elif EXP2_POLY_ORDER == 6
Szabolcs Nagy5e838912018-06-29 09:56:54 +0100128 tmp = tail + r * C1 + r2 * (0.5 + r * C3) + r2 * r2 * (C4 + r * C5 + r2 * C6);
Szabolcs Nagy872aa772018-04-27 16:17:24 +0100129#endif
130 if (unlikely (abstop == 0))
131 return specialcase (tmp, sbits, ki);
132 scale = asdouble (sbits);
Szabolcs Nagy2117b832018-06-14 10:54:06 +0100133 /* Note: tmp == 0 or |tmp| > 2^-65 and scale > 2^-928, so there
134 is no spurious underflow here even without fma. */
Szabolcs Nagy04884bd2018-12-07 14:58:51 +0000135 return eval_as_double (scale + scale * tmp);
Szabolcs Nagy872aa772018-04-27 16:17:24 +0100136}
Szabolcs Nagyb7d568d2018-06-06 12:26:56 +0100137#if USE_GLIBC_ABI
138strong_alias (exp2, __exp2_finite)
139hidden_alias (exp2, __ieee754_exp2)
Szabolcs Nagyf934c862019-08-28 15:15:15 +0100140# if LDBL_MANT_DIG == 53
141long double exp2l (long double x) { return exp2 (x); }
142# endif
Szabolcs Nagyb7d568d2018-06-06 12:26:56 +0100143#endif