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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
|
/* mpfr_sub1sp -- internal function to perform a "real" subtraction
All the op must have the same precision
Copyright 2003-2022 Free Software Foundation, Inc.
Contributed by the AriC and Caramba projects, INRIA.
This file is part of the GNU MPFR Library.
The GNU MPFR Library 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 3 of the License, or (at your
option) any later version.
The GNU MPFR Library 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 Lesser General Public
License for more details.
You should have received a copy of the GNU Lesser General Public License
along with the GNU MPFR Library; see the file COPYING.LESSER. If not, see
https://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
#define MPFR_NEED_LONGLONG_H
#include "mpfr-impl.h"
/* define MPFR_FULLSUB to use alternate code in mpfr_sub1sp2 and mpfr_sub1sp2n
(see comments in mpfr_sub1sp2) */
/* #define MPFR_FULLSUB */
#if MPFR_WANT_ASSERT >= 2
/* Check the result of mpfr_sub1sp with mpfr_sub1.
Note: mpfr_sub1sp itself has two algorithms: one always valid and one
faster for small precisions (up to 3 limbs). The latter one is disabled
if MPFR_GENERIC_ABI is defined. When MPFR_WANT_ASSERT >= 2, it could be
interesting to compare the results of these different algorithms. For
the time being, this is currently done by running the same code on the
same data with and without MPFR_GENERIC_ABI defined, where we have the
following comparisons in small precisions:
mpfr_sub1sp slow <-> mpfr_sub1 when MPFR_GENERIC_ABI is defined;
mpfr_sub1sp fast <-> mpfr_sub1 when MPFR_GENERIC_ABI is not defined.
By transitivity, the absence of failures implies that the 3 results are
the same.
*/
int mpfr_sub1sp_ref (mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mpfr_rnd_t);
int mpfr_sub1sp (mpfr_ptr a, mpfr_srcptr b, mpfr_srcptr c, mpfr_rnd_t rnd_mode)
{
mpfr_t tmpa, tmpb, tmpc;
mpfr_flags_t old_flags, flags, flags2;
int inexb, inexc, inexact, inexact2;
if (rnd_mode == MPFR_RNDF)
return mpfr_sub1sp_ref (a, b, c, rnd_mode);
old_flags = __gmpfr_flags;
mpfr_init2 (tmpa, MPFR_PREC (a));
mpfr_init2 (tmpb, MPFR_PREC (b));
mpfr_init2 (tmpc, MPFR_PREC (c));
inexb = mpfr_set (tmpb, b, MPFR_RNDN);
MPFR_ASSERTN (inexb == 0);
inexc = mpfr_set (tmpc, c, MPFR_RNDN);
MPFR_ASSERTN (inexc == 0);
MPFR_ASSERTN (__gmpfr_flags == old_flags);
inexact2 = mpfr_sub1 (tmpa, tmpb, tmpc, rnd_mode);
flags2 = __gmpfr_flags;
__gmpfr_flags = old_flags;
inexact = mpfr_sub1sp_ref (a, b, c, rnd_mode);
flags = __gmpfr_flags;
/* Convert the ternary values to (-1,0,1). */
inexact2 = VSIGN (inexact2);
inexact = VSIGN (inexact);
if (! mpfr_equal_p (tmpa, a) || inexact != inexact2 || flags != flags2)
{
fprintf (stderr, "sub1 & sub1sp return different values for %s\n"
"Prec_a = %lu, Prec_b = %lu, Prec_c = %lu\nB = ",
mpfr_print_rnd_mode (rnd_mode),
(unsigned long) MPFR_PREC (a),
(unsigned long) MPFR_PREC (b),
(unsigned long) MPFR_PREC (c));
mpfr_fdump (stderr, tmpb);
fprintf (stderr, "C = ");
mpfr_fdump (stderr, tmpc);
fprintf (stderr, "sub1 : ");
mpfr_fdump (stderr, tmpa);
fprintf (stderr, "sub1sp: ");
mpfr_fdump (stderr, a);
fprintf (stderr, "sub1 : ternary = %2d, flags =", inexact2);
flags_fout (stderr, flags2);
fprintf (stderr, "sub1sp: ternary = %2d, flags =", inexact);
flags_fout (stderr, flags);
MPFR_ASSERTN (0);
}
mpfr_clears (tmpa, tmpb, tmpc, (mpfr_ptr) 0);
return inexact;
}
# define mpfr_sub1sp mpfr_sub1sp_ref
#endif /* MPFR_WANT_ASSERT >= 2 */
#if !defined(MPFR_GENERIC_ABI)
/* the sub1sp1_extracted.c is not ready yet */
#if 0 && defined(MPFR_WANT_PROVEN_CODE) && GMP_NUMB_BITS == 64 && \
UINT_MAX == 0xffffffff && MPFR_PREC_BITS == 64 && \
_MPFR_PREC_FORMAT == 3 && _MPFR_EXP_FORMAT == _MPFR_PREC_FORMAT
/* The code assumes that mp_limb_t has 64 bits exactly, unsigned int
has 32 bits exactly, mpfr_prec_t and mpfr_exp_t are of type long,
which has 64 bits exactly. */
#include "sub1sp1_extracted.c"
#else
/* special code for p < GMP_NUMB_BITS */
static int
mpfr_sub1sp1 (mpfr_ptr a, mpfr_srcptr b, mpfr_srcptr c, mpfr_rnd_t rnd_mode,
mpfr_prec_t p)
{
mpfr_exp_t bx = MPFR_GET_EXP (b);
mpfr_exp_t cx = MPFR_GET_EXP (c);
mp_limb_t *ap = MPFR_MANT(a);
mp_limb_t *bp = MPFR_MANT(b);
mp_limb_t *cp = MPFR_MANT(c);
mpfr_prec_t cnt, INITIALIZED(sh);
mp_limb_t rb; /* round bit */
mp_limb_t sb; /* sticky bit */
mp_limb_t a0;
mp_limb_t mask;
mpfr_uexp_t d;
MPFR_ASSERTD(p < GMP_NUMB_BITS);
if (bx == cx)
{
if (MPFR_UNLIKELY(bp[0] == cp[0])) /* result is zero */
{
if (rnd_mode == MPFR_RNDD)
MPFR_SET_NEG(a);
else
MPFR_SET_POS(a);
MPFR_SET_ZERO(a);
MPFR_RET (0);
}
else if (cp[0] > bp[0]) /* borrow: |c| > |b| */
{
a0 = cp[0] - bp[0];
MPFR_SET_OPPOSITE_SIGN (a, b);
}
else /* bp[0] > cp[0] */
{
a0 = bp[0] - cp[0];
MPFR_SET_SAME_SIGN (a, b);
}
/* now a0 != 0 */
MPFR_ASSERTD(a0 != 0);
count_leading_zeros (cnt, a0);
ap[0] = a0 << cnt;
bx -= cnt;
rb = sb = 0;
/* Note: sh is not initialized, but will not be used in this case. */
}
else
{
if (bx < cx) /* swap b and c */
{
mpfr_exp_t tx;
mp_limb_t *tp;
tx = bx; bx = cx; cx = tx;
tp = bp; bp = cp; cp = tp;
MPFR_SET_OPPOSITE_SIGN (a, b);
}
else
{
MPFR_SET_SAME_SIGN (a, b);
}
MPFR_ASSERTD (bx > cx);
d = (mpfr_uexp_t) bx - cx;
sh = GMP_NUMB_BITS - p;
mask = MPFR_LIMB_MASK(sh);
if (d < GMP_NUMB_BITS)
{
sb = - (cp[0] << (GMP_NUMB_BITS - d)); /* neglected part of -c */
/* Note that "a0 = bp[0] - (cp[0] >> d) - (sb != 0);" is faster
on some other machines and has no immediate dependencies for
the first subtraction. In the future, make sure that the code
is recognized as a *single* subtraction with borrow and/or use
a builtin when available (currently provided by Clang, but not
by GCC); create a new macro for that. See the TODO later.
Note also that with Clang, the constant 0 for the first
subtraction instead of a variable breaks the optimization:
https://llvm.org/bugs/show_bug.cgi?id=31754 */
a0 = bp[0] - (sb != 0) - (cp[0] >> d);
/* a0 cannot be zero here since:
a) if d >= 2, then a0 >= 2^(w-1) - (2^(w-2)-1) with
w = GMP_NUMB_BITS, thus a0 - 1 >= 2^(w-2),
b) if d = 1, then since p < GMP_NUMB_BITS we have sb=0.
*/
MPFR_ASSERTD(a0 > 0);
count_leading_zeros (cnt, a0);
if (cnt)
a0 = (a0 << cnt) | (sb >> (GMP_NUMB_BITS - cnt));
sb <<= cnt;
bx -= cnt;
/* sh > 0 since p < GMP_NUMB_BITS */
MPFR_ASSERTD(sh > 0);
rb = a0 & (MPFR_LIMB_ONE << (sh - 1));
sb |= (a0 & mask) ^ rb;
ap[0] = a0 & ~mask;
}
else /* d >= GMP_NUMB_BITS */
{
if (bp[0] > MPFR_LIMB_HIGHBIT)
{
/* We compute b - ulp(b), and the remainder ulp(b) - c satisfies:
1/2 ulp(b) < ulp(b) - c < ulp(b), thus rb = sb = 1. */
ap[0] = bp[0] - (MPFR_LIMB_ONE << sh);
rb = 1;
}
else
{
/* Warning: since we have an exponent decrease, when
p = GMP_NUMB_BITS - 1 and d = GMP_NUMB_BITS, the round bit
corresponds to the upper bit of -c. In that case rb = 0 and
sb = 1, except when c0 = MPFR_LIMB_HIGHBIT where rb = 1 and
sb = 0. */
rb = sh > 1 || d > GMP_NUMB_BITS || cp[0] == MPFR_LIMB_HIGHBIT;
/* sb=1 below is incorrect when p = GMP_NUMB_BITS - 1,
d = GMP_NUMB_BITS and c0 = MPFR_LIMB_HIGHBIT, but in
that case the even rule wound round up too. */
ap[0] = ~mask;
bx --;
/* Warning: if d = GMP_NUMB_BITS and c0 = 1000...000, then
b0 - c0 = |0111...111|1000...000|, which after the shift
becomes |111...111|000...000| thus if p = GMP_NUMB_BITS-1
we have rb = 1 but sb = 0. However, in this case the round
even rule will round up, which is what we get with sb = 1:
the final result will be correct, while sb is incorrect. */
}
sb = 1;
}
}
/* now perform rounding */
/* Warning: MPFR considers underflow *after* rounding with an unbounded
exponent range. However, since b and c have same precision p, they are
multiples of 2^(emin-p), likewise for b-c. Thus if bx < emin, the
subtraction (with an unbounded exponent range) is exact, so that bx is
also the exponent after rounding with an unbounded exponent range. */
if (MPFR_UNLIKELY(bx < __gmpfr_emin))
{
/* For RNDN, mpfr_underflow always rounds away, thus for |a| <= 2^(emin-2)
we have to change to RNDZ. This corresponds to:
(a) either bx < emin - 1
(b) or bx = emin - 1 and ap[0] = 1000....000 (in this case necessarily
rb = sb = 0 since b-c is multiple of 2^(emin-p) */
if (rnd_mode == MPFR_RNDN &&
(bx < __gmpfr_emin - 1 || ap[0] == MPFR_LIMB_HIGHBIT))
{
MPFR_ASSERTD(rb == 0 && sb == 0);
rnd_mode = MPFR_RNDZ;
}
return mpfr_underflow (a, rnd_mode, MPFR_SIGN(a));
}
MPFR_SET_EXP (a, bx);
if ((rb == 0 && sb == 0) || rnd_mode == MPFR_RNDF)
MPFR_RET (0);
else if (rnd_mode == MPFR_RNDN)
{
if (rb == 0 || (sb == 0 && (ap[0] & (MPFR_LIMB_ONE << sh)) == 0))
goto truncate;
else
goto add_one_ulp;
}
else if (MPFR_IS_LIKE_RNDZ(rnd_mode, MPFR_IS_NEG(a)))
{
truncate:
MPFR_RET(-MPFR_SIGN(a));
}
else /* round away from zero */
{
add_one_ulp:
ap[0] += MPFR_LIMB_ONE << sh;
if (MPFR_UNLIKELY(ap[0] == 0))
{
ap[0] = MPFR_LIMB_HIGHBIT;
/* Note: bx+1 cannot exceed __gmpfr_emax, since |a| <= |b|, thus
bx+1 is at most equal to the original exponent of b. */
MPFR_ASSERTD(bx + 1 <= __gmpfr_emax);
MPFR_SET_EXP (a, bx + 1);
}
MPFR_RET(MPFR_SIGN(a));
}
}
#endif /* MPFR_WANT_PROVEN_CODE */
/* special code for p = GMP_NUMB_BITS */
static int
mpfr_sub1sp1n (mpfr_ptr a, mpfr_srcptr b, mpfr_srcptr c, mpfr_rnd_t rnd_mode)
{
mpfr_exp_t bx = MPFR_GET_EXP (b);
mpfr_exp_t cx = MPFR_GET_EXP (c);
mp_limb_t *ap = MPFR_MANT(a);
mp_limb_t *bp = MPFR_MANT(b);
mp_limb_t *cp = MPFR_MANT(c);
mpfr_prec_t cnt;
mp_limb_t rb; /* round bit */
mp_limb_t sb; /* sticky bit */
mp_limb_t a0;
mpfr_uexp_t d;
MPFR_ASSERTD(MPFR_PREC(a) == GMP_NUMB_BITS);
MPFR_ASSERTD(MPFR_PREC(b) == GMP_NUMB_BITS);
MPFR_ASSERTD(MPFR_PREC(c) == GMP_NUMB_BITS);
if (bx == cx)
{
a0 = bp[0] - cp[0];
if (a0 == 0) /* result is zero */
{
if (rnd_mode == MPFR_RNDD)
MPFR_SET_NEG(a);
else
MPFR_SET_POS(a);
MPFR_SET_ZERO(a);
MPFR_RET (0);
}
else if (a0 > bp[0]) /* borrow: |c| > |b| */
{
MPFR_SET_OPPOSITE_SIGN (a, b);
a0 = -a0;
}
else /* bp[0] > cp[0] */
MPFR_SET_SAME_SIGN (a, b);
/* now a0 != 0 */
MPFR_ASSERTD(a0 != 0);
count_leading_zeros (cnt, a0);
ap[0] = a0 << cnt;
bx -= cnt;
rb = sb = 0;
}
else
{
if (bx < cx) /* swap b and c */
{
mpfr_exp_t tx;
mp_limb_t *tp;
tx = bx; bx = cx; cx = tx;
tp = bp; bp = cp; cp = tp;
MPFR_SET_OPPOSITE_SIGN (a, b);
}
else
{
MPFR_SET_SAME_SIGN (a, b);
}
MPFR_ASSERTD (bx > cx);
d = (mpfr_uexp_t) bx - cx;
if (d < GMP_NUMB_BITS)
{
sb = - (cp[0] << (GMP_NUMB_BITS - d)); /* neglected part of -c */
a0 = bp[0] - (sb != 0) - (cp[0] >> d);
/* a0 can only be zero when d=1, b0 = B/2, and c0 = B-1, where
B = 2^GMP_NUMB_BITS, thus b0 - c0/2 = 1/2 */
if (a0 == MPFR_LIMB_ZERO)
{
bx -= GMP_NUMB_BITS;
ap[0] = MPFR_LIMB_HIGHBIT;
rb = sb = 0;
}
else
{
count_leading_zeros (cnt, a0);
if (cnt)
a0 = (a0 << cnt) | (sb >> (GMP_NUMB_BITS - cnt));
sb <<= cnt;
bx -= cnt;
rb = sb & MPFR_LIMB_HIGHBIT;
sb &= ~MPFR_LIMB_HIGHBIT;
ap[0] = a0;
}
}
else /* d >= GMP_NUMB_BITS */
{
/* We compute b - ulp(b) */
if (bp[0] > MPFR_LIMB_HIGHBIT)
{
/* If d = GMP_NUMB_BITS, rb = 0 and sb = 1,
unless c0 = MPFR_LIMB_HIGHBIT in which case rb = 1 and sb = 0.
If d > GMP_NUMB_BITS, rb = sb = 1. */
rb = d > GMP_NUMB_BITS || cp[0] == MPFR_LIMB_HIGHBIT;
sb = d > GMP_NUMB_BITS || cp[0] != MPFR_LIMB_HIGHBIT;
ap[0] = bp[0] - MPFR_LIMB_ONE;
}
else
{
/* Warning: in this case a0 is shifted by one!
If d=GMP_NUMB_BITS:
(a) if c0 = MPFR_LIMB_HIGHBIT, a0 = 111...111, rb = sb = 0
(b) otherwise, a0 = 111...110, rb = -c0 >= 01000...000,
sb = (-c0) << 2
If d=GMP_NUMB_BITS+1: a0 = 111...111
(c) if c0 = MPFR_LIMB_HIGHBIT, rb = 1 and sb = 0
(d) otherwise rb = 0 and sb = 1
If d > GMP_NUMB_BITS+1:
(e) a0 = 111...111, rb = sb = 1
*/
bx --;
if (d == GMP_NUMB_BITS && cp[0] > MPFR_LIMB_HIGHBIT)
{ /* case (b) */
rb = MPFR_LIMB(-cp[0]) >= (MPFR_LIMB_HIGHBIT >> 1);
sb = MPFR_LIMB(-cp[0]) << 2;
ap[0] = -(MPFR_LIMB_ONE << 1);
}
else /* cases (a), (c), (d) and (e) */
{
/* rb=1 in case (e) and case (c) */
rb = d > GMP_NUMB_BITS + 1
|| (d == GMP_NUMB_BITS + 1 && cp[0] == MPFR_LIMB_HIGHBIT);
/* sb = 1 in case (d) and (e) */
sb = d > GMP_NUMB_BITS + 1
|| (d == GMP_NUMB_BITS + 1 && cp[0] > MPFR_LIMB_HIGHBIT);
/* Warning: only set ap[0] last, otherwise in case ap=cp,
the above comparisons involving cp[0] would be wrong */
ap[0] = -MPFR_LIMB_ONE;
}
}
}
}
/* now perform rounding */
/* Warning: MPFR considers underflow *after* rounding with an unbounded
exponent range. However, since b and c have same precision p, they are
multiples of 2^(emin-p), likewise for b-c. Thus if bx < emin, the
subtraction (with an unbounded exponent range) is exact, so that bx is
also the exponent after rounding with an unbounded exponent range. */
if (MPFR_UNLIKELY(bx < __gmpfr_emin))
{
/* For RNDN, mpfr_underflow always rounds away, thus for |a| <= 2^(emin-2)
we have to change to RNDZ. This corresponds to:
(a) either bx < emin - 1
(b) or bx = emin - 1 and ap[0] = 1000....000 (in this case necessarily
rb = sb = 0 since b-c is multiple of 2^(emin-p) */
if (rnd_mode == MPFR_RNDN &&
(bx < __gmpfr_emin - 1 || ap[0] == MPFR_LIMB_HIGHBIT))
{
MPFR_ASSERTD(rb == 0 && sb == 0);
rnd_mode = MPFR_RNDZ;
}
return mpfr_underflow (a, rnd_mode, MPFR_SIGN(a));
}
MPFR_SET_EXP (a, bx);
if ((rb == 0 && sb == 0) || rnd_mode == MPFR_RNDF)
MPFR_RET (0);
else if (rnd_mode == MPFR_RNDN)
{
if (rb == 0 || (sb == 0 && (ap[0] & MPFR_LIMB_ONE) == 0))
goto truncate;
else
goto add_one_ulp;
}
else if (MPFR_IS_LIKE_RNDZ(rnd_mode, MPFR_IS_NEG(a)))
{
truncate:
MPFR_RET(-MPFR_SIGN(a));
}
else /* round away from zero */
{
add_one_ulp:
ap[0] += MPFR_LIMB_ONE;
if (MPFR_UNLIKELY(ap[0] == 0))
{
ap[0] = MPFR_LIMB_HIGHBIT;
/* Note: bx+1 cannot exceed __gmpfr_emax, since |a| <= |b|, thus
bx+1 is at most equal to the original exponent of b. */
MPFR_ASSERTD(bx + 1 <= __gmpfr_emax);
MPFR_SET_EXP (a, bx + 1);
}
MPFR_RET(MPFR_SIGN(a));
}
}
/* special code for GMP_NUMB_BITS < p < 2*GMP_NUMB_BITS */
static int
mpfr_sub1sp2 (mpfr_ptr a, mpfr_srcptr b, mpfr_srcptr c, mpfr_rnd_t rnd_mode,
mpfr_prec_t p)
{
mpfr_exp_t bx = MPFR_GET_EXP (b);
mpfr_exp_t cx = MPFR_GET_EXP (c);
mp_limb_t *ap = MPFR_MANT(a);
mp_limb_t *bp = MPFR_MANT(b);
mp_limb_t *cp = MPFR_MANT(c);
mpfr_prec_t cnt, INITIALIZED(sh);
mp_limb_t rb; /* round bit */
mp_limb_t sb; /* sticky bit */
mp_limb_t mask, a0, a1;
mpfr_uexp_t d;
MPFR_ASSERTD(GMP_NUMB_BITS < p && p < 2 * GMP_NUMB_BITS);
if (bx == cx) /* subtraction is exact in this case */
{
/* first compute a0: if the compiler is smart enough, it will use the generated
borrow to get for free the term (bp[0] < cp[0]) */
a0 = bp[0] - cp[0];
a1 = bp[1] - cp[1] - (bp[0] < cp[0]);
if (a1 == 0 && a0 == 0) /* result is zero */
{
if (rnd_mode == MPFR_RNDD)
MPFR_SET_NEG(a);
else
MPFR_SET_POS(a);
MPFR_SET_ZERO(a);
MPFR_RET (0);
}
else if (a1 >= bp[1]) /* borrow: |c| > |b| */
{
MPFR_SET_OPPOSITE_SIGN (a, b);
/* a = b-c mod 2^(2*GMP_NUMB_BITS) */
a0 = -a0;
a1 = -a1 - (a0 != 0);
}
else /* bp[0] > cp[0] */
MPFR_SET_SAME_SIGN (a, b);
if (a1 == 0)
{
a1 = a0;
a0 = 0;
bx -= GMP_NUMB_BITS;
}
/* now a1 != 0 */
MPFR_ASSERTD(a1 != 0);
count_leading_zeros (cnt, a1);
if (cnt)
{
ap[1] = (a1 << cnt) | (a0 >> (GMP_NUMB_BITS - cnt));
ap[0] = a0 << cnt;
bx -= cnt;
}
else
{
ap[1] = a1;
ap[0] = a0;
}
rb = sb = 0;
/* Note: sh is not initialized, but will not be used in this case. */
}
else
{
mp_limb_t t;
if (bx < cx) /* swap b and c */
{
mpfr_exp_t tx;
mp_limb_t *tp;
tx = bx; bx = cx; cx = tx;
tp = bp; bp = cp; cp = tp;
MPFR_SET_OPPOSITE_SIGN (a, b);
}
else
{
MPFR_SET_SAME_SIGN (a, b);
}
MPFR_ASSERTD (bx > cx);
d = (mpfr_uexp_t) bx - cx;
sh = 2 * GMP_NUMB_BITS - p;
mask = MPFR_LIMB_MASK(sh);
if (d < GMP_NUMB_BITS)
{
t = (cp[1] << (GMP_NUMB_BITS - d)) | (cp[0] >> d);
/* TODO: Change the code to generate a full subtraction with borrow,
avoiding the test on sb and the corresponding correction. Note
that Clang has builtins:
https://clang.llvm.org/docs/LanguageExtensions.html#multiprecision-arithmetic-builtins
but the generated code may not be good:
https://llvm.org/bugs/show_bug.cgi?id=20748
With the current source code, Clang generates on x86_64:
1. sub %rsi,%rbx for the first subtraction in a1;
2. sub %rdi,%rax for the subtraction in a0;
3. sbb $0x0,%rbx for the second subtraction in a1, i.e. just
subtracting the borrow out from (2).
So, Clang recognizes the borrow, but doesn't merge (1) and (3).
Bug: https://llvm.org/bugs/show_bug.cgi?id=25858
For GCC: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=79173
*/
#ifndef MPFR_FULLSUB
a0 = bp[0] - t;
a1 = bp[1] - (cp[1] >> d) - (bp[0] < t);
sb = cp[0] << (GMP_NUMB_BITS - d); /* neglected part of c */
if (sb)
{
a1 -= (a0 == 0);
a0 --;
/* a = a1,a0 cannot become zero here, since:
a) if d >= 2, then a1 >= 2^(w-1) - (2^(w-2)-1) with
w = GMP_NUMB_BITS, thus a1 - 1 >= 2^(w-2),
b) if d = 1, then since p < 2*GMP_NUMB_BITS we have sb=0. */
MPFR_ASSERTD(a1 > 0 || a0 > 0);
sb = -sb; /* 2^GMP_NUMB_BITS - sb */
}
#else
sb = - (cp[0] << (GMP_NUMB_BITS - d));
a0 = bp[0] - t - (sb != 0);
a1 = bp[1] - (cp[1] >> d) - (bp[0] < t || (bp[0] == t && sb != 0));
#endif
if (a1 == 0)
{
/* this implies d=1, which in turn implies sb=0 */
MPFR_ASSERTD(sb == 0);
a1 = a0;
a0 = 0; /* should be a0 = sb */
/* since sb=0 already, no need to set it to 0 */
bx -= GMP_NUMB_BITS;
}
/* now a1 != 0 */
MPFR_ASSERTD(a1 != 0);
count_leading_zeros (cnt, a1);
if (cnt)
{
ap[1] = (a1 << cnt) | (a0 >> (GMP_NUMB_BITS - cnt));
a0 = (a0 << cnt) | (sb >> (GMP_NUMB_BITS - cnt));
sb <<= cnt;
bx -= cnt;
}
else
ap[1] = a1;
/* sh > 0 since p < 2*GMP_NUMB_BITS */
MPFR_ASSERTD(sh > 0);
rb = a0 & (MPFR_LIMB_ONE << (sh - 1));
sb |= (a0 & mask) ^ rb;
ap[0] = a0 & ~mask;
}
else if (d < 2 * GMP_NUMB_BITS)
{ /* GMP_NUMB_BITS <= d < 2*GMP_NUMB_BITS */
/* warning: the most significant bit of sb might become the least
significant bit of a0 below */
sb = (d == GMP_NUMB_BITS) ? cp[0]
: (cp[1] << (2*GMP_NUMB_BITS - d)) | (cp[0] != 0);
t = (cp[1] >> (d - GMP_NUMB_BITS)) + (sb != 0);
/* warning: t might overflow to 0 if d=GMP_NUMB_BITS and sb <> 0 */
a0 = bp[0] - t;
a1 = bp[1] - (bp[0] < t) - (t == 0 && sb != 0);
sb = -sb;
/* since bp[1] has its most significant bit set, we can have an
exponent decrease of at most one */
if (a1 < MPFR_LIMB_HIGHBIT)
{
ap[1] = (a1 << 1) | (a0 >> (GMP_NUMB_BITS - 1));
a0 = (a0 << 1) | (sb >> (GMP_NUMB_BITS - 1));
sb <<= 1;
bx --;
}
else
ap[1] = a1;
rb = a0 & (MPFR_LIMB_ONE << (sh - 1));
sb |= (a0 & mask) ^ rb;
ap[0] = a0 & ~mask;
}
else /* d >= 2*GMP_NUMB_BITS */
{
/* We compute b - ulp(b), and the remainder ulp(b) - c satisfies:
1/2 ulp(b) < ulp(b) - c < ulp(b), thus rb = sb = 1, unless we
had an exponent decrease. */
t = MPFR_LIMB_ONE << sh;
a0 = bp[0] - t;
a1 = bp[1] - (bp[0] < t);
if (a1 < MPFR_LIMB_HIGHBIT)
{
/* necessarily we had b = 1000...000 */
/* Warning: since we have an exponent decrease, when
p = 2*GMP_NUMB_BITS - 1 and d = 2*GMP_NUMB_BITS, the round bit
corresponds to the upper bit of -c. In that case rb = 0 and
sb = 1, except when c = 1000...000 where rb = 1 and sb = 0. */
rb = sh > 1 || d > 2 * GMP_NUMB_BITS
|| (cp[1] == MPFR_LIMB_HIGHBIT && cp[0] == MPFR_LIMB_ZERO);
/* sb=1 below is incorrect when p = 2*GMP_NUMB_BITS - 1,
d = 2*GMP_NUMB_BITS and c = 1000...000, but in
that case the even rule wound round up too. */
ap[0] = ~mask;
ap[1] = MPFR_LIMB_MAX;
bx --;
}
else
{
ap[0] = a0;
ap[1] = a1;
rb = 1;
}
sb = 1;
}
}
/* now perform rounding */
/* Warning: MPFR considers underflow *after* rounding with an unbounded
exponent range. However, since b and c have same precision p, they are
multiples of 2^(emin-p), likewise for b-c. Thus if bx < emin, the
subtraction (with an unbounded exponent range) is exact, so that bx is
also the exponent after rounding with an unbounded exponent range. */
if (MPFR_UNLIKELY(bx < __gmpfr_emin))
{
/* for RNDN, mpfr_underflow always rounds away, thus for |a|<=2^(emin-2)
we have to change to RNDZ */
if (rnd_mode == MPFR_RNDN &&
(bx < __gmpfr_emin - 1 ||
(ap[1] == MPFR_LIMB_HIGHBIT && ap[0] == 0)))
rnd_mode = MPFR_RNDZ;
return mpfr_underflow (a, rnd_mode, MPFR_SIGN(a));
}
MPFR_SET_EXP (a, bx);
if ((rb == 0 && sb == 0) || rnd_mode == MPFR_RNDF)
MPFR_RET (0);
else if (rnd_mode == MPFR_RNDN)
{
if (rb == 0 || (sb == 0 && (ap[0] & (MPFR_LIMB_ONE << sh)) == 0))
goto truncate;
else
goto add_one_ulp;
}
else if (MPFR_IS_LIKE_RNDZ(rnd_mode, MPFR_IS_NEG(a)))
{
truncate:
MPFR_RET(-MPFR_SIGN(a));
}
else /* round away from zero */
{
add_one_ulp:
ap[0] += MPFR_LIMB_ONE << sh;
ap[1] += (ap[0] == 0);
if (MPFR_UNLIKELY(ap[1] == 0))
{
ap[1] = MPFR_LIMB_HIGHBIT;
/* Note: bx+1 cannot exceed __gmpfr_emax, since |a| <= |b|, thus
bx+1 is at most equal to the original exponent of b. */
MPFR_ASSERTD(bx + 1 <= __gmpfr_emax);
MPFR_SET_EXP (a, bx + 1);
}
MPFR_RET(MPFR_SIGN(a));
}
}
/* special code for p = 2*GMP_NUMB_BITS */
static int
mpfr_sub1sp2n (mpfr_ptr a, mpfr_srcptr b, mpfr_srcptr c, mpfr_rnd_t rnd_mode)
{
mpfr_exp_t bx = MPFR_GET_EXP (b);
mpfr_exp_t cx = MPFR_GET_EXP (c);
mp_limb_t *ap = MPFR_MANT(a);
mp_limb_t *bp = MPFR_MANT(b);
mp_limb_t *cp = MPFR_MANT(c);
mpfr_prec_t cnt;
mp_limb_t rb; /* round bit */
mp_limb_t sb; /* sticky bit */
mp_limb_t a0, a1;
mpfr_uexp_t d;
/* this function is inspired by mpfr_sub1sp2 (for the operations of the
2-limb arrays) and by mpfr_sub1sp1n (for the different cases to handle) */
if (bx == cx) /* subtraction is exact in this case */
{
a0 = bp[0] - cp[0];
a1 = bp[1] - cp[1] - (bp[0] < cp[0]);
if (a1 == 0 && a0 == 0) /* result is zero */
{
if (rnd_mode == MPFR_RNDD)
MPFR_SET_NEG(a);
else
MPFR_SET_POS(a);
MPFR_SET_ZERO(a);
MPFR_RET (0);
}
/* since B/2 <= bp[1], cp[1] < B with B=2^GMP_NUMB_BITS,
if no borrow we have 0 <= bp[1] - cp[1] - x < B/2
where x = (bp[0] < cp[0]) is 0 or 1, thus a1 < B/2 <= bp[1] */
else if (a1 >= bp[1]) /* borrow: |c| > |b| */
{
MPFR_SET_OPPOSITE_SIGN (a, b);
/* negate [a1,a0] */
a0 = -a0;
a1 = -a1 - (a0 != 0);
}
else /* no borrow */
MPFR_SET_SAME_SIGN (a, b);
/* now [a1,a0] is the absolute value of b - c,
maybe not normalized */
if (a1 == 0)
{
a1 = a0;
a0 = 0;
bx -= GMP_NUMB_BITS;
}
/* now a1 != 0 */
MPFR_ASSERTD(a1 != 0);
count_leading_zeros (cnt, a1);
if (cnt)
{
/* shift [a1,a0] left by cnt bit and store in result */
ap[1] = (a1 << cnt) | (a0 >> (GMP_NUMB_BITS - cnt));
ap[0] = a0 << cnt;
bx -= cnt;
}
else
{
ap[1] = a1;
ap[0] = a0;
}
rb = sb = 0; /* the subtraction is exact */
}
else
{
mp_limb_t t;
if (bx < cx) /* swap b and c */
{
mpfr_exp_t tx;
mp_limb_t *tp;
tx = bx; bx = cx; cx = tx;
tp = bp; bp = cp; cp = tp;
MPFR_SET_OPPOSITE_SIGN (a, b);
}
else
{
MPFR_SET_SAME_SIGN (a, b);
}
MPFR_ASSERTD (bx > cx);
d = (mpfr_uexp_t) bx - cx;
if (d < GMP_NUMB_BITS)
{
t = (cp[1] << (GMP_NUMB_BITS - d)) | (cp[0] >> d);
/* t is the part that should be subtracted to bp[0]:
| a1 | a0 |
| bp[1] | bp[0] |
| cp[1]>>d | t | sb | */
#ifndef MPFR_FULLSUB
a0 = bp[0] - t;
a1 = bp[1] - (cp[1] >> d) - (bp[0] < t);
sb = cp[0] << (GMP_NUMB_BITS - d); /* neglected part of c */
/* now negate sb and subtract borrow to a0 if sb <> 0 */
if (sb)
{
a1 -= (a0 == 0);
a0 --;
/* a = a1,a0 can only be zero when d=1, b = 0.1000...000*2^bx,
and c = 0.111...111*2^(bx-1). In that case (where we have
sb = MPFR_LIMB_HIGHBIT below), the subtraction is exact, the
result is b/2^(2*GMP_NUMB_BITS). This case is dealt below. */
sb = -sb;
}
#else
sb = - (cp[0] << (GMP_NUMB_BITS - d));
a0 = bp[0] - t - (sb != 0);
a1 = bp[1] - (cp[1] >> d) - (bp[0] < t || (bp[0] == t && sb != 0));
#endif
/* now the result is formed of [a1,a0,sb], which might not be
normalized */
if (a1 == MPFR_LIMB_ZERO)
{
/* this implies d=1 */
MPFR_ASSERTD(d == 1);
a1 = a0;
a0 = sb;
sb = MPFR_LIMB_ZERO;
bx -= GMP_NUMB_BITS;
}
if (a1 == MPFR_LIMB_ZERO) /* case a = a1,a0 = 0 mentioned above */
{
MPFR_ASSERTD(a0 == MPFR_LIMB_HIGHBIT); /* was sb above */
a1 = a0;
a0 = sb;
bx -= GMP_NUMB_BITS;
sb = MPFR_LIMB_ZERO;
}
else
{
count_leading_zeros (cnt, a1);
if (cnt)
{
/* shift [a1,a0,sb] left by cnt bits and adjust exponent */
a1 = (a1 << cnt) | (a0 >> (GMP_NUMB_BITS - cnt));
a0 = (a0 << cnt) | (sb >> (GMP_NUMB_BITS - cnt));
sb <<= cnt;
bx -= cnt;
}
}
rb = sb & MPFR_LIMB_HIGHBIT;
sb = sb & ~MPFR_LIMB_HIGHBIT;
ap[1] = a1;
ap[0] = a0;
}
else if (d < 2 * GMP_NUMB_BITS)
{ /* GMP_NUMB_BITS <= d < 2*GMP_NUMB_BITS:
compute t, the part to be subtracted to bp[0],
and sb, the neglected part of c:
| a1 | a0 |
| bp[1] | bp[0] |
| t | sb | */
/* warning: we should not ignore the low bits from cp[0]
in case d > GMP_NUMB_BITS */
sb = (d == GMP_NUMB_BITS) ? cp[0]
: (cp[1] << (2*GMP_NUMB_BITS - d))
| (cp[0] >> (d - GMP_NUMB_BITS))
| ((cp[0] << (2*GMP_NUMB_BITS - d)) != 0);
t = (cp[1] >> (d - GMP_NUMB_BITS)) + (sb != 0);
/* Warning: t might overflow to 0 if d=GMP_NUMB_BITS, sb <> 0,
and cp[1] = 111...111 */
a0 = bp[0] - t;
a1 = bp[1] - (bp[0] < t) - (t == 0 && sb != 0);
sb = -sb;
/* now the result is [a1,a0,sb]. Since bp[1] has its most significant
bit set, we can have an exponent decrease of at most one */
if (a1 < MPFR_LIMB_HIGHBIT)
{
/* shift [a1,a0] left by 1 bit */
a1 = (a1 << 1) | (a0 >> (GMP_NUMB_BITS - 1));
MPFR_ASSERTD(a1 >= MPFR_LIMB_HIGHBIT);
a0 = (a0 << 1) | (sb >> (GMP_NUMB_BITS - 1));
sb <<= 1;
bx --;
}
ap[1] = a1;
ap[0] = a0;
rb = sb & MPFR_LIMB_HIGHBIT;
sb = sb & ~MPFR_LIMB_HIGHBIT;
}
else
{ /* d >= 2*GMP_NUMB_BITS:
| a1 | a0 |
| bp[1] | bp[0] |
| cp[1] | cp[0] | */
/* we mimic the case d >= GMP_NUMB_BITS of mpfr_sub1sp1n */
int tst = cp[1] == MPFR_LIMB_HIGHBIT && cp[0] == MPFR_LIMB_ZERO;
/* if d = 2 * GMP_NUMB_BITS and tst=1, c = 1/2*ulp(b) */
if (bp[1] > MPFR_LIMB_HIGHBIT || bp[0] > MPFR_LIMB_ZERO)
{
/* no borrow in b - ulp(b) */
rb = d > 2 * GMP_NUMB_BITS || tst;
sb = d > 2 * GMP_NUMB_BITS || !tst;
ap[1] = bp[1] - (bp[0] == MPFR_LIMB_ZERO);
ap[0] = bp[0] - MPFR_LIMB_ONE;
}
else
{
/* b = 1000...000, thus subtracting c yields an exponent shift */
bx --;
if (d == 2 * GMP_NUMB_BITS && !tst) /* c > 1/2*ulp(b) */
{
t = -cp[1] - (cp[0] > MPFR_LIMB_ZERO);
/* the rounding bit is the 2nd most significant bit of t
(where the most significant bit of t is necessarily 0),
and the sticky bit is formed by the remaining bits of t,
and those from -cp[0] */
rb = t >= (MPFR_LIMB_HIGHBIT >> 1);
sb = (t << 2) | cp[0];
ap[1] = MPFR_LIMB_MAX;
ap[0] = -(MPFR_LIMB_ONE << 1);
}
else /* c <= 1/2*ulp(b) */
{
rb = d > 2 * GMP_NUMB_BITS + 1
|| (d == 2 * GMP_NUMB_BITS + 1 && tst);
sb = d > 2 * GMP_NUMB_BITS + 1
|| (d == 2 * GMP_NUMB_BITS + 1 && !tst);
ap[1] = -MPFR_LIMB_ONE;
ap[0] = -MPFR_LIMB_ONE;
}
}
}
}
/* now perform rounding */
/* Warning: MPFR considers underflow *after* rounding with an unbounded
exponent range. However, since b and c have same precision p, they are
multiples of 2^(emin-p), likewise for b-c. Thus if bx < emin, the
subtraction (with an unbounded exponent range) is exact, so that bx is
also the exponent after rounding with an unbounded exponent range. */
if (MPFR_UNLIKELY(bx < __gmpfr_emin))
{
/* for RNDN, mpfr_underflow always rounds away, thus for |a|<=2^(emin-2)
we have to change to RNDZ */
if (rnd_mode == MPFR_RNDN &&
(bx < __gmpfr_emin - 1 ||
(ap[1] == MPFR_LIMB_HIGHBIT && ap[0] == 0)))
rnd_mode = MPFR_RNDZ;
return mpfr_underflow (a, rnd_mode, MPFR_SIGN(a));
}
MPFR_SET_EXP (a, bx);
if ((rb == 0 && sb == 0) || rnd_mode == MPFR_RNDF)
MPFR_RET (0);
else if (rnd_mode == MPFR_RNDN)
{
if (rb == 0 || (sb == 0 && (ap[0] & MPFR_LIMB_ONE) == 0))
goto truncate;
else
goto add_one_ulp;
}
else if (MPFR_IS_LIKE_RNDZ(rnd_mode, MPFR_IS_NEG(a)))
{
truncate:
MPFR_RET(-MPFR_SIGN(a));
}
else /* round away from zero */
{
add_one_ulp:
ap[0] += MPFR_LIMB_ONE;
ap[1] += (ap[0] == 0);
if (MPFR_UNLIKELY(ap[1] == 0))
{
ap[1] = MPFR_LIMB_HIGHBIT;
/* Note: bx+1 cannot exceed __gmpfr_emax, since |a| <= |b|, thus
bx+1 is at most equal to the original exponent of b. */
MPFR_ASSERTD(bx + 1 <= __gmpfr_emax);
MPFR_SET_EXP (a, bx + 1);
}
MPFR_RET(MPFR_SIGN(a));
}
}
/* special code for 2*GMP_NUMB_BITS < p < 3*GMP_NUMB_BITS */
static int
mpfr_sub1sp3 (mpfr_ptr a, mpfr_srcptr b, mpfr_srcptr c, mpfr_rnd_t rnd_mode,
mpfr_prec_t p)
{
mpfr_exp_t bx = MPFR_GET_EXP (b);
mpfr_exp_t cx = MPFR_GET_EXP (c);
mp_limb_t *ap = MPFR_MANT(a);
mp_limb_t *bp = MPFR_MANT(b);
mp_limb_t *cp = MPFR_MANT(c);
mpfr_prec_t cnt, INITIALIZED(sh);
mp_limb_t rb; /* round bit */
mp_limb_t sb; /* sticky bit */
mp_limb_t mask, a0, a1, a2;
mpfr_uexp_t d;
MPFR_ASSERTD(2 * GMP_NUMB_BITS < p && p < 3 * GMP_NUMB_BITS);
if (bx == cx) /* subtraction is exact in this case */
{
a0 = bp[0] - cp[0];
a1 = bp[1] - cp[1] - (bp[0] < cp[0]);
/* a borrow is generated for a when either bp[1] < cp[1],
or bp[1] = cp[1] and bp[0] < cp[0] */
a2 = bp[2] - cp[2]
- (bp[1] < cp[1] || (bp[1] == cp[1] && bp[0] < cp[0]));
if (a2 == 0 && a1 == 0 && a0 == 0) /* result is zero */
{
if (rnd_mode == MPFR_RNDD)
MPFR_SET_NEG(a);
else
MPFR_SET_POS(a);
MPFR_SET_ZERO(a);
MPFR_RET (0);
}
else if (a2 >= bp[2]) /* borrow: |c| > |b| */
{
MPFR_SET_OPPOSITE_SIGN (a, b);
/* a = b-c mod 2^(3*GMP_NUMB_BITS) */
a0 = -a0;
a1 = -a1 - (a0 != 0);
a2 = -a2 - (a0 != 0 || a1 != 0);
}
else /* bp[0] > cp[0] */
MPFR_SET_SAME_SIGN (a, b);
if (a2 == 0)
{
a2 = a1;
a1 = a0;
a0 = 0;
bx -= GMP_NUMB_BITS;
if (a2 == 0)
{
a2 = a1;
a1 = 0;
bx -= GMP_NUMB_BITS;
}
}
/* now a2 != 0 */
MPFR_ASSERTD(a2 != 0);
count_leading_zeros (cnt, a2);
if (cnt)
{
ap[2] = (a2 << cnt) | (a1 >> (GMP_NUMB_BITS - cnt));
ap[1] = (a1 << cnt) | (a0 >> (GMP_NUMB_BITS - cnt));
ap[0] = a0 << cnt;
bx -= cnt;
}
else
{
ap[2] = a2;
ap[1] = a1;
ap[0] = a0;
}
rb = sb = 0;
/* Note: sh is not initialized, but will not be used in this case. */
}
else
{
if (bx < cx) /* swap b and c */
{
mpfr_exp_t tx;
mp_limb_t *tp;
tx = bx; bx = cx; cx = tx;
tp = bp; bp = cp; cp = tp;
MPFR_SET_OPPOSITE_SIGN (a, b);
}
else
{
MPFR_SET_SAME_SIGN (a, b);
}
MPFR_ASSERTD (bx > cx);
d = (mpfr_uexp_t) bx - cx;
sh = 3 * GMP_NUMB_BITS - p;
mask = MPFR_LIMB_MASK(sh);
if (d < GMP_NUMB_BITS)
{
mp_limb_t cy;
/* warning: we must have the most significant bit of sb correct
since it might become the round bit below */
sb = cp[0] << (GMP_NUMB_BITS - d); /* neglected part of c */
a0 = bp[0] - ((cp[1] << (GMP_NUMB_BITS - d)) | (cp[0] >> d));
a1 = bp[1] - ((cp[2] << (GMP_NUMB_BITS - d)) | (cp[1] >> d))
- (a0 > bp[0]);
cy = a1 > bp[1] || (a1 == bp[1] && a0 > bp[0]); /* borrow in a1 */
a2 = bp[2] - (cp[2] >> d) - cy;
/* if sb is non-zero, subtract 1 from a2, a1, a0 since we want a
non-negative neglected part */
if (sb)
{
a2 -= (a1 == 0 && a0 == 0);
a1 -= (a0 == 0);
a0 --;
/* a = a2,a1,a0 cannot become zero here, since:
a) if d >= 2, then a2 >= 2^(w-1) - (2^(w-2)-1) with
w = GMP_NUMB_BITS, thus a2 - 1 >= 2^(w-2),
b) if d = 1, then since p < 3*GMP_NUMB_BITS we have sb=0. */
MPFR_ASSERTD(a2 > 0 || a1 > 0 || a0 > 0);
sb = -sb; /* 2^GMP_NUMB_BITS - sb */
}
if (a2 == 0)
{
/* this implies d=1, which in turn implies sb=0 */
MPFR_ASSERTD(sb == 0);
a2 = a1;
a1 = a0;
a0 = 0; /* should be a0 = sb */
/* since sb=0 already, no need to set it to 0 */
bx -= GMP_NUMB_BITS;
if (a2 == 0)
{
a2 = a1;
a1 = 0; /* should be a1 = a0 */
bx -= GMP_NUMB_BITS;
}
}
/* now a1 != 0 */
MPFR_ASSERTD(a2 != 0);
count_leading_zeros (cnt, a2);
if (cnt)
{
ap[2] = (a2 << cnt) | (a1 >> (GMP_NUMB_BITS - cnt));
ap[1] = (a1 << cnt) | (a0 >> (GMP_NUMB_BITS - cnt));
a0 = (a0 << cnt) | (sb >> (GMP_NUMB_BITS - cnt));
sb <<= cnt;
bx -= cnt;
}
else
{
ap[2] = a2;
ap[1] = a1;
}
/* sh > 0 since p < 2*GMP_NUMB_BITS */
MPFR_ASSERTD(sh > 0);
rb = a0 & (MPFR_LIMB_ONE << (sh - 1));
sb |= (a0 & mask) ^ rb;
ap[0] = a0 & ~mask;
}
else if (d < 2 * GMP_NUMB_BITS)
{
mp_limb_t c0shifted;
/* warning: we must have the most significant bit of sb correct
since it might become the round bit below */
sb = (d == GMP_NUMB_BITS) ? cp[0]
: (cp[1] << (2*GMP_NUMB_BITS - d)) | (cp[0] != 0);
c0shifted = (d == GMP_NUMB_BITS) ? cp[1]
: (cp[2] << (2*GMP_NUMB_BITS-d)) | (cp[1] >> (d - GMP_NUMB_BITS));
a0 = bp[0] - c0shifted;
/* TODO: add a non-regression test for cp[2] == MPFR_LIMB_MAX,
d == GMP_NUMB_BITS and a0 > bp[0]. */
a1 = bp[1] - (cp[2] >> (d - GMP_NUMB_BITS)) - (a0 > bp[0]);
a2 = bp[2] - (a1 > bp[1] || (a1 == bp[1] && a0 > bp[0]));
/* if sb is non-zero, subtract 1 from a2, a1, a0 since we want a
non-negative neglected part */
if (sb)
{
a2 -= (a1 == 0 && a0 == 0);
a1 -= (a0 == 0);
a0 --;
/* a = a2,a1,a0 cannot become zero here, since:
a) if d >= 2, then a2 >= 2^(w-1) - (2^(w-2)-1) with
w = GMP_NUMB_BITS, thus a2 - 1 >= 2^(w-2),
b) if d = 1, then since p < 3*GMP_NUMB_BITS we have sb=0. */
MPFR_ASSERTD(a2 > 0 || a1 > 0 || a0 > 0);
sb = -sb; /* 2^GMP_NUMB_BITS - sb */
}
/* since bp[2] has its most significant bit set, we can have an
exponent decrease of at most one */
if (a2 < MPFR_LIMB_HIGHBIT)
{
ap[2] = (a2 << 1) | (a1 >> (GMP_NUMB_BITS - 1));
ap[1] = (a1 << 1) | (a0 >> (GMP_NUMB_BITS - 1));
a0 = (a0 << 1) | (sb >> (GMP_NUMB_BITS - 1));
sb <<= 1;
bx --;
}
else
{
ap[2] = a2;
ap[1] = a1;
}
rb = a0 & (MPFR_LIMB_ONE << (sh - 1));
sb |= (a0 & mask) ^ rb;
ap[0] = a0 & ~mask;
}
else if (d < 3 * GMP_NUMB_BITS) /* 2*GMP_NUMB_BITS<=d<3*GMP_NUMB_BITS */
{
MPFR_ASSERTD (2*GMP_NUMB_BITS <= d && d < 3*GMP_NUMB_BITS);
/* warning: we must have the most significant bit of sb correct
since it might become the round bit below */
if (d == 2 * GMP_NUMB_BITS)
sb = cp[1] | (cp[0] != 0);
else
sb = cp[2] << (3*GMP_NUMB_BITS - d) | (cp[1] != 0) | (cp[0] != 0);
sb = -sb;
/* TODO: add a non-regression test for cp[2] == MPFR_LIMB_MAX,
d == 2*GMP_NUMB_BITS and sb != 0. */
a0 = bp[0] - (cp[2] >> (d - 2*GMP_NUMB_BITS)) - (sb != 0);
a1 = bp[1] - (a0 > bp[0] || (a0 == bp[0] && sb != 0));
a2 = bp[2] - (a1 > bp[1]);
if (a2 < MPFR_LIMB_HIGHBIT)
{
ap[2] = (a2 << 1) | (a1 >> (GMP_NUMB_BITS - 1));
ap[1] = (a1 << 1) | (a0 >> (GMP_NUMB_BITS - 1));
a0 = (a0 << 1) | (sb >> (GMP_NUMB_BITS - 1));
sb <<= 1;
bx --;
}
else
{
ap[2] = a2;
ap[1] = a1;
}
rb = a0 & (MPFR_LIMB_ONE << (sh - 1));
sb |= (a0 & mask) ^ rb;
ap[0] = a0 & ~mask;
}
else /* d >= 3*GMP_NUMB_BITS */
{
/* We compute b - ulp(b), and the remainder ulp(b) - c satisfies:
1/2 ulp(b) < ulp(b) - c < ulp(b), thus rb = sb = 1. */
mp_limb_t t = MPFR_LIMB_ONE << sh;
a0 = bp[0] - t;
a1 = bp[1] - (bp[0] < t);
a2 = bp[2] - (a1 > bp[1]);
if (a2 < MPFR_LIMB_HIGHBIT)
{
/* necessarily we had b = 1000...000 */
/* Warning: since we have an exponent decrease, when
p = 3*GMP_NUMB_BITS - 1 and d = 3*GMP_NUMB_BITS, the round bit
corresponds to the upper bit of -c. In that case rb = 0 and
sb = 1, except when c = 1000...000 where rb = 1 and sb = 0. */
rb = sh > 1 || d > 3 * GMP_NUMB_BITS
|| (cp[2] == MPFR_LIMB_HIGHBIT && cp[1] == MPFR_LIMB_ZERO &&
cp[0] == MPFR_LIMB_ZERO);
/* sb=1 below is incorrect when p = 2*GMP_NUMB_BITS - 1,
d = 2*GMP_NUMB_BITS and c = 1000...000, but in
that case the even rule wound round up too. */
ap[0] = ~mask;
ap[1] = MPFR_LIMB_MAX;
ap[2] = MPFR_LIMB_MAX;
bx --;
}
else
{
ap[0] = a0;
ap[1] = a1;
ap[2] = a2;
rb = 1;
}
sb = 1;
}
}
/* now perform rounding */
/* Warning: MPFR considers underflow *after* rounding with an unbounded
exponent range. However, since b and c have same precision p, they are
multiples of 2^(emin-p), likewise for b-c. Thus if bx < emin, the
subtraction (with an unbounded exponent range) is exact, so that bx is
also the exponent after rounding with an unbounded exponent range. */
if (MPFR_UNLIKELY(bx < __gmpfr_emin))
{
/* for RNDN, mpfr_underflow always rounds away, thus for |a|<=2^(emin-2)
we have to change to RNDZ */
if (rnd_mode == MPFR_RNDN &&
(bx < __gmpfr_emin - 1 ||
(ap[2] == MPFR_LIMB_HIGHBIT && ap[1] == 0 && ap[0] == 0)))
rnd_mode = MPFR_RNDZ;
return mpfr_underflow (a, rnd_mode, MPFR_SIGN(a));
}
MPFR_SET_EXP (a, bx);
if ((rb == 0 && sb == 0) || rnd_mode == MPFR_RNDF)
MPFR_RET (0);
else if (rnd_mode == MPFR_RNDN)
{
if (rb == 0 || (sb == 0 && (ap[0] & (MPFR_LIMB_ONE << sh)) == 0))
goto truncate;
else
goto add_one_ulp;
}
else if (MPFR_IS_LIKE_RNDZ(rnd_mode, MPFR_IS_NEG(a)))
{
truncate:
MPFR_RET(-MPFR_SIGN(a));
}
else /* round away from zero */
{
add_one_ulp:
ap[0] += MPFR_LIMB_ONE << sh;
ap[1] += (ap[0] == 0);
ap[2] += (ap[1] == 0 && ap[0] == 0);
if (MPFR_UNLIKELY(ap[2] == 0))
{
ap[2] = MPFR_LIMB_HIGHBIT;
/* Note: bx+1 cannot exceed __gmpfr_emax, since |a| <= |b|, thus
bx+1 is at most equal to the original exponent of b. */
MPFR_ASSERTD(bx + 1 <= __gmpfr_emax);
MPFR_SET_EXP (a, bx + 1);
}
MPFR_RET(MPFR_SIGN(a));
}
}
#endif /* !defined(MPFR_GENERIC_ABI) */
/* Rounding Sub */
/*
compute sgn(b)*(|b| - |c|) if |b|>|c| else -sgn(b)*(|c| -|b|)
Returns 0 iff result is exact,
a negative value when the result is less than the exact value,
a positive value otherwise.
*/
/* A0...Ap-1
* Cp Cp+1 ....
* <- C'p+1 ->
* Cp = -1 if calculated from c mantissa
* Cp = 0 if 0 from a or c
* Cp = 1 if calculated from a.
* C'p+1 = First bit not null or 0 if there isn't one
*
* Can't have Cp=-1 and C'p+1=1*/
/* RND = MPFR_RNDZ:
* + if Cp=0 and C'p+1=0,1, Truncate.
* + if Cp=0 and C'p+1=-1, SubOneUlp
* + if Cp=-1, SubOneUlp
* + if Cp=1, AddOneUlp
* RND = MPFR_RNDA (Away)
* + if Cp=0 and C'p+1=0,-1, Truncate
* + if Cp=0 and C'p+1=1, AddOneUlp
* + if Cp=1, AddOneUlp
* + if Cp=-1, Truncate
* RND = MPFR_RNDN
* + if Cp=0, Truncate
* + if Cp=1 and C'p+1=1, AddOneUlp
* + if Cp=1 and C'p+1=-1, Truncate
* + if Cp=1 and C'p+1=0, Truncate if Ap-1=0, AddOneUlp else
* + if Cp=-1 and C'p+1=-1, SubOneUlp
* + if Cp=-1 and C'p+1=0, Truncate if Ap-1=0, SubOneUlp else
*
* If AddOneUlp:
* If carry, then it is 11111111111 + 1 = 10000000000000
* ap[n-1]=MPFR_HIGHT_BIT
* If SubOneUlp:
* If we lose one bit, it is 1000000000 - 1 = 0111111111111
* Then shift, and put as last bit x which is calculated
* according Cp, Cp-1 and rnd_mode.
* If Truncate,
* If it is a power of 2,
* we may have to suboneulp in some special cases.
*
* To simplify, we don't use Cp = 1.
*
*/
MPFR_HOT_FUNCTION_ATTR int
mpfr_sub1sp (mpfr_ptr a, mpfr_srcptr b, mpfr_srcptr c, mpfr_rnd_t rnd_mode)
{
mpfr_exp_t bx, cx;
mpfr_uexp_t d;
mpfr_prec_t p, sh, cnt;
mp_size_t n, k;
mp_limb_t *ap = MPFR_MANT(a);
mp_limb_t *bp = MPFR_MANT(b);
mp_limb_t *cp = MPFR_MANT(c);
mp_limb_t limb;
int inexact;
mp_limb_t rb, sb; /* round and sticky bits. They are interpreted as
negative, i.e., if rb <> 0, then we should subtract 1
at the round bit position, and of sb <> 0, we should
subtract something below the round bit position. */
mp_limb_t rbb = MPFR_LIMB_MAX, sbb = MPFR_LIMB_MAX; /* rbb is the next bit
after the round bit, and sbb the corresponding sticky bit.
gcc claims that they might be used uninitialized. We fill them with invalid
values, which should produce a failure if so. See README.dev file. */
int pow2;
MPFR_TMP_DECL(marker);
MPFR_ASSERTD(MPFR_PREC(a) == MPFR_PREC(b) && MPFR_PREC(b) == MPFR_PREC(c));
MPFR_ASSERTD(MPFR_IS_PURE_FP(b));
MPFR_ASSERTD(MPFR_IS_PURE_FP(c));
/* Read prec and num of limbs */
p = MPFR_GET_PREC (b);
#if !defined(MPFR_GENERIC_ABI)
/* special case for p < GMP_NUMB_BITS */
if (p < GMP_NUMB_BITS)
return mpfr_sub1sp1 (a, b, c, rnd_mode, p);
/* special case for GMP_NUMB_BITS < p < 2*GMP_NUMB_BITS */
if (GMP_NUMB_BITS < p && p < 2 * GMP_NUMB_BITS)
return mpfr_sub1sp2 (a, b, c, rnd_mode, p);
/* special case for p = GMP_NUMB_BITS: we put it *after* mpfr_sub1sp2,
in order not to slow down mpfr_sub1sp2, which should be more frequent */
if (p == GMP_NUMB_BITS)
return mpfr_sub1sp1n (a, b, c, rnd_mode);
/* special case for 2*GMP_NUMB_BITS < p < 3*GMP_NUMB_BITS */
if (2 * GMP_NUMB_BITS < p && p < 3 * GMP_NUMB_BITS)
return mpfr_sub1sp3 (a, b, c, rnd_mode, p);
if (p == 2 * GMP_NUMB_BITS)
return mpfr_sub1sp2n (a, b, c, rnd_mode);
#endif
n = MPFR_PREC2LIMBS (p);
/* Fast cmp of |b| and |c| */
bx = MPFR_GET_EXP (b);
cx = MPFR_GET_EXP (c);
MPFR_TMP_MARK(marker);
k = n - 1;
if (bx == cx)
{
/* Check mantissa since exponents are equal */
while (k >= 0 && MPFR_UNLIKELY(bp[k] == cp[k]))
k--;
/* now k = - 1 if b == c, otherwise k is the largest integer < n such
that bp[k] <> cp[k] */
if (k < 0)
/* b == c ! */
{
/* Return exact number 0 */
if (rnd_mode == MPFR_RNDD)
MPFR_SET_NEG(a);
else
MPFR_SET_POS(a);
MPFR_SET_ZERO(a);
MPFR_RET(0);
}
else if (bp[k] > cp[k])
goto BGreater;
else
{
MPFR_ASSERTD(bp[k] < cp[k]);
goto CGreater;
}
}
else if (bx < cx)
{
/* Swap b and c and set sign */
mpfr_srcptr t;
mpfr_exp_t tx;
mp_limb_t *tp;
tx = bx; bx = cx; cx = tx;
CGreater:
MPFR_SET_OPPOSITE_SIGN(a,b);
t = b; b = c; c = t;
tp = bp; bp = cp; cp = tp;
}
else
{
/* |b| > |c| */
BGreater:
MPFR_SET_SAME_SIGN(a,b);
}
/* Now |b| > |c| */
MPFR_ASSERTD(bx >= cx);
d = (mpfr_uexp_t) bx - cx;
/* printf ("New with diff=%lu\n", (unsigned long) d); */
/* FIXME: The goto's below are too complex (long backward) and make
the code unreadable. */
if (d == 0)
{
/* <-- b -->
<-- c --> : exact sub */
mpn_sub_n (ap, bp, cp, n);
/* Normalize */
ExactNormalize:
limb = ap[n-1];
if (MPFR_LIKELY (limb != 0))
{
/* First limb is not zero. */
count_leading_zeros (cnt, limb);
/* Warning: cnt can be 0 when we come from the case SubD1Lose
with goto ExactNormalize */
if (MPFR_LIKELY(cnt))
{
mpn_lshift (ap, ap, n, cnt); /* Normalize number */
bx -= cnt; /* Update final expo */
}
/* Last limb should be OK */
MPFR_ASSERTD(!(ap[0] & MPFR_LIMB_MASK((unsigned int) (-p)
% GMP_NUMB_BITS)));
}
else
{
/* First limb is zero: this can only occur for n >= 2 */
mp_size_t len;
/* Find the first limb not equal to zero. It necessarily exists
since |b| > |c|. We know that bp[k] > cp[k] and all upper
limbs are equal. */
while (ap[k] == 0)
k--;
limb = ap[k];
/* ap[k] is the non-zero limb of largest index, thus we have
to consider the k+1 least significant limbs */
MPFR_ASSERTD(limb != 0);
count_leading_zeros(cnt, limb);
k++;
len = n - k; /* Number of most significant zero limbs */
MPFR_ASSERTD(k > 0);
if (cnt)
mpn_lshift (ap + len, ap, k, cnt); /* Normalize the High Limb*/
else
/* Must use copyd since src and dst may overlap & dst>=src */
mpn_copyd (ap + len, ap, k);
MPN_ZERO(ap, len); /* Zeroing the last limbs */
bx -= cnt + len * GMP_NUMB_BITS; /* update exponent */
/* ap[len] should have its low bits zero: it is bp[0]-cp[0] */
MPFR_ASSERTD(!(ap[len] & MPFR_LIMB_MASK((unsigned int) (-p)
% GMP_NUMB_BITS)));
}
/* Check exponent underflow (no overflow can happen) */
if (MPFR_UNLIKELY(bx < __gmpfr_emin))
{
MPFR_TMP_FREE(marker);
/* since b and c have same sign, exponent and precision, the
subtraction is exact */
/* printf("(D==0 Underflow)\n"); */
/* for MPFR_RNDN, mpfr_underflow always rounds away from zero,
thus for |a| <= 2^(emin-2) we change to RNDZ. */
if (rnd_mode == MPFR_RNDN &&
(bx < __gmpfr_emin - 1 || mpfr_powerof2_raw (a)))
rnd_mode = MPFR_RNDZ;
return mpfr_underflow (a, rnd_mode, MPFR_SIGN(a));
}
MPFR_SET_EXP (a, bx);
/* No rounding is necessary since the result is exact */
MPFR_ASSERTD(ap[n-1] & MPFR_LIMB_HIGHBIT);
MPFR_TMP_FREE(marker);
return 0;
}
else if (d == 1)
{
/* | <-- b -->
| <-- c --> */
mp_limb_t c0, mask;
MPFR_UNSIGNED_MINUS_MODULO(sh, p);
/* If we lose at least one bit, compute 2*b-c (Exact)
* else compute b-c/2 */
limb = bp[k] - cp[k]/2;
/* Let W = 2^GMP_NUMB_BITS:
we have |b|-|c| >= limb*W^k - (2*W^k-1)/2 >= limb*W^k - W^k + 1/2
thus if limb > W/2, |b|-|c| >= 1/2*W^n.
Moreover if trunc(|c|) represents the first p-1 bits of |c|,
minus the last significant bit called c0 below (in fact c0 is that
bit shifted by sh bits), then we have
|b|-trunc(|c|) >= 1/2*W^n+1, thus the two mpn_sub_n calls
below necessarily yield a > 1/2*W^n. */
if (limb > MPFR_LIMB_HIGHBIT) /* case limb > W/2 */
{
mp_limb_t *tp;
/* The exponent cannot decrease: compute b-c/2 */
/* Shift c in the allocated temporary block */
SubD1NoLose:
c0 = cp[0] & (MPFR_LIMB_ONE << sh);
mask = ~MPFR_LIMB_MASK(sh);
tp = MPFR_TMP_LIMBS_ALLOC (n);
/* FIXME: it might be faster to have one function shifting c by 1
to the right and adding with b to a, which would read c once
only, and avoid a temporary allocation. */
mpn_rshift (tp, cp, n, 1);
tp[0] &= mask; /* Zero last bit of c if set */
mpn_sub_n (ap, bp, tp, n);
MPFR_SET_EXP(a, bx); /* No exponent overflow! */
MPFR_ASSERTD(ap[n-1] & MPFR_LIMB_HIGHBIT);
if (MPFR_LIKELY(c0 == 0))
{
/* Result is exact: no need of rounding! */
MPFR_TMP_FREE(marker);
return 0;
}
/* c0 is non-zero, thus we have to subtract 1/2*ulp(a),
however, we know (see analysis above) that this cannot
make the exponent decrease */
MPFR_ASSERTD( !(ap[0] & ~mask) ); /* Check last bits */
/* No normalize is needed */
/* Rounding is necessary since c0 is non-zero */
/* we have to subtract 1 at the round bit position,
and 0 for the lower bits */
rb = 1; rbb = sbb = 0;
}
else if (MPFR_LIKELY(limb < MPFR_LIMB_HIGHBIT))
{
mp_limb_t *tp;
/* |b| - |c| <= (W/2-1)*W^k + W^k-1 = 1/2*W^n - 1 */
/* The exponent decreases by one. */
SubD1Lose:
/* Compute 2*b-c (Exact) */
#if defined(WANT_GMP_INTERNALS) && defined(HAVE___GMPN_RSBLSH1_N)
/* {ap, n} = 2*{bp, n} - {cp, n} */
/* mpn_rsblsh1_n -- rp[] = (vp[] << 1) - up[] */
__gmpn_rsblsh1_n (ap, cp, bp, n);
#else
tp = MPFR_TMP_LIMBS_ALLOC (n);
/* Shift b in the allocated temporary block */
mpn_lshift (tp, bp, n, 1);
mpn_sub_n (ap, tp, cp, n);
#endif
bx--;
MPFR_ASSERTD(k == n-1);
goto ExactNormalize;
}
else /* limb = MPFR_LIMB_HIGHBIT */
{
/* Case: limb = 100000000000 */
/* Check while b[l] == c'[l] (C' is C shifted by 1) */
/* If b[l]<c'[l] => We lose at least one bit */
/* If b[l]>c'[l] => We don't lose any bit */
/* If l==-1 => We don't lose any bit
AND the result is 100000000000 0000000000 00000000000 */
mp_size_t l = n - 1;
mp_limb_t cl_shifted;
do
{
/* the first loop will compare b[n-2] and c'[n-2] */
cl_shifted = cp[l] << (GMP_NUMB_BITS - 1);
if (--l < 0)
break;
cl_shifted += cp[l] >> 1;
}
while (bp[l] == cl_shifted);
if (MPFR_UNLIKELY(l < 0))
{
if (MPFR_UNLIKELY(cl_shifted))
{
/* Since cl_shifted is what should be subtracted
from ap[-1], if non-zero then necessarily the
precision is a multiple of GMP_NUMB_BITS, and we lose
one bit, thus the (exact) result is a power of 2
minus 1. */
memset (ap, -1, n * MPFR_BYTES_PER_MP_LIMB);
MPFR_SET_EXP (a, bx - 1);
/* No underflow is possible since cx = bx-1 is a valid
exponent. */
}
else
{
/* cl_shifted=0: result is a power of 2. */
MPN_ZERO (ap, n - 1);
ap[n-1] = MPFR_LIMB_HIGHBIT;
MPFR_SET_EXP (a, bx); /* No exponent overflow! */
}
/* No Normalize is needed */
/* No Rounding is needed */
MPFR_TMP_FREE (marker);
return 0;
}
/* cl_shifted is the shifted value c'[l] */
else if (bp[l] > cl_shifted)
goto SubD1NoLose; /* |b|-|c| >= 1/2*W^n */
else
{
/* we cannot have bp[l] = cl_shifted since the only way we
can exit the while loop above is when bp[l] <> cl_shifted
or l < 0, and the case l < 0 was already treated above. */
MPFR_ASSERTD(bp[l] < cl_shifted);
goto SubD1Lose; /* |b|-|c| <= 1/2*W^n-1 and is exact */
}
}
}
else if (MPFR_UNLIKELY(d >= p)) /* the difference of exponents is larger
than the precision of all operands, thus
the result is either b or b - 1 ulp,
with a possible exact result when
d = p, b = 2^e and c = 1/2 ulp(b) */
{
MPFR_UNSIGNED_MINUS_MODULO(sh, p);
/* We can't set A before since we use cp for rounding... */
/* Perform rounding: check if a=b or a=b-ulp(b) */
if (MPFR_UNLIKELY(d == p))
{
/* since c is normalized, we need to subtract 1/2 ulp(b) */
rb = 1;
/* rbb is the bit of weight 1/4 ulp(b) in c. We assume a limb has
at least 2 bits. If the precision is 1, we read in the unused
bits, which should be zero, and this is what we want. */
rbb = cp[n-1] & (MPFR_LIMB_HIGHBIT >> 1);
/* We need also sbb */
sbb = cp[n-1] & MPFR_LIMB_MASK(GMP_NUMB_BITS - 2);
for (k = n-1; sbb == 0 && k > 0;)
sbb = cp[--k];
}
else
{
rb = 0;
if (d == p + 1)
{
rbb = 1;
sbb = cp[n-1] & MPFR_LIMB_MASK(GMP_NUMB_BITS - 1);
for (k = n-1; sbb == 0 && k > 0;)
sbb = cp[--k];
}
else
{
rbb = 0;
sbb = 1; /* since C is non-zero */
}
}
/* Copy mantissa B in A */
MPN_COPY(ap, bp, n);
}
else /* case 2 <= d < p */
{
mpfr_uexp_t dm;
mp_size_t m;
mp_limb_t mask, *tp;
MPFR_UNSIGNED_MINUS_MODULO(sh, p);
tp = MPFR_TMP_LIMBS_ALLOC (n);
/* Shift c in temporary allocated place */
dm = d % GMP_NUMB_BITS;
m = d / GMP_NUMB_BITS;
if (MPFR_UNLIKELY(dm == 0))
{
/* dm = 0 and m > 0: Just copy */
MPFR_ASSERTD(m != 0);
MPN_COPY(tp, cp + m, n - m);
MPN_ZERO(tp + n - m, m);
}
else if (MPFR_LIKELY(m == 0))
{
/* dm >=2 and m == 0: just shift */
MPFR_ASSERTD(dm >= 2);
mpn_rshift (tp, cp, n, dm);
}
else
{
/* dm > 0 and m > 0: shift and zero */
mpn_rshift (tp, cp + m, n - m, dm);
MPN_ZERO (tp + n - m, m);
}
/* FIXME: Instead of doing "cp = tp;", keep using tp to avoid
confusion? Thus in the block below, we don't need
"mp_limb_t *cp = MPFR_MANT(c);". In short, cp should always
be MPFR_MANT(c) defined earlier, possibly after the swap. */
cp = tp;
/* mpfr_print_mant_binary("Before", MPFR_MANT(c), p); */
/* mpfr_print_mant_binary("B= ", MPFR_MANT(b), p); */
/* mpfr_print_mant_binary("After ", cp, p); */
/* Compute rb=Cp and sb=C'p+1 */
{
/* Compute rb and rbb from C */
mp_limb_t *cp = MPFR_MANT(c);
/* The round bit is bit p-d in C, assuming the most significant bit
of C is bit 0 */
mpfr_prec_t x = p - d;
mp_size_t kx = n - 1 - (x / GMP_NUMB_BITS);
mpfr_prec_t sx = GMP_NUMB_BITS - 1 - (x % GMP_NUMB_BITS);
/* the round bit is in cp[kx], at position sx */
MPFR_ASSERTD(p >= d);
rb = cp[kx] & (MPFR_LIMB_ONE << sx);
/* Now compute rbb: since d >= 2 it always exists in C */
if (sx == 0) /* rbb is in the next limb */
{
kx --;
sx = GMP_NUMB_BITS - 1;
}
else
sx --; /* rb and rbb are in the same limb */
rbb = cp[kx] & (MPFR_LIMB_ONE << sx);
/* Now look at the remaining low bits of C to determine sbb */
sbb = cp[kx] & MPFR_LIMB_MASK(sx);
while (sbb == 0 && kx > 0)
sbb = cp[--kx];
}
/* printf("sh=%lu Cp=%d C'p+1=%d\n", sh, rb!=0, sb!=0); */
/* Clean shifted C' */
mask = ~MPFR_LIMB_MASK (sh);
cp[0] &= mask;
/* Subtract the mantissa c from b in a */
mpn_sub_n (ap, bp, cp, n);
/* mpfr_print_mant_binary("Sub= ", ap, p); */
/* Normalize: we lose at most one bit */
if (MPFR_UNLIKELY(MPFR_LIMB_MSB(ap[n-1]) == 0))
{
/* High bit is not set and we have to fix it! */
/* Ap >= 010000xxx001 */
mpn_lshift (ap, ap, n, 1);
/* Ap >= 100000xxx010 */
if (MPFR_UNLIKELY(rb != 0)) /* Check if Cp = -1 */
/* Since Cp == -1, we have to subtract one more */
{
mpn_sub_1 (ap, ap, n, MPFR_LIMB_ONE << sh);
MPFR_ASSERTD(MPFR_LIMB_MSB(ap[n-1]) != 0);
}
/* Ap >= 10000xxx001 */
/* Final exponent -1 since we have shifted the mantissa */
bx--;
/* Update rb and sb */
rb = rbb;
rbb = sbb;
/* We don't have anymore a valid Cp+1!
But since Ap >= 100000xxx001, the final sub can't unnormalize!*/
}
MPFR_ASSERTD( !(ap[0] & ~mask) );
}
rounding:
/* at this point 'a' contains b - high(c), normalized,
and we have to subtract rb * 1/2 ulp(a), rbb * 1/4 ulp(a),
and sbb * 1/8 ulp(a), interpreting rb/rbb/sbb as 1 if non-zero. */
sb = rbb | sbb;
if (rb == 0 && sb == 0)
{
inexact = 0;
goto end_of_sub;
}
pow2 = mpfr_powerof2_raw (a);
if (pow2 && rb != 0) /* subtract 1 ulp */
{
mpn_sub_1 (ap, ap, n, MPFR_LIMB_ONE << sh);
ap[n-1] |= MPFR_LIMB_HIGHBIT;
bx--;
rb = rbb;
rbb = sbb;
sbb = 0;
/* Note: for p=1 this case can only happen with d=1, but then we will
have rb=sb=0 at the next round. */
goto rounding;
}
/* now if a is a power of two, necessary rb = 0,
which means the exact result is always in (pred(a), a),
and the bounds cannot be attained */
if (rnd_mode == MPFR_RNDF)
inexact = 0;
else if (rnd_mode == MPFR_RNDN)
{
if (pow2)
{
MPFR_ASSERTD(rb == 0);
/* since we have at the end of the binade, we have in fact rb = rbb
and sb = sbb */
rb = rbb;
sb = sbb;
}
/* Warning: for p=1, the significand is always odd: the "even" rule
rounds to the value with largest magnitude, thus we have to check
that case separately */
if (rb == 0 ||
(rb != 0 && sb == 0 &&
((ap[0] & (MPFR_LIMB_ONE << sh)) == 0 || p == 1)))
inexact = 1; /* round to a and return 1 */
else /* round to pred(a) and return -1 */
{
subtract:
mpn_sub_1 (ap, ap, n, MPFR_LIMB_ONE << sh);
if (pow2) /* deal with cancellation */
{
ap[n-1] |= MPFR_LIMB_HIGHBIT;
bx--;
}
inexact = -1;
}
}
else /* directed rounding */
{
MPFR_UPDATE_RND_MODE(rnd_mode, MPFR_IS_NEG(a));
if (rnd_mode == MPFR_RNDZ)
goto subtract;
else
inexact = 1;
}
end_of_sub:
/* Update Exponent */
/* bx >= emin. Proof:
If d==0 : Exact case. This is never called.
if 1 < d < p : bx=MPFR_EXP(b) or MPFR_EXP(b)-1 > MPFR_EXP(c) > emin
if d == 1 : bx=MPFR_EXP(b). If we could lose any bits, the exact
normalization is called.
if d >= p : bx=MPFR_EXP(b) >= MPFR_EXP(c) + p > emin
After SubOneUlp, we could have one bit less.
if 1 < d < p : bx >= MPFR_EXP(b)-2 >= MPFR_EXP(c) > emin
if d == 1 : bx >= MPFR_EXP(b)-1 = MPFR_EXP(c) > emin.
if d >= p : bx >= MPFR_EXP(b)-1 > emin since p>=2.
*/
MPFR_ASSERTD( bx >= __gmpfr_emin);
MPFR_SET_EXP (a, bx);
MPFR_TMP_FREE(marker);
MPFR_RET (inexact * MPFR_INT_SIGN (a));
}
|