-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdp.h
303 lines (261 loc) · 11.4 KB
/
dp.h
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
/**
* Copyright (c) 2015 Voidware Ltd.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS," WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*
*/
#ifndef __dp_h__
#define __dp_h__
template<class D> struct DP
{
/* Precison doubler.
* given a class D supporting basic arithmetic (*+/-, and comparisions)
* make a new class DP that doubles the precision.
* D must be of floating point type. to start with D can be float
* or double.
* DP can be applied to itself to re-double precision.
*/
// Constraints on D
// D::D(0);
// Constructors
DP() {}
DP(const D& a) { f_[0] = 0; f_[1] = a; }
// Features
bool operator==(const DP& a) const
{ return f_[0] == a.f_[0] && f_[1] == a.f_[1]; }
bool operator!=(const DP& a) const
{ return f_[0] != a.f_[0] || f_[1] != a.f_[1]; }
bool operator<(const DP&) const;
bool operator<=(const DP&) const;
bool operator>(const DP& b) const
{ return b < *this; }
bool operator>=(const DP& b) const
{ return b <= *this; }
void operator+=(const DP&);
void operator-=(const DP&);
void operator*=(const DP&);
void operator/=(const DP&);
static DP<D> makeSplit(const D& two)
{
return split_*split_ - split_ - split_ + two;
}
D f_[2];
static D split_;
};
template<class D> D DP<D>::split_;
template<class D>
bool DP<D>::operator<(const DP<D>& b) const
{
return f_[1] < b.f_[1] || f_[1] == b.f_[1] && f_[0] < b.f_[0];
}
template<class D>
bool DP<D>::operator<=(const DP<D>& b) const
{
return f_[1] < b.f_[1] || f_[1] == b.f_[1] && f_[0] <= b.f_[0];
}
/* Computes fl(a+b) and err(a+b). Assumes |a| >= |b|. */
#define QUICK_TWO_SUM(a, b, s, err) \
s = a + b; \
err = b - (s - a)
/* Computes fl(a-b) and err(a-b). Assumes |a| >= |b| */
#define QUICK_TWO_DIFF(a, b, s, err) \
s = a - b; \
err = (a - s) - b
/* Computes fl(a+b) and err(a+b). */
#define TWO_SUM(a, b, s, err) \
{ \
D bb; \
s = a + b; \
bb = s - a; \
err = (a - (s - bb)) + (b - bb); \
}
/* Computes fl(a-b) and err(a-b). */
#define TWO_DIFF(a, b, s, err) \
{ \
D bb; \
s = a - b; \
bb = s - a; \
err = (a - (s - bb)) - (b + bb); \
}
/* Computes high word and lo word of a */
#define SPLIT_(a, hi, lo) \
{ \
D t = _split * a; \
hi = t - (t - a); \
lo = a - hi; \
}
/* Computes fl(a*b) and err(a*b). */
#define TWO_PROD(a, b, p, err) \
{ \
D a_hi, a_lo, b_hi, b_lo; \
p = a * b; \
SPLIT_(a, a_hi, a_lo); \
SPLIT_(b, b_hi, b_lo); \
err = a_hi*b_hi - p + a_hi*b_lo + a_lo*b_hi + a_lo*b_lo; \
}
/* This one satisfies IEEE style error bound,
due to K. Briggs and W. Kahan.
*/
#define TWO_TWO_SUM(a0, a1, b0, b1, x0, x1) \
{ \
D _s1, _s2, _t1, _t2, _u1, _u2; \
TWO_SUM(a1, b1, _s1, _s2); \
TWO_SUM(a0, b0, _t1, _t2); \
_s2 += _t1; \
QUICK_TWO_SUM(_s1, _s2, _u1, _u2); \
_s2 = _u2 + _t2; \
QUICK_TWO_SUM(_u1, _s2, x1, x0); \
}
#define TWO_TWO_DIFF(a0, a1, b0, b1, x0, x1) \
{ \
D _s1, _s2, _t1, _t2, _u1, _u2; \
TWO_DIFF(a1, b1, _s1, _s2); \
TWO_DIFF(a0, b0, _t1, _t2); \
_s2 += _t1; \
QUICK_TWO_SUM(_s1, _s2, _u1, _u2); \
_s1 = _u1; \
_s2 = _u2 + _t2; \
QUICK_TWO_SUM(_s1, _s2, x1, x0); \
}
#define TWO_TWO_PROD(a0, a1, b0, b1, x0, x1) \
{ \
D _p1, _p2; \
TWO_PROD(a1, b1, _p1, _p2); \
_p2 += a1*b0; \
_p2 += a0*b1; \
QUICK_TWO_SUM(_p1, _p2, x1, x0); \
}
#define TWO_ONE_SUM(a0, a1, b, x0, x1) \
{ \
D _s1, _s2; \
TWO_SUM(a1, b, _s1, _s2); \
_s2 += a0; \
QUICK_TWO_SUM(_s1, _s2, x1, x0); \
}
#define TWO_ONE_PROD(a0, a1, b, x0, x1) \
{ \
D _p1, _p2; \
TWO_PROD(a1, b, _p1, _p2); \
_p2 += a0*b; \
QUICK_TWO_SUM(_p1, _p2, x1, x0); \
}
#define TWO_TWO_DIV(a0, a1, b0, b1, x0, x1) \
{ \
D q1, q2; \
D _r0, _r1; \
D v0, v1, v2, v3; \
D q3; \
q1 = a1/b1; \
TWO_ONE_PROD(b0, b1, q1, v2, v3); \
TWO_TWO_DIFF(a0, a1, v2, v3, v0, v1); \
q2 = v1/b1; \
TWO_ONE_PROD(b0, b1, q2, v2, v3); \
TWO_TWO_DIFF(v0, v1, v2, v3, _r0, _r1); \
q3 = _r1/b1; \
QUICK_TWO_SUM(q1, q2, v0, v1); \
TWO_ONE_SUM(q2, q1, q3, x0, x1); \
}
/* double-double / double */
#define TWO_ONE_DIV(a0, a1, b, x0, x1) \
{ \
D q1, q2; \
D _p1, _p2; \
D s, e; \
q1 = a1/b; \
TWO_PROD(q1, b, _p1, _p2); \
TWO_DIFF(a1, _p1, s, e); \
e = e + a0 - _p2; \
q2 = DIVOP(ADDOP(s, e), b); \
QUICK_TWO_SUM(q1, q2, x1, x0); \
}
#define TWO_ONE_DIFF(a0, a1, b, x0, x1) \
{ \
D _s1, _s2; \
TWO_DIFF(a1, b, _s1, _s2); \
_s2 += a0; \
QUICK_TWO_SUM(_s1, _s2, x1, x0); \
}
#define TWO_SQR(a, x0, x1) \
{ \
D hi, lo, t; \
x1 = a*a; \
SPLIT_(a, hi, lo); \
t = hi*lo; \
x0 = hi*hi - x1 + t + t + lo*lo; \
}
#define TWO_TWO_SQR(a0, a1, x0, x1) \
{ \
D _p1, _p2; \
D _s1, _s2; \
D t; \
TWO_SQR(a1, _p2, _p1); \
t = a1*a0; \
_p2 += t + t - a0*a0; \
QUICK_TWO_SUM(_p1, _p2, x1, x0); \
}
template<class D>
void DP<D>::operator+=(const DP<D>& a)
{
TWO_TWO_SUM(a.f_[0], a.f_[1], f_[0], f_[1], f_[0], f_[1]);
}
template<class D>
void DP<D>::operator-=(const DP<D>& a)
{
TWO_TWO_DIFF(f_[0], f_[1], a.f_[0], a.f_[1], f_[0], f_[1]);
}
template<class D>
void DP<D>::operator*=(const DP<D>& a)
{
const D& _split = DP<D>::split_;
TWO_TWO_PROD(a.f_[0], a.f_[1], f_[0], f_[1], f_[0], f_[1]);
}
template<class D>
void DP<D>::operator/=(const DP<D>& a)
{
const D& _split = DP<D>::split_;
TWO_TWO_DIV(f_[0], f_[1], a.f_[0], a.f_[1], f_[0], f_[1]);
}
template<class D> DP<D> operator+(const DP<D>& a, const DP<D>& b)
{
DP<D> c;
TWO_TWO_SUM(a.f_[0], a.f_[1], b.f_[0], b.f_[1], c.f_[0], c.f_[1]);
return c;
}
template<class D> DP<D> operator-(const DP<D>& a, const DP<D>& b)
{
DP<D> c;
TWO_TWO_DIFF(a.f_[0], a.f_[1], b.f_[0], b.f_[1], c.f_[0], c.f_[1]);
return c;
}
template<class D> DP<D> operator*(const DP<D>& a, const DP<D>& b)
{
DP<D> c;
const D& _split = DP<D>::split_;
TWO_TWO_PROD(a.f_[0], a.f_[1], b.f_[0], b.f_[1], c.f_[0], c.f_[1]);
return c;
}
template<class D> DP<D> operator/(const DP<D>& a, const DP<D>& b)
{
DP<D> c;
const D& _split = DP<D>::split_;
TWO_TWO_DIV(a.f_[0], a.f_[1], b.f_[0], b.f_[1], c.f_[0], c.f_[1]);
return c;
}
#endif /* __dp_h__ */