-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqdouble.h
3100 lines (2760 loc) · 99.2 KB
/
qdouble.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
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Source: Algorithms for Quad-Double Precision Floating Point Arithmetic. Yozo Hida, Xiaoye S. Li, David H. Bailey.
*/
/*
Do this:
#define QDOUBLE_IMPLEMENTATION
before you include this file in *one* C++ file to create the implementation.
// i.e. it should look like this:
#include ...
#include ...
#include ...
#define QDOUBLE_IMPLEMENTATION
#include "qdouble.h"
*/
#ifndef QDOUBLE_H
#define QDOUBLE_H
#include <limits>
#include <cmath>
#include <string>
#include <iostream>
#ifndef QDOUBLEDEF
#ifdef QDOUBLE_STATIC
#define QDOUBLEDEF static
#else
#define QDOUBLEDEF extern
#endif
#endif
#ifndef QDOUBLEINLINE
#define QDOUBLEINLINE inline
#endif
/* The following code computes s = fl(a+b) and error(a + b), assuming |a| >= |b|. */
QDOUBLEINLINE double quick_two_sum(double a, double b, double& error)
{
double s = a + b;
error = b - (s - a);
return s;
}
/* The following code computes s = fl(a-b) and error(a - b), assuming |a| >= |b|. */
QDOUBLEINLINE double quick_two_diff(double a, double b, double& error)
{
double s = a - b;
error = (a - s) - b;
return s;
}
/* The following code computes s = fl(a+b) and error(a + b). */
QDOUBLEINLINE double two_sum(double a, double b, double& error)
{
double s = a + b;
double v = s - a;
error = (a - (s - v)) + (b - v);
return s;
}
/* The following code computes s = fl(a-b) and error(a - b). */
QDOUBLEINLINE double two_diff(double a, double b, double& error)
{
double s = a - b;
double v = s - a;
error = (a - (s - v)) - (b + v);
return s;
}
/* The following code splits a 53-bit IEEE double precision floating number a into a high word and a low word, each with 26
bits of significand, such that a is the sum of the high word with the low word. The high word will contain the first 26 bits,
while the low word will contain the lower 26 bits.*/
QDOUBLEINLINE void split(double a, double& high, double& low)
{
double temp = 134217729.0 * a; // 134217729.0 = 2^27 + 1
high = temp - (temp - a);
low = a - high;
}
/* The following code computes fl(a x b) and error(a x b). */
QDOUBLEINLINE double two_prod(double a, double b, double& error)
{
double a_high, a_low, b_high, b_low;
double p = a * b;
split(a, a_high, a_low);
split(b, b_high, b_low);
error = ((a_high * b_high - p) + a_high * b_low + a_low * b_high) + a_low * b_low;
return p;
}
/* The following code computes fl(a x a) and error(a x a). */
QDOUBLEINLINE double two_sqr(double a, double& error)
{
double a_high, a_low;
double p = a * a;
split(a, a_high, a_low);
error = ((a_high * a_high - p) + 2.0 * a_high * a_low) + a_low * a_low;
return p;
}
QDOUBLEINLINE void three_sum(double& a, double& b, double& c)
{
double t1, t2, t3;
t1 = two_sum(a, b, t2);
a = two_sum(c, t1, t3);
b = two_sum(t2, t3, c);
}
QDOUBLEINLINE void three_sum2(double& a, double& b, double& c)
{
double t1, t2, t3;
t1 = two_sum(a, b, t2);
a = two_sum(c, t1, t3);
b = t2 + t3;
}
struct qdouble
{
double a[4];
QDOUBLEINLINE qdouble()
{
a[0] = 0.0;
a[1] = 0.0;
a[2] = 0.0;
a[3] = 0.0;
}
QDOUBLEINLINE qdouble(double a0, double a1, double a2, double a3)
{
a[0] = a0;
a[1] = a1;
a[2] = a2;
a[3] = a3;
}
QDOUBLEINLINE qdouble(const double* aa)
{
a[0] = aa[0];
a[1] = aa[1];
a[2] = aa[2];
a[3] = aa[3];
}
QDOUBLEINLINE qdouble(double a0)
{
a[0] = a0;
a[1] = 0.0;
a[2] = 0.0;
a[3] = 0.0;
}
QDOUBLEINLINE qdouble(int i)
{
a[0] = static_cast<double>(i);
a[1] = 0.0;
a[2] = 0.0;
a[3] = 0.0;
}
template <class TType>
QDOUBLEINLINE qdouble(TType i)
{
a[0] = static_cast<double>(i);
a[1] = 0.0;
a[2] = 0.0;
a[3] = 0.0;
}
template <class TType>
QDOUBLEINLINE double operator[] (TType i) const
{
return a[i];
}
template <class TType>
QDOUBLEINLINE double& operator[] (TType i)
{
return a[i];
}
};
static const qdouble qdouble_2pi = qdouble(6.283185307179586232e+00,
2.449293598294706414e-16,
-5.989539619436679332e-33,
2.224908441726730563e-49);
static const qdouble qdouble_pi = qdouble(3.141592653589793116e+00,
1.224646799147353207e-16,
-2.994769809718339666e-33,
1.112454220863365282e-49);
static const qdouble qdouble_pi2 = qdouble(1.570796326794896558e+00,
6.123233995736766036e-17,
-1.497384904859169833e-33,
5.562271104316826408e-50);
static const qdouble qdouble_e = qdouble(2.718281828459045091e+00,
1.445646891729250158e-16,
-2.127717108038176765e-33,
1.515630159841218954e-49);
static const qdouble qdouble_log2 = qdouble(6.931471805599452862e-01,
2.319046813846299558e-17,
5.707708438416212066e-34,
-3.582432210601811423e-50);
static const qdouble qdouble_log10 = qdouble(2.302585092994045901e+00,
-2.170756223382249351e-16,
-9.984262454465776570e-33,
-4.023357454450206379e-49);
static const qdouble qdouble_nan = qdouble(std::numeric_limits<double>::quiet_NaN(),
std::numeric_limits<double>::quiet_NaN(),
std::numeric_limits<double>::quiet_NaN(),
std::numeric_limits<double>::quiet_NaN());
static const qdouble qdouble_inf = qdouble(std::numeric_limits<double>::infinity(),
std::numeric_limits<double>::infinity(),
std::numeric_limits<double>::infinity(),
std::numeric_limits<double>::infinity());
static const double qdouble_eps = 1.21543267145725e-63; // = 2^-209
static const int qdouble_ndigits = 62;
static const double qdouble_min_normalized = 1.6259745436952323e-260; // = 2^(-1022 + 3*53)
static const qdouble qdouble_max = qdouble(
1.79769313486231570815e+308, 9.97920154767359795037e+291,
5.53956966280111259858e+275, 3.07507889307840487279e+259);
static const qdouble qdouble_safe_max = qdouble(
1.7976931080746007281e+308, 9.97920154767359795037e+291,
5.53956966280111259858e+275, 3.07507889307840487279e+259);
QDOUBLEINLINE bool operator == (const qdouble& a, const qdouble& b)
{
return (a[0] == b[0] && a[1] == b[1] && a[2] == b[2] && a[3] == b[3]);
}
QDOUBLEINLINE bool operator == (const qdouble& a, double b)
{
return (a[0] == b && a[1] == 0.0 && a[2] == 0.0 && a[3] == 0.0);
}
QDOUBLEINLINE bool operator == (double a, const qdouble& b)
{
return b == a;
}
QDOUBLEINLINE bool operator != (const qdouble& a, const qdouble& b)
{
return !(a == b);
}
QDOUBLEINLINE bool operator != (const qdouble& a, double b)
{
return !(a == b);
}
QDOUBLEINLINE bool operator != (double a, const qdouble& b)
{
return !(a == b);
}
QDOUBLEINLINE bool operator < (const qdouble& a, double b)
{
return (a[0] < b || (a[0] == b && a[1] < 0.0));
}
QDOUBLEINLINE bool operator > (const qdouble& a, double b)
{
return (a[0] > b || (a[0] == b && a[1] > 0.0));
}
QDOUBLEINLINE bool operator < (double a, const qdouble& b)
{
return b > a;
}
QDOUBLEINLINE bool operator > (double a, const qdouble& b)
{
return b < a;
}
QDOUBLEINLINE bool operator < (const qdouble& a, const qdouble& b)
{
return (a[0] < b[0] ||
(a[0] == b[0] && (a[1] < b[1] ||
(a[1] == b[1] && (a[2] < b[2] ||
(a[2] == b[2] && a[3] < b[3]))))));
}
QDOUBLEINLINE bool operator > (const qdouble& a, const qdouble& b)
{
return (a[0] > b[0] ||
(a[0] == b[0] && (a[1] > b[1] ||
(a[1] == b[1] && (a[2] > b[2] ||
(a[2] == b[2] && a[3] > b[3]))))));
}
QDOUBLEINLINE bool operator <= (const qdouble& a, double b)
{
return (a[0] < b || (a[0] == b && a[1] <= 0.0));
}
QDOUBLEINLINE bool operator >= (const qdouble& a, double b)
{
return (a[0] > b || (a[0] == b && a[1] >= 0.0));
}
QDOUBLEINLINE bool operator <= (double a, const qdouble& b)
{
return b >= a;
}
QDOUBLEINLINE bool operator >= (double a, const qdouble& b)
{
return b <= a;
}
QDOUBLEINLINE bool operator <= (const qdouble& a, const qdouble& b)
{
return (a[0] < b[0] ||
(a[0] == b[0] && (a[1] < b[1] ||
(a[1] == b[1] && (a[2] < b[2] ||
(a[2] == b[2] && a[3] <= b[3]))))));
}
QDOUBLEINLINE bool operator >= (const qdouble& a, const qdouble& b)
{
return (a[0] > b[0] ||
(a[0] == b[0] && (a[1] > b[1] ||
(a[1] == b[1] && (a[2] > b[2] ||
(a[2] == b[2] && a[3] >= b[3]))))));
}
QDOUBLEINLINE bool is_inf(const qdouble& a)
{
return a[0] == std::numeric_limits<double>::infinity();
}
QDOUBLEINLINE bool is_nan(const qdouble& a)
{
return a[0] != a[0] || a[1] != a[1] || a[2] != a[2] || a[3] != a[3];
}
QDOUBLEINLINE void renormalize(double& a0, double& a1, double& a2, double& a3)
{
double s0, s1, s2 = 0.0, s3 = 0.0;
s0 = quick_two_sum(a2, a3, a3);
s0 = quick_two_sum(a1, s0, a2);
a0 = quick_two_sum(a0, s0, a1);
s0 = a0;
s1 = a1;
if (s1 != 0.0) {
s1 = quick_two_sum(s1, a2, s2);
if (s2 != 0.0)
s2 = quick_two_sum(s2, a3, s3);
else
s1 = quick_two_sum(s1, a3, s2);
}
else {
s0 = quick_two_sum(s0, a2, s1);
if (s1 != 0.0)
s1 = quick_two_sum(s1, a3, s2);
else
s0 = quick_two_sum(s0, a3, s1);
}
a0 = s0;
a1 = s1;
a2 = s2;
a3 = s3;
}
QDOUBLEINLINE void renormalize(double& a0, double& a1, double& a2, double& a3, double& a4)
{
double s0, s1, s2 = 0.0, s3 = 0.0;
s0 = quick_two_sum(a3, a4, a4);
s0 = quick_two_sum(a2, s0, a3);
s0 = quick_two_sum(a1, s0, a2);
a0 = quick_two_sum(a0, s0, a1);
s0 = a0;
s1 = a1;
s0 = quick_two_sum(a0, a1, s1);
if (s1 != 0.0)
{
s1 = quick_two_sum(s1, a2, s2);
if (s2 != 0.0)
{
s2 = quick_two_sum(s2, a3, s3);
if (s3 != 0.0)
s3 += a4;
else
s2 += a4;
}
else
{
s1 = quick_two_sum(s1, a3, s2);
if (s2 != 0.0)
s2 = quick_two_sum(s2, a4, s3);
else
s1 = quick_two_sum(s1, a4, s2);
}
}
else
{
s0 = quick_two_sum(s0, a2, s1);
if (s1 != 0.0)
{
s1 = quick_two_sum(s1, a3, s2);
if (s2 != 0.0)
s2 = quick_two_sum(s2, a4, s3);
else
s1 = quick_two_sum(s1, a4, s2);
}
else
{
s0 = quick_two_sum(s0, a3, s1);
if (s1 != 0.0)
s1 = quick_two_sum(s1, a4, s2);
else
s0 = quick_two_sum(s0, a4, s1);
}
}
a0 = s0;
a1 = s1;
a2 = s2;
a3 = s3;
}
QDOUBLEINLINE qdouble operator + (const qdouble& a, double b)
{
double b0, b1, b2, b3;
double e;
b0 = two_sum(a[0], b, e);
b1 = two_sum(a[1], e, e);
b2 = two_sum(a[2], e, e);
b3 = two_sum(a[3], e, e);
renormalize(b0, b1, b2, b3, e);
return qdouble(b0, b1, b2, b3);
}
QDOUBLEINLINE qdouble operator + (double a, const qdouble& b)
{
return (b + a);
}
/*
s = double_accumulate(u,v,x) adds x to the double double pair (u,v).
The output is the significant component s if the remaining components
contain more than one double worth of significand. u and v are
modified to represent the other two components in the sum.
*/
QDOUBLEINLINE double double_accumulate(double& u, double& v, double x)
{
double s;
bool zu, zv;
s = two_sum(v, x, v);
s = two_sum(u, s, u);
zu = (u != 0.0);
zv = (v != 0.0);
if (zu && zv)
return s;
if (!zv)
{
v = u;
u = s;
}
else
{
u = s;
}
return 0.0;
}
QDOUBLEINLINE qdouble operator + (const qdouble& a, const qdouble& b)
{
int i, j, k;
double s, t;
double u, v; /* double-length accumulator */
double x[4] = { 0.0, 0.0, 0.0, 0.0 };
i = j = k = 0;
if (std::abs(a[i]) > std::abs(b[j]))
u = a[i++];
else
u = b[j++];
if (std::abs(a[i]) > std::abs(b[j]))
v = a[i++];
else
v = b[j++];
u = quick_two_sum(u, v, v);
while (k < 4) {
if (i >= 4 && j >= 4) {
x[k] = u;
if (k < 3)
x[++k] = v;
break;
}
if (i >= 4)
t = b[j++];
else if (j >= 4)
t = a[i++];
else if (std::abs(a[i]) > std::abs(b[j])) {
t = a[i++];
}
else
t = b[j++];
s = double_accumulate(u, v, t);
if (s != 0.0) {
x[k++] = s;
}
}
/* add the rest. */
for (k = i; k < 4; ++k)
x[3] += a[k];
for (k = j; k < 4; ++k)
x[3] += b[k];
renormalize(x[0], x[1], x[2], x[3]);
return qdouble(x[0], x[1], x[2], x[3]);
}
QDOUBLEINLINE qdouble operator - (const qdouble& a)
{
return qdouble(-a[0], -a[1], -a[2], -a[3]);
}
QDOUBLEINLINE qdouble operator - (const qdouble& a, double b)
{
return a + (-b);
}
QDOUBLEINLINE qdouble operator - (double a, const qdouble& b)
{
return (-b) + a;
}
QDOUBLEINLINE qdouble operator - (const qdouble& a, const qdouble& b)
{
return a + (-b);
}
QDOUBLEINLINE qdouble& operator += (qdouble& a, const qdouble& b)
{
a = (a + b);
return a;
}
QDOUBLEINLINE qdouble& operator += (qdouble& a, double b)
{
a = (a + b);
return a;
}
QDOUBLEINLINE qdouble& operator -= (qdouble& a, const qdouble& b)
{
a = (a - b);
return a;
}
QDOUBLEINLINE qdouble& operator -= (qdouble& a, double b)
{
a = (a - b);
return a;
}
QDOUBLEINLINE qdouble operator * (const qdouble& a, double b)
{
double p0, p1, p2, p3;
double q0, q1, q2;
double s0, s1, s2, s3, s4;
p0 = two_prod(a[0], b, q0);
p1 = two_prod(a[1], b, q1);
p2 = two_prod(a[2], b, q2);
p3 = a[3] * b;
s0 = p0;
s1 = two_sum(q0, p1, s2);
three_sum(s2, q1, p2);
three_sum2(q1, q2, p3);
s3 = q1;
s4 = q2 + p2;
renormalize(s0, s1, s2, s3, s4);
return qdouble(s0, s1, s2, s3);
}
QDOUBLEINLINE qdouble operator * (double a, const qdouble& b)
{
return b * a;
}
QDOUBLEINLINE qdouble operator * (const qdouble& a, const qdouble& b)
{
double p0, p1, p2, p3, p4, p5;
double q0, q1, q2, q3, q4, q5;
double p6, p7, p8, p9;
double q6, q7, q8, q9;
double r0, r1;
double t0, t1;
double s0, s1, s2;
p0 = two_prod(a[0], b[0], q0);
p1 = two_prod(a[0], b[1], q1);
p2 = two_prod(a[1], b[0], q2);
p3 = two_prod(a[0], b[2], q3);
p4 = two_prod(a[1], b[1], q4);
p5 = two_prod(a[2], b[0], q5);
/* Start Accumulation */
three_sum(p1, p2, q0);
/* Six-Three Sum of p2, q1, q2, p3, p4, p5. */
three_sum(p2, q1, q2);
three_sum(p3, p4, p5);
/* compute (s0, s1, s2) = (p2, q1, q2) + (p3, p4, p5). */
s0 = two_sum(p2, p3, t0);
s1 = two_sum(q1, p4, t1);
s2 = q2 + p5;
s1 = two_sum(s1, t0, t0);
s2 += (t0 + t1);
/* O(eps^3) order terms */
p6 = two_prod(a[0], b[3], q6);
p7 = two_prod(a[1], b[2], q7);
p8 = two_prod(a[2], b[1], q8);
p9 = two_prod(a[3], b[0], q9);
/* Nine-Two-Sum of q0, s1, q3, q4, q5, p6, p7, p8, p9. */
q0 = two_sum(q0, q3, q3);
q4 = two_sum(q4, q5, q5);
p6 = two_sum(p6, p7, p7);
p8 = two_sum(p8, p9, p9);
/* Compute (t0, t1) = (q0, q3) + (q4, q5). */
t0 = two_sum(q0, q4, t1);
t1 += (q3 + q5);
/* Compute (r0, r1) = (p6, p7) + (p8, p9). */
r0 = two_sum(p6, p8, r1);
r1 += (p7 + p9);
/* Compute (q3, q4) = (t0, t1) + (r0, r1). */
q3 = two_sum(t0, r0, q4);
q4 += (t1 + r1);
/* Compute (t0, t1) = (q3, q4) + s1. */
t0 = two_sum(q3, s1, t1);
t1 += q4;
/* O(eps^4) terms -- Nine-One-Sum */
t1 += a[1] * b[3] + a[2] * b[2] + a[3] * b[1] + q6 + q7 + q8 + q9 + s2;
renormalize(p0, p1, s0, t0, t1);
return qdouble(p0, p1, s0, t0);
}
QDOUBLEINLINE qdouble sqr(const qdouble& a)
{
double p0, p1, p2, p3, p4, p5;
double q0, q1, q2, q3;
double s0, s1;
double t0, t1;
p0 = two_sqr(a[0], q0);
p1 = two_prod(2.0 * a[0], a[1], q1);
p2 = two_prod(2.0 * a[0], a[2], q2);
p3 = two_sqr(a[1], q3);
p1 = two_sum(q0, p1, q0);
q0 = two_sum(q0, q1, q1);
p2 = two_sum(p2, p3, p3);
s0 = two_sum(q0, p2, t0);
s1 = two_sum(q1, p3, t1);
s1 = two_sum(s1, t0, t0);
t0 += t1;
s1 = quick_two_sum(s1, t0, t0);
p2 = quick_two_sum(s0, s1, t1);
p3 = quick_two_sum(t1, t0, q0);
p4 = 2.0 * a[0] * a[3];
p5 = 2.0 * a[1] * a[2];
p4 = two_sum(p4, p5, p5);
q2 = two_sum(q2, q3, q3);
t0 = two_sum(p4, q2, t1);
t1 = t1 + p5 + q3;
p3 = two_sum(p3, t0, p4);
p4 = p4 + q0 + t1;
renormalize(p0, p1, p2, p3, p4);
return qdouble(p0, p1, p2, p3);
}
QDOUBLEINLINE qdouble operator / (const qdouble& a, const qdouble& b)
{
double q0, q1, q2, q3;
qdouble r;
q0 = a[0] / b[0];
r = a - (b * q0);
q1 = r[0] / b[0];
r -= (b * q1);
q2 = r[0] / b[0];
r -= (b * q2);
q3 = r[0] / b[0];
r -= (b * q3);
double q4 = r[0] / b[0];
renormalize(q0, q1, q2, q3, q4);
return qdouble(q0, q1, q2, q3);
}
QDOUBLEINLINE qdouble operator / (double a, const qdouble& b)
{
return qdouble(a) / b;
}
QDOUBLEINLINE qdouble operator / (const qdouble& a, double b)
{
return a / qdouble(b);
}
QDOUBLEINLINE qdouble& operator *= (qdouble& a, const qdouble& b)
{
a = (a * b);
return a;
}
QDOUBLEINLINE qdouble& operator *= (qdouble& a, double b)
{
a = (a * b);
return a;
}
QDOUBLEINLINE qdouble& operator /= (qdouble& a, const qdouble& b)
{
a = (a / b);
return a;
}
QDOUBLEINLINE qdouble& operator /= (qdouble& a, double b)
{
a = (a / b);
return a;
}
QDOUBLEINLINE qdouble abs(const qdouble& a)
{
return (a[0] < 0.0) ? -a : a;
}
QDOUBLEINLINE qdouble fabs(const qdouble& a)
{
return abs(a);
}
QDOUBLEINLINE qdouble floor(const qdouble& a)
{
double x0, x1, x2, x3;
x1 = x2 = x3 = 0.0;
x0 = std::floor(a[0]);
if (x0 == a[0])
{
x1 = std::floor(a[1]);
if (x1 == a[1])
{
x2 = std::floor(a[2]);
if (x2 == a[2])
{
x3 = std::floor(a[3]);
}
}
renormalize(x0, x1, x2, x3);
return qdouble(x0, x1, x2, x3);
}
return qdouble(x0, x1, x2, x3);
}
QDOUBLEINLINE qdouble ceil(const qdouble& a)
{
double x0, x1, x2, x3;
x1 = x2 = x3 = 0.0;
x0 = std::floor(a[0]);
if (x0 == a[0])
{
x1 = std::ceil(a[1]);
if (x1 == a[1])
{
x2 = std::ceil(a[2]);
if (x2 == a[2])
{
x3 = std::ceil(a[3]);
}
}
renormalize(x0, x1, x2, x3);
return qdouble(x0, x1, x2, x3);
}
return qdouble(x0, x1, x2, x3);
}
QDOUBLEINLINE double to_double(const qdouble& a)
{
return a[0];
}
QDOUBLEINLINE int to_int(const qdouble& a)
{
return static_cast<int>(a[0]);
}
QDOUBLEINLINE qdouble mul_pwr2(const qdouble& a, double b)
{
return qdouble(a[0] * b, a[1] * b, a[2] * b, a[3] * b);
}
QDOUBLEINLINE qdouble sqrt(const qdouble& a)
{
/* Strategy:
Perform the following Newton iteration:
x' = x + (1 - a * x^2) * x / 2;
which converges to 1/sqrt(a), starting with the
double precision approximation to 1/sqrt(a).
Since Newton's iteration more or less doubles the
number of correct digits, we only need to perform it
twice.
*/
if (a == 0.0)
return 0.0;
if (a < 0.0)
{
return qdouble_nan;
}
qdouble r = (1.0 / std::sqrt(a[0]));
qdouble h = mul_pwr2(a, 0.5);
r += ((0.5 - h * sqr(r)) * r);
r += ((0.5 - h * sqr(r)) * r);
r += ((0.5 - h * sqr(r)) * r);
r *= a;
return r;
}
QDOUBLEINLINE qdouble ldexp(const qdouble& a, int n)
{
return qdouble(std::ldexp(a[0], n), std::ldexp(a[1], n),
std::ldexp(a[2], n), std::ldexp(a[3], n));
}
static const int qdouble_n_inv_fact = 15;
static const qdouble qdouble_inv_fact[qdouble_n_inv_fact] = {
qdouble(1.66666666666666657e-01, 9.25185853854297066e-18,
5.13581318503262866e-34, 2.85094902409834186e-50),
qdouble(4.16666666666666644e-02, 2.31296463463574266e-18,
1.28395329625815716e-34, 7.12737256024585466e-51),
qdouble(8.33333333333333322e-03, 1.15648231731787138e-19,
1.60494162032269652e-36, 2.22730392507682967e-53),
qdouble(1.38888888888888894e-03, -5.30054395437357706e-20,
-1.73868675534958776e-36, -1.63335621172300840e-52),
qdouble(1.98412698412698413e-04, 1.72095582934207053e-22,
1.49269123913941271e-40, 1.29470326746002471e-58),
qdouble(2.48015873015873016e-05, 2.15119478667758816e-23,
1.86586404892426588e-41, 1.61837908432503088e-59),
qdouble(2.75573192239858925e-06, -1.85839327404647208e-22,
8.49175460488199287e-39, -5.72661640789429621e-55),
qdouble(2.75573192239858883e-07, 2.37677146222502973e-23,
-3.26318890334088294e-40, 1.61435111860404415e-56),
qdouble(2.50521083854417202e-08, -1.44881407093591197e-24,
2.04267351467144546e-41, -8.49632672007163175e-58),
qdouble(2.08767569878681002e-09, -1.20734505911325997e-25,
1.70222792889287100e-42, 1.41609532150396700e-58),
qdouble(1.60590438368216133e-10, 1.25852945887520981e-26,
-5.31334602762985031e-43, 3.54021472597605528e-59),
qdouble(1.14707455977297245e-11, 2.06555127528307454e-28,
6.88907923246664603e-45, 5.72920002655109095e-61),
qdouble(7.64716373181981641e-13, 7.03872877733453001e-30,
-7.82753927716258345e-48, 1.92138649443790242e-64),
qdouble(4.77947733238738525e-14, 4.39920548583408126e-31,
-4.89221204822661465e-49, 1.20086655902368901e-65),
qdouble(2.81145725434552060e-15, 1.65088427308614326e-31,
-2.87777179307447918e-50, 4.27110689256293549e-67)
};
QDOUBLEINLINE qdouble exp(const qdouble& a)
{
/* Strategy: We first reduce the size of x by noting that
exp(kr + m * log(2)) = 2^m * exp(r)^k
where m and k are integers. By choosing m appropriately
we can make |kr| <= log(2) / 2 = 0.347. Then exp(r) is
evaluated using the familiar Taylor series. Reducing the
argument substantially speeds up the convergence. */
const double k = ldexp(1.0, 16);
const double inv_k = 1.0 / k;
if (a[0] <= -709.0)
return 0.0;
if (a[0] >= 709.0)
return qdouble_inf;
if (a == 0.0)
return 1.0;
if (a == 1.0)
return qdouble_e;
double m = std::floor(a[0] / qdouble_log2[0] + 0.5);
qdouble r = mul_pwr2(a - qdouble_log2 * m, inv_k);
qdouble s, p, t;
double thresh = inv_k * qdouble_eps;
p = sqr(r);
s = r + mul_pwr2(p, 0.5);
int i = 0;
do
{
p *= r;
t = p * qdouble_inv_fact[i++];
s += t;
} while (std::abs(to_double(t)) > thresh && i < 9);
s = mul_pwr2(s, 2.0) + sqr(s);
s = mul_pwr2(s, 2.0) + sqr(s);
s = mul_pwr2(s, 2.0) + sqr(s);
s = mul_pwr2(s, 2.0) + sqr(s);
s = mul_pwr2(s, 2.0) + sqr(s);
s = mul_pwr2(s, 2.0) + sqr(s);
s = mul_pwr2(s, 2.0) + sqr(s);
s = mul_pwr2(s, 2.0) + sqr(s);
s = mul_pwr2(s, 2.0) + sqr(s);
s = mul_pwr2(s, 2.0) + sqr(s);
s = mul_pwr2(s, 2.0) + sqr(s);
s = mul_pwr2(s, 2.0) + sqr(s);
s = mul_pwr2(s, 2.0) + sqr(s);
s = mul_pwr2(s, 2.0) + sqr(s);
s = mul_pwr2(s, 2.0) + sqr(s);
s = mul_pwr2(s, 2.0) + sqr(s);
s += 1.0;
return ldexp(s, static_cast<int>(m));
}
/* Logarithm. Computes log(x) in quad-double precision.
This is a natural logarithm (i.e., base e). */
QDOUBLEINLINE qdouble log(const qdouble& a)