-
Notifications
You must be signed in to change notification settings - Fork 1
/
exp.cxx
352 lines (320 loc) · 12.3 KB
/
exp.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/* Mathematical functions to quad precision.
*
* Copyright (C) 2023 Markus Wallerberger and others
* SPDX-License-Identifier: MIT
*
* Most strategies are adapted from DoubleFloats.jl which is
* Copyright (C) 2018-2023 Julia Math
* and also licensed MIT
*/
#include "taylor.h"
#include "xprec/ddouble.h"
#include <cassert>
#ifndef XPREC_API_EXPORT
#define XPREC_API_EXPORT
#endif
namespace xprec {
inline DDouble expm1_kernel_taylor(DDouble x, int n)
{
assert(fabs(x.hi()) < 1.0);
DDouble xpow = x * x;
DDouble r = x.add_small(PowerOfTwo(0.5) * xpow);
for (int k = 3; k <= n; ++k) {
xpow *= x;
r = r.add_small(reciprocal_factorial(k) * xpow);
}
return r;
}
static DDouble expm1_128th(int n)
{
static const DDouble EXPM1_128TH[65] = {
{-0.22119921692859512, -1.0231869534531498e-17},
{-0.2150910066825083, 2.3730789082448644e-18},
{-0.20893488914970398, -1.2452907836084123e-18},
{-0.20273048858867557, -1.7352379329257716e-18},
{-0.19647742631093926, -8.86329269357526e-18},
{-0.1901753206579207, -5.503037155875603e-18},
{-0.18382378697766022, 6.554697808700811e-18},
{-0.17742243760133541, 4.017189331283796e-18},
{-0.17097088181959966, 1.5116689608969005e-19},
{-0.16446872585873493, -2.139647400453233e-18},
{-0.15791557285661764, -1.1212311825056607e-17},
{-0.15131102283849604, -5.6669249355115135e-18},
{-0.14465467269257745, -1.0550675610571318e-17},
{-0.1379461161454243, 5.763785158040174e-18},
{-0.13118494373715683, 6.146598011714697e-19},
{-0.12437074279646179, 6.303176605470021e-18},
{-0.1175030974154046, 3.2658820639011965e-18},
{-0.11058158842404436, -2.0874178115990373e-19},
{-0.10360579336484958, -5.827134285622915e-18},
{-0.09657528646691328, -6.93416919089852e-18},
{-0.08948963861996587, -5.494907630146725e-18},
{-0.08234841734818418, -4.8348859420484686e-18},
{-0.07515118678379516, -3.2635260492015698e-18},
{-0.06789750764047242, -1.167464604196626e-18},
{-0.06058693718652421, -7.077887227488846e-19},
{-0.053219029217871104, 7.855973989608505e-19},
{-0.045793334030811685, 7.6989787849942455e-19},
{-0.03830939839457471, 3.351106556546642e-18},
{-0.03076676552365592, 5.607402565184088e-19},
{-0.023164975049937968, 1.6576088297760018e-18},
{-0.015503562994591593, -6.554927149823924e-19},
{-0.007782061739756488, -2.171849242067691e-19},
{0.0, 0.0},
{0.007843097206447977, 6.611915286438626e-19},
{0.015747708586685748, -2.862138367894185e-19},
{0.023714316602357916, 7.772270440766338e-19},
{0.03174340749910267, 7.614433403626514e-19},
{0.03983547133623, 1.1038442468719412e-18},
{0.0479910020166327, 2.232142242481688e-18},
{0.05621049731693197, -6.970919938961464e-19},
{0.06449445891785943, -2.2934210303960824e-18},
{0.07284339243487745, -2.006173739106304e-18},
{0.0812578074490396, 4.627898188856025e-18},
{0.08973821753809323, -7.438154204619872e-19},
{0.09828514030782586, -6.438065156763691e-18},
{0.10689909742365748, 2.1251455338215007e-19},
{0.11558061464248076, -2.5290380495681964e-18},
{0.12433022184475072, -1.0222490708858767e-18},
{0.13314845306682632, -5.370737708558031e-18},
{0.1420358465335656, -1.2069701773647767e-17},
{0.15099294469117644, 9.857598007072166e-18},
{0.16002029424032516, -9.002941214515411e-18},
{0.16911844616950442, -1.3811845173682628e-17},
{0.17828795578866324, -1.1203883895767038e-18},
{0.1875293827631006, 6.415816207759217e-19},
{0.19684329114762478, -5.89991778046089e-18},
{0.2062302494209807, 1.1540139455476613e-17},
{0.21569083052054744, 1.3287595785286163e-17},
{0.22522561187730758, -4.729368350680563e-19},
{0.234835175451091, -3.104366491258746e-19},
{0.24452010776609515, 8.861603894276184e-18},
{0.25428099994668374, 1.3050032175111173e-17},
{0.2641184477534664, -1.541497933603795e-17},
{0.2740330516196609, 2.3636421950197868e-17},
{0.2840254166877415, -2.133257464457841e-17}};
assert(abs(n) <= 32);
return EXPM1_128TH[n + 32];
}
static DDouble expm1_quarter(DDouble x)
{
// We need to make sure that (1 + x) does not lose possible significant
// digits, so no matter what strategy we choose here, the convergence
// needs to go out to x = log(1.5) = 0.22. We have it work for until a
// quarter, because that's a nice round power of two.
assert(fabs(x.hi()) <= 0.25);
// The idea is to use the identity
//
// expm1(x) = expm1(x0) + exp(x0) * expm1(x - x0)
//
// to reduce the expansion order.
double n = std::round(128 * x.hi());
double x0 = n / 128;
DDouble y = x - x0;
DDouble expm1_x0 = expm1_128th(n);
DDouble exp_x0 = ExDouble(1.0).add_small(expm1_x0);
DDouble exp_y = expm1_kernel_taylor(y, 10);
return expm1_x0.add_small(exp_x0 * exp_y);
}
class Product {
public:
constexpr Product() : _isempty(true), _res(1) { }
template <typename T>
Product &operator*=(const T &x)
{
if (_isempty)
_res = x;
else
_res *= x;
_isempty = false;
return *this;
}
constexpr operator DDouble() const { return _res; }
private:
bool _isempty;
DDouble _res;
};
static DDouble exp_halves(int x)
{
const static DDouble EXP_HALVES[31] = {
{1.6487212707001282, -4.731568479435833e-17},
{2.718281828459045, 1.4456468917292502e-16},
{4.4816890703380645, 3.0481759556536343e-16},
{7.38905609893065, -1.7971139497839148e-16},
{12.182493960703473, 2.0334002173348147e-16},
{20.085536923187668, -1.8275625525512858e-16},
{33.11545195869231, 2.2435601403927554e-15},
{54.598150033144236, 2.8741578015844115e-15},
{90.01713130052181, 2.550844346114049e-15},
{148.4131591025766, 3.4863514900464198e-15},
{244.69193226422038, 4.129320187450839e-15},
{403.4287934927351, 1.2359628024450387e-14},
{665.1416330443618, 2.990469256473133e-14},
{1096.6331584284585, 9.869752640434095e-14},
{1808.0424144560632, 3.6612201665204784e-14},
{2980.9579870417283, -2.7103295816873633e-14},
{4914.768840299134, 2.17317454126359e-14},
{8103.083927575384, -2.1530877621067177e-13},
{13359.726829661873, -8.496858340658619e-13},
{22026.465794806718, -1.3780134700517372e-12},
{36315.502674246636, 1.577797006387782e-12},
{59874.14171519782, 1.7895764888916994e-12},
{98715.7710107605, 3.036676373480473e-12},
{162754.79141900392, 5.30065881322063e-12},
{268337.2865208745, -2.0035114163950887e-11},
{442413.3920089205, 1.2118711752313224e-11},
{729416.3698477013, 5.1483277361034595e-11},
{1.2026042841647768e6, -1.5000525764327354e-11},
{1.9827592635375687e6, 2.845770459793355e-11},
{3.2690173724721107e6, -3.075806431120808e-11},
{5.389698476283012e6, 4.098121666636582e-10}};
const static DDouble EXP_SIXTEENS[44] = {
{8.886110520507872e6, 5.321182483501564e-10},
{7.896296018268069e13, 0.007660978022635108},
{7.016735912097631e20, 30185.471599886117},
{6.235149080811617e27, 1.3899738872492847e11},
{5.54062238439351e34, 2.1811937023229343e18},
{4.923458286012058e41, 1.3869835129739753e25},
{4.375039447261341e48, 1.035824156236645e32},
{3.887708405994595e55, 2.707966110366217e39},
{3.454660656717546e62, 1.8553902103629043e46},
{3.0698496406442424e69, 4.375620509828095e52},
{2.7279023188106115e76, 6.6492459414351406e59},
{2.4240441494100796e83, -3.8332753349400205e66},
{2.1540324218248465e90, 6.568050851363196e73},
{1.9140970165092822e97, -1.497464557916617e81},
{1.700887763567586e104, 1.4773861394382237e88},
{1.5114276650041035e111, 1.4805989167614457e94},
{1.3430713274979614e118, -6.561438244448466e101},
{1.1934680253072109e125, -3.301231394418859e108},
{1.0605288775572162e132, 5.4744408887427266e115},
{9.423976816163585e138, -2.7555072985830676e122},
{8.374249953113352e145, -3.529195534423469e129},
{7.441451060972311e152, 4.251237045552673e136},
{6.612555656075053e159, -3.4828210031110127e143},
{5.875990038289236e166, 7.682543674132907e149},
{5.221469689764144e173, -3.041154182825333e157},
{4.639855674272614e180, -3.3453058659461497e164},
{4.123027032079202e187, 1.8602059512155307e171},
{3.663767388609735e194, -1.8555200045340274e178},
{3.255664193661862e201, 5.148254191579011e184},
{2.8930191842539453e208, -2.8880381060655904e191},
{2.5707688209230085e215, 1.1853726094570251e199},
{2.2844135865397565e222, 1.3549224944023444e206},
{2.0299551604542052e229, 1.2942147572086164e213},
{1.803840590747136e236, 1.820681001928355e218},
{1.6029126850757262e243, -2.463627227554342e226},
{1.4243659274306933e250, -5.204358467973364e233},
{1.2657073052794837e257, -3.983584155610672e240},
{1.124721500132769e264, -8.843155706148207e247},
{9.994399554971195e270, 8.925025806205413e253},
{8.881133903158874e277, -4.948247489077345e261},
{7.891873741089921e284, 2.4630459641303726e268},
{7.012806227721897e291, -1.1759583274063904e275},
{6.231657119844268e298, 1.1619020533730335e281},
{5.5375193892845935e305, 1.5239358093004245e289}};
if (x < 0) {
return reciprocal(exp_halves(-x));
}
assert(x <= 1439);
Product res;
int x_halves = x % 32;
if (x_halves)
res *= EXP_HALVES[x_halves - 1];
int x_sixteens = x / 32;
if (x_sixteens)
res *= EXP_SIXTEENS[x_sixteens - 1];
return res;
}
XPREC_API_EXPORT
DDouble exp(DDouble x)
{
if (isnan(x))
return x;
if (x.hi() >= 709.0)
return DDouble(INFINITY, 0);
if (x.hi() <= -709.0)
return DDouble(0);
// x = y/2 + z
double y = std::round(2 * x.hi());
DDouble z = x - y / 2;
// exp(z + y/2) = (1 + expm1(z)) exp(1/2)^y
DDouble exp_z = ExDouble(1.0).add_small(expm1_quarter(z));
DDouble exp_y = exp_halves(int(y));
return exp_z * exp_y;
}
XPREC_API_EXPORT
DDouble expm1(DDouble x)
{
// For small values, we call the expm1 kernel directly
if (fabs(x.hi()) < 0.25)
return expm1_quarter(x);
// Otherwise, we do a naive computation
DDouble res = exp(x);
if (x.hi() < 75)
res -= 1.0;
return res;
}
XPREC_API_EXPORT
DDouble log(DDouble x)
{
// Start with logarithm of hi part
DDouble log_x = std::log(x.hi());
if (!isfinite(log_x))
return log_x;
// Abramowitz and Stegun give the following series expansion (4.1.30):
//
// log(x) = log(x0) + 2 (x - x0)/(x + x0) + O(x - x0)^3
//
DDouble x0 = exp(log_x);
DDouble corr = PowerOfTwo(2.0) * (x - x0) / (x + x0);
log_x += corr;
return log_x;
}
XPREC_API_EXPORT
DDouble log1p(DDouble x)
{
// Start with logarithm of hi part
DDouble log_x = std::log1p(x.hi());
if (!isfinite(log_x))
return log_x;
// Again, we can use the same correction, but log1p <-> expm1
//
// log(1 + x) = log(1 + x0) + 2 (x - x0)/(2 + x + x0) + O(x - x0)^3
//
// One need not worry about cancellation in the denominator for
// x close to -1, since that is where we have an intrinsic loss of
// precision anyway
DDouble x0 = expm1(log_x);
DDouble corr = PowerOfTwo(2.0) * (x - x0) / (2 + x + x0);
log_x += corr;
return log_x;
}
XPREC_API_EXPORT
DDouble pow(DDouble x, int n)
{
if (n < 0) {
DDouble res = pow(x, -n);
return reciprocal(res);
}
if (n == 0) {
// XXX handle nan's etc.
return DDouble(1.0);
}
// Get first non-zero power
while ((n & 1) == 0) {
n >>= 1;
x *= x;
}
// Multiply and square
DDouble res = x;
while (n >>= 1) {
x *= x;
if ((n & 1) == 1)
res *= x;
}
return res;
}
XPREC_API_EXPORT
DDouble pow(DDouble x, DDouble y) { return exp(log(x) * y); }
} // namespace xprec