-
Notifications
You must be signed in to change notification settings - Fork 14
/
expm1.c
2515 lines (1835 loc) · 77 KB
/
expm1.c
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
/*
* Correctly rounded expm1 = e^x - 1
*
* Author : Christoph Lauter (ENS Lyon)
* One bug fix by Florent de Dinechin
*
* This file is part of the crlibm library developed by the Arenaire
* project at Ecole Normale Superieure de Lyon
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include "crlibm.h"
#include "crlibm_private.h"
#include "triple-double.h"
#include "expm1.h"
#ifdef BUILD_INTERVAL_FUNCTIONS
#include "interval.h"
#endif
#define DEBUG 0
void expm1_direct_td(double *expm1h, double *expm1m, double *expm1l,
double x, double xSqHalfh, double xSqHalfl, double xSqh, double xSql, int expoX) {
double highPoly, tt1h, t1h, t1l, t2h, t2l, t3h, t3l, t4h, t4l, t5h, t5l, t6h, t6l;
double tt6h, tt6m, tt6l, t7h, t7m, t7l, lowPolyh, lowPolym, lowPolyl;
double fullHighPolyh, fullHighPolym, fullHighPolyl, polyh, polym, polyl;
double xCubeh, xCubem, xCubel, tt7h, tt7m, tt7l, t8h, t8m, t8l;
double expm1hover, expm1mover, expm1lover;
double r1h, r1m, r1l, r2h, r2m, r2l, r3h, r3m, r3l;
double rr1h, rr1m, rr1l, rr2h, rr2m, rr2l, rr3h, rr3m, rr3l;
double fullHighPolyhover, fullHighPolymover, fullHighPolylover;
/* Double precision evaluation steps */
#if defined(PROCESSOR_HAS_FMA) && !defined(AVOID_FMA)
highPoly = FMA(FMA(FMA(FMA(accuDirectpolyC15h ,x,accuDirectpolyC14h),x,accuDirectpolyC13h),x,
accuDirectpolyC12h),x,accuDirectpolyC11h);
#else
highPoly = accuDirectpolyC11h + x * (accuDirectpolyC12h + x * (accuDirectpolyC13h + x * (
accuDirectpolyC14h + x * accuDirectpolyC15h)));
#endif
tt1h = x * highPoly;
/* Triple-double steps for x + x^2/2 and x^3*/
Add123(&lowPolyh,&lowPolym,&lowPolyl,x,xSqHalfh,xSqHalfl); /* infty - 52/53 */
Mul123(&xCubeh,&xCubem,&xCubel,x,xSqh,xSql); /* 154 - 47/53 */
/* Double-double evaluation steps */
Add12(t1h,t1l,accuDirectpolyC10h,tt1h);
MulAdd212(&t2h,&t2l,accuDirectpolyC9h,accuDirectpolyC9m,x,t1h,t1l);
MulAdd212(&t3h,&t3l,accuDirectpolyC8h,accuDirectpolyC8m,x,t2h,t2l);
MulAdd212(&t4h,&t4l,accuDirectpolyC7h,accuDirectpolyC7m,x,t3h,t3l);
MulAdd212(&t5h,&t5l,accuDirectpolyC6h,accuDirectpolyC6m,x,t4h,t4l);
MulAdd212(&t6h,&t6l,accuDirectpolyC5h,accuDirectpolyC5m,x,t5h,t5l);
/* Triple-double evaluation steps */
Mul123(&tt6h,&tt6m,&tt6l,x,t6h,t6l); /* 154 - 47/53 */
Add233(&t7h,&t7m,&t7l,accuDirectpolyC4h,accuDirectpolyC4m,tt6h,tt6m,tt6l); /* 150 - 43/53 */
Mul133(&tt7h,&tt7m,&tt7l,x,t7h,t7m,t7l); /* 143 - 38/53 */
Add33(&t8h,&t8m,&t8l,accuDirectpolyC3h,accuDirectpolyC3m,accuDirectpolyC3l,tt7h,tt7m,tt7l); /* 135 - 33/53 */
Mul33(&fullHighPolyhover,&fullHighPolymover,&fullHighPolylover,xCubeh,xCubem,xCubel,t8h,t8m,t8l); /* 130 - 29/53 */
Renormalize3(&fullHighPolyh,&fullHighPolym,&fullHighPolyl,
fullHighPolyhover,fullHighPolymover,fullHighPolylover); /* infty - 52/53 */
Add33(&polyh,&polym,&polyl,lowPolyh,lowPolym,lowPolyl,fullHighPolyh,fullHighPolym,fullHighPolyl);
/* 149 - 47/53 */
/* Reconstruction steps */
/* If we have not performed any range reduction, we have no reconstruction to do */
if (expoX >= 0) {
/* If we are here, we must perform reconstruction */
/* First reconstruction step */
Add133(&r1h,&r1m,&r1l,2,polyh,polym,polyl);
Mul33(&rr1h,&rr1m,&rr1l,r1h,r1m,r1l,polyh,polym,polyl);
if (expoX >= 1) {
/* Second reconstruction step */
Add133(&r2h,&r2m,&r2l,2,rr1h,rr1m,rr1l);
Mul33(&rr2h,&rr2m,&rr2l,r2h,r2m,r2l,rr1h,rr1m,rr1l);
if (expoX >= 2) {
/* Third reconstruction step */
Add133(&r3h,&r3m,&r3l,2,rr2h,rr2m,rr2l);
Mul33(&rr3h,&rr3m,&rr3l,r3h,r3m,r3l,rr2h,rr2m,rr2l);
/* expoX may be maximally 2 */
expm1hover = rr3h;
expm1mover = rr3m;
expm1lover = rr3l;
} else {
expm1hover = rr2h;
expm1mover = rr2m;
expm1lover = rr2l;
}
} else {
expm1hover = rr1h;
expm1mover = rr1m;
expm1lover = rr1l;
}
} else {
expm1hover = polyh;
expm1mover = polym;
expm1lover = polyl;
}
/* Renormalize before returning */
Renormalize3(expm1h,expm1m,expm1l,expm1hover,expm1mover,expm1lover);
}
void expm1_common_td(double *expm1h, double *expm1m, double *expm1l,
double rh, double rm, double rl,
double tbl1h, double tbl1m, double tbl1l,
double tbl2h, double tbl2m, double tbl2l,
int M) {
double highPoly, highPolyMulth, highPolyMultm, highPolyMultl;
double rhSquareh, rhSquarel, rhSquareHalfh, rhSquareHalfl;
double rhCubeh, rhCubem, rhCubel;
double t1h, t1l, t2h, t2l, t3h, t3l, t4h, t4l, t5, t6;
double lowPolyh, lowPolym, lowPolyl;
double ph, pm, pl, phnorm, pmnorm, rmlMultPh, rmlMultPl;
double qh, ql, fullPolyh, fullPolym, fullPolyl;
double polyWithTbl1h, polyWithTbl1m, polyWithTbl1l;
double polyAddOneh,polyAddOnem,polyAddOnel;
double polyWithTablesh, polyWithTablesm, polyWithTablesl;
double exph, expm, expl, expm1hover, expm1mover, expm1lover;
db_number polyWithTableshdb, polyWithTablesmdb, polyWithTablesldb;
/* Polynomial approximation - double precision steps */
#if defined(PROCESSOR_HAS_FMA) && !defined(AVOID_FMA)
highPoly = FMA(FMA(accuCommonpolyC7h,rh,accuCommonpolyC6h),rh,accuCommonpolyC5h);
#else
highPoly = accuCommonpolyC5h + rh * (accuCommonpolyC6h + rh * accuCommonpolyC7h);
#endif
/* Polynomial approximation - double-double precision steps */
Mul12(&t1h,&t1l,rh,highPoly);
Add22(&t2h,&t2l,accuCommonpolyC4h,accuCommonpolyC4m,t1h,t1l);
Mul122(&t3h,&t3l,rh,t2h,t2l);
Add22(&t4h,&t4l,accuCommonpolyC3h,accuCommonpolyC3m,t3h,t3l);
Mul12(&rhSquareh,&rhSquarel,rh,rh);
Mul123(&rhCubeh,&rhCubem,&rhCubel,rh,rhSquareh,rhSquarel);
rhSquareHalfh = 0.5 * rhSquareh;
rhSquareHalfl = 0.5 * rhSquarel;
/* Polynomial approximation - triple-double precision steps */
Renormalize3(&lowPolyh,&lowPolym,&lowPolyl,rh,rhSquareHalfh,rhSquareHalfl);
Mul233(&highPolyMulth,&highPolyMultm,&highPolyMultl,t4h,t4l,rhCubeh,rhCubem,rhCubel);
Add33(&ph,&pm,&pl,lowPolyh,lowPolym,lowPolyl,highPolyMulth,highPolyMultm,highPolyMultl);
/* Reconstruction */
Add12(phnorm,pmnorm,ph,pm);
Mul22(&rmlMultPh,&rmlMultPl,rm,rl,phnorm,pmnorm);
Add22(&qh,&ql,rm,rl,rmlMultPh,rmlMultPl);
Add233Cond(&fullPolyh,&fullPolym,&fullPolyl,qh,ql,ph,pm,pl);
Add12(polyAddOneh,t5,1,fullPolyh);
Add12Cond(polyAddOnem,t6,t5,fullPolym);
polyAddOnel = t6 + fullPolyl;
Mul33(&polyWithTbl1h,&polyWithTbl1m,&polyWithTbl1l,tbl1h,tbl1m,tbl1l,polyAddOneh,polyAddOnem,polyAddOnel);
Mul33(&polyWithTablesh,&polyWithTablesm,&polyWithTablesl,
tbl2h,tbl2m,tbl2l,
polyWithTbl1h,polyWithTbl1m,polyWithTbl1l);
/* Multiplication by 2^(M)
We perform it in integer to overcome the non-representability of 2^(1024)
This case is possible for M = 1024 and polyWithTablesh < 1
The overlap in the triple-double polyWithTables[hml] stays unchanged.
*/
polyWithTableshdb.d = polyWithTablesh;
polyWithTablesmdb.d = polyWithTablesm;
polyWithTablesldb.d = polyWithTablesl;
/* TODO FIXME probably at least the first of these tests is useless,
but I leave this to Christoph to check it. Let us be
conservative. Florent */
if(polyWithTableshdb.d!=0)
polyWithTableshdb.i[HI] += M << 20;
if(polyWithTablesmdb.d!=0)
polyWithTablesmdb.i[HI] += M << 20;
if(polyWithTablesldb.d!=0)
polyWithTablesldb.i[HI] += M << 20;
exph = polyWithTableshdb.d;
expm = polyWithTablesmdb.d;
expl = polyWithTablesldb.d;
/* Subtraction of -1
We use a conditional Add133
*/
Add133Cond(&expm1hover,&expm1mover,&expm1lover,-1,exph,expm,expl);
/* Renormalization */
Renormalize3(expm1h,expm1m,expm1l,expm1hover,expm1mover,expm1lover);
}
double expm1_rn(double x) {
db_number xdb, shiftedXMultdb, polyTblhdb, polyTblmdb;
int xIntHi, expoX, k, M, index1, index2;
double highPoly, tt1h, t1h, t1l, xSqh, xSql, xSqHalfh, xSqHalfl, xCubeh, xCubel, t2h, t2l, templ, tt3h, tt3l;
double polyh, polyl, expm1h, expm1m, expm1l;
double r1h, r1l, r1t, rr1h, rr1l;
double r2h, r2l, r2t, rr2h, rr2l;
double r3h, r3l, r3t, rr3h, rr3l;
double xMultLog2InvMult2L, shiftedXMult, kd, s1, s2, s3, s4, s5, rh, rm, rl;
double rhSquare, rhC3, rhSquareHalf, monomialCube, rhFour, monomialFour;
double tbl1h, tbl1m, tbl1l, tbl2h, tbl2m, tbl2l;
double highPolyWithSquare, tablesh, tablesl, t8, t9, t10, t11, t12, t13;
double exph, expm, t1, t2, t3;
double msLog2Div2LMultKh, msLog2Div2LMultKm, msLog2Div2LMultKl;
double middlePoly, doublePoly;
xdb.d = x;
/* Strip off the sign of x for the following tests */
xIntHi = xdb.i[HI] & 0x7fffffff;
/* Test if we are so small that we can return (a corrected) x as correct rounding */
if (xIntHi < RETURNXBOUND) {
return x;
}
/* Filter out special cases like overflow, -1 in result, infinities and NaNs
The filters are not sharp, we have positive arguments that flow through
*/
if (xIntHi >= SIMPLEOVERFLOWBOUND) {
/* Test if we are +/-inf or NaN */
if (xIntHi >= 0x7ff00000) {
/* Test if NaN */
if (((xIntHi & 0x000fffff) | xdb.i[LO]) != 0) {
/* NaN */
return x+x; /* return NaN */
}
/* Test if +inf or -inf */
if (xdb.i[HI] > 0) {
/* +inf */
return x+x; /* return +inf */
}
/* If we are here, we are -inf */
return -1.0;
}
/* If we are here, we are overflowed or a common case that flows through */
/* Test if we are actually overflowed */
if (x > OVERFLOWBOUND) {
return LARGEST * LARGEST; /* return +inf and set flag */
}
}
/* Test if we know already that we are -1.0 (+ correction depending on rounding mode) in result */
if (x < MINUSONEBOUND) {
return -1.0;
}
/* Test if we have |x| <= 1/4-1/2ulp(1/4) for knowing if we use exp(x) or approximate directly */
if (xIntHi < DIRECTINTERVALBOUND) {
/* We approximate expm1 directly after a range reduction as follows
expm1(x) = (expm1(x/2) + 2) * expm1(x/2)
We perform the range reduction in such a way that finally |x| < 1/32
*/
/* Extract the exponent of |x| and add 5 (2^5 = 32) */
expoX = ((xIntHi & 0x7ff00000) >> 20) - (1023 - 5);
/* If this particularily biased exponent expoX is negative, we are already less than 1/32 */
if (expoX >= 0) {
/* If we are here, we must perform range reduction */
/* We multiply x by 2^(-expoX-1) by bit manipulation
x cannot be denormalized so there is no danger
*/
xdb.i[HI] += (-expoX-1) << 20;
/* We reassign the new x and maintain xIntHi */
xIntHi = xdb.i[HI] & 0x7fffffff;
x = xdb.d;
}
/* Here, we have always |x| < 1/32 */
/* Double precision evaluation steps and one double-double step */
Mul12(&xSqh,&xSql,x,x);
#if defined(PROCESSOR_HAS_FMA) && !defined(AVOID_FMA)
middlePoly = FMA(quickDirectpolyC5h,x,quickDirectpolyC4h);
#else
middlePoly = quickDirectpolyC4h + x * quickDirectpolyC5h;
#endif
doublePoly = middlePoly;
/* Special path: for small |x| we can truncate the polynomial */
if (xIntHi > SPECIALINTERVALBOUND) {
#if defined(PROCESSOR_HAS_FMA) && !defined(AVOID_FMA)
highPoly = FMA(FMA(FMA(quickDirectpolyC9h ,x,quickDirectpolyC8h),x,
quickDirectpolyC7h),x,quickDirectpolyC6h);
#else
highPoly = quickDirectpolyC6h + x * (quickDirectpolyC7h + x * (
quickDirectpolyC8h + x * quickDirectpolyC9h));
#endif
highPolyWithSquare = xSqh * highPoly;
doublePoly = middlePoly + highPolyWithSquare;
}
/* Double-double evaluation steps */
tt1h = x * doublePoly;
xSqHalfh = 0.5 * xSqh;
xSqHalfl = 0.5 * xSql;
Add12(t2h,templ,x,xSqHalfh);
t2l = templ + xSqHalfl;
Add12(t1h,t1l,quickDirectpolyC3h,tt1h);
Mul122(&xCubeh,&xCubel,x,xSqh,xSql);
Mul22(&tt3h,&tt3l,xCubeh,xCubel,t1h,t1l);
Add22(&polyh,&polyl,t2h,t2l,tt3h,tt3l);
/* Reconstruction */
/* If we have not performed any range reduction, we have no reconstruction to do */
if (expoX >= 0) {
/* If we are here, we must perform reconstruction */
/* First reconstruction step */
Add12(r1h,r1t,2,polyh);
r1l = r1t + polyl;
Mul22(&rr1h,&rr1l,r1h,r1l,polyh,polyl);
if (expoX >= 1) {
/* Second reconstruction step */
Add12(r2h,r2t,2,rr1h);
r2l = r2t + rr1l;
Mul22(&rr2h,&rr2l,r2h,r2l,rr1h,rr1l);
if (expoX >= 2) {
/* Third reconstruction step */
Add12(r3h,r3t,2,rr2h);
r3l = r3t + rr2l;
Mul22(&rr3h,&rr3l,r3h,r3l,rr2h,rr2l);
/* expoX may be maximally 2 */
expm1h = rr3h;
expm1m = rr3l;
} else {
expm1h = rr2h;
expm1m = rr2l;
}
} else {
expm1h = rr1h;
expm1m = rr1l;
}
} else {
expm1h = polyh;
expm1m = polyl;
}
/* Rounding test */
if(expm1h == (expm1h + (expm1m * ROUNDCSTDIRECTRN)))
return expm1h;
else
{
#if DEBUG
printf("Launch accurate phase (direct interval)\n");
#endif
expm1_direct_td(&expm1h, &expm1m, &expm1l, x, xSqHalfh, xSqHalfl, xSqh, xSql, expoX);
ReturnRoundToNearest3(expm1h, expm1m, expm1l);
} /* Accurate phase launched */
/* We cannot be here, since we return in all cases before */
}
/* If we are here, we can use expm1(x) = exp(x) - 1 */
/* Range reduction - exact part: compute k as double and as int */
xMultLog2InvMult2L = x * log2InvMult2L;
shiftedXMult = xMultLog2InvMult2L + shiftConst;
kd = shiftedXMult - shiftConst;
shiftedXMultdb.d = shiftedXMult;
k = shiftedXMultdb.i[LO];
M = k >> 12;
index1 = k & INDEXMASK1;
index2 = (k & INDEXMASK2) >> 6;
/* Range reduction - part affected by error - must be redone in accurate phase */
Mul12(&s1,&s2,msLog2Div2Lh,kd);
s3 = kd * msLog2Div2Lm;
s4 = s2 + s3;
s5 = x + s1;
Add12Cond(rh,rm,s5,s4);
/* Table reads - read only two double-doubles by now */
tbl1h = twoPowerIndex1[index1].hi;
tbl1m = twoPowerIndex1[index1].mi;
tbl2h = twoPowerIndex2[index2].hi;
tbl2m = twoPowerIndex2[index2].mi;
/* Quick phase starts here */
rhSquare = rh * rh;
rhC3 = quickCommonpolyC3h * rh;
rhSquareHalf = 0.5 * rhSquare;
monomialCube = rhC3 * rhSquare;
rhFour = rhSquare * rhSquare;
monomialFour = quickCommonpolyC4h * rhFour;
highPoly = monomialCube + monomialFour;
highPolyWithSquare = rhSquareHalf + highPoly;
/* Reconstruction: integration of table values */
Mul22(&tablesh,&tablesl,tbl1h,tbl1m,tbl2h,tbl2m);
t8 = rm + highPolyWithSquare;
t9 = rh + t8;
t10 = tablesh * t9;
Add12(t11,t12,tablesh,t10);
t13 = t12 + tablesl;
Add12(polyTblhdb.d,polyTblmdb.d,t11,t13);
/* Reconstruction: multiplication by 2^M */
/* Implement the multiplication by addition to overcome the
problem of the non-representability of 2^1024 (M = 1024)
This case is possible if polyTblhdb.d < 1
*/
polyTblhdb.i[HI] += M << 20;
if(polyTblmdb.d!=0.0) /* predicted true, but it happens for x=-4.1588039009762204, thanks Morten */
polyTblmdb.i[HI] += M << 20;
exph = polyTblhdb.d;
expm = polyTblmdb.d;
/* Subtraction of 1
Testing if the operation is necessary is more expensive than
performing it in any case.
We may cancellate at most 2 bits in the subtraction for
arguments 1/4 <= x <= ln(2) (0.25 <= x <= 0.69)
We must therefore use conditional Add12s
Since we perform a subtraction, we may not have addition overflow towards +inf
*/
Add12Cond(t1,t2,-1,exph);
t3 = t2 + expm;
Add12Cond(expm1h,expm1m,t1,t3);
/* Rounding test */
if(expm1h == (expm1h + (expm1m * ROUNDCSTCOMMONRN))) {
return expm1h;
} else {
/* Rest of argument reduction for accurate phase */
Mul133(&msLog2Div2LMultKh,&msLog2Div2LMultKm,&msLog2Div2LMultKl,kd,msLog2Div2Lh,msLog2Div2Lm,msLog2Div2Ll);
t1 = x + msLog2Div2LMultKh;
Add12Cond(rh,t2,t1,msLog2Div2LMultKm);
Add12Cond(rm,rl,t2,msLog2Div2LMultKl);
/* Table reads for accurate phase */
tbl1l = twoPowerIndex1[index1].lo;
tbl2l = twoPowerIndex2[index2].lo;
#if DEBUG
printf("Launch accurate phase (common interval)\n");
#endif
/* Call accurate phase */
expm1_common_td(&expm1h, &expm1m, &expm1l, rh, rm, rl, tbl1h, tbl1m, tbl1l, tbl2h, tbl2m, tbl2l, M);
/* Final rounding */
ReturnRoundToNearest3(expm1h, expm1m, expm1l);
} /* Accurate phase launched */
/* We cannot be here since we return before in any case */
}
double expm1_rd(double x) {
db_number xdb, shiftedXMultdb, polyTblhdb, polyTblmdb;
int xIntHi, expoX, k, M, index1, index2;
double highPoly, tt1h, t1h, t1l, xSqh, xSql, xSqHalfh, xSqHalfl, xCubeh, xCubel, t2h, t2l, templ, tt3h, tt3l;
double polyh, polyl, expm1h, expm1m, expm1l;
double r1h, r1l, r1t, rr1h, rr1l;
double r2h, r2l, r2t, rr2h, rr2l;
double r3h, r3l, r3t, rr3h, rr3l;
double xMultLog2InvMult2L, shiftedXMult, kd, s1, s2, s3, s4, s5, rh, rm, rl;
double rhSquare, rhC3, rhSquareHalf, monomialCube, rhFour, monomialFour;
double tbl1h, tbl1m, tbl1l, tbl2h, tbl2m, tbl2l;
double highPolyWithSquare, tablesh, tablesl, t8, t9, t10, t11, t12, t13;
double exph, expm, t1, t2, t3;
double msLog2Div2LMultKh, msLog2Div2LMultKm, msLog2Div2LMultKl;
double middlePoly, doublePoly;
xdb.d = x;
/* Strip off the sign of x for the following tests */
xIntHi = xdb.i[HI] & 0x7fffffff;
/* Test if we are so small that we can return (a corrected) x as correct rounding */
if (xIntHi < RETURNXBOUND) {
/* The only algebraic result is 0 for x = +/- 0; in this case, we can return x = +/- 0
The truncation rest x^2/2 + x^3/6 + ... is always positive
but less than 1 ulp in this case, so we round down by returning x
*/
return x;
}
/* Filter out special cases like overflow, -1 in result, infinities and NaNs
The filters are not sharp, we have positive arguments that flow through
*/
if (xIntHi >= SIMPLEOVERFLOWBOUND) {
/* Test if we are +/-inf or NaN */
if (xIntHi >= 0x7ff00000) {
/* Test if NaN */
if (((xIntHi & 0x000fffff) | xdb.i[LO]) != 0) {
/* NaN */
return x+x; /* return NaN */
}
/* Test if +inf or -inf */
if (xdb.i[HI] > 0) {
/* +inf */
return x+x; /* return +inf */
}
/* If we are here, we are -inf */
return -1.0;
}
/* If we are here, we are overflowed or a common case that flows through */
/* Test if we are actually overflowed */
if (x > OVERFLOWBOUND) {
/* We would be overflowed but as we are rounding downwards
the nearest number lesser than the exact result is the greatest
normal. In any case, we must raise the inexact flag.
*/
return LARGEST * (1.0 + SMALLEST);
}
}
/* Test if we know already that we are -1.0 (+ correction depending on rounding mode) in result */
if (x < MINUSONEBOUND) {
/* We round down, so we are -1.0 */
return -1.0;
}
/* Test if we have |x| <= 1/4-1/2ulp(1/4) for knowing if we use exp(x) or approximate directly */
if (xIntHi < DIRECTINTERVALBOUND) {
/* We approximate expm1 directly after a range reduction as follows
expm1(x) = (expm1(x/2) + 2) * expm1(x/2)
We perform the range reduction in such a way that finally |x| < 1/32
*/
/* Extract the exponent of |x| and add 5 (2^5 = 32) */
expoX = ((xIntHi & 0x7ff00000) >> 20) - (1023 - 5);
/* If this particularily biased exponent expoX is negative, we are already less than 1/32 */
if (expoX >= 0) {
/* If we are here, we must perform range reduction */
/* We multiply x by 2^(-expoX-1) by bit manipulation
x cannot be denormalized so there is no danger
*/
xdb.i[HI] += (-expoX-1) << 20;
/* We reassign the new x and maintain xIntHi */
xIntHi = xdb.i[HI] & 0x7fffffff;
x = xdb.d;
}
/* Here, we have always |x| < 1/32 */
/* Double precision evaluation steps and one double-double step */
Mul12(&xSqh,&xSql,x,x);
#if defined(PROCESSOR_HAS_FMA) && !defined(AVOID_FMA)
middlePoly = FMA(quickDirectpolyC5h,x,quickDirectpolyC4h);
#else
middlePoly = quickDirectpolyC4h + x * quickDirectpolyC5h;
#endif
doublePoly = middlePoly;
/* Special path: for small |x| we can truncate the polynomial */
if (xIntHi > SPECIALINTERVALBOUND) {
#if defined(PROCESSOR_HAS_FMA) && !defined(AVOID_FMA)
highPoly = FMA(FMA(FMA(quickDirectpolyC9h ,x,quickDirectpolyC8h),x,
quickDirectpolyC7h),x,quickDirectpolyC6h);
#else
highPoly = quickDirectpolyC6h + x * (quickDirectpolyC7h + x * (
quickDirectpolyC8h + x * quickDirectpolyC9h));
#endif
highPolyWithSquare = xSqh * highPoly;
doublePoly = middlePoly + highPolyWithSquare;
}
/* Double-double evaluation steps */
tt1h = x * doublePoly;
xSqHalfh = 0.5 * xSqh;
xSqHalfl = 0.5 * xSql;
Add12(t2h,templ,x,xSqHalfh);
t2l = templ + xSqHalfl;
Add12(t1h,t1l,quickDirectpolyC3h,tt1h);
Mul122(&xCubeh,&xCubel,x,xSqh,xSql);
Mul22(&tt3h,&tt3l,xCubeh,xCubel,t1h,t1l);
Add22(&polyh,&polyl,t2h,t2l,tt3h,tt3l);
/* Reconstruction */
/* If we have not performed any range reduction, we have no reconstruction to do */
if (expoX >= 0) {
/* If we are here, we must perform reconstruction */
/* First reconstruction step */
Add12(r1h,r1t,2,polyh);
r1l = r1t + polyl;
Mul22(&rr1h,&rr1l,r1h,r1l,polyh,polyl);
if (expoX >= 1) {
/* Second reconstruction step */
Add12(r2h,r2t,2,rr1h);
r2l = r2t + rr1l;
Mul22(&rr2h,&rr2l,r2h,r2l,rr1h,rr1l);
if (expoX >= 2) {
/* Third reconstruction step */
Add12(r3h,r3t,2,rr2h);
r3l = r3t + rr2l;
Mul22(&rr3h,&rr3l,r3h,r3l,rr2h,rr2l);
/* expoX may be maximally 2 */
expm1h = rr3h;
expm1m = rr3l;
} else {
expm1h = rr2h;
expm1m = rr2l;
}
} else {
expm1h = rr1h;
expm1m = rr1l;
}
} else {
expm1h = polyh;
expm1m = polyl;
}
/* Rounding test */
TEST_AND_RETURN_RD(expm1h, expm1m, ROUNDCSTDIRECTRD);
{
expm1_direct_td(&expm1h, &expm1m, &expm1l, x, xSqHalfh, xSqHalfl, xSqh, xSql, expoX);
ReturnRoundDownwards3(expm1h, expm1m, expm1l);
} /* Accurate phase launched */
/* We cannot be here, since we return in all cases before */
}
/* If we are here, we can use expm1(x) = exp(x) - 1 */
/* Range reduction - exact part: compute k as double and as int */
xMultLog2InvMult2L = x * log2InvMult2L;
shiftedXMult = xMultLog2InvMult2L + shiftConst;
kd = shiftedXMult - shiftConst;
shiftedXMultdb.d = shiftedXMult;
k = shiftedXMultdb.i[LO];
M = k >> 12;
index1 = k & INDEXMASK1;
index2 = (k & INDEXMASK2) >> 6;
/* Range reduction - part affected by error - must be redone in accurate phase */
Mul12(&s1,&s2,msLog2Div2Lh,kd);
s3 = kd * msLog2Div2Lm;
s4 = s2 + s3;
s5 = x + s1;
Add12Cond(rh,rm,s5,s4);
/* Table reads - read only two double-doubles by now */
tbl1h = twoPowerIndex1[index1].hi;
tbl1m = twoPowerIndex1[index1].mi;
tbl2h = twoPowerIndex2[index2].hi;
tbl2m = twoPowerIndex2[index2].mi;
/* Quick phase starts here */
rhSquare = rh * rh;
rhC3 = quickCommonpolyC3h * rh;
rhSquareHalf = 0.5 * rhSquare;
monomialCube = rhC3 * rhSquare;
rhFour = rhSquare * rhSquare;
monomialFour = quickCommonpolyC4h * rhFour;
highPoly = monomialCube + monomialFour;
highPolyWithSquare = rhSquareHalf + highPoly;
/* Reconstruction: integration of table values */
Mul22(&tablesh,&tablesl,tbl1h,tbl1m,tbl2h,tbl2m);
t8 = rm + highPolyWithSquare;
t9 = rh + t8;
t10 = tablesh * t9;
Add12(t11,t12,tablesh,t10);
t13 = t12 + tablesl;
Add12(polyTblhdb.d,polyTblmdb.d,t11,t13);
/* Reconstruction: multiplication by 2^M */
/* Implement the multiplication by addition to overcome the
problem of the non-representability of 2^1024 (M = 1024)
This case is possible if polyTblhdb.d < 1
*/
polyTblhdb.i[HI] += M << 20;
if(polyTblmdb.d!=0.0) /* predicted true, but it happens for x=-4.1588039009762204, thanks Morten */
polyTblmdb.i[HI] += M << 20;
exph = polyTblhdb.d;
expm = polyTblmdb.d;
/* Subtraction of 1
Testing if the operation is necessary is more expensive than
performing it in any case.
We may cancellate at most 2 bits in the subtraction for
arguments 1/4 <= x <= ln(2) (0.25 <= x <= 0.69)
We must therefore use conditional Add12s
Since we perform a subtraction, we may not have addition overflow towards +inf
*/
Add12Cond(t1,t2,-1,exph);
t3 = t2 + expm;
Add12Cond(expm1h,expm1m,t1,t3);
/* Rounding test */
TEST_AND_RETURN_RD(expm1h, expm1m, ROUNDCSTCOMMONRD);
{
/* Rest of argument reduction for accurate phase */
Mul133(&msLog2Div2LMultKh,&msLog2Div2LMultKm,&msLog2Div2LMultKl,kd,msLog2Div2Lh,msLog2Div2Lm,msLog2Div2Ll);
t1 = x + msLog2Div2LMultKh;
Add12Cond(rh,t2,t1,msLog2Div2LMultKm);
Add12Cond(rm,rl,t2,msLog2Div2LMultKl);
/* Table reads for accurate phase */
tbl1l = twoPowerIndex1[index1].lo;
tbl2l = twoPowerIndex2[index2].lo;
/* Call accurate phase */
expm1_common_td(&expm1h, &expm1m, &expm1l, rh, rm, rl, tbl1h, tbl1m, tbl1l, tbl2h, tbl2m, tbl2l, M);
/* Final rounding */
ReturnRoundDownwards3(expm1h, expm1m, expm1l);
} /* Accurate phase launched */
/* We cannot be here since we return before in any case */
}
double expm1_ru(double x) {
db_number xdb, shiftedXMultdb, polyTblhdb, polyTblmdb;
int xIntHi, expoX, k, M, index1, index2;
double highPoly, tt1h, t1h, t1l, xSqh, xSql, xSqHalfh, xSqHalfl, xCubeh, xCubel, t2h, t2l, templ, tt3h, tt3l;
double polyh, polyl, expm1h, expm1m, expm1l;
double r1h, r1l, r1t, rr1h, rr1l;
double r2h, r2l, r2t, rr2h, rr2l;
double r3h, r3l, r3t, rr3h, rr3l;
double xMultLog2InvMult2L, shiftedXMult, kd, s1, s2, s3, s4, s5, rh, rm, rl;
double rhSquare, rhC3, rhSquareHalf, monomialCube, rhFour, monomialFour;
double tbl1h, tbl1m, tbl1l, tbl2h, tbl2m, tbl2l;
double highPolyWithSquare, tablesh, tablesl, t8, t9, t10, t11, t12, t13;
double exph, expm, t1, t2, t3;
double msLog2Div2LMultKh, msLog2Div2LMultKm, msLog2Div2LMultKl;
double middlePoly, doublePoly;
xdb.d = x;
/* Strip off the sign of x for the following tests */
xIntHi = xdb.i[HI] & 0x7fffffff;
/* Test if we are so small that we can return (a corrected) x as correct rounding */
if (xIntHi < RETURNXBOUND) {
/* The only algebraic result is 0 for x = +/-0; in this case, we return x = +/-0
The truncation rest x^2/2 + x^3/6 + ... is always positive
but less than 1 ulp in this case, so we round by adding 1 ulp
*/
if (x == 0.0) return x;
if (xdb.i[HI] & 0x80000000) {
/* x is negative
We add 1 ulp by subtracting 1 in long
*/
xdb.l--;
} else {
/* x is positive
We add 1 ulp by adding 1 in long
*/
xdb.l++;
}
return xdb.d;
}
/* Filter out special cases like overflow, -1 in result, infinities and NaNs
The filters are not sharp, we have positive arguments that flow through
*/
if (xIntHi >= SIMPLEOVERFLOWBOUND) {
/* Test if we are +/-inf or NaN */
if (xIntHi >= 0x7ff00000) {
/* Test if NaN */
if (((xIntHi & 0x000fffff) | xdb.i[LO]) != 0) {
/* NaN */
return x+x; /* return NaN */
}
/* Test if +inf or -inf */
if (xdb.i[HI] > 0) {
/* +inf */
return x+x; /* return +inf */
}
/* If we are here, we are -inf */
return -1.0;
}
/* If we are here, we are overflowed or a common case that flows through */
/* Test if we are actually overflowed */
if (x > OVERFLOWBOUND) {
return LARGEST * LARGEST; /* return +inf and set flag */
}
}
/* Test if we know already that we are -1.0 (+ correction depending on rounding mode) in result */
if (x < MINUSONEBOUND) {
/* Round up so we are -1.0 + 1ulp */
return MINUSONEPLUSONEULP;
}
/* Test if we have |x| <= 1/4-1/2ulp(1/4) for knowing if we use exp(x) or approximate directly */
if (xIntHi < DIRECTINTERVALBOUND) {
/* We approximate expm1 directly after a range reduction as follows
expm1(x) = (expm1(x/2) + 2) * expm1(x/2)
We perform the range reduction in such a way that finally |x| < 1/32
*/
/* Extract the exponent of |x| and add 5 (2^5 = 32) */
expoX = ((xIntHi & 0x7ff00000) >> 20) - (1023 - 5);
/* If this particularily biased exponent expoX is negative, we are already less than 1/32 */
if (expoX >= 0) {
/* If we are here, we must perform range reduction */
/* We multiply x by 2^(-expoX-1) by bit manipulation
x cannot be denormalized so there is no danger
*/
xdb.i[HI] += (-expoX-1) << 20;
/* We reassign the new x and maintain xIntHi */
xIntHi = xdb.i[HI] & 0x7fffffff;
x = xdb.d;
}
/* Here, we have always |x| < 1/32 */
/* Double precision evaluation steps and one double-double step */
Mul12(&xSqh,&xSql,x,x);
#if defined(PROCESSOR_HAS_FMA) && !defined(AVOID_FMA)
middlePoly = FMA(quickDirectpolyC5h,x,quickDirectpolyC4h);