aboutsummaryrefslogtreecommitdiff
path: root/libgcc/config/libbid/bid128_mul.c
blob: f5fe5e86871b72d435dfa5b0c73325acf10d4394 (plain)
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
/* Copyright (C) 2007  Free Software Foundation, Inc.

This file is part of GCC.

GCC is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 2, or (at your option) any later
version.

In addition to the permissions in the GNU General Public License, the
Free Software Foundation gives you unlimited permission to link the
compiled version of this file into combinations with other programs,
and to distribute those combinations without any restriction coming
from the use of this file.  (The General Public License restrictions
do apply in other respects; for example, they cover modification of
the file, and distribution when not linked into a combine
executable.)

GCC 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 General Public License
along with GCC; see the file COPYING.  If not, write to the Free
Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.  */

#include "bid_internal.h"

#if DECIMAL_CALL_BY_REFERENCE
void
__bid128_mul (UINT128 * pres, UINT128 * px,
            UINT128 *
            py _RND_MODE_PARAM _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
            _EXC_INFO_PARAM) {
  UINT128 x = *px, y = *py;

#if !DECIMAL_GLOBAL_ROUNDING
  unsigned int rnd_mode = *prnd_mode;

#endif
#else
UINT128
__bid128_mul (UINT128 x,
            UINT128 y _RND_MODE_PARAM _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
            _EXC_INFO_PARAM) {

#endif
  UINT128 res;
  UINT64 x_sign, y_sign, sign;
  UINT64 x_exp, y_exp;

  // Note: C1.w[1], C1.w[0] represent x_signif_hi, x_signif_lo (all are UINT64)
  // Note: C2.w[1], C2.w[0] represent y_signif_hi, y_signif_lo (all are UINT64)
  UINT64 tmp64, tmp64A;
  BID_UI64DOUBLE tmp1, tmp2;
  int x_nr_bits, y_nr_bits;
  int q1, q2, ind, shift;
  UINT128 C1, C2;
  UINT128 Cstar;  // C* represents up to 34 decimal digits ~ 113 bits
  UINT384 fstar;
  int q;
  UINT128 P128, R128; // for underflow path
  UINT192 P192, R192; // for underflow path
  UINT256 C, P256, R256;
  UINT384 P384;
  UINT512 P512;
  int incr_exp = 0; // for underflow path
  int incr_exp1 = 0; // for underflow path
  int tmp_fpa = 0;  // if possible underflow and q>=34, use to undo the rounding
  UINT64 C1_hi, C2_hi;
  UINT64 C1_lo, C2_lo;
  int is_inexact = 0;
  int is_midpoint_lt_even = 0, is_midpoint_gt_even = 0;
  int is_inexact_lt_midpoint = 0, is_inexact_gt_midpoint = 0;
  int is_midpoint_lt_even1 = 0, is_midpoint_gt_even1 = 0;
  int is_inexact_lt_midpoint1 = 0, is_inexact_gt_midpoint1 = 0;
  int is_overflow = 0;
  int no_underflow = 0;

  // unpack the arguments
  // unpack x
  x_sign = x.w[1] & MASK_SIGN; // 0 for positive, MASK_SIGN for negative
  x_exp = x.w[1] & MASK_EXP; // biased and shifted left 49 bit positions
  C1_hi = x.w[1] & MASK_COEFF;
  C1_lo = x.w[0];

  // unpack y
  y_sign = y.w[1] & MASK_SIGN; // 0 for positive, MASK_SIGN for negative
  y_exp = y.w[1] & MASK_EXP; // biased and shifted left 49 bit positions
  C2_hi = y.w[1] & MASK_COEFF;
  C2_lo = y.w[0];
  sign = x_sign ^ y_sign;

  // check for NaN or Infinity
  if (((x.w[1] & MASK_SPECIAL) == MASK_SPECIAL)
      || ((y.w[1] & MASK_SPECIAL) == MASK_SPECIAL)) {

    // x is special or y is special
    if ((x.w[1] & MASK_NAN) == MASK_NAN) { // x is NAN
      if ((x.w[1] & MASK_SNAN) == MASK_SNAN) { // x is SNAN
        // set invalid flag
        *pfpsf |= INVALID_EXCEPTION;

        // return quiet (x)
        res.w[1] = x.w[1] & 0xfdffffffffffffffull;
        res.w[0] = x.w[0];
      } else { // x is QNaN
        if ((y.w[1] & MASK_SNAN) == MASK_SNAN) { // y is SNAN
          // set invalid flag
          *pfpsf |= INVALID_EXCEPTION;
        }
        // return x
        res.w[1] = x.w[1];
        res.w[0] = x.w[0];
      }
      BID_RETURN (res);
    } else if ((y.w[1] & MASK_NAN) == MASK_NAN) { // y is NAN
      if ((y.w[1] & MASK_SNAN) == MASK_SNAN) { // y is SNAN
        // set invalid flag
        *pfpsf |= INVALID_EXCEPTION;

        // return quiet (y)
        res.w[1] = y.w[1] & 0xfdffffffffffffffull;
        res.w[0] = y.w[0];
      } else { // y is QNaN
        // return y
        res.w[1] = y.w[1];
        res.w[0] = y.w[0];
      }
      BID_RETURN (res);
    } else { // neither x nor y is NaN; at least one is infinity
      if ((x.w[1] & MASK_ANY_INF) == MASK_INF) { // x is infinity
        if (((y.w[1] & MASK_ANY_INF) == MASK_INF) || (C2_hi != 0x0ull)
            || (C2_lo != 0x0ull)) {

          // y is infinity OR y is finite 
          // if same sign, return +inf otherwise return -inf
          if (!sign) {
            res.w[1] = 0x7800000000000000ull; // +inf
            res.w[0] = 0x0000000000000000ull;
          } else { // x and y are infinities of opposite signs
            res.w[1] = 0xf800000000000000ull; // -inf
            res.w[0] = 0x0000000000000000ull;
          }
        } else { // if y is 0
          // set invalid flag
          *pfpsf |= INVALID_EXCEPTION;

          // return QNaN Indefinite
          res.w[1] = 0x7c00000000000000ull;
          res.w[0] = 0x0000000000000000ull;
        }
      } else { // x is not NaN or infinity, so y must be infinity
        if ((C1_hi != 0x0ull) || (C1_lo != 0x0ull)) {

          // x is finite
          // if same sign, return +inf otherwise return -inf
          if (!sign) {
            res.w[1] = 0x7800000000000000ull; // +inf
            res.w[0] = 0x0000000000000000ull;
          } else { // y and x are of opposite signs
            res.w[1] = 0xf800000000000000ull; // -inf
            res.w[0] = 0x0000000000000000ull;
          }
        } else { // if x is 0
          // set invalid flag
          *pfpsf |= INVALID_EXCEPTION;

          // return QNaN Indefinite
          res.w[1] = 0x7c00000000000000ull;
          res.w[0] = 0x0000000000000000ull;
        }
      }
      BID_RETURN (res);
    }
  }
  // test for non-canonical values:
  // - values whose encoding begins with x00, x01, or x10 and whose
  //   coefficient is larger than 10^34 -1, or
  // - values whose encoding begins with x1100, x1101, x1110 (if NaNs
  //   and infinitis were eliminated already this test is reduced to
  //   checking for x10x)

  // test for non-canonical values of the argument x
  if ((((C1_hi > 0x0001ed09bead87c0ull) || 
      ((C1_hi == 0x0001ed09bead87c0ull) && (C1_lo > 0x378d8e63ffffffffull))) && 
      ((x.w[1] & 0x6000000000000000ull) != 0x6000000000000000ull)) || 
      ((x.w[1] & 0x6000000000000000ull) == 0x6000000000000000ull)) {
    // check for the case where the exponent is shifted right by 2 bits!
    if ((x.w[1] & 0x6000000000000000ull) == 0x6000000000000000ull) {
      x_exp = (x.w[1] << 2) & MASK_EXP; // same position as for G[0]G[1] != 11
    }
    x.w[1] = x.w[1] & 0x8000000000000000ull; // preserve the sign bit
    x.w[0] = 0;
    C1_hi = 0;
    C1_lo = 0;
  }
  // test for non-canonical values of the argument y
  if ((((C2_hi > 0x0001ed09bead87c0ull)
       || ((C2_hi == 0x0001ed09bead87c0ull)
           && (C2_lo > 0x378d8e63ffffffffull)))
      && ((y.w[1] & 0x6000000000000000ull) != 0x6000000000000000ull))
      || ((y.w[1] & 0x6000000000000000ull) == 0x6000000000000000ull)) {

    // check for the case where the exponent is shifted right by 2 bits!
    if ((y.w[1] & 0x6000000000000000ull) == 0x6000000000000000ull) {
      y_exp = (y.w[1] << 2) & MASK_EXP; // same position as for G[0]G[1] != 11
    }
    y.w[1] = y.w[1] & 0x8000000000000000ull; // preserve the sign bit
    y.w[0] = 0;
    C2_hi = 0;
    C2_lo = 0;
  }
  if (((C1_hi == 0x0ull) && (C1_lo == 0x0ull)) || ((C2_hi == 0x0ull)
      && (C2_lo == 0x0ull))) {

    // x is 0 and y is not special OR y is 0 and x is not special
    // if same sign, return +0 otherwise return -0
    ind = (x_exp >> 49) + (y_exp >> 49) - 6176;
    if (ind < 0)
      ind = 0;
    if (ind > 0x2fff)
      ind = 0x2fff; // 6111 + 6176
    if ((x.w[1] & MASK_SIGN) == (y.w[1] & MASK_SIGN)) {
      res.w[1] = 0x0000000000000000ull | ((UINT64) ind << 49); // +0.0
      res.w[0] = 0x0000000000000000ull;
    } else { // opposite signs
      res.w[1] = 0x8000000000000000ull | ((UINT64) ind << 49); // -0.0
      res.w[0] = 0x0000000000000000ull;
    }
    BID_RETURN (res);
  } else { // x and y are not special and are not zero
    // unpack x
    C1.w[1] = C1_hi;
    C1.w[0] = C1_lo;

    // q1 = nr. of decimal digits in x
    // determine first the nr. of bits in x
    if (C1.w[1] == 0) {
      if (C1.w[0] >= 0x0020000000000000ull) { // x >= 2^53
        // split the 64-bit value in two 32-bit halves to avoid rounding errors
        if (C1.w[0] >= 0x0000000100000000ull) { // x >= 2^32
          tmp1.d = (double) (C1.w[0] >> 32); // exact conversion
          x_nr_bits =
            33 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
        } else { // x < 2^32
          tmp1.d = (double) (C1.w[0]); // exact conversion
          x_nr_bits =
            1 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
      }} else { // if x < 2^53
        tmp1.d = (double) C1.w[0]; // exact conversion
        x_nr_bits =
          1 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
    }} else { // C1.w[1] != 0 => nr. bits = 64 + nr_bits (C1.w[1])
      tmp1.d = (double) C1.w[1]; // exact conversion
      x_nr_bits =
        65 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
    } q1 = __bid_nr_digits[x_nr_bits - 1].digits;
    if (q1 == 0) {
      q1 = __bid_nr_digits[x_nr_bits - 1].digits1;
      if (C1.w[1] > __bid_nr_digits[x_nr_bits - 1].threshold_hi
          || (C1.w[1] == __bid_nr_digits[x_nr_bits - 1].threshold_hi
          && C1.w[0] >= __bid_nr_digits[x_nr_bits - 1].threshold_lo))
        q1++;
    }
    C2.w[1] = C2_hi;
    C2.w[0] = C2_lo;
    if (C2.w[1] == 0) {
      if (C2.w[0] >= 0x0020000000000000ull) { // y >= 2^53
        // split the 64-bit value in two 32-bit halves to avoid rounding errors
        if (C2.w[0] >= 0x0000000100000000ull) { // y >= 2^32
          tmp2.d = (double) (C2.w[0] >> 32); // exact conversion
          y_nr_bits =
            32 + ((((unsigned int) (tmp2.ui64 >> 52)) & 0x7ff) - 0x3ff);
        } else { // y < 2^32
          tmp2.d = (double) C2.w[0]; // exact conversion
          y_nr_bits =
            ((((unsigned int) (tmp2.ui64 >> 52)) & 0x7ff) - 0x3ff);
      }} else { // if y < 2^53
        tmp2.d = (double) C2.w[0]; // exact conversion
        y_nr_bits =
          ((((unsigned int) (tmp2.ui64 >> 52)) & 0x7ff) - 0x3ff);
    }} else { // C2.w[1] != 0 => nr. bits = 64 + nr_bits (C2.w[1])
      tmp2.d = (double) C2.w[1]; // exact conversion
      y_nr_bits =
        64 + ((((unsigned int) (tmp2.ui64 >> 52)) & 0x7ff) - 0x3ff);
    } q2 = __bid_nr_digits[y_nr_bits].digits;
    if (q2 == 0) {
      q2 = __bid_nr_digits[y_nr_bits].digits1;
      if (C2.w[1] > __bid_nr_digits[y_nr_bits].threshold_hi
          || (C2.w[1] == __bid_nr_digits[y_nr_bits].threshold_hi
          && C2.w[0] >= __bid_nr_digits[y_nr_bits].threshold_lo))
        q2++;
    }
    // the exact product has either q1 + q2 - 1 or q1 + q2 decimal digits
    // where 2 <= q1 + q2 <= 68
    // calculate C' = C1 * C2 and determine q
    C.w[3] = C.w[2] = C.w[1] = C.w[0] = 0;
    if (q1 + q2 <= 19) { // if 2 <= q1 + q2 <= 19, C' = C1 * C2 fits in 64 bits
      C.w[0] = C1.w[0] * C2.w[0];

      // if C' < 10^(q1+q2-1) then q = q1 + q2 - 1 else q = q1 + q2
      if (C.w[0] < __bid_ten2k64[q1 + q2 - 1])
        q = q1 + q2 - 1; // q in [1, 18]
      else
        q = q1 + q2; // q in [2, 19]
      // length of C1 * C2 rounded up to a multiple of 64 bits is len = 64;
    } else if (q1 + q2 == 20) { // C' = C1 * C2 fits in 64 or 128 bits
      // q1 <= 19 and q2 <= 19 so both C1 and C2 fit in 64 bits
      __mul_64x64_to_128MACH (C, C1.w[0], C2.w[0]);

      // if C' < 10^(q1+q2-1) = 10^19 then q = q1+q2-1 = 19 else q = q1+q2 = 20
      if (C.w[1] == 0 && C.w[0] < __bid_ten2k64[19]) { // 19 = q1+q2-1
        // length of C1 * C2 rounded up to a multiple of 64 bits is len = 64;
        q = 19; // 19 = q1 + q2 - 1
      } else {

        // if (C.w[1] == 0)
        //   length of C1 * C2 rounded up to a multiple of 64 bits is len = 64;
        // else
        //   length of C1 * C2 rounded up to a multiple of 64 bits is len = 128;
        q = 20; // 20 = q1 + q2
      }
    } else if (q1 + q2 <= 38) { // 21 <= q1 + q2 <= 38
      // C' = C1 * C2 fits in 64 or 128 bits
      // (64 bits possibly, but only when q1 + q2 = 21 and C' has 20 digits)
      // at least one of C1, C2 has at most 19 decimal digits & fits in 64 bits
      if (q1 <= 19) {
        __mul_128x64_to_128 (C, C1.w[0], C2);
      } else { // q2 <= 19
        __mul_128x64_to_128 (C, C2.w[0], C1);
      }

      // if C' < 10^(q1+q2-1) then q = q1 + q2 - 1 else q = q1 + q2
      if (C.w[1] < __bid_ten2k128[q1 + q2 - 21].w[1]
          || (C.w[1] == __bid_ten2k128[q1 + q2 - 21].w[1]
          && C.w[0] < __bid_ten2k128[q1 + q2 - 21].w[0])) {

        // if (C.w[1] == 0) // q = 20, necessarily
        //   length of C1 * C2 rounded up to a multiple of 64 bits is len = 64;
        // else
        //   length of C1 * C2 rounded up to a multiple of 64 bits is len = 128;
        q = q1 + q2 - 1; // q in [20, 37]
      } else {

        // length of C1 * C2 rounded up to a multiple of 64 bits is len = 128;
        q = q1 + q2; // q in [21, 38]
      }
    } else if (q1 + q2 == 39) { // C' = C1 * C2 fits in 128 or 192 bits
      // both C1 and C2 fit in 128 bits (actually in 113 bits)
      // may replace this by 128x128_to192
      __mul_128x128_to_256 (C, C1, C2); // C.w[3] is 0
      // if C' < 10^(q1+q2-1) = 10^38 then q = q1+q2-1 = 38 else q = q1+q2 = 39
      if (C.w[2] == 0 && (C.w[1] < __bid_ten2k128[18].w[1] || 
          (C.w[1] == __bid_ten2k128[18].w[1] && C.w[0] < __bid_ten2k128[18].w[0]))) { 
          // 18 = 38 - 20 = q1+q2-1 - 20
        // length of C1 * C2 rounded up to a multiple of 64 bits is len = 128;
        q = 38; // 38 = q1 + q2 - 1
      } else {

        // if (C.w[2] == 0)
        // length of C1 * C2 rounded up to a multiple of 64 bits is len = 128;
        // else
        //   length of C1 * C2 rounded up to a multiple of 64 bits is len = 192;
        q = 39; // 39 = q1 + q2
      }
    } else if (q1 + q2 <= 57) { // 40 <= q1 + q2 <= 57
      // C' = C1 * C2 fits in 128 or 192 bits
      // (128 bits possibly, but only when q1 + q2 = 40 and C' has 39 digits)
      // both C1 and C2 fit in 128 bits (actually in 113 bits); at most one
      // may fit in 64 bits
      if (C1.w[1] == 0) { // C1 fits in 64 bits
        // __mul_64x128_full (REShi64, RESlo128, A64, B128)
        __mul_64x128_full (C.w[2], C, C1.w[0], C2);
      } else if (C2.w[1] == 0) { // C2 fits in 64 bits
        // __mul_64x128_full (REShi64, RESlo128, A64, B128)
        __mul_64x128_full (C.w[2], C, C2.w[0], C1);
      } else { // both C1 and C2 require 128 bits
        // may use __mul_128x128_to_192 (C.w[2], C.w[0], C2.w[0], C1);
        __mul_128x128_to_256 (C, C1, C2); // C.w[3] = 0
      }

      // if C' < 10^(q1+q2-1) then q = q1 + q2 - 1 else q = q1 + q2
      if (C.w[2] < __bid_ten2k256[q1 + q2 - 40].w[2]
          || (C.w[2] == __bid_ten2k256[q1 + q2 - 40].w[2]
          && (C.w[1] < __bid_ten2k256[q1 + q2 - 40].w[1]
              || (C.w[1] == __bid_ten2k256[q1 + q2 - 40].w[1]
              && C.w[0] < __bid_ten2k256[q1 + q2 - 40].w[0])))) {

        // if (C.w[2] == 0) // q = 39, necessarily
        //   length of C1 * C2 rounded up to a multiple of 64 bits is len = 128;
        // else
        //   length of C1 * C2 rounded up to a multiple of 64 bits is len = 192;
        q = q1 + q2 - 1; // q in [39, 56]
      } else {

        // length of C1 * C2 rounded up to a multiple of 64 bits is len = 192;
        q = q1 + q2; // q in [40, 57]
      }
    } else if (q1 + q2 == 58) { // C' = C1 * C2 fits in 192 or 256 bits
      // both C1 and C2 fit in 128 bits (actually in 113 bits); at most one
      // may fit in 64 bits
      if (C1.w[1] == 0) { // C1 * C2 will fit in 192 bits
        __mul_64x128_full (C.w[2], C, C1.w[0], C2); // may use 64x128_to_192
      } else if (C2.w[1] == 0) { // C1 * C2 will fit in 192 bits
        __mul_64x128_full (C.w[2], C, C2.w[0], C1); // may use 64x128_to_192
      } else { // C1 * C2 will fit in 192 bits or in 256 bits
        __mul_128x128_to_256 (C, C1, C2);
      }

      // if C' < 10^(q1+q2-1) = 10^57 then q = q1+q2-1 = 57 else q = q1+q2 = 58
      if (C.w[3] == 0 && (C.w[2] < __bid_ten2k256[18].w[2] || 
          (C.w[2] == __bid_ten2k256[18].w[2] && (C.w[1] < __bid_ten2k256[18].w[1] || 
          (C.w[1] == __bid_ten2k256[18].w[1] && C.w[0] < __bid_ten2k256[18].w[0]))))) {
          // 18 = 57 - 39 = q1+q2-1 - 39
        // length of C1 * C2 rounded up to a multiple of 64 bits is len = 192;
        q = 57; // 57 = q1 + q2 - 1
      } else {

        // if (C.w[3] == 0)
        //   length of C1 * C2 rounded up to a multiple of 64 bits is len = 192;
        // else
        //   length of C1 * C2 rounded up to a multiple of 64 bits is len = 256;
        q = 58; // 58 = q1 + q2
      }
    } else { // if 59 <= q1 + q2 <= 68
      // C' = C1 * C2 fits in 192 or 256 bits
      // (192 bits possibly, but only when q1 + q2 = 59 and C' has 58 digits)
      // both C1 and C2 fit in 128 bits (actually in 113 bits); none fits in
      // 64 bits
      // may use __mul_128x128_to_192 (C.w[2], C.w[0], C2.w[0], C1);
      __mul_128x128_to_256 (C, C1, C2); // C.w[3] = 0
      // if C' < 10^(q1+q2-1) then q = q1 + q2 - 1 else q = q1 + q2
      if (C.w[3] < __bid_ten2k256[q1 + q2 - 40].w[3]
          || (C.w[3] == __bid_ten2k256[q1 + q2 - 40].w[3]
          && (C.w[2] < __bid_ten2k256[q1 + q2 - 40].w[2]
              || (C.w[2] == __bid_ten2k256[q1 + q2 - 40].w[2]
              && (C.w[1] < __bid_ten2k256[q1 + q2 - 40].w[1]
                  || (C.w[1] == __bid_ten2k256[q1 + q2 - 40].w[1]
                  && C.w[0] < __bid_ten2k256[q1 + q2 - 40].w[0])))))) {

        // if (C.w[3] == 0) // q = 58, necessarily
        //   length of C1 * C2 rounded up to a multiple of 64 bits is len = 192;
        // else
        //   length of C1 * C2 rounded up to a multiple of 64 bits is len = 256;
        q = q1 + q2 - 1; // q in [58, 67]
      } else {

        // length of C1 * C2 rounded up to a multiple of 64 bits is len = 256;
        q = q1 + q2; // q in [59, 68]
      }
    }
    if (((UINT64) q << 49) + x_exp + y_exp <
        ((UINT64) P34 << 49) + EXP_MIN + BIN_EXP_BIAS) {

      // possible underflow
      // q + ex + ey < P34 + EMIN <=> q - P34 < EMIN - ex - ey <=> q - P34 < ind
      goto _underflow_path;
    }
    if (q <= 34) { // 2 <= q <= 34 the result is exact, and fits in 113 bits
      tmp64 = x_exp + y_exp;
      if (tmp64 > EXP_MAX + BIN_EXP_BIAS) { // possible overflow
        ind = (tmp64 - EXP_MAX - BIN_EXP_BIAS) >> 49;
        if (ind > 34 - q) { // overflow in all rounding modes
          // |res| >= 10^p * 10^emax = 10^(p-1) * 10^(emax+1)
          // assemble the result
          if (rnd_mode == ROUNDING_TO_NEAREST
              || rnd_mode == ROUNDING_TIES_AWAY) {
            res.w[1] = sign | 0x7800000000000000ull;
            res.w[0] = 0x0ull;
          } else if (rnd_mode == ROUNDING_DOWN) {
            if (sign) { // res = -inf
              res.w[1] = 0xf800000000000000ull;
              res.w[0] = 0x0ull;
            } else { // res = +MAXFP
              res.w[1] = 0x5fffed09bead87c0ull;
              res.w[0] = 0x378d8e63ffffffffull;
            }
          } else if (rnd_mode == ROUNDING_UP) {
            if (sign) { // res = -MAXFP
              res.w[1] = 0xdfffed09bead87c0ull;
              res.w[0] = 0x378d8e63ffffffffull;
            } else { // res = +inf
              res.w[1] = 0x7800000000000000ull;
              res.w[0] = 0x0ull;
            }
          } else { // if (rnd_mode == ROUNDING_TO_ZERO)
            // |res| = (10^34 - 1) * 10^6111 = +MAXFP
            res.w[1] = sign | 0x5fffed09bead87c0ull;
            res.w[0] = 0x378d8e63ffffffffull;
          }

          // set the inexact flag
          *pfpsf |= INEXACT_EXCEPTION;

          // set the overflow flag
          *pfpsf |= OVERFLOW_EXCEPTION;

          // is_overflow = 1;
          BID_RETURN (res);
        } else { // tmp64 > EXP_MAX + BIN_EXP_BIAS but 
          // ind = ((tmp64-EXP_MAX-BIN_EXP_BIAS)>>49) <= 34 - q
          // the exponent will be the maximum exponent
          // multiply C by 10^ind; the result fits in 34 digits
          if (ind <= 19) { // multiply by __bid_ten2k64[ind]
            if (q <= 19) { // 64x64 -> 128
              __mul_64x64_to_128MACH (C, C.w[0], __bid_ten2k64[ind]);
            } else { // 128 x 64 -> 128
              // may optimize to multiply 64 x 128 -> 128
              __mul_64x128_full (tmp64, C, __bid_ten2k64[ind], C);
            }
          } else { // if 20 <= ind <= 32 multiply by __bid_ten2k128[ind - 20]
            // it must be that C.w[1] = 0, as C < 10^14
            // may optimize to multiply 64 x 128 -> 128
            __mul_64x128_full (tmp64, C, C.w[0], __bid_ten2k128[ind - 20]);
          }
          res.w[0] = C.w[0];
          res.w[1] = C.w[1];
          res.w[1] |= EXP_MAX; // EXP MAX
        }
      } else {
        res.w[0] = C.w[0];
        res.w[1] = C.w[1];
        res.w[1] |= (tmp64 - BIN_EXP_BIAS);
      }
      res.w[1] |= sign;
    } else if (q <= 38) { // 35 <= q <= 38; exact coefficient fits in 128 bits
      // C = C + 1/2 * 10^x where the result C fits in 127 bits
      ind = q - 35;
      tmp64 = C.w[0];
      C.w[0] = C.w[0] + __bid_midpoint64[ind];
      if (C.w[0] < tmp64)
        C.w[1]++;

      // x = q - p = q - 34, 1 <= x <= 4
      // kx = 10^(-x) = __bid_ten2mk128M[ind]
      // C* = (C + 1/2 * 10^x) * 10^(-x)
      // the approximation of 10^(-x) was rounded up to 128 bits
      __mul_128x128_to_256 (P256, C, __bid_ten2mk128M[ind]);
      Cstar.w[1] = P256.w[3];
      Cstar.w[0] = P256.w[2];
      fstar.w[2] = Cstar.w[0] & __bid_maskhigh128M[ind]; // fstar.w[3|4|5]=0
      fstar.w[1] = P256.w[1];
      fstar.w[0] = P256.w[0];

      // calculate C* and f*
      // C* is actually floor(C*) in this case
      // C* and f* need shifting and masking, as shown by
      // __bid_shiftright128M[] and __bid_maskhigh128M[]
      // the top Ex bits of 10^(-x) are T* = __bid_ten2mk128truncM[ind], e.g.
      // if x=1, T*=__bid_ten2mk128truncM[0]=0xcccccccccccccccccccccccccccccccc
      // if (0 < f* < 10^(-x)) then the result is a midpoint
      //   if floor(C*) is even then C* = floor(C*) - logical right
      //       shift; C* has p decimal digits, correct by Prop. 1)
      //   else if floor(C*) is odd C* = floor(C*)-1 (logical right
      //       shift; C* has p decimal digits, correct by Pr. 1)
      // else
      //   C* = floor(C*) (logical right shift; C has p decimal digits,
      //       correct by Property 1)
      // n = C* * 10^(e+x)

      // shift right C* by Ex-128 = __bid_shiftright128M[ind]
      shift = __bid_shiftright128M[ind]; // 3 <= shift <= 13
      Cstar.w[0] = (Cstar.w[0] >> shift) | (Cstar.w[1] << (64 - shift));
      Cstar.w[1] = (Cstar.w[1] >> shift);

      // determine inexactness of the rounding of C*
      // if (0 < f* - 1/2 < 10^(-x)) then
      //   the result is exact
      // else // if (f* - 1/2 > T*) then
      //   the result is inexact
      if (fstar.w[2] > __bid_one_half128M[ind]
          || (fstar.w[2] == __bid_one_half128M[ind]
          && (fstar.w[1] || fstar.w[0]))) {

        // f* > 1/2 and the result may be exact
        // Calculate f* - 1/2
        tmp64 = fstar.w[2] - __bid_one_half128M[ind];
        if (tmp64 || fstar.w[1] > __bid_ten2mk128truncM[ind].w[1] || 
            (fstar.w[1] == __bid_ten2mk128truncM[ind].w[1] && 
            fstar.w[0] > __bid_ten2mk128truncM[ind].w[0])) { // f* - 1/2 > 10^(-x)
          // set the inexact flag
          *pfpsf |= INEXACT_EXCEPTION;
          is_inexact_lt_midpoint = 1;
        }        // else the result is exact
      } else { // the result is inexact; f2* <= 1/2
        // set the inexact flag
        *pfpsf |= INEXACT_EXCEPTION;
        tmp_fpa = 1;
        is_inexact_gt_midpoint = 1;
      }

      // check for midpoints (could do this before determining inexactness)
      if ((fstar.w[2] == 0) && (fstar.w[1] || fstar.w[0])
          && (fstar.w[1] < __bid_ten2mk128truncM[ind].w[1]
              || (fstar.w[1] == __bid_ten2mk128truncM[ind].w[1]
              && fstar.w[0] <= __bid_ten2mk128truncM[ind].w[0]))) {

        // the result is a midpoint
        if (Cstar.w[0] & 0x01) { // Cstar.w[0] is odd; MP in [EVEN, ODD]
          // if floor(C*) is odd C = floor(C*) - 1; the result may be 0
          Cstar.w[0]--; // Cstar.w[0] is now even
          if (tmp_fpa == 1)
            tmp_fpa = 0;
          is_midpoint_gt_even = 1;
          is_inexact_lt_midpoint = 0;
          is_inexact_gt_midpoint = 0;
        } else { // else MP in [ODD, EVEN]
          is_midpoint_lt_even = 1;
          is_inexact_lt_midpoint = 0;
          is_inexact_gt_midpoint = 0;
        }
      }
      // check for rounding overflow
      if (Cstar.w[1] == 0x0001ed09bead87c0ull && 
          Cstar.w[0] == 0x378d8e6400000000ull) { // if  Cstar = 10^34
        tmp64 = x_exp + y_exp + ((UINT64) (ind + 2) << 49);
        Cstar.w[1] = 0x0000314dc6448d93ull; // Cstar = 10^33
        Cstar.w[0] = 0x38c15b0a00000000ull;

        // if rounding overflow made the exponent equal to emin, set underflow
        if (tmp64 == EXP_MIN + BIN_EXP_BIAS)
          *pfpsf |= UNDERFLOW_EXCEPTION;
      } else { // 10^33 <= Cstar <= 10^34 - 1
        tmp64 = x_exp + y_exp + ((UINT64) (ind + 1) << 49); // ind+1 = q-34
      }
      if (tmp64 >= EXP_MAX + BIN_EXP_BIAS) { // possibble overflow
        // exp >= emax for the result rounded to nearest even
        if (rnd_mode == ROUNDING_TO_NEAREST
            || rnd_mode == ROUNDING_TIES_AWAY) {
          if (tmp64 > EXP_MAX + BIN_EXP_BIAS) {

            // |res| >= 10^(p-1) * 10^(emax+1) <=> exp >= emax+1
            res.w[1] = sign | 0x7800000000000000ull; // +/-inf
            res.w[0] = 0x0ull;
            *pfpsf |= INEXACT_EXCEPTION; // set the inexact flag
            *pfpsf |= OVERFLOW_EXCEPTION; // set the overflow flag
            is_overflow = 1;
          } else { // not overflow
            res.w[0] = Cstar.w[0];
            res.w[1] = Cstar.w[1];
            res.w[1] |= (tmp64 - BIN_EXP_BIAS);
          }
        } else if (rnd_mode == ROUNDING_DOWN) {
          if (!sign && (tmp64 > EXP_MAX + BIN_EXP_BIAS) && 
              !(tmp64 == EXP_MAX + BIN_EXP_BIAS + EXP_P1 && 
              Cstar.w[1] == 0x0000314dc6448d93ull && 
              Cstar.w[0] == 0x38c15b0a00000000ull && // 10^33 * 10^(emax+1)
              (is_midpoint_lt_even || is_inexact_gt_midpoint))) {

            // res = +MAXFP
            res.w[1] = 0x5fffed09bead87c0ull;
            res.w[0] = 0x378d8e63ffffffffull; // (10^34-1) * 10^emax
            *pfpsf |= INEXACT_EXCEPTION; // set the inexact flag
            *pfpsf |= OVERFLOW_EXCEPTION; // set the overflow flag
            is_overflow = 1;
          } else if (sign && ((tmp64 > EXP_MAX + BIN_EXP_BIAS) || 
              ((tmp64 == EXP_MAX + BIN_EXP_BIAS) && 
              Cstar.w[1] == 0x0001ed09bead87c0ull && 
              Cstar.w[0] == 0x378d8e63ffffffffull && // (10^34-1) * 10^emax
              is_inexact_lt_midpoint))) {
            res.w[1] = 0xf800000000000000ull; // -inf
            res.w[0] = 0x0ull;
            *pfpsf |= INEXACT_EXCEPTION; // set the inexact flag
            *pfpsf |= OVERFLOW_EXCEPTION; // set the overflow flag
            is_overflow = 1;
          } else { // not overflow
            res.w[0] = Cstar.w[0];
            res.w[1] = Cstar.w[1];
            res.w[1] |= (tmp64 - BIN_EXP_BIAS);
          }
        } else if (rnd_mode == ROUNDING_UP) {
          if (sign && (tmp64 > EXP_MAX + BIN_EXP_BIAS) && 
              !(tmp64 == EXP_MAX + BIN_EXP_BIAS + EXP_P1 && 
              Cstar.w[1] == 0x0000314dc6448d93ull && 
              Cstar.w[0] == 0x38c15b0a00000000ull && // 10^33 * 10^(emax+1)
              (is_midpoint_lt_even || is_inexact_gt_midpoint))) {
            // res = -MAXFP
            res.w[1] = 0xdfffed09bead87c0ull;
            res.w[0] = 0x378d8e63ffffffffull; // -(10^34-1) * 10^emax
            *pfpsf |= INEXACT_EXCEPTION; // set the inexact flag
            *pfpsf |= OVERFLOW_EXCEPTION; // set the overflow flag
            is_overflow = 1;
          } else if (!sign && ((tmp64 > EXP_MAX + BIN_EXP_BIAS) || 
              ((tmp64 == EXP_MAX + BIN_EXP_BIAS) && 
              Cstar.w[1] == 0x0001ed09bead87c0ull && 
              Cstar.w[0] == 0x378d8e63ffffffffull && // (10^34-1) * 10^emax
              is_inexact_lt_midpoint))) {
            res.w[1] = 0x7800000000000000ull; // inf
            res.w[0] = 0x0ull;
            *pfpsf |= INEXACT_EXCEPTION; // set the inexact flag
            *pfpsf |= OVERFLOW_EXCEPTION; // set the overflow flag
            is_overflow = 1;
          } else { // not overflow
            res.w[0] = Cstar.w[0];
            res.w[1] = Cstar.w[1];
            res.w[1] |= (tmp64 - BIN_EXP_BIAS);
          }
        } else { // if (rnd_mode == ROUNDING_TO_ZERO)
          if (!sign && (tmp64 > EXP_MAX + BIN_EXP_BIAS) && 
              !(tmp64 == EXP_MAX + BIN_EXP_BIAS + EXP_P1 && 
              Cstar.w[1] == 0x0000314dc6448d93ull && 
              Cstar.w[0] == 0x38c15b0a00000000ull && // 10^33 * 10^(emax+1)
              (is_midpoint_lt_even || is_inexact_gt_midpoint))) {
            // res = +MAXFP
            res.w[1] = 0x5fffed09bead87c0ull;
            res.w[0] = 0x378d8e63ffffffffull; // (10^34-1) * 10^emax
            *pfpsf |= INEXACT_EXCEPTION; // set the inexact flag
            *pfpsf |= OVERFLOW_EXCEPTION; // set the overflow flag
            is_overflow = 1;
          } else if (sign && (tmp64 > EXP_MAX + BIN_EXP_BIAS) && 
              !(tmp64 == EXP_MAX + BIN_EXP_BIAS + EXP_P1 && 
              Cstar.w[1] == 0x0000314dc6448d93ull && 
              Cstar.w[0] == 0x38c15b0a00000000ull && // 10^33 * 10^(emax+1)
              (is_midpoint_lt_even || is_inexact_gt_midpoint))) {
            // res = -MAXFP
            res.w[1] = 0xdfffed09bead87c0ull;
            res.w[0] = 0x378d8e63ffffffffull; // -(10^34-1) * 10^emax
            *pfpsf |= INEXACT_EXCEPTION; // set the inexact flag
            *pfpsf |= OVERFLOW_EXCEPTION; // set the overflow flag
            is_overflow = 1;
          } else { // not overflow
            res.w[0] = Cstar.w[0];
            res.w[1] = Cstar.w[1];
            res.w[1] |= (tmp64 - BIN_EXP_BIAS);
          }
        }
        if (is_overflow) { // return for overflow
          // set the inexact flag
          *pfpsf |= INEXACT_EXCEPTION;

          // set the overflow flag
          *pfpsf |= OVERFLOW_EXCEPTION;

          // is_overflow = 1;
          BID_RETURN (res);
        }
      } else {
        res.w[0] = Cstar.w[0];
        res.w[1] = Cstar.w[1];
        res.w[1] |= (tmp64 - BIN_EXP_BIAS);
      }
      res.w[1] |= sign;
    } else if (q <= 57) { // 39 <= q <= 57; exact coefficient takes 128-192 bits
      // C = C + 1/2 * 10^x where the result C fits in 190 bits
      // (10^57 - 1 + 1/2 * 10^23 can be represented with 190 bits)
      // x = q - p = q - 34, 5 <= x <= 23
      // kx = 10^(-x) = __bid_ten2mk192M[ind]
      // C* = (C + 1/2 * 10^x) * 10^(-x)
      // the approximation of 10^(-x) was rounded up to 192 bits
      ind = q - 39; // 0 <= ind <= 18
      tmp64 = C.w[0];
      tmp64A = C.w[1];

      // Note:
      // if 5 <= x <= 19 <=> 0 <= ind <= 14 then
      //   f* has 256 bits
      // else // if 20 <= x <= 23 <=> 15 <= ind <= 18 then
      //   f* has 320 bits
      if (ind <= 14) { // x - 1 = q - 35 = ind + 4 <= 18 
        // add one 64-bit word
        C.w[0] = C.w[0] + __bid_midpoint64[ind + 4];
        if (C.w[0] < tmp64)
          C.w[1]++;
        if (C.w[1] < tmp64A)
          C.w[2]++;
        __mul_192x192_to_384 (P384, C, __bid_ten2mk192M[ind])
          // calculate C* and f*; C* is actually floor(C*) in this case
          // C* and f* need shifting and masking, as shown by 
          // __bid_shiftright192M[] and __bid_maskhigh192M[]
          // C* has 128 bits; P384.w[5], P384.w[4], P384.w[3] need to be
          // shifted right by Ex-192 = __bid_shiftright192M[ind]
          shift = __bid_shiftright192M[ind]; // 16 <= shift <= 63
        Cstar.w[0] = (P384.w[3] >> shift) | (P384.w[4] << (64 - shift));
        Cstar.w[1] = (P384.w[4] >> shift) | (P384.w[5] << (64 - shift));

        // f* has 256 bits
        fstar.w[3] = P384.w[3] & __bid_maskhigh192M[ind];
        fstar.w[2] = P384.w[2];
        fstar.w[1] = P384.w[1];
        fstar.w[0] = P384.w[0];

        // the top Ex bits of 10^(-x) are T* = __bid_ten2mk192truncM[ind], e.g. 
        // if x=5, T* = __bid_ten2mk192truncM[0] =
        //   0xa7c5ac471b4784230fcf80dc33721d53cddd6e04c0592103
        // if (0 < f* < 10^(-x)) then the result is a midpoint
        //   if floor(C*) is even then C* = floor(C*) - logical right
        //       shift; C* has p decimal digits, correct by Prop. 1)
        //   else if floor(C*) is odd C* = floor(C*)-1 (logical right
        //       shift; C* has p decimal digits, correct by Pr. 1)
        // else
        //   C* = floor(C*) (logical right shift; C has p decimal digits,
        //       correct by Property 1)
        // n = C* * 10^(e+x)

        // determine inexactness of the rounding of C*
        // if (0 < f* - 1/2 < T* ~= 10^(-x)) then
        //   the result is exact
        // else // if (f* - 1/2 >= T*) then
        //   the result is inexact
        if (fstar.w[3] > __bid_one_half192M[ind]
            || (fstar.w[3] == __bid_one_half192M[ind]
            && (fstar.w[2] || fstar.w[1] || fstar.w[0]))) {

          // f* > 1/2 and the result may be exact
          // Calculate f* - 1/2
          tmp64 = fstar.w[3] - __bid_one_half192M[ind];
          if (tmp64 || fstar.w[2] > __bid_ten2mk192truncM[ind].w[2] || 
              (fstar.w[2] == __bid_ten2mk192truncM[ind].w[2] && 
              fstar.w[1] > __bid_ten2mk192truncM[ind].w[1]) || 
              (fstar.w[2] == __bid_ten2mk192truncM[ind].w[2] && 
              fstar.w[1] == __bid_ten2mk192truncM[ind].w[1] && 
              fstar.w[0] > __bid_ten2mk192truncM[ind].w[0])) { // f* - 1/2 > 10^(-x)
            // set the inexact flag
            *pfpsf |= INEXACT_EXCEPTION;
            is_inexact_lt_midpoint = 1;
          }        // else the result is exact
        } else { // the result is inexact; f2* <= 1/2
          // set the inexact flag
          *pfpsf |= INEXACT_EXCEPTION;
          tmp_fpa = 1;
          is_inexact_gt_midpoint = 1;
        }

        // check for midpoints (could do this before determining inexactness)
        if ((fstar.w[3] == 0)
            && (fstar.w[2] || fstar.w[1] || fstar.w[0])
            && (fstar.w[2] < __bid_ten2mk192truncM[ind].w[2]
                || (fstar.w[2] == __bid_ten2mk192truncM[ind].w[2]
                && fstar.w[1] < __bid_ten2mk192truncM[ind].w[1])
                || (fstar.w[2] == __bid_ten2mk192truncM[ind].w[2]
                && fstar.w[1] == __bid_ten2mk192truncM[ind].w[1]
                && fstar.w[0] <= __bid_ten2mk192truncM[ind].w[0]))) {

          // the result is a midpoint
          if (Cstar.w[0] & 0x01) { // Cstar.w[0] is odd; MP in [EVEN, ODD]
            // if floor(C*) is odd C = floor(C*) - 1; the result may be 0
            Cstar.w[0]--; // Cstar.w[0] is now even
            if (tmp_fpa == 1)
              tmp_fpa = 0;
            is_midpoint_gt_even = 1;
            is_inexact_lt_midpoint = 0;
            is_inexact_gt_midpoint = 0;
          } else { // else MP in [ODD, EVEN]
            is_midpoint_lt_even = 1;
            is_inexact_lt_midpoint = 0;
            is_inexact_gt_midpoint = 0;
          }
        }
      } else { // if ind >= 15 <=> x - 1 = q - 35 = ind + 4 >= 19
        // add two 64-bit words
        C.w[0] = C.w[0] + __bid_midpoint128[ind - 15].w[0];
        C.w[1] = C.w[1] + __bid_midpoint128[ind - 15].w[1];
        if (C.w[0] < tmp64)
          C.w[1]++;
        if (C.w[1] < tmp64A)
          C.w[2]++;
        __mul_192x192_to_384 (P384, C, __bid_ten2mk192M[ind])
          // calculate C* and f*; C* is actually floor(C*) in this case
          // C* and f* need shifting and masking, as shown by
          // __bid_shiftright192M[] and __bid_maskhigh192M[]
          // C* has 128 bits; P384.w[5], P384.w[4], need to be
          // shifted right by Ex-256 = __bid_shiftright192M[ind]
          shift = __bid_shiftright192M[ind]; // 2 <= shift <= 12
        Cstar.w[0] = (P384.w[4] >> shift) | (P384.w[5] << (64 - shift));
        Cstar.w[1] = (P384.w[5] >> shift);

        // f* has 320 bits
        fstar.w[4] = P384.w[4] & __bid_maskhigh192M[ind];
        fstar.w[3] = P384.w[3];
        fstar.w[2] = P384.w[2];
        fstar.w[1] = P384.w[1];
        fstar.w[0] = P384.w[0];

        // the top Ex bits of 10^(-x) are T* = __bid_ten2mk192truncM[ind], e.g. 
        // if x=23, T* = __bid_ten2mk192truncM[18] =
        //   0xc16d9a0095928a2775b7053c0f1782938d6f439b43088650
        // if (0 < f* < 10^(-x)) then the result is a midpoint
        //   if floor(C*) is even then C* = floor(C*) - logical right
        //       shift; C* has p decimal digits, correct by Prop. 1)
        //   else if floor(C*) is odd C* = floor(C*)-1 (logical right
        //       shift; C* has p decimal digits, correct by Pr. 1)
        // else
        //   C* = floor(C*) (logical right shift; C has p decimal digits,
        //       correct by Property 1)
        // n = C* * 10^(e+x)

        // determine inexactness of the rounding of C*
        // if (0 < f* - 1/2 < T* ~= 10^(-x)) then
        //   the result is exact
        // else // if (f* - 1/2 >= T*) then
        //   the result is inexact
        if (fstar.w[4] > __bid_one_half192M[ind]
            || (fstar.w[4] == __bid_one_half192M[ind]
            && (fstar.w[3] || fstar.w[2] || fstar.w[1] || fstar.w[0]))) {

          // f* > 1/2 and the result may be exact
          // Calculate f* - 1/2
          tmp64 = fstar.w[4] - __bid_one_half192M[ind];
          if (tmp64 || fstar.w[3] || fstar.w[2] > __bid_ten2mk192truncM[ind].w[2] || 
              (fstar.w[2] == __bid_ten2mk192truncM[ind].w[2] && 
              fstar.w[1] > __bid_ten2mk192truncM[ind].w[1]) || 
              (fstar.w[2] == __bid_ten2mk192truncM[ind].w[2] && 
              fstar.w[1] == __bid_ten2mk192truncM[ind].w[1] && 
              fstar.w[0] > __bid_ten2mk192truncM[ind].w[0])) { // f* - 1/2 > 10^(-x)
            // set the inexact flag
            *pfpsf |= INEXACT_EXCEPTION;
            is_inexact_lt_midpoint = 1;
          } // else the result is exact
        } else { // the result is inexact; f2* <= 1/2
          // set the inexact flag
          *pfpsf |= INEXACT_EXCEPTION;
          tmp_fpa = 1;
          is_inexact_gt_midpoint = 1;
        }

        // check for midpoints (could do this before determining inexactness)
        if ((fstar.w[4] == 0) && (fstar.w[3] == 0)
            && (fstar.w[2] || fstar.w[1] || fstar.w[0])
            && (fstar.w[2] < __bid_ten2mk192truncM[ind].w[2]
                || (fstar.w[2] == __bid_ten2mk192truncM[ind].w[2]
                && fstar.w[1] < __bid_ten2mk192truncM[ind].w[1])
                || (fstar.w[2] == __bid_ten2mk192truncM[ind].w[2]
                && fstar.w[1] == __bid_ten2mk192truncM[ind].w[1]
                && fstar.w[0] <= __bid_ten2mk192truncM[ind].w[0]))) {

          // the result is a midpoint
          if (Cstar.w[0] & 0x01) { // Cstar.w[0] is odd; MP in [EVEN, ODD]
            // if floor(C*) is odd C = floor(C*) - 1; the result may be 0
            Cstar.w[0]--; // Cstar.w[0] is now even
            if (tmp_fpa == 1)
              tmp_fpa = 0;
            is_midpoint_gt_even = 1;
            is_inexact_lt_midpoint = 0;
            is_inexact_gt_midpoint = 0;
          } else { // else MP in [ODD, EVEN]
            is_midpoint_lt_even = 1;
            is_inexact_lt_midpoint = 0;
            is_inexact_gt_midpoint = 0;
          }
        }
      }

      // check for rounding overflow
      if (Cstar.w[1] == 0x0001ed09bead87c0ull && 
          Cstar.w[0] == 0x378d8e6400000000ull) { // if  Cstar = 10^34
        tmp64 = x_exp + y_exp + ((UINT64) (ind + 6) << 49);
        Cstar.w[1] = 0x0000314dc6448d93ull; // Cstar = 10^33
        Cstar.w[0] = 0x38c15b0a00000000ull;
      } else { // 10^33 <= Cstar <= 10^34 - 1
        tmp64 = x_exp + y_exp + ((UINT64) (ind + 5) << 49); // ind+5 = q-34
      }
      if (tmp64 >= EXP_MAX + BIN_EXP_BIAS) { // possibble overflow
        // exp >= emax for the result rounded to nearest even
        if (rnd_mode == ROUNDING_TO_NEAREST
            || rnd_mode == ROUNDING_TIES_AWAY) {
          if (tmp64 > EXP_MAX + BIN_EXP_BIAS) {

            // |res| >= 10^(p-1) * 10^(emax+1) <=> exp >= emax+1
            res.w[1] = sign | 0x7800000000000000ull; // +/-inf
            res.w[0] = 0x0ull;
            *pfpsf |= INEXACT_EXCEPTION; // set the inexact flag
            *pfpsf |= OVERFLOW_EXCEPTION; // set the overflow flag
            is_overflow = 1;
          } else { // not overflow
            res.w[0] = Cstar.w[0];
            res.w[1] = Cstar.w[1];
            res.w[1] |= (tmp64 - BIN_EXP_BIAS);
          }
        } else if (rnd_mode == ROUNDING_DOWN) {
          if (!sign && (tmp64 > EXP_MAX + BIN_EXP_BIAS) && 
              !(tmp64 == EXP_MAX + BIN_EXP_BIAS + EXP_P1 && 
              Cstar.w[1] == 0x0000314dc6448d93ull && 
              Cstar.w[0] == 0x38c15b0a00000000ull && // 10^33 * 10^(emax+1)
              (is_midpoint_lt_even || is_inexact_gt_midpoint))) {
            // res = +MAXFP
            res.w[1] = 0x5fffed09bead87c0ull;
            res.w[0] = 0x378d8e63ffffffffull; // (10^34-1) * 10^emax
            *pfpsf |= INEXACT_EXCEPTION; // set the inexact flag
            *pfpsf |= OVERFLOW_EXCEPTION; // set the overflow flag
            is_overflow = 1;
          } else if (sign && ((tmp64 > EXP_MAX + BIN_EXP_BIAS) || 
              ((tmp64 == EXP_MAX + BIN_EXP_BIAS) && 
              Cstar.w[1] == 0x0001ed09bead87c0ull && 
              Cstar.w[0] == 0x378d8e63ffffffffull && // (10^34-1) * 10^emax
              is_inexact_lt_midpoint))) {
            res.w[1] = 0xf800000000000000ull; // -inf
            res.w[0] = 0x0ull;
            *pfpsf |= INEXACT_EXCEPTION; // set the inexact flag
            *pfpsf |= OVERFLOW_EXCEPTION; // set the overflow flag
            is_overflow = 1;
          } else { // not overflow
            res.w[0] = Cstar.w[0];
            res.w[1] = Cstar.w[1];
            res.w[1] |= (tmp64 - BIN_EXP_BIAS);
          }
        } else if (rnd_mode == ROUNDING_UP) {
          if (sign && (tmp64 > EXP_MAX + BIN_EXP_BIAS) && 
              !(tmp64 == EXP_MAX + BIN_EXP_BIAS + EXP_P1 && 
              Cstar.w[1] == 0x0000314dc6448d93ull && 
              Cstar.w[0] == 0x38c15b0a00000000ull && // 10^33 * 10^(emax+1)
              (is_midpoint_lt_even || is_inexact_gt_midpoint))) {
            // res = -MAXFP
            res.w[1] = 0xdfffed09bead87c0ull;
            res.w[0] = 0x378d8e63ffffffffull; // -(10^34-1) * 10^emax
            *pfpsf |= INEXACT_EXCEPTION; // set the inexact flag
            *pfpsf |= OVERFLOW_EXCEPTION; // set the overflow flag
            is_overflow = 1;
          } else if (!sign && ((tmp64 > EXP_MAX + BIN_EXP_BIAS) || 
              ((tmp64 == EXP_MAX + BIN_EXP_BIAS) && 
              Cstar.w[1] == 0x0001ed09bead87c0ull && 
              Cstar.w[0] == 0x378d8e63ffffffffull && // (10^34-1) * 10^emax
              is_inexact_lt_midpoint))) {
            res.w[1] = 0x7800000000000000ull; // inf
            res.w[0] = 0x0ull;
            *pfpsf |= INEXACT_EXCEPTION; // set the inexact flag
            *pfpsf |= OVERFLOW_EXCEPTION; // set the overflow flag
            is_overflow = 1;
          } else { // not overflow
            res.w[0] = Cstar.w[0];
            res.w[1] = Cstar.w[1];
            res.w[1] |= (tmp64 - BIN_EXP_BIAS);
          }
        } else { // if (rnd_mode == ROUNDING_TO_ZERO)
          if (!sign && (tmp64 > EXP_MAX + BIN_EXP_BIAS) && 
              !(tmp64 == EXP_MAX + BIN_EXP_BIAS + EXP_P1 && 
              Cstar.w[1] == 0x0000314dc6448d93ull && 
              Cstar.w[0] == 0x38c15b0a00000000ull && // 10^33 * 10^(emax+1)
              (is_midpoint_lt_even || is_inexact_gt_midpoint))) {
            // res = +MAXFP
            res.w[1] = 0x5fffed09bead87c0ull;
            res.w[0] = 0x378d8e63ffffffffull; // (10^34-1) * 10^emax
            *pfpsf |= INEXACT_EXCEPTION; // set the inexact flag
            *pfpsf |= OVERFLOW_EXCEPTION; // set the overflow flag
            is_overflow = 1;
          } else if (sign && (tmp64 > EXP_MAX + BIN_EXP_BIAS) && 
              !(tmp64 == EXP_MAX + BIN_EXP_BIAS + EXP_P1 && 
              Cstar.w[1] == 0x0000314dc6448d93ull && 
              Cstar.w[0] == 0x38c15b0a00000000ull && // 10^33 * 10^(emax+1)
              (is_midpoint_lt_even || is_inexact_gt_midpoint))) {
            // res = -MAXFP
            res.w[1] = 0xdfffed09bead87c0ull;
            res.w[0] = 0x378d8e63ffffffffull; // -(10^34-1) * 10^emax
            *pfpsf |= INEXACT_EXCEPTION; // set the inexact flag
            *pfpsf |= OVERFLOW_EXCEPTION; // set the overflow flag
            is_overflow = 1;
          } else { // not overflow
            res.w[0] = Cstar.w[0];
            res.w[1] = Cstar.w[1];
            res.w[1] |= (tmp64 - BIN_EXP_BIAS);
          }
        }
        if (is_overflow) { // return for overflow
          // set the inexact flag
          *pfpsf |= INEXACT_EXCEPTION;

          // set the overflow flag
          *pfpsf |= OVERFLOW_EXCEPTION;

          // is_overflow = 1;
        BID_RETURN (res)}
      } else {
        res.w[0] = Cstar.w[0];
        res.w[1] = Cstar.w[1];
        res.w[1] |= (tmp64 - BIN_EXP_BIAS);
      }
      res.w[1] |= sign;
    } else { // if (58 <= q <= 68) exact coefficient takes 192-226 bits
      // C = C + 1/2 * 10^x where the result C fits in 226 bits
      // (10^68 - 1 + 1/2 * 10^34 can be represented with 226 bits)
      // x = q - p = q - 34, 24 <= x <= 34
      // kx = 10^(-x) = __bid_ten2mk256M[ind]
      // C* = (C + 1/2 * 10^x) * 10^(-x)
      // the approximation of 10^(-x) was rounded up to 256 bits
      ind = q - 58; // 0 <= ind <= 10
      tmp64 = C.w[0];
      tmp64A = C.w[1];

      // Note:
      // f* has 384 bits (more than 320 bits)
      // x - 1 = q - 35 = ind + 23
      // add two 64-bit words; e.g. for ind=0 <=> q=58, add 1/2*10^24
      C.w[0] = C.w[0] + __bid_midpoint128[ind + 4].w[0];
      C.w[1] = C.w[1] + __bid_midpoint128[ind + 4].w[1];
      if (C.w[0] < tmp64)
        C.w[1]++;
      if (C.w[1] < tmp64A)
        C.w[2]++;
      if (C.w[2] == 0)
        C.w[3]++;
      __mul_256x256_to_512 (P512, C, __bid_ten2mk256M[ind])
        // calculate C* and f*; C* is actually floor(C*) in this case
        // C* and f* need shifting and masking, as shown by 
        // __bid_shiftright256M[] and __bid_maskhigh256M[]
        // C* has 128 bits; P512.w[7], P512.w[6], P512.w[5] need to be
        // shifted right by Ex-320 = __bid_shiftright256M[ind]
        shift = __bid_shiftright256M[ind]; // 15 <= shift <= 48
      if (shift == 32) {
        Cstar.w[0] =
          ((P512.w[5] >> 31) >> 1) | ((P512.w[6] << 31) << 1);
        Cstar.w[1] =
          ((P512.w[6] >> 31) >> 1) | ((P512.w[7] << 31) << 1);
      } else {
        Cstar.w[0] = (P512.w[5] >> shift) | (P512.w[6] << (64 - shift));
        Cstar.w[1] = (P512.w[6] >> shift) | (P512.w[7] << (64 - shift));
      }
      // f* has 384 bits
      fstar.w[5] = P512.w[5] & __bid_maskhigh256M[ind];
      fstar.w[4] = P512.w[4];
      fstar.w[3] = P512.w[3];
      fstar.w[2] = P512.w[2];
      fstar.w[1] = P512.w[1];
      fstar.w[0] = P512.w[0];

      // the top Ex bits of 10^(-x) are T* = __bid_ten2mk256truncM[ind], e.g. 
      // if x=24, T* = __bid_ten2mk256truncM[0] =
      //   0x9abe14cd44753b52c4926a9672793542d78c3615cf3a050cf23472530ce6e3ec =~
      //   10^(-24) * 2^335
      // if (0 < f* < 10^(-x)) then the result is a midpoint
      //   if floor(C*) is even then C* = floor(C*) - logical right
      //       shift; C* has p decimal digits, correct by Prop. 1)
      //   else if floor(C*) is odd C* = floor(C*)-1 (logical right
      //       shift; C* has p decimal digits, correct by Pr. 1)
      // else
      //   C* = floor(C*) (logical right shift; C has p decimal digits,
      //       correct by Property 1)
      // n = C* * 10^(e+x)

      // determine inexactness of the rounding of C*
      // if (0 < f* - 1/2 < T* ~= 10^(-x)) then
      //   the result is exact
      // else // if (f* - 1/2 >= T*) then
      //   the result is inexact
      if (fstar.w[5] > __bid_one_half256M[ind]
          || (fstar.w[5] == __bid_one_half256M[ind]
          && (fstar.w[4] || fstar.w[3] || fstar.w[2] || fstar.w[1]
              || fstar.w[0]))) {

        // f* > 1/2 and the result may be exact
        // Calculate f* - 1/2
        tmp64 = fstar.w[5] - __bid_one_half256M[ind]; // tmp64 >= 0
        if (tmp64 || fstar.w[4] || fstar.w[3] > __bid_ten2mk256truncM[ind].w[3] || 
            (fstar.w[3] == __bid_ten2mk256truncM[ind].w[3] && 
            fstar.w[2] > __bid_ten2mk256truncM[ind].w[2]) || 
            (fstar.w[3] == __bid_ten2mk256truncM[ind].w[3] && 
            fstar.w[2] == __bid_ten2mk256truncM[ind].w[2] && 
            fstar.w[1] > __bid_ten2mk256truncM[ind].w[1]) || 
            (fstar.w[3] == __bid_ten2mk256truncM[ind].w[3] && 
            fstar.w[2] == __bid_ten2mk256truncM[ind].w[2] && 
            fstar.w[1] == __bid_ten2mk256truncM[ind].w[1] && 
            fstar.w[0] > __bid_ten2mk256truncM[ind].w[0])) { // f* - 1/2 > 10^(-x)
          // set the inexact flag
          *pfpsf |= INEXACT_EXCEPTION;
          is_inexact_lt_midpoint = 1;
        } // else the result is exact
      } else { // the result is inexact; f2* <= 1/2
        // set the inexact flag
        *pfpsf |= INEXACT_EXCEPTION;
        tmp_fpa = 1;
        is_inexact_gt_midpoint = 1;
      }

      // check for midpoints (could do this before determining inexactness)
      if ((fstar.w[5] == 0) && (fstar.w[4] == 0)
          && (fstar.w[3] || fstar.w[2] || fstar.w[1] || fstar.w[0])
          && (fstar.w[3] < __bid_ten2mk256truncM[ind].w[3]
              || (fstar.w[3] == __bid_ten2mk256truncM[ind].w[3]
              && fstar.w[2] < __bid_ten2mk256truncM[ind].w[2])
              || (fstar.w[3] == __bid_ten2mk256truncM[ind].w[3]
              && fstar.w[2] == __bid_ten2mk256truncM[ind].w[2]
              && fstar.w[1] < __bid_ten2mk256truncM[ind].w[1])
              || (fstar.w[3] == __bid_ten2mk256truncM[ind].w[3]
              && fstar.w[2] == __bid_ten2mk256truncM[ind].w[2]
              && fstar.w[1] == __bid_ten2mk256truncM[ind].w[1]
              && fstar.w[0] <= __bid_ten2mk256truncM[ind].w[1]))) {

        // the result is a midpoint
        if (Cstar.w[0] & 0x01) { // Cstar.w[0] is odd; MP in [EVEN, ODD]
          // if floor(C*) is odd C = floor(C*) - 1; the result may be 0
          Cstar.w[0]--; // Cstar.w[0] is now even
          if (tmp_fpa == 1)
            tmp_fpa = 0;
          is_midpoint_gt_even = 1;
          is_inexact_lt_midpoint = 0;
          is_inexact_gt_midpoint = 0;
        } else { // else MP in [ODD, EVEN]
          is_midpoint_lt_even = 1;
          is_inexact_lt_midpoint = 0;
          is_inexact_gt_midpoint = 0;
        }
      }
      // check for rounding overflow
      if (Cstar.w[1] == 0x0001ed09bead87c0ull && 
          Cstar.w[0] == 0x378d8e6400000000ull) { // if  Cstar = 10^34
        tmp64 = x_exp + y_exp + ((UINT64) (ind + 25) << 49);
        Cstar.w[1] = 0x0000314dc6448d93ull; // Cstar = 10^33
        Cstar.w[0] = 0x38c15b0a00000000ull;
      } else { // 10^33 <= Cstar <= 10^34 - 1
        tmp64 = x_exp + y_exp + ((UINT64) (ind + 24) << 49); // ind+24 = q-34
      }
      if (tmp64 >= EXP_MAX + BIN_EXP_BIAS) { // possibble overflow
        // exp >= emax for the result rounded to nearest even
        if (rnd_mode == ROUNDING_TO_NEAREST
            || rnd_mode == ROUNDING_TIES_AWAY) {
          if (tmp64 > EXP_MAX + BIN_EXP_BIAS) {

            // |res| >= 10^(p-1) * 10^(emax+1) <=> exp >= emax+1
            res.w[1] = sign | 0x7800000000000000ull; // +/-inf
            res.w[0] = 0x0ull;
            *pfpsf |= INEXACT_EXCEPTION; // set the inexact flag
            *pfpsf |= OVERFLOW_EXCEPTION; // set the overflow flag
            is_overflow = 1;
          } else { // not overflow
            res.w[0] = Cstar.w[0];
            res.w[1] = Cstar.w[1];
            res.w[1] |= (tmp64 - BIN_EXP_BIAS);
          }
        } else if (rnd_mode == ROUNDING_DOWN) {
          if (!sign && (tmp64 > EXP_MAX + BIN_EXP_BIAS) && 
              !(tmp64 == EXP_MAX + BIN_EXP_BIAS + EXP_P1 && 
              Cstar.w[1] == 0x0000314dc6448d93ull && 
              Cstar.w[0] == 0x38c15b0a00000000ull && // 10^33 * 10^(emax+1)
              (is_midpoint_lt_even || is_inexact_gt_midpoint))) {
            // res = +MAXFP
            res.w[1] = 0x5fffed09bead87c0ull;
            res.w[0] = 0x378d8e63ffffffffull; // (10^34-1) * 10^emax
            *pfpsf |= INEXACT_EXCEPTION; // set the inexact flag
            *pfpsf |= OVERFLOW_EXCEPTION; // set the overflow flag
            is_overflow = 1;
          } else if (sign && ((tmp64 > EXP_MAX + BIN_EXP_BIAS) || 
              ((tmp64 == EXP_MAX + BIN_EXP_BIAS) && 
              Cstar.w[1] == 0x0001ed09bead87c0ull && 
              Cstar.w[0] == 0x378d8e63ffffffffull && // (10^34-1) * 10^emax
              is_inexact_lt_midpoint))) {
            res.w[1] = 0xf800000000000000ull; // -inf
            res.w[0] = 0x0ull;
            *pfpsf |= INEXACT_EXCEPTION; // set the inexact flag
            *pfpsf |= OVERFLOW_EXCEPTION; // set the overflow flag
            is_overflow = 1;
          } else { // not overflow
            res.w[0] = Cstar.w[0];
            res.w[1] = Cstar.w[1];
            res.w[1] |= (tmp64 - BIN_EXP_BIAS);
          }
        } else if (rnd_mode == ROUNDING_UP) {
          if (sign && (tmp64 > EXP_MAX + BIN_EXP_BIAS) && 
              !(tmp64 == EXP_MAX + BIN_EXP_BIAS + EXP_P1 && 
              Cstar.w[1] == 0x0000314dc6448d93ull && 
              Cstar.w[0] == 0x38c15b0a00000000ull && // 10^33 * 10^(emax+1)
              (is_midpoint_lt_even || is_inexact_gt_midpoint))) {
            // res = -MAXFP
            res.w[1] = 0xdfffed09bead87c0ull;
            res.w[0] = 0x378d8e63ffffffffull; // -(10^34-1) * 10^emax
            *pfpsf |= INEXACT_EXCEPTION; // set the inexact flag
            *pfpsf |= OVERFLOW_EXCEPTION; // set the overflow flag
            is_overflow = 1;
          } else if (!sign && ((tmp64 > EXP_MAX + BIN_EXP_BIAS) || 
              ((tmp64 == EXP_MAX + BIN_EXP_BIAS) && 
              Cstar.w[1] == 0x0001ed09bead87c0ull && 
              Cstar.w[0] == 0x378d8e63ffffffffull && // (10^34-1) * 10^emax
              is_inexact_lt_midpoint))) {
            res.w[1] = 0x7800000000000000ull; // inf
            res.w[0] = 0x0ull;
            *pfpsf |= INEXACT_EXCEPTION; // set the inexact flag
            *pfpsf |= OVERFLOW_EXCEPTION; // set the overflow flag
            is_overflow = 1;
          } else { // not overflow
            res.w[0] = Cstar.w[0];
            res.w[1] = Cstar.w[1];
            res.w[1] |= (tmp64 - BIN_EXP_BIAS);
          }
        } else { // if (rnd_mode == ROUNDING_TO_ZERO)
          if (!sign && (tmp64 > EXP_MAX + BIN_EXP_BIAS) && 
              !(tmp64 == EXP_MAX + BIN_EXP_BIAS + EXP_P1 && 
              Cstar.w[1] == 0x0000314dc6448d93ull && 
              Cstar.w[0] == 0x38c15b0a00000000ull && // 10^33 * 10^(emax+1)
              (is_midpoint_lt_even || is_inexact_gt_midpoint))) {
            // res = +MAXFP
            res.w[1] = 0x5fffed09bead87c0ull;
            res.w[0] = 0x378d8e63ffffffffull; // (10^34-1) * 10^emax
            *pfpsf |= INEXACT_EXCEPTION; // set the inexact flag
            *pfpsf |= OVERFLOW_EXCEPTION; // set the overflow flag
            is_overflow = 1;
          } else if (sign && (tmp64 > EXP_MAX + BIN_EXP_BIAS) && 
              !(tmp64 == EXP_MAX + BIN_EXP_BIAS + EXP_P1 && 
              Cstar.w[1] == 0x0000314dc6448d93ull && 
              Cstar.w[0] == 0x38c15b0a00000000ull && // 10^33 * 10^(emax+1)
              (is_midpoint_lt_even || is_inexact_gt_midpoint))) {
            // res = -MAXFP
            res.w[1] = 0xdfffed09bead87c0ull;
            res.w[0] = 0x378d8e63ffffffffull; // -(10^34-1) * 10^emax
            *pfpsf |= INEXACT_EXCEPTION; // set the inexact flag
            *pfpsf |= OVERFLOW_EXCEPTION; // set the overflow flag
            is_overflow = 1;
          } else { // not overflow
            res.w[0] = Cstar.w[0];
            res.w[1] = Cstar.w[1];
            res.w[1] |= (tmp64 - BIN_EXP_BIAS);
          }
        }
        if (is_overflow) { // return for overflow
          // set the inexact flag
          *pfpsf |= INEXACT_EXCEPTION;

          // set the overflow flag
          *pfpsf |= OVERFLOW_EXCEPTION;

          // is_overflow = 1;
          BID_RETURN (res);
        }
      } else {
        res.w[0] = Cstar.w[0];
        res.w[1] = Cstar.w[1];
        res.w[1] |= (tmp64 - BIN_EXP_BIAS);
      }
      res.w[1] |= sign;
    }

    // general correction from RN to RA, RM, RP, RZ
    if (rnd_mode != ROUNDING_TO_NEAREST && !is_overflow) { // overflow is solved
      x_exp = res.w[1] & MASK_EXP; // biased and shifted left 49 bit positions
      C1_hi = res.w[1] & MASK_COEFF;
      C1_lo = res.w[0];
      if ((!sign && ((rnd_mode == ROUNDING_UP && is_inexact_lt_midpoint) || 
          ((rnd_mode == ROUNDING_TIES_AWAY || rnd_mode == ROUNDING_UP) && 
          is_midpoint_gt_even))) || 
          (sign && ((rnd_mode == ROUNDING_DOWN && is_inexact_lt_midpoint) || 
          ((rnd_mode == ROUNDING_TIES_AWAY || rnd_mode == ROUNDING_DOWN) && 
          is_midpoint_gt_even)))) {

        // C1 = C1 + 1
        C1_lo = C1_lo + 1;
        if (C1_lo == 0) { // rounding overflow in the low 64 bits
          C1_hi = C1_hi + 1;
          if (C1_hi == 0x0001ed09bead87c0ull
              && C1_lo == 0x378d8e6400000000ull) {

            // C1 = 10^34 => rounding overflow
            C1_hi = 0x0000314dc6448d93ull;
            C1_lo = 0x38c15b0a00000000ull; // 10^33
            x_exp = x_exp + EXP_P1;
          }
        }
      } else if ((is_midpoint_lt_even || is_inexact_gt_midpoint)
          && ((sign && (rnd_mode == ROUNDING_UP || 
          rnd_mode == ROUNDING_TO_ZERO)) || 
          (!sign && (rnd_mode == ROUNDING_DOWN || 
          rnd_mode == ROUNDING_TO_ZERO)))) {

        // C1 = C1 - 1
        C1_lo = C1_lo - 1;
        if (C1_lo == 0xffffffffffffffffull)
          C1_hi--;

        // check if we crossed into the lower decade
        if (C1_hi == 0x0000314dc6448d93ull && C1_lo == 0x38c15b09ffffffffull) {
          // 10^33 - 1
          C1_hi = 0x0001ed09bead87c0ull; // 10^34 - 1
          C1_lo = 0x378d8e63ffffffffull;
          x_exp = x_exp - EXP_P1; // no underflow (TO CHECK)
        }
      } else {
        ; // exact, the result is already correct
      }

      // assemble the result
      res.w[1] = x_exp | C1_hi;
      res.w[0] = C1_lo;
    }
    res.w[1] |= sign;
    BID_RETURN (res);
  }
_underflow_path:
  // got here because q - P34 < ind where ind = EMIN - ex - ey
  // q is the number of digits in C; ind is the [positive] exponent of the
  // negative power of 10 that must multiply C in order to make the result's
  // exponent equal to e_min - P34 + 1 = -6176
  ind =
    (int) (((SINT64) EXP_MIN + (SINT64) BIN_EXP_BIAS - (SINT64) x_exp -
            (SINT64) y_exp) >> 49);

  // q - P34 < ind => -P34 + 1 < ind => -P34 + 2 <= ind
  // ind = EMIN - ex - ey < -6176 + 6176 + 6176 = 6176
  if (q < ind) { // q - ind < 0; result rounds to 0 when rounding to nearest
    // set the inexact and underflow flags
    *pfpsf |= (INEXACT_EXCEPTION | UNDERFLOW_EXCEPTION);
    res.w[1] = EXP_MIN; // EXP_MIN = 0x0
    res.w[0] = 0x0;
    if (rnd_mode != ROUNDING_TO_NEAREST) {
      if ((rnd_mode == ROUNDING_DOWN && sign) || 
          (rnd_mode == ROUNDING_UP && !sign))
        res.w[0] = 0x0000000000000001ull;
    }
  } else if (q == ind) { // q - ind = 0; result rounds to 0 or +/-1*10^EMIN
    // set the inexact and underflow flags
    *pfpsf |= (INEXACT_EXCEPTION | UNDERFLOW_EXCEPTION);

    // if C <= 5*10^(q-1) then C = 0 else C = 1
    if (q <= 19) {
      if (C.w[0] == __bid_midpoint64[q - 1]) { // C = 0.5 * 10^emin
        if (rnd_mode == ROUNDING_TO_NEAREST || (rnd_mode == ROUNDING_DOWN
            && !sign) || (rnd_mode == ROUNDING_UP && sign)
            || rnd_mode == ROUNDING_TO_ZERO) {
          res.w[1] = EXP_MIN;
          res.w[0] = 0;
        } else {
          res.w[1] = EXP_MIN;
          res.w[0] = 1;
        }
      } else if (C.w[0] < __bid_midpoint64[q - 1]) { // C < 0.5 * 10^emin
        if (rnd_mode == ROUNDING_TO_NEAREST
            || rnd_mode == ROUNDING_TIES_AWAY
            || (rnd_mode == ROUNDING_DOWN && !sign)
            || (rnd_mode == ROUNDING_UP && sign)
            || rnd_mode == ROUNDING_TO_ZERO) {
          res.w[1] = EXP_MIN;
          res.w[0] = 0;
        } else {
          res.w[1] = EXP_MIN;
          res.w[0] = 1;
        }
      } else { // C > 0.5 * 10^emin
        if (rnd_mode == ROUNDING_TO_NEAREST
            || rnd_mode == ROUNDING_TIES_AWAY
            || (rnd_mode == ROUNDING_DOWN && sign)
            || (rnd_mode == ROUNDING_UP && !sign)) {
          res.w[1] = EXP_MIN;
          res.w[0] = 1;
        } else {
          res.w[1] = EXP_MIN;
          res.w[0] = 0;
        }
      }
    } else if (q <= 38) { // 20 <= q <= 38
      // if q <= P34 = 34 the exact result rounded to P34 digits with unbounded 
      // exponent will have an exponent smaller than e_min; otherwise if
      // 35 <= q <= 38, it depends
      if (C.w[1] == __bid_midpoint128[q - 20].w[1] && 
          C.w[0] == __bid_midpoint128[q - 20].w[0]) { // C = 0.5 * 10^emin
        if (rnd_mode == ROUNDING_TO_NEAREST || (rnd_mode == ROUNDING_DOWN
            && !sign) || (rnd_mode == ROUNDING_UP && sign)
            || rnd_mode == ROUNDING_TO_ZERO) {
          res.w[1] = EXP_MIN;
          res.w[0] = 0;
        } else {
          res.w[1] = EXP_MIN;
          res.w[0] = 1;
        }
      } else if (C.w[1] < __bid_midpoint128[q - 20].w[1] || 
          (C.w[1] == __bid_midpoint128[q - 20].w[1] && 
          C.w[0] < __bid_midpoint128[q - 20].w[0])) { // C < 0.5 * 10^emin 
        if (rnd_mode == ROUNDING_TO_NEAREST
            || rnd_mode == ROUNDING_TIES_AWAY
            || (rnd_mode == ROUNDING_DOWN && !sign)
            || (rnd_mode == ROUNDING_UP && sign)
            || rnd_mode == ROUNDING_TO_ZERO) {
          res.w[1] = EXP_MIN;
          res.w[0] = 0;
        } else {
          res.w[1] = EXP_MIN;
          res.w[0] = 1;
        }
      } else { // C > 0.5 * 10^emin
        if (rnd_mode == ROUNDING_TO_NEAREST
            || rnd_mode == ROUNDING_TIES_AWAY
            || (rnd_mode == ROUNDING_DOWN && sign)
            || (rnd_mode == ROUNDING_UP && !sign)) {
          res.w[1] = EXP_MIN;
          res.w[0] = 1;
        } else {
          res.w[1] = EXP_MIN;
          res.w[0] = 0;
        }
      }
    } else if (q <= 58) { // 39 <= q <= 58
      // Note: for q = 58 C may take 256 bits, so need to test C.w[3]
      if (C.w[3] == 0x0 && C.w[2] == __bid_midpoint192[q - 39].w[2] && 
          C.w[1] == __bid_midpoint192[q - 39].w[1] && 
          C.w[0] == __bid_midpoint192[q - 39].w[0]) { // C = 0.5 * 10^emin
        if (rnd_mode == ROUNDING_TO_NEAREST || (rnd_mode == ROUNDING_DOWN
            && !sign) || (rnd_mode == ROUNDING_UP && sign)
            || rnd_mode == ROUNDING_TO_ZERO) {
          res.w[1] = EXP_MIN;
          res.w[0] = 0;
        } else {
          res.w[1] = EXP_MIN;
          res.w[0] = 1;
        }
      } else if ((C.w[3] == 0x0 && C.w[2] < __bid_midpoint192[q - 39].w[2]) || 
          (C.w[3] == 0x0 && C.w[2] == __bid_midpoint192[q - 39].w[2] && 
          C.w[1] < __bid_midpoint192[q - 39].w[1]) || (C.w[3] == 0x0 && 
          C.w[2] == __bid_midpoint192[q - 39].w[2] && 
          C.w[1] == __bid_midpoint192[q - 39].w[1] && 
          C.w[0] < __bid_midpoint192[q - 39].w[0])) { // C < 0.5 * 10^emin
        if (rnd_mode == ROUNDING_TO_NEAREST
            || rnd_mode == ROUNDING_TIES_AWAY
            || (rnd_mode == ROUNDING_DOWN && !sign)
            || (rnd_mode == ROUNDING_UP && sign)
            || rnd_mode == ROUNDING_TO_ZERO) {
          res.w[1] = EXP_MIN;
          res.w[0] = 0;
        } else {
          res.w[1] = EXP_MIN;
          res.w[0] = 1;
        }
      } else { // C > 0.5 * 10^emin
        if (rnd_mode == ROUNDING_TO_NEAREST
            || rnd_mode == ROUNDING_TIES_AWAY
            || (rnd_mode == ROUNDING_DOWN && sign)
            || (rnd_mode == ROUNDING_UP && !sign)) {
          res.w[1] = EXP_MIN;
          res.w[0] = 1;
        } else {
          res.w[1] = EXP_MIN;
          res.w[0] = 0;
        }
      }
    } else { // if (q <= 68), i.e. 59 <= q <= 68
      if (C.w[3] == __bid_midpoint256[q - 59].w[3] && 
          C.w[2] == __bid_midpoint256[q - 59].w[2] && 
          C.w[1] == __bid_midpoint256[q - 59].w[1] && 
          C.w[0] == __bid_midpoint256[q - 59].w[0]) { // C = 0.5 * 10^emin
        if (rnd_mode == ROUNDING_TO_NEAREST || (rnd_mode == ROUNDING_DOWN
            && !sign) || (rnd_mode == ROUNDING_UP && sign)
            || rnd_mode == ROUNDING_TO_ZERO) {
          res.w[1] = EXP_MIN;
          res.w[0] = 0;
        } else {
          res.w[1] = EXP_MIN;
          res.w[0] = 1;
        }
      } else if (C.w[3] < __bid_midpoint256[q - 59].w[3] || 
          (C.w[3] == __bid_midpoint256[q - 59].w[3] && 
          C.w[2] < __bid_midpoint256[q - 59].w[2]) || 
          (C.w[3] == __bid_midpoint256[q - 59].w[3] && 
          C.w[2] == __bid_midpoint256[q - 59].w[2] && 
          C.w[1] < __bid_midpoint256[q - 59].w[1]) || 
          (C.w[3] == __bid_midpoint256[q - 59].w[3] && 
          C.w[2] == __bid_midpoint256[q - 59].w[2] && 
          C.w[1] == __bid_midpoint256[q - 59].w[1] && 
          C.w[0] < __bid_midpoint256[q - 59].w[0])) { // C < 0.5 * 10^emin
        if (rnd_mode == ROUNDING_TO_NEAREST
            || rnd_mode == ROUNDING_TIES_AWAY
            || (rnd_mode == ROUNDING_DOWN && !sign)
            || (rnd_mode == ROUNDING_UP && sign)
            || rnd_mode == ROUNDING_TO_ZERO) {
          res.w[1] = EXP_MIN;
          res.w[0] = 0;
        } else {
          res.w[1] = EXP_MIN;
          res.w[0] = 1;
        }
      } else { // C > 0.5 * 10^emin
        if (rnd_mode == ROUNDING_TO_NEAREST
            || rnd_mode == ROUNDING_TIES_AWAY
            || (rnd_mode == ROUNDING_DOWN && sign)
            || (rnd_mode == ROUNDING_UP && !sign)) {
          res.w[1] = EXP_MIN;
          res.w[0] = 1;
        } else {
          res.w[1] = EXP_MIN;
          res.w[0] = 0;
        }
      }
    }
  } else { // if 0 < q - ind < P34 <=> 1 <= q - ind <= P34 - 1 = 33
    // In general -P34 + 2 <= ind <= 6176 => -P34 + 2 <= ind < q =>
    // -P34 + 2 <= ind <= q - 1
    if (rnd_mode != ROUNDING_TO_NEAREST) {
      is_inexact_lt_midpoint = 0;
      is_inexact_gt_midpoint = 0;
      is_midpoint_lt_even = 0;
      is_midpoint_gt_even = 0;
    }
    if (ind <= 0) { // 0 <= -ind
      // the result is exact
      res.w[1] = (x_exp + y_exp - BIN_EXP_BIAS) | C.w[1];
      res.w[0] = C.w[0];

      // because the result is exact the U and I status flags are not set
    } else {

      // if ind > 0 <=> 1 <= ind <= q - 1; must remove ind digits
      // from C, which may have up to 68 digits; note that q >= ind + 1 >= 2
      // Note: there is no underflow in some cases when the coefficient of
      // the result is 10^33 or 10^33 - 1
      if (q <= 18) { // 2 <= q <= 18
        __bid_round64_2_18 (q, ind, C.w[0], &res.w[0], &incr_exp,
                      &is_midpoint_lt_even, &is_midpoint_gt_even,
                      &is_inexact_lt_midpoint, &is_inexact_gt_midpoint);
        if (incr_exp) {

          // multiply by 10; this cannot be 10^33
          __mul_64x64_to_128MACH (res, res.w[0], __bid_ten2k64[1]);
          res.w[1] |= (UINT64) EXP_MIN;
        } else { // underflow
          res.w[1] = (UINT64) EXP_MIN;
        }
        if (is_midpoint_lt_even || is_midpoint_gt_even
            || is_inexact_lt_midpoint || is_inexact_gt_midpoint) {

          // set the inexact and underflow flags
          *pfpsf |= (INEXACT_EXCEPTION | UNDERFLOW_EXCEPTION);
        }
      } else if (q <= 38) { // 19 <= q <= 38
        P128.w[1] = C.w[1];
        P128.w[0] = C.w[0];
        __bid_round128_19_38 (q, ind, P128, &res, &incr_exp,
                        &is_midpoint_lt_even, &is_midpoint_gt_even,
                        &is_inexact_lt_midpoint,
                        &is_inexact_gt_midpoint);
        if (incr_exp) {

          // multiply by 10 and check is this is 10^33, because in that case
          // it is possible that this is not underflow
          if (q - ind <= 19) {
            __mul_64x64_to_128MACH (res, res.w[0], __bid_ten2k64[1]);
          } else { // if 20 <= q - ind
            __mul_128x64_to_128 (res, __bid_ten2k64[1], res);
          }
          if ((q - ind + 1) == P34) { // the result is 10^(P34-1)
            // if the result rounded directly to P34 digits is the same, then
            // there is no underflow
            __bid_round128_19_38 (q, ind - 1, P128, &R128, &incr_exp1,
                            &is_midpoint_lt_even1,
                            &is_midpoint_gt_even1,
                            &is_inexact_lt_midpoint1,
                            &is_inexact_gt_midpoint1);
            if (res.w[1] == R128.w[1] && res.w[0] == R128.w[0]) {
              no_underflow = 1;
            }
          }
          // res.w[1] |= (UINT64)EXP_MIN; // redundant
        } else { // underflow
          // res.w[1] = (UINT64)EXP_MIN | res.w[1]; // redundant
        }
        if (is_midpoint_lt_even || is_midpoint_gt_even
            || is_inexact_lt_midpoint || is_inexact_gt_midpoint) {

          // set the inexact and underflow flags
          *pfpsf |= INEXACT_EXCEPTION;
          is_inexact = 1;
          if (!no_underflow)
            *pfpsf |= UNDERFLOW_EXCEPTION;
        }
      } else if (q <= 57) { // 39 <= q <= 57
        P192.w[2] = C.w[2];
        P192.w[1] = C.w[1];
        P192.w[0] = C.w[0];
        __bid_round192_39_57 (q, ind, P192, &R192, &incr_exp,
                        &is_midpoint_lt_even, &is_midpoint_gt_even,
                        &is_inexact_lt_midpoint,
                        &is_inexact_gt_midpoint);
        if (incr_exp) {

          // multiply by 10 and check is this is 10^33, because in that case
          // it is possible that this is not underflow
          res.w[1] = R192.w[1]; // res has q - ind digits
          res.w[0] = R192.w[0];
          if (q - ind <= 19) {
            __mul_64x64_to_128MACH (res, res.w[0], __bid_ten2k64[1]);
          } else { // if 20 <= q - ind
            __mul_128x64_to_128 (res, __bid_ten2k64[1], res);
          }
          if ((q - ind + 1) == P34) { // the result is 10^(P34-1) 
            // if the result rounded directly to P34 digits is the same, then
            // there is no underflow
            __bid_round192_39_57 (q, ind - 1, P192, &R192, &incr_exp1,
                            &is_midpoint_lt_even1,
                            &is_midpoint_gt_even1,
                            &is_inexact_lt_midpoint1,
                            &is_inexact_gt_midpoint1);
            if (res.w[1] == R192.w[1] && res.w[0] == R192.w[0]) {
              no_underflow = 1;
            }
          }
          // res.w[1] |= (UINT64)EXP_MIN; // redundant
        } else { // underflow
          res.w[1] = (UINT64) EXP_MIN | R192.w[1];
          res.w[0] = R192.w[0];
        }
        if (is_midpoint_lt_even || is_midpoint_gt_even
            || is_inexact_lt_midpoint || is_inexact_gt_midpoint) {

          // set the inexact and underflow flags
          *pfpsf |= INEXACT_EXCEPTION;
          is_inexact = 1;
          if (!no_underflow)
            *pfpsf |= UNDERFLOW_EXCEPTION;
        }
      } else if (q <= 76) { // 58 <= q <= 76 (actually 58 <= q <= 68)
        P256.w[3] = C.w[3];
        P256.w[2] = C.w[2];
        P256.w[1] = C.w[1];
        P256.w[0] = C.w[0];
        __bid_round256_58_76 (q, ind, P256, &R256, &incr_exp,
                        &is_midpoint_lt_even, &is_midpoint_gt_even,
                        &is_inexact_lt_midpoint,
                        &is_inexact_gt_midpoint);
        if (incr_exp) {

          // multiply by 10 and check is this is 10^33, because in that case
          // it is possible that this is not underflow
          res.w[1] = R256.w[1]; // res has q - ind digits
          res.w[0] = R256.w[0];
          if (q - ind <= 19) {
            __mul_64x64_to_128MACH (res, res.w[0], __bid_ten2k64[1]);
          } else { // if 20 <= q - ind
            __mul_128x64_to_128 (res, __bid_ten2k64[1], res);
          }
          if ((q - ind + 1) == P34) { // the result is 10^(P34-1) 
            // if the result rounded directly to P34 digits is the same, then
            // there is no underflow
            __bid_round256_58_76 (q, ind - 1, P256, &R256, &incr_exp1,
                            &is_midpoint_lt_even1,
                            &is_midpoint_gt_even1,
                            &is_inexact_lt_midpoint1,
                            &is_inexact_gt_midpoint1);
            if (res.w[1] == R256.w[1] && res.w[0] == R256.w[0]) {
              no_underflow = 1;
            }
          }
          // res.w[1] |= (UINT64)EXP_MIN; // redundant
        } else { // underflow
          res.w[1] = (UINT64) EXP_MIN | R256.w[1];
          res.w[0] = R256.w[0];
        }
        if (is_midpoint_lt_even || is_midpoint_gt_even
            || is_inexact_lt_midpoint || is_inexact_gt_midpoint) {

          // set the inexact and underflow flags
          *pfpsf |= INEXACT_EXCEPTION;
          is_inexact = 1;
          if (!no_underflow)
            *pfpsf |= UNDERFLOW_EXCEPTION;
        }
      }
    }

    // general correction from RN to RA, RM, RP, RZ
    if (rnd_mode != ROUNDING_TO_NEAREST) {
      x_exp = res.w[1] & MASK_EXP; // biased and shifted left 49 bit positions
      // this must be e_min
      C1_hi = res.w[1] & MASK_COEFF;
      C1_lo = res.w[0];
      if ((!sign && ((rnd_mode == ROUNDING_UP && is_inexact_lt_midpoint) || 
          ((rnd_mode == ROUNDING_TIES_AWAY || rnd_mode == ROUNDING_UP) && 
          is_midpoint_gt_even))) || 
          (sign && ((rnd_mode == ROUNDING_DOWN && is_inexact_lt_midpoint) || 
          ((rnd_mode == ROUNDING_TIES_AWAY || rnd_mode == ROUNDING_DOWN) && 
          is_midpoint_gt_even)))) {

        // C1 = C1 + 1
        C1_lo = C1_lo + 1;
        if (C1_lo == 0) { // rounding overflow in the low 64 bits
          C1_hi = C1_hi + 1;
          if (C1_hi == 0x0001ed09bead87c0ull
              && C1_lo == 0x378d8e6400000000ull) {

            // C1 = 10^34 => rounding overflow (not possible) TO CHECK
            C1_hi = 0x0000314dc6448d93ull;
            C1_lo = 0x38c15b0a00000000ull; // 10^33
            x_exp = x_exp + EXP_P1; // this must be e_min
          }
        }
      } else if ((is_midpoint_lt_even || is_inexact_gt_midpoint) && 
          ((sign && 
          (rnd_mode == ROUNDING_UP || rnd_mode == ROUNDING_TO_ZERO)) || 
          (!sign && 
          (rnd_mode == ROUNDING_DOWN || rnd_mode == ROUNDING_TO_ZERO)))) {

        // C1 = C1 - 1 (the exponent is emin already)
        C1_lo = C1_lo - 1;
        if (C1_lo == 0xffffffffffffffffull)
          C1_hi--;

        // cannot cross into the lower decade anymore, but the result can be 0
      } else {
        ; // exact, the result is already correct
      }

      // no overflow is possible
      // assemble the result
      res.w[1] = x_exp | C1_hi;
      res.w[0] = C1_lo;

      // Now fix the case where the general rounding routine returned a non-tiny
      // result, but after the correction for rounding modes other than to
      // nearest, the result is less in magnitude than 100...0[34] * 10^(-6176)
      // (this is due to the fact that the general rounding routine works only
      // with rounding to nearest)
      if (is_inexact && (x_exp == EXP_MIN)
          && (C1_hi < 0x0000314dc6448d93ull
              || (C1_hi == 0x0000314dc6448d93ull
              && C1_lo < 0x38c15b0a00000000ull))) {
        *pfpsf |= UNDERFLOW_EXCEPTION;
      }
    }
  }
  res.w[1] |= sign;
  BID_RETURN (res);
}