-
Notifications
You must be signed in to change notification settings - Fork 14
/
pow.c
executable file
·1396 lines (1037 loc) · 37.8 KB
/
pow.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
#include <stdio.h>
#include "crlibm.h"
#include "crlibm_private.h"
#include "triple-double.h"
#include "pow.h"
/* Some macros for specific operations in power */
#define USE_BUILTINS 0
/* decompose
Decomposes a positive double precision
variable x (normal or subnormal) such that
2^(resE) * resm = x
where resm = 2 * k + 1 for integer k
resE is an integer variable pointer
resm is a double precision variable pointer
x is a double precision variable.
*/
#if USE_BUILTINS
#define decompose(resm,resE,x) \
{ \
int __decompose_ex, __decompose_d; \
int64_t __decompose_temp; \
db_number __decompose_xdb; \
\
__decompose_xdb.d = (x); \
__decompose_ex = 0; \
if (!(__decompose_xdb.i[HI] & 0xfff00000)) { \
__decompose_xdb.d = (x) * 0.18014398509481984e17; \
__decompose_ex = -54; \
} \
__decompose_ex += (__decompose_xdb.i[HI] >> 20) - 1023; \
__decompose_xdb.i[HI] &= 0x000fffff; \
__decompose_temp = __decompose_xdb.l | 0x0010000000000000llu; \
__decompose_d = __builtin_ctz(__decompose_temp); \
__decompose_xdb.i[HI] |= (1075 - __decompose_d) << 20; \
*(resE) = __decompose_d - 52 + __decompose_ex; \
*(resm) = __decompose_xdb.d; \
}
#else
#define decompose(resm,resE,x) \
{ \
int __decompose_ex, __decompose_d; \
int64_t __decompose_temp; \
db_number __decompose_xdb, __decompose_tempdb; \
\
__decompose_xdb.d = (x); \
__decompose_ex = 0; \
if (!(__decompose_xdb.i[HI] & 0xfff00000)) { \
__decompose_xdb.d = (x) * 0.18014398509481984e17; \
__decompose_ex = -54; \
} \
__decompose_ex += (__decompose_xdb.i[HI] >> 20) - 1023; \
__decompose_xdb.i[HI] &= 0x000fffff; \
__decompose_temp = __decompose_xdb.l | 0x0010000000000000llu; \
__decompose_temp = (~__decompose_temp & (__decompose_temp - 1)) + 1; \
__decompose_tempdb.d = (double) __decompose_temp; \
__decompose_d = (__decompose_tempdb.i[HI] >> 20) - 1023; \
__decompose_xdb.i[HI] |= (1075 - __decompose_d) << 20; \
*(resE) = __decompose_d - 52 + __decompose_ex; \
*(resm) = __decompose_xdb.d; \
}
#endif
/* isOddInteger
Determines if a given double precision number x is an odd integer
*/
#define isOddInteger(x) (ABS(((ABS(x) + 0.9007199254740992e16) - 0.9007199254740992e16) - ABS(x)) == 1.0)
/* isInteger
Determines if a given double precision number x is integer
*/
#define isInteger(x) ((ABS(x) >= 0.4503599627370496e16) || (((ABS(x) + 0.4503599627370496e16) - 0.4503599627370496e16) == ABS(x)))
/* log2_130
Approximates
resh + resm + resl = (ed + log2(1 + (xh + xm)) + log2(r[index])) * (1 + eps)
where ||eps|| <= 2^(-130)
*/
static inline void log2_130(double *resh, double *resm, double *resl,
int index, double ed, double xh, double xm) {
double p_t_1_0h;
double p_t_2_0h;
double p_t_3_0h;
double p_t_4_0h;
double p_t_5_0h;
double p_t_6_0h;
double p_t_7_0h;
double p_t_8_0h;
double p_t_9_0h, p_t_9_0m;
double p_t_10_0h, p_t_10_0m;
double p_t_11_0h, p_t_11_0m;
double p_t_12_0h, p_t_12_0m;
double p_t_13_0h, p_t_13_0m;
double p_t_14_0h, p_t_14_0m;
double p_t_15_0h, p_t_15_0m;
double p_t_16_0h, p_t_16_0m, p_t_16_0l;
double p_t_17_0h, p_t_17_0m, p_t_17_0l;
double p_t_18_0h, p_t_18_0m, p_t_18_0l;
double p_t_19_0h, p_t_19_0m, p_t_19_0l;
double p_t_20_0h, p_t_20_0m, p_t_20_0l;
double p_t_21_0h, p_t_21_0m, p_t_21_0l;
double p_t_21_1h, p_t_21_1m, p_t_21_1l;
double p_t_22_0h, p_t_22_0m, p_t_22_0l;
double p_t_23_0h, p_t_23_0m, p_t_23_0l;
double p_resh, p_resm, p_resl;
double log2yh, log2ym, log2yl;
double log2xh, log2xm, log2xl;
double logih, logim, logil;
p_t_1_0h = log2_130_p_coeff_13h;
p_t_2_0h = p_t_1_0h * xh;
p_t_3_0h = log2_130_p_coeff_12h + p_t_2_0h;
p_t_4_0h = p_t_3_0h * xh;
p_t_5_0h = log2_130_p_coeff_11h + p_t_4_0h;
p_t_6_0h = p_t_5_0h * xh;
p_t_7_0h = log2_130_p_coeff_10h + p_t_6_0h;
p_t_8_0h = p_t_7_0h * xh;
Add12(p_t_9_0h,p_t_9_0m,log2_130_p_coeff_9h,p_t_8_0h);
Mul22(&p_t_10_0h,&p_t_10_0m,p_t_9_0h,p_t_9_0m,xh,xm);
Add122(&p_t_11_0h,&p_t_11_0m,log2_130_p_coeff_8h,p_t_10_0h,p_t_10_0m);
MulAdd22(&p_t_12_0h,&p_t_12_0m,log2_130_p_coeff_7h,log2_130_p_coeff_7m,xh,xm,p_t_11_0h,p_t_11_0m);
MulAdd22(&p_t_13_0h,&p_t_13_0m,log2_130_p_coeff_6h,log2_130_p_coeff_6m,xh,xm,p_t_12_0h,p_t_12_0m);
MulAdd22(&p_t_14_0h,&p_t_14_0m,log2_130_p_coeff_5h,log2_130_p_coeff_5m,xh,xm,p_t_13_0h,p_t_13_0m);
Mul22(&p_t_15_0h,&p_t_15_0m,p_t_14_0h,p_t_14_0m,xh,xm);
Add23(&p_t_16_0h,&p_t_16_0m,&p_t_16_0l,log2_130_p_coeff_4h,log2_130_p_coeff_4m,p_t_15_0h,p_t_15_0m);
Mul233(&p_t_17_0h,&p_t_17_0m,&p_t_17_0l,xh,xm,p_t_16_0h,p_t_16_0m,p_t_16_0l);
Add233(&p_t_18_0h,&p_t_18_0m,&p_t_18_0l,log2_130_p_coeff_3h,log2_130_p_coeff_3m,p_t_17_0h,p_t_17_0m,p_t_17_0l);
Mul233(&p_t_19_0h,&p_t_19_0m,&p_t_19_0l,xh,xm,p_t_18_0h,p_t_18_0m,p_t_18_0l);
Add33(&p_t_20_0h,&p_t_20_0m,&p_t_20_0l,log2_130_p_coeff_2h,log2_130_p_coeff_2m,log2_130_p_coeff_2l,p_t_19_0h,p_t_19_0m,p_t_19_0l);
Mul233(&p_t_21_0h,&p_t_21_0m,&p_t_21_0l,xh,xm,p_t_20_0h,p_t_20_0m,p_t_20_0l);
Renormalize3(&p_t_21_1h,&p_t_21_1m,&p_t_21_1l,p_t_21_0h,p_t_21_0m,p_t_21_0l);
Add33(&p_t_22_0h,&p_t_22_0m,&p_t_22_0l,log2_130_p_coeff_1h,log2_130_p_coeff_1m,log2_130_p_coeff_1l,p_t_21_1h,p_t_21_1m,p_t_21_1l);
Mul233(&p_t_23_0h,&p_t_23_0m,&p_t_23_0l,xh,xm,p_t_22_0h,p_t_22_0m,p_t_22_0l);
p_resh = p_t_23_0h;
p_resm = p_t_23_0m;
p_resl = p_t_23_0l;
logih = argredtable[index].logih;
logim = argredtable[index].logim;
logil = argredtable[index].logil;
Add33(&log2yh,&log2ym,&log2yl,logih,logim,logil,p_resh,p_resm,p_resl);
Add133(&log2xh,&log2xm,&log2xl,ed,log2yh,log2ym,log2yl);
Renormalize3(resh,resm,resl,log2xh,log2xm,log2xl);
}
/* exp2_120
Approximates
2^H * (resh + resm + resl) = 2^(xh + xm + xl) * (1 + eps)
where ||eps|| <= 2^(-119.5)
*/
static inline void exp2_120(int *H, double *resh, double *resm, double *resl,
double xh, double xm, double xl) {
double xhMult2L, rhMult2L, r;
int k, index1, index2;
db_number shiftedxhMult2Ldb;
double rh, rm, rl;
double t1h, t1m, t1l, t2, t3;
double p_t_1_0h;
double p_t_2_0h;
double p_t_3_0h;
double p_t_4_0h;
double p_t_5_0h, p_t_5_0m;
double p_t_6_0h, p_t_6_0m;
double p_t_7_0h, p_t_7_0m;
double p_t_8_0h, p_t_8_0m;
double p_t_9_0h, p_t_9_0m, p_t_9_0l;
double p_t_10_0h, p_t_10_0m, p_t_10_0l;
double p_t_11_0h, p_t_11_0m, p_t_11_0l;
double p_resh, p_resm, p_resl;
double tbl1h, tbl1m, tbl1l, tbl2h, tbl2m, tbl2l;
double tablesh, tablesm, tablesl;
double exp2h, exp2m, exp2l;
/* Argument reduction
Produce exactly
2^H * 2^(i1/2^8) * 2^(i2/2^13) * 2^(rh + rm + rl)
*/
xhMult2L = xh * two13;
shiftedxhMult2Ldb.d = shiftConst + xhMult2L;
rhMult2L = xhMult2L - (shiftedxhMult2Ldb.d - shiftConst);
r = rhMult2L * twoM13;
k = shiftedxhMult2Ldb.i[LO];
*H = k >> 13;
index1 = k & INDEXMASK1;
index2 = (k & INDEXMASK2) >> 5;
Add12Cond(t1h, t2, r, xm);
Add12Cond(t1m, t1l, t2, xl);
Add12(rh, t3, t1h, t1m);
Add12(rm, rl, t3, t1l);
/* Polynomial approximation of 2^(rh + rm + rl) */
p_t_1_0h = exp2_120_p_coeff_6h;
p_t_2_0h = p_t_1_0h * rh;
p_t_3_0h = exp2_120_p_coeff_5h + p_t_2_0h;
p_t_4_0h = p_t_3_0h * rh;
Add12(p_t_5_0h,p_t_5_0m,exp2_120_p_coeff_4h,p_t_4_0h);
MulAdd22(&p_t_6_0h,&p_t_6_0m,exp2_120_p_coeff_3h,exp2_120_p_coeff_3m,rh,rm,p_t_5_0h,p_t_5_0m);
MulAdd22(&p_t_7_0h,&p_t_7_0m,exp2_120_p_coeff_2h,exp2_120_p_coeff_2m,rh,rm,p_t_6_0h,p_t_6_0m);
Mul22(&p_t_8_0h,&p_t_8_0m,p_t_7_0h,p_t_7_0m,rh,rm);
Add23(&p_t_9_0h,&p_t_9_0m,&p_t_9_0l,exp2_120_p_coeff_1h,exp2_120_p_coeff_1m,p_t_8_0h,p_t_8_0m);
Mul33(&p_t_10_0h,&p_t_10_0m,&p_t_10_0l,rh,rm,rl,p_t_9_0h,p_t_9_0m,p_t_9_0l);
Add133(&p_t_11_0h,&p_t_11_0m,&p_t_11_0l,exp2_120_p_coeff_0h,p_t_10_0h,p_t_10_0m,p_t_10_0l);
Renormalize3(&p_resh,&p_resm,&p_resl,p_t_11_0h,p_t_11_0m,p_t_11_0l);
/* Table access */
tbl1h = twoPowerIndex1[index1].hi;
tbl1m = twoPowerIndex1[index1].mi;
tbl1l = twoPowerIndex1[index1].lo;
tbl2h = twoPowerIndex2[index2].hi;
tbl2m = twoPowerIndex2[index2].mi;
tbl2l = twoPowerIndex2[index2].lo;
/* Reconstruction */
Mul33(&tablesh,&tablesm,&tablesl,tbl1h,tbl1m,tbl1l,tbl2h,tbl2m,tbl2l);
Mul33(&exp2h,&exp2m,&exp2l,tablesh,tablesm,tablesl,p_resh,p_resm,p_resl);
Renormalize3(resh,resm,resl,exp2h,exp2m,exp2l);
}
/* pow_120
Approximates
2^H * (resh + resm + resl) = 2^(y * (ed + log2(1 + (zh + zm)) + log2(r[index]))) * (1 + eps)
where ||eps|| <= 2^(-118.5) if -1075 <= y * (ed + log2(1 + (zh + zm)) + log2(r[index])) <= 1024
Approximates further (ed + log2(1 + (zh + zm)) + log2(r[index))) by log2xh
where
log2xh = (ed + log2(1 + (zh + zm)) + log2(r[index))) * (1 + eps2)
where |eps2| <= 2^(-52)
and log2xh is exact if 2^ed * ((1 + (zh + zm)) * r[index]) is an integer power of 2.
*/
void pow_120(int *H, double *resh, double *resm, double *resl, double *log2xh,
double y, int index, double ed, double zh, double zm) {
double ylog2xh, ylog2xm, ylog2xl;
double log2xm, log2xl;
/* Compute log2(x) */
log2_130(log2xh,&log2xm,&log2xl,index,ed,zh,zm);
/* Compute y * log2(x) */
Mul133(&ylog2xh,&ylog2xm,&ylog2xl,y,*log2xh,log2xm,log2xl);
/* Compute 2^(y * log2(x)) */
exp2_120(H,resh,resm,resl,ylog2xh,ylog2xm,ylog2xl);
}
/* pow_round_and_check_rn
Checks whether
2^H * (powh + powm + powl)
which is an approximate to x^y with a relative error or less than 2^(-118.5),
can be rounded correctly to double precision in round-to-nearest-ties-to-even
mode or whether the Table Maker's Dilemma occurs or the case is exact.
Returns 1 if rounding is possible and affects pow with the rounding
Returns 0 if rounding is not possible or if the case is exact
If the returned value is 0, it affects
G, kh, kl
such that
2^G * (kh + kl)
is an approximate to x^y with an relative error of 2^(-117)
and such that rounding
2^G * kh
to double precision is an exact operation.
*/
static inline int pow_round_and_check_rn(double *pow,
int H, double powh, double powm, double powl,
int *G, double *kh, double *kl) {
double th, tm, tl;
int K, K1, K2;
db_number twodb, two2db;
double twoH1074powh, twoH1074powm, shiftedpowh, delta;
double scaledth;
double t1m, t1l;
/* We start by bringing H and powh + powm + powl
to a form such that
1 <= powh + powm + powl < 2
*/
if ((powh < 1.0) ||
((powh == 1.0) && (powm < 0.0))) {
powh *= 2.0;
powm *= 2.0;
powl *= 2.0;
H--;
}
if ((powh > 2.0) ||
((powh == 2.0) && (powm >= 0.0))) {
powh *= 0.5;
powm *= 0.5;
powl *= 0.5;
H++;
}
/* Check now whether we have normal or subnormal rounding
The rounding is subnormal iff H <= -1023
In both cases, we bring H, powh + powm + powl to a form
2^K * (th + tm + tl) = 2^H * (powh + powm + powm) * (1 + eps)
where
(i) |eps| <= 2^(-118 - 53) = 2^(-171)
(ii) 2^(-K) * ulp(2^K * th) = 1
(iii) the rounding of 2^K * th to double precision is exact
(or produces +inf)
*/
if (H <= -1023) {
/* Subnormal rounding
In this case, we can neglect powl
because the rounding bit of the RN rounding
is in powh
*/
twodb.i[HI] = (H + (1074 + 1023)) << 20;
twodb.i[LO] = 0;
twoH1074powh = twodb.d * powh;
twoH1074powm = twodb.d * powm;
shiftedpowh = two52 + twoH1074powh;
th = shiftedpowh - two52;
delta = twoH1074powh - th;
Add12Cond(tm,tl,delta,twoH1074powm);
K = -1074;
} else {
/* Normal rounding
In this case, we have exactly:
2^K * (th + tm + tl) = 2^H * (powh + powm + powm)
*/
th = powh * two52;
tm = powm * two52;
tl = powl * two52;
K = H - 52;
}
/* Return results for exactness case test */
*G = K;
*kh = th;
*kl = tm;
/* Compute now
delta = ABS(0.5 - ABS(tm + tl)) * (1 + eps)
where |eps| <= 2^(-53)
The addition 0.5 + (-ABS(tm)) is exact by Sterbenz' lemma
*/
if (tm > 0.0) {
t1m = - tm;
t1l = - tl;
} else {
t1m = tm;
t1l = tl;
}
delta = ABS((0.5 + t1m) - t1l);
/* We cannot decide the rounding or have an exact case
iff
delta <= 2^(-118) * th
We can see this in the following drawing:
result = round(
+-----------------+------+----------...----+------------
2^K * | th | +/-1 | 0 | delta )
+-----------------+------+----------...----+------------
| <------------- 118 bits -------------> |
*/
scaledth = th * PRECISEROUNDCST;
if (delta > scaledth) {
/* We can round exactly to nearest
We must correct th to the rounding of
th + tm + tl iff tm is greater in
absolute value than 0.5
or if tm is equal to 0.5 in absolute value and
the sign of tm and tl
is equal:
| |
...------|--------|--------|------...
|------->--> |
^ tm tl ^
th round(th + tm + tl)
If tm is negative, we must correct th by decreasing it
otherwise we must correct th by increasing it.
*/
if (ABS(tm) >= 0.5) {
if (ABS(tm) == 0.5) {
if (tm < 0.0) {
if (tl < 0.0) {
/* The same sign of tm and tl, tm is negative */
th -= 1.0;
}
} else {
if (tl > 0.0) {
/* The same sign of tm and tl, tm is positive */
th += 1.0;
}
}
} else {
/* tm > 0.5 */
if (tm < 0.0) {
th -= 1.0;
} else {
th += 1.0;
}
}
}
/* Perform now the multiplication 2^K * th
Note that we must be able to produce
(i) 0 if we have total underflow
(ii) a subnormal result
(iii) a normal result
(iv) +inf if we have overflow in a TMD case
We produce K1 + K2 = K with K1 = floor(K/2)
and multiply in two steps.
*/
K1 = K >> 1;
K2 = K - K1;
twodb.i[HI] = (K1 + 1023) << 20;
twodb.i[LO] = 0;
two2db.i[HI] = (K2 + 1023) << 20;
two2db.i[LO] = 0;
*pow = two2db.d * (twodb.d * th);
return 1;
}
/* Otherwise we return 0 because we cannot round */
return 0;
}
/* pow_exact_case
Checks whether x^y is an exact or half-ulp case for rounding
into double precision.
This means the procedure checks whether x^y can be written on
not more than 54 bits.
The procedure uses 2^G * (kh + kl) supposing the following properties:
* kh + kl holds on at most 54 bits
* 2^G * kh is representable in double precision (even in the subnormal range)
* 2^G * (kh + kl) approximates x^y with an error eps less than 2^(-117), i.e.
2^G * (kh + kl) = x^y * (1 + eps) where |eps| <= 2^(-117)
* log2xh approximates log2(x) with an accuracy equivalent to at least 52 bits
In particular log2xh is exact if x is an integer power of 2
Returns 1 if the case is exact or half-ulp
Returns 0 otherwise
If returning 1, affects pow with 2^G * (kh + kl) rounded to double precision
*/
int pow_exact_case(double *pow,
double x, double y,
int G, double kh, double kl, double log2xh) {
db_number xdb, ydb, tempdb, shiftedEydb, temp2db;
int E, F, G1, G2;
double m, n, yh, yl, Eyh, Eyl, ed, Ey;
double delta;
double nearestEy;
double value;
/* For testing whether x^y is an exact or half-ulp case,
we have two main cases:
(i) x is an integer power of 2
(ii) x is not an integer power of 2
We start by testing if x is an integer power of 2.
*/
xdb.d = x;
if ((xdb.i[HI] & 0xfff00000) == 0) {
/* x is subnormal, scale by 2^52 */
xdb.d *= 0.4503599627370496e16;
}
if (((xdb.i[HI] & 0x000fffff) | xdb.i[LO]) == 0) {
/* x is an integer power of 2
x^y is exact or midpoint iff log2(x) * y is integer
Since we know that x is an integer power of 2,
log2xh is equal to this integer. Since the exponent
of the double precision number is bounded by the
exponent range, we know that log2xh is integer and
bounded by 2^11. It is therefore possible to
split y into two parts of 21 and 32 bits, to perform
the multiplication componentwise. Since the result
is bounded by 2^11, it suffices to compute the
nearest integer to the higher word product, and to
compare the rounding difference to the low word product.
This splitting is faster than the usual Dekker splitting.
*/
ydb.d = y;
ydb.i[LO] = 0;
yh = ydb.d;
yl = y - yh;
Eyh = log2xh * yh;
Eyl = log2xh * yl;
delta = ((0.6755399441055744e16 + Eyh) - 0.6755399441055744e16) - Eyh; /* addition rounds, subtractions exact */
if (delta != Eyl) return 0;
} else {
/* x is not an integer power of 2
We have clearly an inexact case if y is negative
or if y is greater than 35
*/
if ((y < 0.0) || (y > 35.0)) return 0;
/* Decompose now y into
y = 2^F * n
Checking F and n, we can then already decide
some cases using the fact that the worst-case
accuracy for x^n, n in [|0;35|], is less (in bits)
than the accuracy of the approximation of x^y we have
already in 2^G * (kh + kl).
*/
decompose(&n,&F,y);
if ((n > 35.0) || (F < -5)) return 0;
if (F < 0) {
/* Here, -5 <= F <= -1, 3 <= n <= 35, n an odd integer
We decompose x into 2^E * m where m is an odd integer
Let H, sh and sl such that 2^H * (sh + sl) = 2^G * (kh + kl) and
sh + sl is an odd integer.
If we have E * 2^F * n = H, we can apply the worst case argument
because we know the worst case for
m^(2^F * n) with m odd integer, -5 <= F <= -1, 3 <= n <= 35
when rounding to 53 bits in both rounding modes.
E is bounded in magnitude by 2^11. 2^F * n is equal to y by
construction. Since n <= 35, y contains at most 6 significant bits.
The arithmetical multiplication E * y is therefore exact and less
than or equal to 2^17.
We check first whether E * y is an integer. If this is the case,
we compute sh + sl = 2^(G - E * y) * (kh + kl). Finally,
we check whether sh + sl is an odd integer.
*/
decompose(&m,&E,x);
ed = (double) E;
Ey = ed * y; /* Exact */
/* Check whether Ey is an integer using the simple shift technique
The addition rounds, the substraction is exact by Sterbenz' lemma.
If Ey is an integer, the low order word of shiftedEydb is equal to this
integer.
*/
shiftedEydb.d = 0.6755399441055744e16 + Ey;
nearestEy = shiftedEydb.d - 0.6755399441055744e16;
if (nearestEy != Ey) return 0;
/* Here E * y is integer.
Produce now 2^(G - E * y).
*/
tempdb.i[HI] = (((G - shiftedEydb.i[LO]) + 1023) << 20);
tempdb.i[LO] = 0;
/* Check now if sh + sl = tempdb.d * (kh + kl) is an odd
integer.
Since kh and kl are not overlapped, we have two cases:
(i) kl is equal to 0, in which case tempdb.d * kh must be an odd integer
(ii) kl is not equal to 0, in which case tempdb.d * kl must be an odd integer
*/
if (kl == 0.0) value = kh; else value = kl;
value *= tempdb.d; /* Exact because multiplication by positive integer power of 2 */
if (!isOddInteger(value)) return 0;
/* Here the case is exact by the worst-case argument */
}
/* Here, we have either F >= 0 or an exact case
If F >= 0, we also have an exact case because
2^F * n = y <= 35, y therefore integer and because
we can apply the worst case argument.
*/
}
/*
Here, the case is exact, affect pow with 2^G * (kh + kl) rounded to
nearest in double precision
Since kh + kl holds on at most 54 bits, 2^G * kh produces
never any rounding error and kh and kl are not overlapping,
2^G * kh is equal to the rounding if
(i) kl is equal to 0
(ii) kl is not equal to 0 and the mantissa of 2^G * kh is even
If in condition (ii), kl is not equal to 0 and the mantissa of
2^G * kh is not even, we correct it depending on the sign of kl.
Remark that G can be such that 2^G is no longer a normal.
Produce therefore 2^(floor(G/2)) and 2^(G - floor(G/2)) and
multiply in two steps.
*/
G1 = G >> 1;
G2 = G - G1;
tempdb.i[HI] = (G1 + 1023) << 20;
tempdb.i[LO] = 0;
temp2db.i[HI] = (G2 + 1023) << 20;
temp2db.i[LO] = 0;
tempdb.d *= (kh * temp2db.d);
if ((kl != 0.0) && ((tempdb.i[LO] & 1) != 0)) {
/* We must correct the rounding to the rounding to nearest ties to even */
if (kl > 0.0) {
tempdb.l++;
} else {
tempdb.l++;
}
}
*pow = tempdb.d;
return 1;
}
double pow_exact_rn(double x, double y, double sign,
int index, double ed, double zh, double zm) {
int H, G;
double powh, powm, powl;
double pow;
double kh, kl;
double log2xh;
pow_120(&H, &powh, &powm, &powl, &log2xh, y, index, ed, zh, zm);
if (pow_round_and_check_rn(&pow,H,powh,powm,powl,&G,&kh,&kl))
return sign * pow;
if (pow_exact_case(&pow,x,y,G,kh,kl,log2xh))
return sign * pow;
// printf("Could not decide the rounding to nearest of power.\n");
return -5.0;
}
double pow_rn(double x, double y) {
db_number xdb, ydb, yhdb, shiftedylog2xhMult2Ldb, powdb, twodb;
double sign;
int E, index;
double log2FastApprox, ed, ylog2xFast, f;
double yh, yl, ri, logih, logim, yrih, yril, th, zh, zm;
double p_t_1_0h;
double p_t_2_0h;
double p_t_3_0h;
double p_t_4_0h;
double p_t_5_0h;
double p_t_9_0h;
double p_t_10_0h;
double p_t_11_0h, p_t_11_0m;
double p_t_12_0h, p_t_12_0m;
double log2zh, log2zm;
double log2yh, log2ym;
double log2xh, log2xm;
double ylog2xh, ylog2xm;
double rh, r;
int k, index1, index2, H;
double tbl1, tbl2h, tbl2m;
double ph;
double powh, powm;
double twoH1074powh, twoH1074powm;
double shiftedpowh, nearestintpowh, delta, deltaint;
double rest;
double lowerTerms;
double temp1;
double zhSq, zhFour, p35, p46, p36, p7;
double xSq;
/* Fast rejection of special cases */
xdb.d = x;
ydb.d = y;
if (((((xdb.i[HI] >> 20) + 1) & 0x3ff) <= 1) ||
((((ydb.i[HI] >> 20) + 1) & 0x3ff) <= 1)) {
/* Handle special cases before handling NaNs and Infinities */
if (x == 1.0) return 1.0;
if (y == 0.0) return 1.0;
if (y == 1.0) return x;
if (y == 2.0) return x * x; /* Remark: may yield uncorrect rounding on x86 for subnormal results */
if (y == -1.0) return 1 / x;
if ((x == 0.0) && ((ydb.i[HI] & 0x7ff00000) != 0x7ff00000)) {
/* x = +/-0 and y is neither NaN nor Infinity
We have four cases
(i) y < 0:
(a) y odd integer: return +/- Inf and raise divide-by-zero
(b) y not odd integer: return + Ind and raise divide-by-zero
(ii) (a) y odd integer: return +/- 0
(b) y not odd integer: return +0
Note that y = 0.0 has already been filtered out.
*/
if (y < 0.0) {
if (isOddInteger(y))
return 1/x;
else
return 1/(x * x);
} else {
if (isOddInteger(y))
return x;
else
return x * x;
}
}
/* Handle NaNs and Infinities
Note: the cases (x,y) = (1,NaN) and (x,y) = (NaN,0) have already been handled.
*/
if ((ydb.i[HI] & 0x7ff00000) == 0x7ff00000) {
/* Here y is NaN or Inf */
if (((ydb.i[HI] & 0x000fffff) | ydb.i[LO]) != 0) {
/* Here y is NaN, we return NaN */
return y;
}
/* Here y is +/- Inf
There are three main cases:
(i) x = -1: return 1
(ii) abs(x) > 1:
(a) y = +Inf: return +Inf
(b) y = -Inf: return +0
(iii) abs(x) < 1:
(a) y = +Inf: return +0
(b) y = -Inf: return +Inf
Note: the case x = 1 has already been filtered out
*/
if (x == -1.0)
return 1.0;
/* Here x != 1, x != -1 */
if ((ABS(x) > 1.0) ^ ((ydb.i[HI] & 0x80000000) == 0)) {
/* abs(x) > 1 and y = -Inf or abs(x) < 1 and y = +Inf */
return 0.0;
} else {
/* abs(x) > 1 and y = +Inf or abs(x) < 1 and y = -Inf */
return ABS(y);
}
}
/* Here y is neither Inf nor NaN */
if ((xdb.i[HI] & 0x7ff00000) == 0x7ff00000) {
/* Here x is NaN or Inf */
if (((xdb.i[HI] & 0x000fffff) | xdb.i[LO]) != 0) {
/* Here x is NaN, we return NaN */
return x;
}
/* Here x is +/- Inf
There are two main cases:
(i) x is +Inf
(ii) x is -Inf
*/
if ((xdb.i[HI] & 0x80000000) == 0) {
/* x is +Inf
(a) y > 0: return +Inf
(b) y < 0: return +0
Note: y = 0 has already been filtered out
*/
if (y > 0.0) {
return x;
} else {
return 0.0;
}
} else {
/* x is -Inf
There are four cases:
(a) y > 0:
(*) y is an odd integer: return -Inf
(**) y is not an odd integer: return +Inf
(b) y < 0:
(*) y is an odd integer: return -0
(**) y is not an odd integer: return +0
Note: y = 0 has already been filtered out
*/
if (y > 0.0) {
if (isOddInteger(y)) {
return x;
} else {
return -x;
}
} else {
if (isOddInteger(y)) {
return -0.0;
} else {
return 0.0;
}
}
}
}
}
/* Here both x and y are finite numbers */
/* Test now whether we have the case
(-x)^y = (-1)^y * x^y
where x is positive
*/
sign = 1.0;
if (x < 0.0) {
/* x is negative
x^y is defined only if y is integer
*/
if (!isInteger(y)) {
/* y is not integer
return NaN and raise invalid exception
*/
return 0.0/0.0;
}
/* Here y is integer
Remove the sign of x and put (-1)^y in sign
*/
x = -x; xdb.i[HI] &= 0x7fffffff;
if (isOddInteger(y)) {
sign = -sign;
}
}
/* Here x is strictly positive and finite */
/* Test now if abs(y) is in a range giving finite, non-trivial results x^y
We have
x^y = 2^(y * log2(x)) = 2^(y * log2(2^E * m)) = 2^(y * (E + log2(1 + f)))
We have overflow iff x^y >= 2^(1024), i.e. y * (E + log2(1 + f)) >= 1024
We have underflow (flush to zero) iff x^y <= 2^(-1076), i.e. y * (E + log2(1 + f)) <= - 1076
We round to 1.0 iff abs(y * (E + log2(1 + f))) <= 2^(-54)
We approximate log2(1 + f) as
log2(1 + f) = c * f * alpha(f)
where c is a constant and 0.853 < alpha(f) < 1.21
So we have surely
(i) overflow or underflow if abs(y * (E + c * f)) >= ceil(1076/0.853) = 1261
(ii) trivial rounding to 1.0 if abs(y * (E + c * f)) <= 2^(-55) <= 2^(-54)/1.21
*/
/* We start the computation of the logarithm of x */
E = 0;
if ((xdb.i[HI] & 0xfff00000) == 0) {
/* x is subnormal, make it normal */
xdb.d *= two52;
E = -52;
}
E += (xdb.i[HI]>>20)-1023; /* extract the exponent */
index = (xdb.i[HI] & 0x000fffff);