Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wine-mirror
GitHub Repository: wine-mirror/wine
Path: blob/master/libs/musl/src/math/exp.c
4397 views
1
/*
2
* Double-precision e^x function.
3
*
4
* Copyright (c) 2018, Arm Limited.
5
* SPDX-License-Identifier: MIT
6
*/
7
8
#include <math.h>
9
#include <stdint.h>
10
#include "libm.h"
11
#include "exp_data.h"
12
13
#define N (1 << EXP_TABLE_BITS)
14
#define InvLn2N __exp_data.invln2N
15
#define NegLn2hiN __exp_data.negln2hiN
16
#define NegLn2loN __exp_data.negln2loN
17
#define Shift __exp_data.shift
18
#define T __exp_data.tab
19
#define C2 __exp_data.poly[5 - EXP_POLY_ORDER]
20
#define C3 __exp_data.poly[6 - EXP_POLY_ORDER]
21
#define C4 __exp_data.poly[7 - EXP_POLY_ORDER]
22
#define C5 __exp_data.poly[8 - EXP_POLY_ORDER]
23
24
/* Handle cases that may overflow or underflow when computing the result that
25
is scale*(1+TMP) without intermediate rounding. The bit representation of
26
scale is in SBITS, however it has a computed exponent that may have
27
overflown into the sign bit so that needs to be adjusted before using it as
28
a double. (int32_t)KI is the k used in the argument reduction and exponent
29
adjustment of scale, positive k here means the result may overflow and
30
negative k means the result may underflow. */
31
static inline double specialcase(double x, double_t tmp, uint64_t sbits, uint64_t ki, matherr_t matherr)
32
{
33
double_t scale, y;
34
35
if ((ki & 0x80000000) == 0) {
36
/* k > 0, the exponent of scale might have overflowed by <= 460. */
37
sbits -= 1009ull << 52;
38
scale = asdouble(sbits);
39
y = 0x1p1009 * (scale + scale * tmp);
40
if (isinf(y))
41
return matherr(_OVERFLOW, "exp", x, 0, y);
42
return eval_as_double(y);
43
}
44
/* k < 0, need special care in the subnormal range. */
45
sbits += 1022ull << 52;
46
scale = asdouble(sbits);
47
y = scale + scale * tmp;
48
if (y < 1.0) {
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. */
62
fp_force_eval(fp_barrier(0x1p-1022) * 0x1p-1022);
63
y = 0x1p-1022 * y;
64
return matherr(_UNDERFLOW, "exp", x, 0, y);
65
}
66
y = 0x1p-1022 * y;
67
return eval_as_double(y);
68
}
69
70
/* Top 12 bits of a double (sign and exponent bits). */
71
static inline uint32_t top12(double x)
72
{
73
return asuint64(x) >> 52;
74
}
75
76
double __cdecl __exp(double x, matherr_t matherr)
77
{
78
uint32_t abstop;
79
uint64_t ki, idx, top, sbits;
80
double_t kd, z, r, r2, scale, tail, tmp;
81
82
if (isnan(x))
83
return matherr(_DOMAIN, "exp", x, 0, x);
84
85
abstop = top12(x) & 0x7ff;
86
if (predict_false(abstop - top12(0x1p-54) >= top12(512.0) - top12(0x1p-54))) {
87
if (abstop - top12(0x1p-54) >= 0x80000000)
88
/* Avoid spurious underflow for tiny x. */
89
/* Note: 0 is common input. */
90
return WANT_ROUNDING ? 1.0 + x : 1.0;
91
if (abstop >= top12(1024.0)) {
92
if (asuint64(x) == asuint64(-INFINITY))
93
return 0.0;
94
if (abstop >= top12(INFINITY))
95
return 1.0 + x;
96
if (asuint64(x) >> 63)
97
return matherr(_UNDERFLOW, "exp", x, 0, fp_barrier(DBL_MIN) * DBL_MIN);
98
else
99
return matherr(_OVERFLOW, "exp", x, 0, fp_barrier(DBL_MAX) * DBL_MAX);
100
}
101
/* Large x is special cased below. */
102
abstop = 0;
103
}
104
105
/* exp(x) = 2^(k/N) * exp(r), with exp(r) in [2^(-1/2N),2^(1/2N)]. */
106
/* x = ln2/N*k + r, with int k and r in [-ln2/2N, ln2/2N]. */
107
z = InvLn2N * x;
108
kd = round(z);
109
ki = (int64_t)kd;
110
r = x + kd * NegLn2hiN + kd * NegLn2loN;
111
/* 2^(k/N) ~= scale * (1 + tail). */
112
idx = 2 * (ki % N);
113
top = ki << (52 - EXP_TABLE_BITS);
114
tail = asdouble(T[idx]);
115
/* This is only a valid scale when -1023*N < k < 1024*N. */
116
sbits = T[idx + 1] + top;
117
/* exp(x) = 2^(k/N) * exp(r) ~= scale + scale * (tail + exp(r) - 1). */
118
/* Evaluation is optimized assuming superscalar pipelined execution. */
119
r2 = r * r;
120
/* Without fma the worst case error is 0.25/N ulp larger. */
121
/* Worst case error is less than 0.5+1.11/N+(abs poly error * 2^53) ulp. */
122
tmp = tail + r + r2 * (C2 + r * C3) + r2 * r2 * (C4 + r * C5);
123
if (predict_false(abstop == 0))
124
return specialcase(x, tmp, sbits, ki, matherr);
125
scale = asdouble(sbits);
126
/* Note: tmp == 0 or |tmp| > 2^-200 and scale > 2^-739, so there
127
is no spurious underflow here even without fma. */
128
return eval_as_double(scale + scale * tmp);
129
}
130
131
double __cdecl exp(double x)
132
{
133
return __exp(x, math_error);
134
}
135
136