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
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
|
/* real.c - software floating point emulation.
Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998,
1999, 2000, 2002 Free Software Foundation, Inc.
Contributed by Stephen L. Moshier (moshier@world.std.com).
Re-written by Richard Henderson <rth@redhat.com>
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.
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, 59 Temple Place - Suite 330, Boston, MA
02111-1307, USA. */
#include "config.h"
#include "system.h"
#include "tree.h"
#include "toplev.h"
#include "real.h"
#include "tm_p.h"
/* The floating point model used internally is not exactly IEEE 754
compliant, and close to the description in the ISO C standard,
section 5.2.4.2.2 Characteristics of floating types.
Specifically
x = s * b^e * \sum_{k=1}^p f_k * b^{-k}
where
s = sign (+- 1)
b = base or radix, here always 2
e = exponent
p = precision (the number of base-b digits in the significand)
f_k = the digits of the significand.
We differ from typical IEEE 754 encodings in that the entire
significand is fractional. Normalized significands are in the
range [0.5, 1.0).
A requirement of the model is that P be larger than than the
largest supported target floating-point type by at least 2 bits.
This gives us proper rounding when we truncate to the target type.
In addition, E must be large enough to hold the smallest supported
denormal number in a normalized form.
Both of these requirements are easily satisfied. The largest target
significand is 113 bits; we store at least 160. The smallest
denormal number fits in 17 exponent bits; we store 29.
Note that the decimal string conversion routines are sensitive to
rounding error. Since the raw arithmetic routines do not themselves
have guard digits or rounding, the computation of 10**exp can
accumulate more than a few digits of error. The previous incarnation
of real.c successfully used a 144 bit fraction; given the current
layout of REAL_VALUE_TYPE we're forced to expand to at least 160 bits.
Target floating point models that use base 16 instead of base 2
(i.e. IBM 370), are handled during round_for_format, in which we
canonicalize the exponent to be a multiple of 4 (log2(16)), and
adjust the significand to match. */
/* Used to classify two numbers simultaneously. */
#define CLASS2(A, B) ((A) << 2 | (B))
#if HOST_BITS_PER_LONG != 64 && HOST_BITS_PER_LONG != 32
#error "Some constant folding done by hand to avoid shift count warnings"
#endif
static void get_zero PARAMS ((REAL_VALUE_TYPE *, int));
static void get_canonical_qnan PARAMS ((REAL_VALUE_TYPE *, int));
static void get_canonical_snan PARAMS ((REAL_VALUE_TYPE *, int));
static void get_inf PARAMS ((REAL_VALUE_TYPE *, int));
static void sticky_rshift_significand PARAMS ((REAL_VALUE_TYPE *,
const REAL_VALUE_TYPE *,
unsigned int));
static void rshift_significand PARAMS ((REAL_VALUE_TYPE *,
const REAL_VALUE_TYPE *,
unsigned int));
static void lshift_significand PARAMS ((REAL_VALUE_TYPE *,
const REAL_VALUE_TYPE *,
unsigned int));
static void lshift_significand_1 PARAMS ((REAL_VALUE_TYPE *,
const REAL_VALUE_TYPE *));
static bool add_significands PARAMS ((REAL_VALUE_TYPE *r,
const REAL_VALUE_TYPE *,
const REAL_VALUE_TYPE *));
static bool sub_significands PARAMS ((REAL_VALUE_TYPE *,
const REAL_VALUE_TYPE *,
const REAL_VALUE_TYPE *));
static void neg_significand PARAMS ((REAL_VALUE_TYPE *,
const REAL_VALUE_TYPE *));
static int cmp_significands PARAMS ((const REAL_VALUE_TYPE *,
const REAL_VALUE_TYPE *));
static int cmp_significand_0 PARAMS ((const REAL_VALUE_TYPE *));
static void set_significand_bit PARAMS ((REAL_VALUE_TYPE *, unsigned int));
static void clear_significand_bit PARAMS ((REAL_VALUE_TYPE *, unsigned int));
static bool test_significand_bit PARAMS ((REAL_VALUE_TYPE *, unsigned int));
static void clear_significand_below PARAMS ((REAL_VALUE_TYPE *,
unsigned int));
static bool div_significands PARAMS ((REAL_VALUE_TYPE *,
const REAL_VALUE_TYPE *,
const REAL_VALUE_TYPE *));
static void normalize PARAMS ((REAL_VALUE_TYPE *));
static void do_add PARAMS ((REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
const REAL_VALUE_TYPE *, int));
static void do_multiply PARAMS ((REAL_VALUE_TYPE *,
const REAL_VALUE_TYPE *,
const REAL_VALUE_TYPE *));
static void do_divide PARAMS ((REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
const REAL_VALUE_TYPE *));
static int do_compare PARAMS ((const REAL_VALUE_TYPE *,
const REAL_VALUE_TYPE *, int));
static void do_fix_trunc PARAMS ((REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *));
static unsigned long rtd_divmod PARAMS ((REAL_VALUE_TYPE *,
REAL_VALUE_TYPE *));
static const REAL_VALUE_TYPE * ten_to_ptwo PARAMS ((int));
static const REAL_VALUE_TYPE * ten_to_mptwo PARAMS ((int));
static const REAL_VALUE_TYPE * real_digit PARAMS ((int));
static void times_pten PARAMS ((REAL_VALUE_TYPE *, int));
static void round_for_format PARAMS ((const struct real_format *,
REAL_VALUE_TYPE *));
/* Initialize R with a positive zero. */
static inline void
get_zero (r, sign)
REAL_VALUE_TYPE *r;
int sign;
{
memset (r, 0, sizeof (*r));
r->sign = sign;
}
/* Initialize R with the canonical quiet NaN. */
static inline void
get_canonical_qnan (r, sign)
REAL_VALUE_TYPE *r;
int sign;
{
memset (r, 0, sizeof (*r));
r->class = rvc_nan;
r->sign = sign;
r->sig[SIGSZ-1] = SIG_MSB >> 1;
}
static inline void
get_canonical_snan (r, sign)
REAL_VALUE_TYPE *r;
int sign;
{
memset (r, 0, sizeof (*r));
r->class = rvc_nan;
r->sign = sign;
r->sig[SIGSZ-1] = SIG_MSB >> 2;
}
static inline void
get_inf (r, sign)
REAL_VALUE_TYPE *r;
int sign;
{
memset (r, 0, sizeof (*r));
r->class = rvc_inf;
r->sign = sign;
}
/* Right-shift the significand of A by N bits; put the result in the
significand of R. If any one bits are shifted out, set the least
significant bit of R. */
static void
sticky_rshift_significand (r, a, n)
REAL_VALUE_TYPE *r;
const REAL_VALUE_TYPE *a;
unsigned int n;
{
unsigned long sticky = 0;
unsigned int i, ofs = 0;
if (n >= HOST_BITS_PER_LONG)
{
for (i = 0, ofs = n / HOST_BITS_PER_LONG; i < ofs; ++i)
sticky |= a->sig[i];
n &= HOST_BITS_PER_LONG - 1;
}
if (n != 0)
{
sticky |= a->sig[ofs] & (((unsigned long)1 << n) - 1);
for (i = 0; i < SIGSZ; ++i)
{
r->sig[i]
= (((ofs + i >= SIGSZ ? 0 : a->sig[ofs + i]) >> n)
| ((ofs + i + 1 >= SIGSZ ? 0 : a->sig[ofs + i + 1])
<< (HOST_BITS_PER_LONG - n)));
}
}
else
{
for (i = 0; ofs + i < SIGSZ; ++i)
r->sig[i] = a->sig[ofs + i];
for (; i < SIGSZ; ++i)
r->sig[i] = 0;
}
r->sig[0] |= (sticky != 0);
}
/* Right-shift the significand of A by N bits; put the result in the
significand of R. */
static void
rshift_significand (r, a, n)
REAL_VALUE_TYPE *r;
const REAL_VALUE_TYPE *a;
unsigned int n;
{
unsigned int i, ofs = n / HOST_BITS_PER_LONG;
n &= HOST_BITS_PER_LONG - 1;
if (n != 0)
{
for (i = 0; i < SIGSZ; ++i)
{
r->sig[i]
= (((ofs + i >= SIGSZ ? 0 : a->sig[ofs + i]) >> n)
| ((ofs + i + 1 >= SIGSZ ? 0 : a->sig[ofs + i + 1])
<< (HOST_BITS_PER_LONG - n)));
}
}
else
{
for (i = 0; ofs + i < SIGSZ; ++i)
r->sig[i] = a->sig[ofs + i];
for (; i < SIGSZ; ++i)
r->sig[i] = 0;
}
}
/* Left-shift the significand of A by N bits; put the result in the
significand of R. */
static void
lshift_significand (r, a, n)
REAL_VALUE_TYPE *r;
const REAL_VALUE_TYPE *a;
unsigned int n;
{
unsigned int i, ofs = n / HOST_BITS_PER_LONG;
n &= HOST_BITS_PER_LONG - 1;
if (n == 0)
{
for (i = 0; ofs + i < SIGSZ; ++i)
r->sig[SIGSZ-1-i] = a->sig[SIGSZ-1-i-ofs];
for (; i < SIGSZ; ++i)
r->sig[SIGSZ-1-i] = 0;
}
else
for (i = 0; i < SIGSZ; ++i)
{
r->sig[SIGSZ-1-i]
= (((ofs + i >= SIGSZ ? 0 : a->sig[SIGSZ-1-i-ofs]) << n)
| ((ofs + i + 1 >= SIGSZ ? 0 : a->sig[SIGSZ-1-i-ofs-1])
>> (HOST_BITS_PER_LONG - n)));
}
}
/* Likewise, but N is specialized to 1. */
static inline void
lshift_significand_1 (r, a)
REAL_VALUE_TYPE *r;
const REAL_VALUE_TYPE *a;
{
unsigned int i;
for (i = SIGSZ - 1; i > 0; --i)
r->sig[i] = (a->sig[i] << 1) | (a->sig[i-1] >> (HOST_BITS_PER_LONG - 1));
r->sig[0] = a->sig[0] << 1;
}
/* Add the significands of A and B, placing the result in R. Return
true if there was carry out of the most significant word. */
static inline bool
add_significands (r, a, b)
REAL_VALUE_TYPE *r;
const REAL_VALUE_TYPE *a, *b;
{
bool carry = false;
int i;
for (i = 0; i < SIGSZ; ++i)
{
unsigned long ai = a->sig[i];
unsigned long ri = ai + b->sig[i];
if (carry)
{
carry = ri < ai;
carry |= ++ri == 0;
}
else
carry = ri < ai;
r->sig[i] = ri;
}
return carry;
}
/* Subtract the significands of A and B, placing the result in R.
Return true if there was carry out of the most significant word. */
static inline bool
sub_significands (r, a, b)
REAL_VALUE_TYPE *r;
const REAL_VALUE_TYPE *a, *b;
{
bool carry = false;
int i;
for (i = 0; i < SIGSZ; ++i)
{
unsigned long ai = a->sig[i];
unsigned long ri = ai - b->sig[i];
if (carry)
{
carry = ri > ai;
carry |= ~--ri == 0;
}
else
carry = ri > ai;
r->sig[i] = ri;
}
return carry;
}
/* Negate the significand A, placing the result in R. */
static inline void
neg_significand (r, a)
REAL_VALUE_TYPE *r;
const REAL_VALUE_TYPE *a;
{
bool carry = true;
int i;
for (i = 0; i < SIGSZ; ++i)
{
unsigned long ri, ai = a->sig[i];
if (carry)
{
if (ai)
{
ri = -ai;
carry = false;
}
else
ri = ai;
}
else
ri = ~ai;
r->sig[i] = ri;
}
}
/* Compare significands. Return tri-state vs zero. */
static inline int
cmp_significands (a, b)
const REAL_VALUE_TYPE *a, *b;
{
int i;
for (i = SIGSZ - 1; i >= 0; --i)
{
unsigned long ai = a->sig[i];
unsigned long bi = b->sig[i];
if (ai > bi)
return 1;
if (ai < bi)
return -1;
}
return 0;
}
/* Return true if A is non-zero. */
static inline int
cmp_significand_0 (a)
const REAL_VALUE_TYPE *a;
{
int i;
for (i = SIGSZ - 1; i >= 0; --i)
if (a->sig[i])
return 1;
return 0;
}
/* Set bit N of the significand of R. */
static inline void
set_significand_bit (r, n)
REAL_VALUE_TYPE *r;
unsigned int n;
{
r->sig[n / HOST_BITS_PER_LONG]
|= (unsigned long)1 << (n % HOST_BITS_PER_LONG);
}
/* Clear bit N of the significand of R. */
static inline void
clear_significand_bit (r, n)
REAL_VALUE_TYPE *r;
unsigned int n;
{
r->sig[n / HOST_BITS_PER_LONG]
&= ~((unsigned long)1 << (n % HOST_BITS_PER_LONG));
}
/* Test bit N of the significand of R. */
static inline bool
test_significand_bit (r, n)
REAL_VALUE_TYPE *r;
unsigned int n;
{
/* ??? Compiler bug here if we return this expression directly.
The conversion to bool strips the "&1" and we wind up testing
e.g. 2 != 0 -> true. Seen in gcc version 3.2 20020520. */
int t = (r->sig[n / HOST_BITS_PER_LONG] >> (n % HOST_BITS_PER_LONG)) & 1;
return t;
}
/* Clear bits 0..N-1 of the significand of R. */
static void
clear_significand_below (r, n)
REAL_VALUE_TYPE *r;
unsigned int n;
{
int i, w = n / HOST_BITS_PER_LONG;
for (i = 0; i < w; ++i)
r->sig[i] = 0;
r->sig[w] &= ~(((unsigned long)1 << (n % HOST_BITS_PER_LONG)) - 1);
}
/* Divide the significands of A and B, placing the result in R. Return
true if the division was inexact. */
static inline bool
div_significands (r, a, b)
REAL_VALUE_TYPE *r;
const REAL_VALUE_TYPE *a, *b;
{
REAL_VALUE_TYPE u;
int i, bit = SIGNIFICAND_BITS - 1;
unsigned long msb, inexact;
u = *a;
memset (r->sig, 0, sizeof (r->sig));
msb = 0;
goto start;
do
{
msb = u.sig[SIGSZ-1] & SIG_MSB;
lshift_significand_1 (&u, &u);
start:
if (msb || cmp_significands (&u, b) >= 0)
{
sub_significands (&u, &u, b);
set_significand_bit (r, bit);
}
}
while (--bit >= 0);
for (i = 0, inexact = 0; i < SIGSZ; i++)
inexact |= u.sig[i];
return inexact != 0;
}
/* Adjust the exponent and significand of R such that the most
significant bit is set. We underflow to zero and overflow to
infinity here, without denormals. (The intermediate representation
exponent is large enough to handle target denormals normalized.) */
static void
normalize (r)
REAL_VALUE_TYPE *r;
{
int shift = 0, exp;
int i, j;
/* Find the first word that is nonzero. */
for (i = SIGSZ - 1; i >= 0; i--)
if (r->sig[i] == 0)
shift += HOST_BITS_PER_LONG;
else
break;
/* Zero significand flushes to zero. */
if (i < 0)
{
r->class = rvc_zero;
r->exp = 0;
return;
}
/* Find the first bit that is nonzero. */
for (j = 0; ; j++)
if (r->sig[i] & ((unsigned long)1 << (HOST_BITS_PER_LONG - 1 - j)))
break;
shift += j;
if (shift > 0)
{
exp = r->exp - shift;
if (exp > MAX_EXP)
get_inf (r, r->sign);
else if (exp < -MAX_EXP)
get_zero (r, r->sign);
else
{
r->exp = exp;
lshift_significand (r, r, shift);
}
}
}
/* Return R = A + (SUBTRACT_P ? -B : B). */
static void
do_add (r, a, b, subtract_p)
REAL_VALUE_TYPE *r;
const REAL_VALUE_TYPE *a, *b;
int subtract_p;
{
int dexp, sign, exp;
REAL_VALUE_TYPE t;
/* Determine if we need to add or subtract. */
sign = a->sign;
subtract_p = (sign ^ b->sign) ^ subtract_p;
switch (CLASS2 (a->class, b->class))
{
case CLASS2 (rvc_zero, rvc_zero):
/* -0 + -0 = -0, -0 - +0 = -0; all other cases yield +0. */
get_zero (r, sign & !subtract_p);
return;
case CLASS2 (rvc_zero, rvc_normal):
case CLASS2 (rvc_zero, rvc_inf):
case CLASS2 (rvc_zero, rvc_nan):
/* 0 + ANY = ANY. */
case CLASS2 (rvc_normal, rvc_nan):
case CLASS2 (rvc_inf, rvc_nan):
case CLASS2 (rvc_nan, rvc_nan):
/* ANY + NaN = NaN. */
case CLASS2 (rvc_normal, rvc_inf):
/* R + Inf = Inf. */
*r = *b;
r->sign = sign ^ subtract_p;
return;
case CLASS2 (rvc_normal, rvc_zero):
case CLASS2 (rvc_inf, rvc_zero):
case CLASS2 (rvc_nan, rvc_zero):
/* ANY + 0 = ANY. */
case CLASS2 (rvc_nan, rvc_normal):
case CLASS2 (rvc_nan, rvc_inf):
/* NaN + ANY = NaN. */
case CLASS2 (rvc_inf, rvc_normal):
/* Inf + R = Inf. */
*r = *a;
return;
case CLASS2 (rvc_inf, rvc_inf):
if (subtract_p)
/* Inf - Inf = NaN. */
get_canonical_qnan (r, 0);
else
/* Inf + Inf = Inf. */
*r = *a;
return;
case CLASS2 (rvc_normal, rvc_normal):
break;
default:
abort ();
}
/* Swap the arguments such that A has the larger exponent. */
dexp = a->exp - b->exp;
if (dexp < 0)
{
const REAL_VALUE_TYPE *t;
t = a, a = b, b = t;
dexp = -dexp;
sign ^= subtract_p;
}
exp = a->exp;
/* If the exponents are not identical, we need to shift the
significand of B down. */
if (dexp > 0)
{
/* If the exponents are too far apart, the significands
do not overlap, which makes the subtraction a noop. */
if (dexp >= SIGNIFICAND_BITS)
{
*r = *a;
r->sign = sign;
return;
}
sticky_rshift_significand (&t, b, dexp);
b = &t;
}
if (subtract_p)
{
if (sub_significands (r, a, b))
{
/* We got a borrow out of the subtraction. That means that
A and B had the same exponent, and B had the larger
significand. We need to swap the sign and negate the
significand. */
sign ^= 1;
neg_significand (r, r);
}
}
else
{
if (add_significands (r, a, b))
{
/* We got carry out of the addition. This means we need to
shift the significand back down one bit and increase the
exponent. */
sticky_rshift_significand (r, r, 1);
r->sig[SIGSZ-1] |= SIG_MSB;
if (++exp > MAX_EXP)
{
get_inf (r, sign);
return;
}
}
}
r->class = rvc_normal;
r->sign = sign;
r->exp = exp;
/* Re-normalize the result. */
normalize (r);
/* Special case: if the subtraction results in zero, the result
is positive. */
if (r->class == rvc_zero)
r->sign = 0;
}
/* Return R = A * B. */
static void
do_multiply (r, a, b)
REAL_VALUE_TYPE *r;
const REAL_VALUE_TYPE *a, *b;
{
REAL_VALUE_TYPE u, t, *rr;
unsigned int i, j, k;
int sign = a->sign ^ b->sign;
switch (CLASS2 (a->class, b->class))
{
case CLASS2 (rvc_zero, rvc_zero):
case CLASS2 (rvc_zero, rvc_normal):
case CLASS2 (rvc_normal, rvc_zero):
/* +-0 * ANY = 0 with appropriate sign. */
get_zero (r, sign);
return;
case CLASS2 (rvc_zero, rvc_nan):
case CLASS2 (rvc_normal, rvc_nan):
case CLASS2 (rvc_inf, rvc_nan):
case CLASS2 (rvc_nan, rvc_nan):
/* ANY * NaN = NaN. */
*r = *b;
r->sign = sign;
return;
case CLASS2 (rvc_nan, rvc_zero):
case CLASS2 (rvc_nan, rvc_normal):
case CLASS2 (rvc_nan, rvc_inf):
/* NaN * ANY = NaN. */
*r = *a;
r->sign = sign;
return;
case CLASS2 (rvc_zero, rvc_inf):
case CLASS2 (rvc_inf, rvc_zero):
/* 0 * Inf = NaN */
get_canonical_qnan (r, sign);
return;
case CLASS2 (rvc_inf, rvc_inf):
case CLASS2 (rvc_normal, rvc_inf):
case CLASS2 (rvc_inf, rvc_normal):
/* Inf * Inf = Inf, R * Inf = Inf */
overflow:
get_inf (r, sign);
return;
case CLASS2 (rvc_normal, rvc_normal):
break;
default:
abort ();
}
if (r == a || r == b)
rr = &t;
else
rr = r;
get_zero (rr, 0);
/* Collect all the partial products. Since we don't have sure access
to a widening multiply, we split each long into two half-words.
Consider the long-hand form of a four half-word multiplication:
A B C D
* E F G H
--------------
DE DF DG DH
CE CF CG CH
BE BF BG BH
AE AF AG AH
We construct partial products of the widened half-word products
that are known to not overlap, e.g. DF+DH. Each such partial
product is given its proper exponent, which allows us to sum them
and obtain the finished product. */
for (i = 0; i < SIGSZ * 2; ++i)
{
unsigned long ai = a->sig[i / 2];
if (i & 1)
ai >>= HOST_BITS_PER_LONG / 2;
else
ai &= ((unsigned long)1 << (HOST_BITS_PER_LONG / 2)) - 1;
if (ai == 0)
continue;
for (j = 0; j < 2; ++j)
{
int exp = (a->exp - (2*SIGSZ-1-i)*(HOST_BITS_PER_LONG/2)
+ (b->exp - (1-j)*(HOST_BITS_PER_LONG/2)));
if (exp > MAX_EXP)
goto overflow;
if (exp < -MAX_EXP)
/* Would underflow to zero, which we shouldn't bother adding. */
continue;
u.class = rvc_normal;
u.sign = 0;
u.exp = exp;
for (k = j; k < SIGSZ * 2; k += 2)
{
unsigned long bi = b->sig[k / 2];
if (k & 1)
bi >>= HOST_BITS_PER_LONG / 2;
else
bi &= ((unsigned long)1 << (HOST_BITS_PER_LONG / 2)) - 1;
u.sig[k / 2] = ai * bi;
}
normalize (&u);
do_add (rr, rr, &u, 0);
}
}
rr->sign = sign;
if (rr != r)
*r = t;
}
/* Return R = A / B. */
static void
do_divide (r, a, b)
REAL_VALUE_TYPE *r;
const REAL_VALUE_TYPE *a, *b;
{
int exp, sign = a->sign ^ b->sign;
REAL_VALUE_TYPE t, *rr;
bool inexact;
switch (CLASS2 (a->class, b->class))
{
case CLASS2 (rvc_zero, rvc_zero):
/* 0 / 0 = NaN. */
case CLASS2 (rvc_inf, rvc_inf):
/* Inf / Inf = NaN. */
get_canonical_qnan (r, sign);
return;
case CLASS2 (rvc_zero, rvc_normal):
case CLASS2 (rvc_zero, rvc_inf):
/* 0 / ANY = 0. */
case CLASS2 (rvc_normal, rvc_inf):
/* R / Inf = 0. */
underflow:
get_zero (r, sign);
return;
case CLASS2 (rvc_normal, rvc_zero):
/* R / 0 = Inf. */
case CLASS2 (rvc_inf, rvc_zero):
/* Inf / 0 = Inf. */
get_inf (r, sign);
return;
case CLASS2 (rvc_zero, rvc_nan):
case CLASS2 (rvc_normal, rvc_nan):
case CLASS2 (rvc_inf, rvc_nan):
case CLASS2 (rvc_nan, rvc_nan):
/* ANY / NaN = NaN. */
*r = *b;
r->sign = sign;
return;
case CLASS2 (rvc_nan, rvc_zero):
case CLASS2 (rvc_nan, rvc_normal):
case CLASS2 (rvc_nan, rvc_inf):
/* NaN / ANY = NaN. */
*r = *a;
r->sign = sign;
return;
case CLASS2 (rvc_inf, rvc_normal):
/* Inf / R = Inf. */
overflow:
get_inf (r, sign);
return;
case CLASS2 (rvc_normal, rvc_normal):
break;
default:
abort ();
}
if (r == a || r == b)
rr = &t;
else
rr = r;
rr->class = rvc_normal;
rr->sign = sign;
exp = a->exp - b->exp + 1;
if (exp > MAX_EXP)
goto overflow;
if (exp < -MAX_EXP)
goto underflow;
rr->exp = exp;
inexact = div_significands (rr, a, b);
/* Re-normalize the result. */
normalize (rr);
rr->sig[0] |= inexact;
if (rr != r)
*r = t;
}
/* Return a tri-state comparison of A vs B. Return NAN_RESULT if
one of the two operands is a NaN. */
static int
do_compare (a, b, nan_result)
const REAL_VALUE_TYPE *a, *b;
int nan_result;
{
int ret;
switch (CLASS2 (a->class, b->class))
{
case CLASS2 (rvc_zero, rvc_zero):
/* Sign of zero doesn't matter for compares. */
return 0;
case CLASS2 (rvc_inf, rvc_zero):
case CLASS2 (rvc_inf, rvc_normal):
case CLASS2 (rvc_normal, rvc_zero):
return (a->sign ? -1 : 1);
case CLASS2 (rvc_inf, rvc_inf):
return -a->sign - -b->sign;
case CLASS2 (rvc_zero, rvc_normal):
case CLASS2 (rvc_zero, rvc_inf):
case CLASS2 (rvc_normal, rvc_inf):
return (b->sign ? 1 : -1);
case CLASS2 (rvc_zero, rvc_nan):
case CLASS2 (rvc_normal, rvc_nan):
case CLASS2 (rvc_inf, rvc_nan):
case CLASS2 (rvc_nan, rvc_nan):
case CLASS2 (rvc_nan, rvc_zero):
case CLASS2 (rvc_nan, rvc_normal):
case CLASS2 (rvc_nan, rvc_inf):
return nan_result;
case CLASS2 (rvc_normal, rvc_normal):
break;
default:
abort ();
}
if (a->sign != b->sign)
return -a->sign - -b->sign;
if (a->exp > b->exp)
ret = 1;
else if (a->exp < b->exp)
ret = -1;
else
ret = cmp_significands (a, b);
return (a->sign ? -ret : ret);
}
/* Return A truncated to an integral value toward zero. */
static void
do_fix_trunc (r, a)
REAL_VALUE_TYPE *r;
const REAL_VALUE_TYPE *a;
{
*r = *a;
switch (r->class)
{
case rvc_zero:
case rvc_inf:
case rvc_nan:
break;
case rvc_normal:
if (r->exp <= 0)
get_zero (r, r->sign);
else if (r->exp < SIGNIFICAND_BITS)
clear_significand_below (r, SIGNIFICAND_BITS - r->exp);
break;
default:
abort ();
}
}
/* Perform the binary or unary operation described by CODE.
For a unary operation, leave OP1 NULL. */
void
real_arithmetic (r, icode, op0, op1)
REAL_VALUE_TYPE *r;
int icode;
const REAL_VALUE_TYPE *op0, *op1;
{
enum tree_code code = icode;
switch (code)
{
case PLUS_EXPR:
do_add (r, op0, op1, 0);
break;
case MINUS_EXPR:
do_add (r, op0, op1, 1);
break;
case MULT_EXPR:
do_multiply (r, op0, op1);
break;
case RDIV_EXPR:
do_divide (r, op0, op1);
break;
case MIN_EXPR:
if (op1->class == rvc_nan)
*r = *op1;
else if (do_compare (op0, op1, -1) < 0)
*r = *op0;
else
*r = *op1;
break;
case MAX_EXPR:
if (op1->class == rvc_nan)
*r = *op1;
else if (do_compare (op0, op1, 1) < 0)
*r = *op1;
else
*r = *op0;
break;
case NEGATE_EXPR:
*r = *op0;
r->sign ^= 1;
break;
case ABS_EXPR:
*r = *op0;
r->sign = 0;
break;
case FIX_TRUNC_EXPR:
do_fix_trunc (r, op0);
break;
default:
abort ();
}
}
/* Legacy. Similar, but return the result directly. */
REAL_VALUE_TYPE
real_arithmetic2 (icode, op0, op1)
int icode;
const REAL_VALUE_TYPE *op0, *op1;
{
REAL_VALUE_TYPE r;
real_arithmetic (&r, icode, op0, op1);
return r;
}
bool
real_compare (icode, op0, op1)
int icode;
const REAL_VALUE_TYPE *op0, *op1;
{
enum tree_code code = icode;
switch (code)
{
case LT_EXPR:
return do_compare (op0, op1, 1) < 0;
case LE_EXPR:
return do_compare (op0, op1, 1) <= 0;
case GT_EXPR:
return do_compare (op0, op1, -1) > 0;
case GE_EXPR:
return do_compare (op0, op1, -1) >= 0;
case EQ_EXPR:
return do_compare (op0, op1, -1) == 0;
case NE_EXPR:
return do_compare (op0, op1, -1) != 0;
case UNORDERED_EXPR:
return op0->class == rvc_nan || op1->class == rvc_nan;
case ORDERED_EXPR:
return op0->class != rvc_nan && op1->class != rvc_nan;
case UNLT_EXPR:
return do_compare (op0, op1, -1) < 0;
case UNLE_EXPR:
return do_compare (op0, op1, -1) <= 0;
case UNGT_EXPR:
return do_compare (op0, op1, 1) > 0;
case UNGE_EXPR:
return do_compare (op0, op1, 1) >= 0;
case UNEQ_EXPR:
return do_compare (op0, op1, 0) == 0;
default:
abort ();
}
}
/* Return floor log2(R). */
int
real_exponent (r)
const REAL_VALUE_TYPE *r;
{
switch (r->class)
{
case rvc_zero:
return 0;
case rvc_inf:
case rvc_nan:
return (unsigned int)-1 >> 1;
case rvc_normal:
return r->exp;
default:
abort ();
}
}
/* R = OP0 * 2**EXP. */
void
real_ldexp (r, op0, exp)
REAL_VALUE_TYPE *r;
const REAL_VALUE_TYPE *op0;
int exp;
{
*r = *op0;
switch (r->class)
{
case rvc_zero:
case rvc_inf:
case rvc_nan:
break;
case rvc_normal:
exp += op0->exp;
if (exp > MAX_EXP)
get_inf (r, r->sign);
else if (exp < -MAX_EXP)
get_zero (r, r->sign);
else
r->exp = exp;
break;
default:
abort ();
}
}
/* Determine whether a floating-point value X is infinite. */
bool
real_isinf (r)
const REAL_VALUE_TYPE *r;
{
return (r->class == rvc_inf);
}
/* Determine whether a floating-point value X is a NaN. */
bool
real_isnan (r)
const REAL_VALUE_TYPE *r;
{
return (r->class == rvc_nan);
}
/* Determine whether a floating-point value X is negative. */
bool
real_isneg (r)
const REAL_VALUE_TYPE *r;
{
return r->sign;
}
/* Determine whether a floating-point value X is minus zero. */
bool
real_isnegzero (r)
const REAL_VALUE_TYPE *r;
{
return r->sign && r->class == rvc_zero;
}
/* Compare two floating-point objects for bitwise identity. */
extern bool
real_identical (a, b)
const REAL_VALUE_TYPE *a, *b;
{
int i;
if (a->class != b->class)
return false;
if (a->sign != b->sign)
return false;
switch (a->class)
{
case rvc_zero:
case rvc_inf:
break;
case rvc_normal:
if (a->exp != b->exp)
return false;
/* FALLTHRU */
case rvc_nan:
for (i = 0; i < SIGSZ; ++i)
if (a->sig[i] != b->sig[i])
return false;
break;
default:
abort ();
}
return true;
}
/* Try to change R into its exact multiplicative inverse in machine
mode MODE. Return true if successful. */
bool
exact_real_inverse (mode, r)
enum machine_mode mode;
REAL_VALUE_TYPE *r;
{
const REAL_VALUE_TYPE *one = real_digit (1);
REAL_VALUE_TYPE u;
int i;
if (r->class != rvc_normal)
return false;
/* Check for a power of two: all significand bits zero except the MSB. */
for (i = 0; i < SIGSZ-1; ++i)
if (r->sig[i] != 0)
return false;
if (r->sig[SIGSZ-1] != SIG_MSB)
return false;
/* Find the inverse and truncate to the required mode. */
do_divide (&u, one, r);
real_convert (&u, mode, &u);
/* The rounding may have overflowed. */
if (u.class != rvc_normal)
return false;
for (i = 0; i < SIGSZ-1; ++i)
if (u.sig[i] != 0)
return false;
if (u.sig[SIGSZ-1] != SIG_MSB)
return false;
*r = u;
return true;
}
/* Render R as an integer. */
HOST_WIDE_INT
real_to_integer (r)
const REAL_VALUE_TYPE *r;
{
unsigned HOST_WIDE_INT i;
switch (r->class)
{
case rvc_zero:
underflow:
return 0;
case rvc_inf:
case rvc_nan:
overflow:
i = (unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1);
if (!r->sign)
i--;
return i;
case rvc_normal:
if (r->exp <= 0)
goto underflow;
if (r->exp > HOST_BITS_PER_WIDE_INT)
goto overflow;
if (HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG)
i = r->sig[SIGSZ-1];
else if (HOST_BITS_PER_WIDE_INT == 2*HOST_BITS_PER_LONG)
{
i = r->sig[SIGSZ-1];
i = i << (HOST_BITS_PER_LONG - 1) << 1;
i |= r->sig[SIGSZ-2];
}
else
abort ();
i >>= HOST_BITS_PER_WIDE_INT - r->exp;
if (r->sign)
i = -i;
return i;
default:
abort ();
}
}
/* Likewise, but to an integer pair, HI+LOW. */
void
real_to_integer2 (plow, phigh, r)
HOST_WIDE_INT *plow, *phigh;
const REAL_VALUE_TYPE *r;
{
REAL_VALUE_TYPE t;
HOST_WIDE_INT low, high;
int exp;
switch (r->class)
{
case rvc_zero:
underflow:
low = high = 0;
break;
case rvc_inf:
case rvc_nan:
overflow:
high = (unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1);
if (r->sign)
low = 0;
else
{
high--;
low = -1;
}
break;
case rvc_normal:
exp = r->exp;
if (exp <= 0)
goto underflow;
if (exp >= 2*HOST_BITS_PER_WIDE_INT)
goto overflow;
rshift_significand (&t, r, 2*HOST_BITS_PER_WIDE_INT - exp);
if (HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG)
{
high = t.sig[SIGSZ-1];
low = t.sig[SIGSZ-2];
}
else if (HOST_BITS_PER_WIDE_INT == 2*HOST_BITS_PER_LONG)
{
high = t.sig[SIGSZ-1];
high = high << (HOST_BITS_PER_LONG - 1) << 1;
high |= t.sig[SIGSZ-2];
low = t.sig[SIGSZ-3];
low = low << (HOST_BITS_PER_LONG - 1) << 1;
low |= t.sig[SIGSZ-4];
}
else
abort ();
if (r->sign)
{
if (low == 0)
high = -high;
else
low = -low, high = ~high;
}
break;
default:
abort ();
}
*plow = low;
*phigh = high;
}
/* A subroutine of real_to_decimal. Compute the quotient and remainder
of NUM / DEN. Return the quotient and place the remainder in NUM.
It is expected that NUM / DEN are close enough that the quotient is
small. */
static unsigned long
rtd_divmod (num, den)
REAL_VALUE_TYPE *num, *den;
{
unsigned long q, msb;
int expn = num->exp, expd = den->exp;
if (expn < expd)
return 0;
q = msb = 0;
goto start;
do
{
msb = num->sig[SIGSZ-1] & SIG_MSB;
q <<= 1;
lshift_significand_1 (num, num);
start:
if (msb || cmp_significands (num, den) >= 0)
{
sub_significands (num, num, den);
q |= 1;
}
}
while (--expn >= expd);
num->exp = expd;
normalize (num);
return q;
}
/* Render R as a decimal floating point constant. Emit DIGITS significant
digits in the result, bounded by BUF_SIZE. If DIGITS is 0, choose the
maximum for the representation. If CROP_TRAILING_ZEROS, strip trailing
zeros. */
#define M_LOG10_2 0.30102999566398119521
void
real_to_decimal (str, r_orig, buf_size, digits, crop_trailing_zeros)
char *str;
const REAL_VALUE_TYPE *r_orig;
size_t buf_size, digits;
int crop_trailing_zeros;
{
const REAL_VALUE_TYPE *one, *ten;
REAL_VALUE_TYPE r, pten, u, v;
int dec_exp, cmp_one, digit;
size_t max_digits;
char *p, *first, *last;
bool sign;
r = *r_orig;
switch (r.class)
{
case rvc_zero:
strcpy (str, (r.sign ? "-0.0" : "0.0"));
return;
case rvc_normal:
break;
case rvc_inf:
strcpy (str, (r.sign ? "-Inf" : "+Inf"));
return;
case rvc_nan:
/* ??? Print the significand as well, if not canonical? */
strcpy (str, (r.sign ? "-NaN" : "+NaN"));
return;
default:
abort ();
}
/* Estimate the decimal exponent, and compute the length of the string it
will print as. Be conservative and add one to account for possible
overflow or rounding error. */
dec_exp = r.exp * M_LOG10_2;
for (max_digits = 1; dec_exp ; max_digits++)
dec_exp /= 10;
/* Bound the number of digits printed by the size of the output buffer. */
max_digits = buf_size - 1 - 1 - 2 - max_digits - 1;
if (max_digits > buf_size)
abort ();
if (digits > max_digits)
digits = max_digits;
/* Bound the number of digits printed by the size of the representation. */
max_digits = SIGNIFICAND_BITS * M_LOG10_2;
if (digits == 0 || digits > max_digits)
digits = max_digits;
one = real_digit (1);
ten = ten_to_ptwo (0);
sign = r.sign;
r.sign = 0;
dec_exp = 0;
pten = *one;
cmp_one = do_compare (&r, one, 0);
if (cmp_one > 0)
{
int m;
/* Number is greater than one. Convert significand to an integer
and strip trailing decimal zeros. */
u = r;
u.exp = SIGNIFICAND_BITS - 1;
/* Largest M, such that 10**2**M fits within SIGNIFICAND_BITS. */
m = floor_log2 (max_digits);
/* Iterate over the bits of the possible powers of 10 that might
be present in U and eliminate them. That is, if we find that
10**2**M divides U evenly, keep the division and increase
DEC_EXP by 2**M. */
do
{
REAL_VALUE_TYPE t;
do_divide (&t, &u, ten_to_ptwo (m));
do_fix_trunc (&v, &t);
if (cmp_significands (&v, &t) == 0)
{
u = t;
dec_exp += 1 << m;
}
}
while (--m >= 0);
/* Revert the scaling to integer that we performed earlier. */
u.exp += r.exp - (SIGNIFICAND_BITS - 1);
r = u;
/* Find power of 10. Do this by dividing out 10**2**M when
this is larger than the current remainder. Fill PTEN with
the power of 10 that we compute. */
m = floor_log2 ((int)(r.exp * M_LOG10_2)) + 1;
do
{
const REAL_VALUE_TYPE *ptentwo = ten_to_ptwo (m);
if (do_compare (&u, ptentwo, 0) >= 0)
{
do_divide (&u, &u, ptentwo);
do_multiply (&pten, &pten, ptentwo);
dec_exp += 1 << m;
}
}
while (--m >= 0);
}
else if (cmp_one < 0)
{
int m;
/* Number is less than one. Pad significand with leading
decimal zeros. */
v = r;
while (1)
{
/* Stop if we'd shift bits off the bottom. */
if (v.sig[0] & 7)
break;
do_multiply (&u, &v, ten);
/* Stop if we're now >= 1. */
if (u.exp > 0)
break;
v = u;
dec_exp -= 1;
}
r = v;
/* Find power of 10. Do this by multiplying in P=10**2**M when
the current remainder is smaller than 1/P. Fill PTEN with the
power of 10 that we compute. */
m = floor_log2 ((int)(-r.exp * M_LOG10_2)) + 1;
do
{
const REAL_VALUE_TYPE *ptentwo = ten_to_ptwo (m);
const REAL_VALUE_TYPE *ptenmtwo = ten_to_mptwo (m);
if (do_compare (&v, ptenmtwo, 0) <= 0)
{
do_multiply (&v, &v, ptentwo);
do_multiply (&pten, &pten, ptentwo);
dec_exp -= 1 << m;
}
}
while (--m >= 0);
/* Invert the positive power of 10 that we've collected so far. */
do_divide (&pten, one, &pten);
}
p = str;
if (sign)
*p++ = '-';
first = p++;
/* At this point, PTEN should contain the nearest power of 10 smaller
than R, such that this division produces the first digit.
Using a divide-step primitive that returns the complete integral
remainder avoids the rounding error that would be produced if
we were to use do_divide here and then simply multiply by 10 for
each subsequent digit. */
digit = rtd_divmod (&r, &pten);
/* Be prepared for error in that division via underflow ... */
if (digit == 0 && cmp_significand_0 (&r))
{
/* Multiply by 10 and try again. */
do_multiply (&r, &r, ten);
digit = rtd_divmod (&r, &pten);
dec_exp -= 1;
if (digit == 0)
abort ();
}
/* ... or overflow. */
if (digit == 10)
{
*p++ = '1';
if (--digits > 0)
*p++ = '0';
dec_exp += 1;
}
else if (digit > 10)
abort ();
else
*p++ = digit + '0';
/* Generate subsequent digits. */
while (--digits > 0)
{
do_multiply (&r, &r, ten);
digit = rtd_divmod (&r, &pten);
*p++ = digit + '0';
}
last = p;
/* Generate one more digit with which to do rounding. */
do_multiply (&r, &r, ten);
digit = rtd_divmod (&r, &pten);
/* Round the result. */
if (digit == 5)
{
/* Round to nearest. If R is non-zero there are additional
non-zero digits to be extracted. */
if (cmp_significand_0 (&r))
digit++;
/* Round to even. */
else if ((p[-1] - '0') & 1)
digit++;
}
if (digit > 5)
{
while (p > first)
{
digit = *--p;
if (digit == '9')
*p = '0';
else
{
*p = digit + 1;
break;
}
}
/* Carry out of the first digit. This means we had all 9's and
now have all 0's. "Prepend" a 1 by overwriting the first 0. */
if (p == first)
{
first[1] = '1';
dec_exp++;
}
}
/* Insert the decimal point. */
first[0] = first[1];
first[1] = '.';
/* If requested, drop trailing zeros. Never crop past "1.0". */
if (crop_trailing_zeros)
while (last > first + 3 && last[-1] == '0')
last--;
/* Append the exponent. */
sprintf (last, "e%+d", dec_exp);
}
/* Render R as a hexadecimal floating point constant. Emit DIGITS
significant digits in the result, bounded by BUF_SIZE. If DIGITS is 0,
choose the maximum for the representation. If CROP_TRAILING_ZEROS,
strip trailing zeros. */
void
real_to_hexadecimal (str, r, buf_size, digits, crop_trailing_zeros)
char *str;
const REAL_VALUE_TYPE *r;
size_t buf_size, digits;
int crop_trailing_zeros;
{
int i, j, exp = r->exp;
char *p, *first;
char exp_buf[16];
size_t max_digits;
switch (r->class)
{
case rvc_zero:
exp = 0;
break;
case rvc_normal:
break;
case rvc_inf:
strcpy (str, (r->sign ? "-Inf" : "+Inf"));
return;
case rvc_nan:
/* ??? Print the significand as well, if not canonical? */
strcpy (str, (r->sign ? "-NaN" : "+NaN"));
return;
default:
abort ();
}
if (digits == 0)
digits = SIGNIFICAND_BITS / 4;
/* Bound the number of digits printed by the size of the output buffer. */
sprintf (exp_buf, "p%+d", exp);
max_digits = buf_size - strlen (exp_buf) - r->sign - 4 - 1;
if (max_digits > buf_size)
abort ();
if (digits > max_digits)
digits = max_digits;
p = str;
if (r->sign)
*p++ = '-';
*p++ = '0';
*p++ = 'x';
*p++ = '0';
*p++ = '.';
first = p;
for (i = SIGSZ - 1; i >= 0; --i)
for (j = HOST_BITS_PER_LONG - 4; j >= 0; j -= 4)
{
*p++ = "0123456789abcdef"[(r->sig[i] >> j) & 15];
if (--digits == 0)
goto out;
}
out:
if (crop_trailing_zeros)
while (p > first + 1 && p[-1] == '0')
p--;
sprintf (p, "p%+d", exp);
}
/* Initialize R from a decimal or hexadecimal string. The string is
assumed to have been syntax checked already. */
void
real_from_string (r, str)
REAL_VALUE_TYPE *r;
const char *str;
{
int exp = 0;
bool sign = false;
get_zero (r, 0);
if (*str == '-')
{
sign = true;
str++;
}
else if (*str == '+')
str++;
if (str[0] == '0' && str[1] == 'x')
{
/* Hexadecimal floating point. */
int pos = SIGNIFICAND_BITS - 4, d;
str += 2;
while (*str == '0')
str++;
while (1)
{
d = hex_value (*str);
if (d == _hex_bad)
break;
if (pos >= 0)
{
r->sig[pos / HOST_BITS_PER_LONG]
|= (unsigned long) d << (pos % HOST_BITS_PER_LONG);
pos -= 4;
}
exp += 4;
str++;
}
if (*str == '.')
{
str++;
if (pos == SIGNIFICAND_BITS - 4)
{
while (*str == '0')
str++, exp -= 4;
}
while (1)
{
d = hex_value (*str);
if (d == _hex_bad)
break;
if (pos >= 0)
{
r->sig[pos / HOST_BITS_PER_LONG]
|= (unsigned long) d << (pos % HOST_BITS_PER_LONG);
pos -= 4;
}
str++;
}
}
if (*str == 'p' || *str == 'P')
{
bool exp_neg = false;
str++;
if (*str == '-')
{
exp_neg = true;
str++;
}
else if (*str == '+')
str++;
d = 0;
while (ISDIGIT (*str))
{
d *= 10;
d += *str - '0';
if (d > MAX_EXP)
{
/* Overflowed the exponent. */
if (exp_neg)
goto underflow;
else
goto overflow;
}
str++;
}
if (exp_neg)
d = -d;
exp += d;
}
r->class = rvc_normal;
r->exp = exp;
normalize (r);
}
else
{
/* Decimal floating point. */
const REAL_VALUE_TYPE *ten = ten_to_ptwo (0);
int d;
while (*str == '0')
str++;
while (ISDIGIT (*str))
{
d = *str++ - '0';
do_multiply (r, r, ten);
if (d)
do_add (r, r, real_digit (d), 0);
}
if (*str == '.')
{
str++;
if (r->class == rvc_zero)
{
while (*str == '0')
str++, exp--;
}
while (ISDIGIT (*str))
{
d = *str++ - '0';
do_multiply (r, r, ten);
if (d)
do_add (r, r, real_digit (d), 0);
exp--;
}
}
if (*str == 'e' || *str == 'E')
{
bool exp_neg = false;
str++;
if (*str == '-')
{
exp_neg = true;
str++;
}
else if (*str == '+')
str++;
d = 0;
while (ISDIGIT (*str))
{
d *= 10;
d += *str - '0';
if (d > MAX_EXP)
{
/* Overflowed the exponent. */
if (exp_neg)
goto underflow;
else
goto overflow;
}
str++;
}
if (exp_neg)
d = -d;
exp += d;
}
if (exp)
times_pten (r, exp);
}
r->sign = sign;
return;
underflow:
get_zero (r, sign);
return;
overflow:
get_inf (r, sign);
return;
}
/* Legacy. Similar, but return the result directly. */
REAL_VALUE_TYPE
real_from_string2 (s, mode)
const char *s;
enum machine_mode mode;
{
REAL_VALUE_TYPE r;
real_from_string (&r, s);
if (mode != VOIDmode)
real_convert (&r, mode, &r);
return r;
}
/* Initialize R from the integer pair HIGH+LOW. */
void
real_from_integer (r, mode, low, high, unsigned_p)
REAL_VALUE_TYPE *r;
enum machine_mode mode;
unsigned HOST_WIDE_INT low;
HOST_WIDE_INT high;
int unsigned_p;
{
if (low == 0 && high == 0)
get_zero (r, 0);
else
{
r->class = rvc_normal;
r->sign = high < 0 && !unsigned_p;
r->exp = 2 * HOST_BITS_PER_WIDE_INT;
if (r->sign)
{
high = ~high;
if (low == 0)
high += 1;
else
low = -low;
}
if (HOST_BITS_PER_LONG == HOST_BITS_PER_WIDE_INT)
{
r->sig[SIGSZ-1] = high;
r->sig[SIGSZ-2] = low;
memset (r->sig, 0, sizeof(long)*(SIGSZ-2));
}
else if (HOST_BITS_PER_LONG*2 == HOST_BITS_PER_WIDE_INT)
{
r->sig[SIGSZ-1] = high >> (HOST_BITS_PER_LONG - 1) >> 1;
r->sig[SIGSZ-2] = high;
r->sig[SIGSZ-3] = low >> (HOST_BITS_PER_LONG - 1) >> 1;
r->sig[SIGSZ-4] = low;
if (SIGSZ > 4)
memset (r->sig, 0, sizeof(long)*(SIGSZ-4));
}
else
abort ();
normalize (r);
}
if (mode != VOIDmode)
real_convert (r, mode, r);
}
/* Returns 10**2**N. */
static const REAL_VALUE_TYPE *
ten_to_ptwo (n)
int n;
{
static REAL_VALUE_TYPE tens[EXP_BITS];
if (n < 0 || n >= EXP_BITS)
abort ();
if (tens[n].class == rvc_zero)
{
if (n < (HOST_BITS_PER_WIDE_INT == 64 ? 5 : 4))
{
HOST_WIDE_INT t = 10;
int i;
for (i = 0; i < n; ++i)
t *= t;
real_from_integer (&tens[n], VOIDmode, t, 0, 1);
}
else
{
const REAL_VALUE_TYPE *t = ten_to_ptwo (n - 1);
do_multiply (&tens[n], t, t);
}
}
return &tens[n];
}
/* Returns 10**(-2**N). */
static const REAL_VALUE_TYPE *
ten_to_mptwo (n)
int n;
{
static REAL_VALUE_TYPE tens[EXP_BITS];
if (n < 0 || n >= EXP_BITS)
abort ();
if (tens[n].class == rvc_zero)
do_divide (&tens[n], real_digit (1), ten_to_ptwo (n));
return &tens[n];
}
/* Returns N. */
static const REAL_VALUE_TYPE *
real_digit (n)
int n;
{
static REAL_VALUE_TYPE num[10];
if (n < 0 || n > 9)
abort ();
if (n > 0 && num[n].class == rvc_zero)
real_from_integer (&num[n], VOIDmode, n, 0, 1);
return &num[n];
}
/* Multiply R by 10**EXP. */
static void
times_pten (r, exp)
REAL_VALUE_TYPE *r;
int exp;
{
REAL_VALUE_TYPE pten, *rr;
bool negative = (exp < 0);
int i;
if (negative)
{
exp = -exp;
pten = *real_digit (1);
rr = &pten;
}
else
rr = r;
for (i = 0; exp > 0; ++i, exp >>= 1)
if (exp & 1)
do_multiply (rr, rr, ten_to_ptwo (i));
if (negative)
do_divide (r, r, &pten);
}
/* Fills R with +Inf. */
void
real_inf (r)
REAL_VALUE_TYPE *r;
{
get_inf (r, 0);
}
/* Fills R with a NaN whose significand is described by STR. If QUIET,
we force a QNaN, else we force an SNaN. The string, if not empty,
is parsed as a number and placed in the significand. Return true
if the string was successfully parsed. */
bool
real_nan (r, str, quiet, mode)
REAL_VALUE_TYPE *r;
const char *str;
int quiet;
enum machine_mode mode;
{
const struct real_format *fmt;
fmt = real_format_for_mode[mode - QFmode];
if (fmt == NULL)
abort ();
if (*str == 0)
{
if (quiet)
get_canonical_qnan (r, 0);
else
get_canonical_snan (r, 0);
}
else
{
int base = 10, d;
bool neg = false;
memset (r, 0, sizeof (*r));
r->class = rvc_nan;
/* Parse akin to strtol into the significand of R. */
while (ISSPACE (*str))
str++;
if (*str == '-')
str++, neg = true;
else if (*str == '+')
str++;
if (*str == '0')
{
if (*++str == 'x')
str++, base = 16;
else
base = 8;
}
while ((d = hex_value (*str)) < base)
{
REAL_VALUE_TYPE u;
switch (base)
{
case 8:
lshift_significand (r, r, 3);
break;
case 16:
lshift_significand (r, r, 4);
break;
case 10:
lshift_significand_1 (&u, r);
lshift_significand (r, r, 3);
add_significands (r, r, &u);
break;
default:
abort ();
}
get_zero (&u, 0);
u.sig[0] = d;
add_significands (r, r, &u);
str++;
}
/* Must have consumed the entire string for success. */
if (*str != 0)
return false;
/* Shift the significand into place such that the bits
are in the most significant bits for the format. */
lshift_significand (r, r, SIGNIFICAND_BITS - fmt->p);
/* Our MSB is always unset for NaNs. */
r->sig[SIGSZ-1] &= ~SIG_MSB;
/* Force quiet or signalling NaN. */
if (quiet)
r->sig[SIGSZ-1] |= SIG_MSB >> 1;
else
r->sig[SIGSZ-1] &= ~(SIG_MSB >> 1);
/* Force at least one bit of the significand set. */
for (d = 0; d < SIGSZ; ++d)
if (r->sig[d])
break;
if (d == SIGSZ)
r->sig[SIGSZ-1] |= SIG_MSB >> 2;
/* Our intermediate format forces QNaNs to have MSB-1 set.
If the target format has QNaNs with the top bit unset,
mirror the output routines and invert the top two bits. */
if (!fmt->qnan_msb_set)
r->sig[SIGSZ-1] ^= (SIG_MSB >> 1) | (SIG_MSB >> 2);
}
return true;
}
/* Fills R with 2**N. */
void
real_2expN (r, n)
REAL_VALUE_TYPE *r;
int n;
{
memset (r, 0, sizeof (*r));
n++;
if (n > MAX_EXP)
r->class = rvc_inf;
else if (n < -MAX_EXP)
;
else
{
r->class = rvc_normal;
r->exp = n;
r->sig[SIGSZ-1] = SIG_MSB;
}
}
static void
round_for_format (fmt, r)
const struct real_format *fmt;
REAL_VALUE_TYPE *r;
{
int p2, np2, i, w;
unsigned long sticky;
bool guard, lsb;
int emin2m1, emax2;
p2 = fmt->p * fmt->log2_b;
emin2m1 = (fmt->emin - 1) * fmt->log2_b;
emax2 = fmt->emax * fmt->log2_b;
np2 = SIGNIFICAND_BITS - p2;
switch (r->class)
{
underflow:
get_zero (r, r->sign);
case rvc_zero:
if (!fmt->has_signed_zero)
r->sign = 0;
return;
overflow:
get_inf (r, r->sign);
case rvc_inf:
return;
case rvc_nan:
clear_significand_below (r, np2);
/* If we've cleared the entire significand, we need one bit
set for this to continue to be a NaN. */
for (i = 0; i < SIGSZ; ++i)
if (r->sig[i])
break;
if (i == SIGSZ)
r->sig[SIGSZ-1] = SIG_MSB >> 2;
return;
case rvc_normal:
break;
default:
abort ();
}
/* If we're not base2, normalize the exponent to a multiple of
the true base. */
if (fmt->log2_b != 1)
{
int shift = r->exp & (fmt->log2_b - 1);
if (shift)
{
shift = fmt->log2_b - shift;
sticky_rshift_significand (r, r, shift);
r->exp += shift;
}
}
/* Check the range of the exponent. If we're out of range,
either underflow or overflow. */
if (r->exp > emax2)
goto overflow;
else if (r->exp <= emin2m1)
{
int diff;
if (!fmt->has_denorm)
{
/* Don't underflow completely until we've had a chance to round. */
if (r->exp < emin2m1)
goto underflow;
}
else
{
diff = emin2m1 - r->exp + 1;
if (diff > p2)
goto underflow;
/* De-normalize the significand. */
sticky_rshift_significand (r, r, diff);
r->exp += diff;
}
}
/* There are P2 true significand bits, followed by one guard bit,
followed by one sticky bit, followed by stuff. Fold nonzero
stuff into the sticky bit. */
sticky = 0;
for (i = 0, w = (np2 - 1) / HOST_BITS_PER_LONG; i < w; ++i)
sticky |= r->sig[i];
sticky |=
r->sig[w] & (((unsigned long)1 << ((np2 - 1) % HOST_BITS_PER_LONG)) - 1);
guard = test_significand_bit (r, np2 - 1);
lsb = test_significand_bit (r, np2);
/* Round to even. */
if (guard && (sticky || lsb))
{
REAL_VALUE_TYPE u;
get_zero (&u, 0);
set_significand_bit (&u, np2);
if (add_significands (r, r, &u))
{
/* Overflow. Means the significand had been all ones, and
is now all zeros. Need to increase the exponent, and
possibly re-normalize it. */
if (++r->exp > emax2)
goto overflow;
r->sig[SIGSZ-1] = SIG_MSB;
if (fmt->log2_b != 1)
{
int shift = r->exp & (fmt->log2_b - 1);
if (shift)
{
shift = fmt->log2_b - shift;
sticky_rshift_significand (r, r, shift);
r->exp += shift;
if (r->exp > emax2)
goto overflow;
}
}
}
}
/* Catch underflow that we deferred until after rounding. */
if (r->exp <= emin2m1)
goto underflow;
/* Clear out trailing garbage. */
clear_significand_below (r, np2);
}
/* Extend or truncate to a new mode. */
void
real_convert (r, mode, a)
REAL_VALUE_TYPE *r;
enum machine_mode mode;
const REAL_VALUE_TYPE *a;
{
const struct real_format *fmt;
fmt = real_format_for_mode[mode - QFmode];
if (fmt == NULL)
abort ();
*r = *a;
round_for_format (fmt, r);
/* round_for_format de-normalizes denormals. Undo just that part. */
if (r->class == rvc_normal)
normalize (r);
}
/* Legacy. Likewise, except return the struct directly. */
REAL_VALUE_TYPE
real_value_truncate (mode, a)
enum machine_mode mode;
REAL_VALUE_TYPE a;
{
REAL_VALUE_TYPE r;
real_convert (&r, mode, &a);
return r;
}
/* Return true if truncating to MODE is exact. */
bool
exact_real_truncate (mode, a)
enum machine_mode mode;
const REAL_VALUE_TYPE *a;
{
REAL_VALUE_TYPE t;
real_convert (&t, mode, a);
return real_identical (&t, a);
}
/* Write R to the given target format. Place the words of the result
in target word order in BUF. There are always 32 bits in each
long, no matter the size of the host long.
Legacy: return word 0 for implementing REAL_VALUE_TO_TARGET_SINGLE. */
long
real_to_target_fmt (buf, r_orig, fmt)
long *buf;
const REAL_VALUE_TYPE *r_orig;
const struct real_format *fmt;
{
REAL_VALUE_TYPE r;
long buf1;
r = *r_orig;
round_for_format (fmt, &r);
if (!buf)
buf = &buf1;
(*fmt->encode) (fmt, buf, &r);
return *buf;
}
/* Similar, but look up the format from MODE. */
long
real_to_target (buf, r, mode)
long *buf;
const REAL_VALUE_TYPE *r;
enum machine_mode mode;
{
const struct real_format *fmt;
fmt = real_format_for_mode[mode - QFmode];
if (fmt == NULL)
abort ();
return real_to_target_fmt (buf, r, fmt);
}
/* Read R from the given target format. Read the words of the result
in target word order in BUF. There are always 32 bits in each
long, no matter the size of the host long. */
void
real_from_target_fmt (r, buf, fmt)
REAL_VALUE_TYPE *r;
const long *buf;
const struct real_format *fmt;
{
(*fmt->decode) (fmt, r, buf);
}
/* Similar, but look up the format from MODE. */
void
real_from_target (r, buf, mode)
REAL_VALUE_TYPE *r;
const long *buf;
enum machine_mode mode;
{
const struct real_format *fmt;
fmt = real_format_for_mode[mode - QFmode];
if (fmt == NULL)
abort ();
(*fmt->decode) (fmt, r, buf);
}
/* Return the number of bits in the significand for MODE. */
/* ??? Legacy. Should get access to real_format directly. */
int
significand_size (mode)
enum machine_mode mode;
{
const struct real_format *fmt;
fmt = real_format_for_mode[mode - QFmode];
if (fmt == NULL)
return 0;
return fmt->p * fmt->log2_b;
}
/* Return a hash value for the given real value. */
/* ??? The "unsigned int" return value is intended to be hashval_t,
but I didn't want to pull hashtab.h into real.h. */
unsigned int
real_hash (r)
const REAL_VALUE_TYPE *r;
{
unsigned int h;
size_t i;
h = r->class | (r->sign << 2);
switch (r->class)
{
case rvc_zero:
case rvc_inf:
break;
case rvc_normal:
h |= r->exp << 3;
/* FALLTHRU */
case rvc_nan:
if (sizeof(unsigned long) > sizeof(unsigned int))
for (i = 0; i < SIGSZ; ++i)
{
unsigned long s = r->sig[i];
h ^= s ^ (s >> (HOST_BITS_PER_LONG / 2));
}
else
for (i = 0; i < SIGSZ; ++i)
h ^= r->sig[i];
break;
default:
abort ();
}
return h;
}
/* IEEE single-precision format. */
static void encode_ieee_single PARAMS ((const struct real_format *fmt,
long *, const REAL_VALUE_TYPE *));
static void decode_ieee_single PARAMS ((const struct real_format *,
REAL_VALUE_TYPE *, const long *));
static void
encode_ieee_single (fmt, buf, r)
const struct real_format *fmt;
long *buf;
const REAL_VALUE_TYPE *r;
{
unsigned long image, sig, exp;
bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
image = r->sign << 31;
sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 24)) & 0x7fffff;
switch (r->class)
{
case rvc_zero:
break;
case rvc_inf:
if (fmt->has_inf)
image |= 255 << 23;
else
image |= 0x7fffffff;
break;
case rvc_nan:
if (fmt->has_nans)
{
image |= 255 << 23;
image |= sig;
if (!fmt->qnan_msb_set)
image ^= 1 << 23 | 1 << 22;
}
else
image |= 0x7fffffff;
break;
case rvc_normal:
/* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
whereas the intermediate representation is 0.F x 2**exp.
Which means we're off by one. */
if (denormal)
exp = 0;
else
exp = r->exp + 127 - 1;
image |= exp << 23;
image |= sig;
break;
default:
abort ();
}
buf[0] = image;
}
static void
decode_ieee_single (fmt, r, buf)
const struct real_format *fmt;
REAL_VALUE_TYPE *r;
const long *buf;
{
unsigned long image = buf[0] & 0xffffffff;
bool sign = (image >> 31) & 1;
int exp = (image >> 23) & 0xff;
memset (r, 0, sizeof (*r));
image <<= HOST_BITS_PER_LONG - 24;
image &= ~SIG_MSB;
if (exp == 0)
{
if (image && fmt->has_denorm)
{
r->class = rvc_normal;
r->sign = sign;
r->exp = -126;
r->sig[SIGSZ-1] = image << 1;
normalize (r);
}
else if (fmt->has_signed_zero)
r->sign = sign;
}
else if (exp == 255 && (fmt->has_nans || fmt->has_inf))
{
if (image)
{
r->class = rvc_nan;
r->sign = sign;
if (!fmt->qnan_msb_set)
image ^= (SIG_MSB >> 1 | SIG_MSB >> 2);
r->sig[SIGSZ-1] = image;
}
else
{
r->class = rvc_inf;
r->sign = sign;
}
}
else
{
r->class = rvc_normal;
r->sign = sign;
r->exp = exp - 127 + 1;
r->sig[SIGSZ-1] = image | SIG_MSB;
}
}
const struct real_format ieee_single_format =
{
encode_ieee_single,
decode_ieee_single,
2,
1,
24,
-125,
128,
true,
true,
true,
true,
true
};
/* IEEE double-precision format. */
static void encode_ieee_double PARAMS ((const struct real_format *fmt,
long *, const REAL_VALUE_TYPE *));
static void decode_ieee_double PARAMS ((const struct real_format *,
REAL_VALUE_TYPE *, const long *));
static void
encode_ieee_double (fmt, buf, r)
const struct real_format *fmt;
long *buf;
const REAL_VALUE_TYPE *r;
{
unsigned long image_lo, image_hi, sig_lo, sig_hi, exp;
bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
image_hi = r->sign << 31;
image_lo = 0;
if (HOST_BITS_PER_LONG == 64)
{
sig_hi = r->sig[SIGSZ-1];
sig_lo = (sig_hi >> (64 - 53)) & 0xffffffff;
sig_hi = (sig_hi >> (64 - 53 + 1) >> 31) & 0xfffff;
}
else
{
sig_hi = r->sig[SIGSZ-1];
sig_lo = r->sig[SIGSZ-2];
sig_lo = (sig_hi << 21) | (sig_lo >> 11);
sig_hi = (sig_hi >> 11) & 0xfffff;
}
switch (r->class)
{
case rvc_zero:
break;
case rvc_inf:
if (fmt->has_inf)
image_hi |= 2047 << 20;
else
{
image_hi |= 0x7fffffff;
image_lo = 0xffffffff;
}
break;
case rvc_nan:
if (fmt->has_nans)
{
image_hi |= 2047 << 20;
image_hi |= sig_hi;
if (!fmt->qnan_msb_set)
image_hi ^= 1 << 19 | 1 << 18;
image_lo = sig_lo;
}
else
{
image_hi |= 0x7fffffff;
image_lo = 0xffffffff;
}
break;
case rvc_normal:
/* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
whereas the intermediate representation is 0.F x 2**exp.
Which means we're off by one. */
if (denormal)
exp = 0;
else
exp = r->exp + 1023 - 1;
image_hi |= exp << 20;
image_hi |= sig_hi;
image_lo = sig_lo;
break;
default:
abort ();
}
if (FLOAT_WORDS_BIG_ENDIAN)
buf[0] = image_hi, buf[1] = image_lo;
else
buf[0] = image_lo, buf[1] = image_hi;
}
static void
decode_ieee_double (fmt, r, buf)
const struct real_format *fmt;
REAL_VALUE_TYPE *r;
const long *buf;
{
unsigned long image_hi, image_lo;
bool sign;
int exp;
if (FLOAT_WORDS_BIG_ENDIAN)
image_hi = buf[0], image_lo = buf[1];
else
image_lo = buf[0], image_hi = buf[1];
image_lo &= 0xffffffff;
image_hi &= 0xffffffff;
sign = (image_hi >> 31) & 1;
exp = (image_hi >> 20) & 0x7ff;
memset (r, 0, sizeof (*r));
image_hi <<= 32 - 21;
image_hi |= image_lo >> 21;
image_hi &= 0x7fffffff;
image_lo <<= 32 - 21;
if (exp == 0)
{
if ((image_hi || image_lo) && fmt->has_denorm)
{
r->class = rvc_normal;
r->sign = sign;
r->exp = -1022;
if (HOST_BITS_PER_LONG == 32)
{
image_hi = (image_hi << 1) | (image_lo >> 31);
image_lo <<= 1;
r->sig[SIGSZ-1] = image_hi;
r->sig[SIGSZ-2] = image_lo;
}
else
{
image_hi = (image_hi << 31 << 2) | (image_lo << 1);
r->sig[SIGSZ-1] = image_hi;
}
normalize (r);
}
else if (fmt->has_signed_zero)
r->sign = sign;
}
else if (exp == 2047 && (fmt->has_nans || fmt->has_inf))
{
if (image_hi || image_lo)
{
r->class = rvc_nan;
r->sign = sign;
if (HOST_BITS_PER_LONG == 32)
{
r->sig[SIGSZ-1] = image_hi;
r->sig[SIGSZ-2] = image_lo;
}
else
r->sig[SIGSZ-1] = (image_hi << 31 << 1) | image_lo;
if (!fmt->qnan_msb_set)
r->sig[SIGSZ-1] ^= (SIG_MSB >> 1 | SIG_MSB >> 2);
}
else
{
r->class = rvc_inf;
r->sign = sign;
}
}
else
{
r->class = rvc_normal;
r->sign = sign;
r->exp = exp - 1023 + 1;
if (HOST_BITS_PER_LONG == 32)
{
r->sig[SIGSZ-1] = image_hi | SIG_MSB;
r->sig[SIGSZ-2] = image_lo;
}
else
r->sig[SIGSZ-1] = (image_hi << 31 << 1) | image_lo | SIG_MSB;
}
}
const struct real_format ieee_double_format =
{
encode_ieee_double,
decode_ieee_double,
2,
1,
53,
-1021,
1024,
true,
true,
true,
true,
true
};
/* IEEE extended double precision format. This comes in three
flavours: Intel's as a 12 byte image, Intel's as a 16 byte image,
and Motorola's. */
static void encode_ieee_extended PARAMS ((const struct real_format *fmt,
long *, const REAL_VALUE_TYPE *));
static void decode_ieee_extended PARAMS ((const struct real_format *,
REAL_VALUE_TYPE *, const long *));
static void encode_ieee_extended_128 PARAMS ((const struct real_format *fmt,
long *,
const REAL_VALUE_TYPE *));
static void decode_ieee_extended_128 PARAMS ((const struct real_format *,
REAL_VALUE_TYPE *,
const long *));
static void
encode_ieee_extended (fmt, buf, r)
const struct real_format *fmt;
long *buf;
const REAL_VALUE_TYPE *r;
{
unsigned long image_hi, sig_hi, sig_lo;
bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
image_hi = r->sign << 15;
sig_hi = sig_lo = 0;
switch (r->class)
{
case rvc_zero:
break;
case rvc_inf:
if (fmt->has_inf)
{
image_hi |= 32767;
/* Intel requires the explicit integer bit to be set, otherwise
it considers the value a "pseudo-infinity". Motorola docs
say it doesn't care. */
sig_hi = 0x80000000;
}
else
{
image_hi |= 32767;
sig_lo = sig_hi = 0xffffffff;
}
break;
case rvc_nan:
if (fmt->has_nans)
{
image_hi |= 32767;
if (HOST_BITS_PER_LONG == 32)
{
sig_hi = r->sig[SIGSZ-1];
sig_lo = r->sig[SIGSZ-2];
}
else
{
sig_lo = r->sig[SIGSZ-1];
sig_hi = sig_lo >> 31 >> 1;
sig_lo &= 0xffffffff;
}
if (!fmt->qnan_msb_set)
sig_hi ^= 1 << 30 | 1 << 29;
/* Intel requires the explicit integer bit to be set, otherwise
it considers the value a "pseudo-nan". Motorola docs say it
doesn't care. */
sig_hi |= 0x80000000;
}
else
{
image_hi |= 32767;
sig_lo = sig_hi = 0xffffffff;
}
break;
case rvc_normal:
{
int exp = r->exp;
/* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
whereas the intermediate representation is 0.F x 2**exp.
Which means we're off by one.
Except for Motorola, which consider exp=0 and explicit
integer bit set to continue to be normalized. In theory
this descrepency has been taken care of by the difference
in fmt->emin in round_for_format. */
if (denormal)
exp = 0;
else
{
exp += 16383 - 1;
if (exp < 0)
abort ();
}
image_hi |= exp;
if (HOST_BITS_PER_LONG == 32)
{
sig_hi = r->sig[SIGSZ-1];
sig_lo = r->sig[SIGSZ-2];
}
else
{
sig_lo = r->sig[SIGSZ-1];
sig_hi = sig_lo >> 31 >> 1;
sig_lo &= 0xffffffff;
}
}
break;
default:
abort ();
}
if (FLOAT_WORDS_BIG_ENDIAN)
buf[0] = image_hi << 16, buf[1] = sig_hi, buf[2] = sig_lo;
else
buf[0] = sig_lo, buf[1] = sig_hi, buf[2] = image_hi;
}
static void
encode_ieee_extended_128 (fmt, buf, r)
const struct real_format *fmt;
long *buf;
const REAL_VALUE_TYPE *r;
{
buf[3 * !FLOAT_WORDS_BIG_ENDIAN] = 0;
encode_ieee_extended (fmt, buf+!!FLOAT_WORDS_BIG_ENDIAN, r);
}
static void
decode_ieee_extended (fmt, r, buf)
const struct real_format *fmt;
REAL_VALUE_TYPE *r;
const long *buf;
{
unsigned long image_hi, sig_hi, sig_lo;
bool sign;
int exp;
if (FLOAT_WORDS_BIG_ENDIAN)
image_hi = buf[0] >> 16, sig_hi = buf[1], sig_lo = buf[2];
else
sig_lo = buf[0], sig_hi = buf[1], image_hi = buf[2];
sig_lo &= 0xffffffff;
sig_hi &= 0xffffffff;
image_hi &= 0xffffffff;
sign = (image_hi >> 15) & 1;
exp = image_hi & 0x7fff;
memset (r, 0, sizeof (*r));
if (exp == 0)
{
if ((sig_hi || sig_lo) && fmt->has_denorm)
{
r->class = rvc_normal;
r->sign = sign;
/* When the IEEE format contains a hidden bit, we know that
it's zero at this point, and so shift up the significand
and decrease the exponent to match. In this case, Motorola
defines the explicit integer bit to be valid, so we don't
know whether the msb is set or not. */
r->exp = fmt->emin;
if (HOST_BITS_PER_LONG == 32)
{
r->sig[SIGSZ-1] = sig_hi;
r->sig[SIGSZ-2] = sig_lo;
}
else
r->sig[SIGSZ-1] = (sig_hi << 31 << 1) | sig_lo;
normalize (r);
}
else if (fmt->has_signed_zero)
r->sign = sign;
}
else if (exp == 32767 && (fmt->has_nans || fmt->has_inf))
{
/* See above re "pseudo-infinities" and "pseudo-nans".
Short summary is that the MSB will likely always be
set, and that we don't care about it. */
sig_hi &= 0x7fffffff;
if (sig_hi || sig_lo)
{
r->class = rvc_nan;
r->sign = sign;
if (HOST_BITS_PER_LONG == 32)
{
r->sig[SIGSZ-1] = sig_hi;
r->sig[SIGSZ-2] = sig_lo;
}
else
r->sig[SIGSZ-1] = (sig_hi << 31 << 1) | sig_lo;
if (!fmt->qnan_msb_set)
r->sig[SIGSZ-1] ^= (SIG_MSB >> 1 | SIG_MSB >> 2);
}
else
{
r->class = rvc_inf;
r->sign = sign;
}
}
else
{
r->class = rvc_normal;
r->sign = sign;
r->exp = exp - 16383 + 1;
if (HOST_BITS_PER_LONG == 32)
{
r->sig[SIGSZ-1] = sig_hi;
r->sig[SIGSZ-2] = sig_lo;
}
else
r->sig[SIGSZ-1] = (sig_hi << 31 << 1) | sig_lo;
}
}
static void
decode_ieee_extended_128 (fmt, r, buf)
const struct real_format *fmt;
REAL_VALUE_TYPE *r;
const long *buf;
{
decode_ieee_extended (fmt, r, buf+!!FLOAT_WORDS_BIG_ENDIAN);
}
const struct real_format ieee_extended_motorola_format =
{
encode_ieee_extended,
decode_ieee_extended,
2,
1,
64,
-16382,
16384,
true,
true,
true,
true,
true
};
const struct real_format ieee_extended_intel_96_format =
{
encode_ieee_extended,
decode_ieee_extended,
2,
1,
64,
-16381,
16384,
true,
true,
true,
true,
true
};
const struct real_format ieee_extended_intel_128_format =
{
encode_ieee_extended_128,
decode_ieee_extended_128,
2,
1,
64,
-16381,
16384,
true,
true,
true,
true,
true
};
/* IBM 128-bit extended precision format: a pair of IEEE double precision
numbers whose sum is equal to the extended precision value. The number
with greater magnitude is first. This format has the same magnitude
range as an IEEE double precision value, but effectively 106 bits of
significand precision. Infinity and NaN are represented by their IEEE
double precision value stored in the first number, the second number is
ignored. Zeroes, Infinities, and NaNs are set in both doubles
due to precedent. */
static void encode_ibm_extended PARAMS ((const struct real_format *fmt,
long *, const REAL_VALUE_TYPE *));
static void decode_ibm_extended PARAMS ((const struct real_format *,
REAL_VALUE_TYPE *, const long *));
static void
encode_ibm_extended (fmt, buf, r)
const struct real_format *fmt ATTRIBUTE_UNUSED;
long *buf;
const REAL_VALUE_TYPE *r;
{
REAL_VALUE_TYPE u, v;
switch (r->class)
{
case rvc_zero:
/* Both doubles have sign bit set. */
buf[0] = FLOAT_WORDS_BIG_ENDIAN ? r->sign << 31 : 0;
buf[1] = FLOAT_WORDS_BIG_ENDIAN ? 0 : r->sign << 31;
buf[2] = buf[0];
buf[3] = buf[1];
break;
case rvc_inf:
case rvc_nan:
/* Both doubles set to Inf / NaN. */
encode_ieee_double (&ieee_double_format, &buf[0], r);
buf[2] = buf[0];
buf[3] = buf[1];
return;
case rvc_normal:
/* u = IEEE double precision portion of significand. */
u = *r;
clear_significand_below (&u, SIGNIFICAND_BITS - 53);
/* v = remainder containing additional 53 bits of significand. */
do_add (&v, r, &u, 1);
encode_ieee_double (&ieee_double_format, &buf[0], &u);
encode_ieee_double (&ieee_double_format, &buf[2], &v);
break;
default:
abort ();
}
}
static void
decode_ibm_extended (fmt, r, buf)
const struct real_format *fmt ATTRIBUTE_UNUSED;
REAL_VALUE_TYPE *r;
const long *buf;
{
REAL_VALUE_TYPE u, v;
decode_ieee_double (&ieee_double_format, &u, &buf[0]);
if (u.class != rvc_zero && u.class != rvc_inf && u.class != rvc_nan)
{
decode_ieee_double (&ieee_double_format, &v, &buf[2]);
do_add (r, &u, &v, 0);
}
else
*r = u;
}
const struct real_format ibm_extended_format =
{
encode_ibm_extended,
decode_ibm_extended,
2,
1,
53 + 53,
-1021,
1024,
true,
true,
true,
true,
true
};
/* IEEE quad precision format. */
static void encode_ieee_quad PARAMS ((const struct real_format *fmt,
long *, const REAL_VALUE_TYPE *));
static void decode_ieee_quad PARAMS ((const struct real_format *,
REAL_VALUE_TYPE *, const long *));
static void
encode_ieee_quad (fmt, buf, r)
const struct real_format *fmt;
long *buf;
const REAL_VALUE_TYPE *r;
{
unsigned long image3, image2, image1, image0, exp;
bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
REAL_VALUE_TYPE u;
image3 = r->sign << 31;
image2 = 0;
image1 = 0;
image0 = 0;
rshift_significand (&u, r, SIGNIFICAND_BITS - 113);
switch (r->class)
{
case rvc_zero:
break;
case rvc_inf:
if (fmt->has_inf)
image3 |= 32767 << 16;
else
{
image3 |= 0x7fffffff;
image2 = 0xffffffff;
image1 = 0xffffffff;
image0 = 0xffffffff;
}
break;
case rvc_nan:
if (fmt->has_nans)
{
image3 |= 32767 << 16;
if (HOST_BITS_PER_LONG == 32)
{
image0 = u.sig[0];
image1 = u.sig[1];
image2 = u.sig[2];
image3 |= u.sig[3] & 0xffff;
}
else
{
image0 = u.sig[0];
image1 = image0 >> 31 >> 1;
image2 = u.sig[1];
image3 |= (image2 >> 31 >> 1) & 0xffff;
image0 &= 0xffffffff;
image2 &= 0xffffffff;
}
if (!fmt->qnan_msb_set)
image3 ^= 1 << 15 | 1 << 14;
}
else
{
image3 |= 0x7fffffff;
image2 = 0xffffffff;
image1 = 0xffffffff;
image0 = 0xffffffff;
}
break;
case rvc_normal:
/* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
whereas the intermediate representation is 0.F x 2**exp.
Which means we're off by one. */
if (denormal)
exp = 0;
else
exp = r->exp + 16383 - 1;
image3 |= exp << 16;
if (HOST_BITS_PER_LONG == 32)
{
image0 = u.sig[0];
image1 = u.sig[1];
image2 = u.sig[2];
image3 |= u.sig[3] & 0xffff;
}
else
{
image0 = u.sig[0];
image1 = image0 >> 31 >> 1;
image2 = u.sig[1];
image3 |= (image2 >> 31 >> 1) & 0xffff;
image0 &= 0xffffffff;
image2 &= 0xffffffff;
}
break;
default:
abort ();
}
if (FLOAT_WORDS_BIG_ENDIAN)
{
buf[0] = image3;
buf[1] = image2;
buf[2] = image1;
buf[3] = image0;
}
else
{
buf[0] = image0;
buf[1] = image1;
buf[2] = image2;
buf[3] = image3;
}
}
static void
decode_ieee_quad (fmt, r, buf)
const struct real_format *fmt;
REAL_VALUE_TYPE *r;
const long *buf;
{
unsigned long image3, image2, image1, image0;
bool sign;
int exp;
if (FLOAT_WORDS_BIG_ENDIAN)
{
image3 = buf[0];
image2 = buf[1];
image1 = buf[2];
image0 = buf[3];
}
else
{
image0 = buf[0];
image1 = buf[1];
image2 = buf[2];
image3 = buf[3];
}
image0 &= 0xffffffff;
image1 &= 0xffffffff;
image2 &= 0xffffffff;
sign = (image3 >> 31) & 1;
exp = (image3 >> 16) & 0x7fff;
image3 &= 0xffff;
memset (r, 0, sizeof (*r));
if (exp == 0)
{
if ((image3 | image2 | image1 | image0) && fmt->has_denorm)
{
r->class = rvc_normal;
r->sign = sign;
r->exp = -16382 + (SIGNIFICAND_BITS - 112);
if (HOST_BITS_PER_LONG == 32)
{
r->sig[0] = image0;
r->sig[1] = image1;
r->sig[2] = image2;
r->sig[3] = image3;
}
else
{
r->sig[0] = (image1 << 31 << 1) | image0;
r->sig[1] = (image3 << 31 << 1) | image2;
}
normalize (r);
}
else if (fmt->has_signed_zero)
r->sign = sign;
}
else if (exp == 32767 && (fmt->has_nans || fmt->has_inf))
{
if (image3 | image2 | image1 | image0)
{
r->class = rvc_nan;
r->sign = sign;
if (HOST_BITS_PER_LONG == 32)
{
r->sig[0] = image0;
r->sig[1] = image1;
r->sig[2] = image2;
r->sig[3] = image3;
}
else
{
r->sig[0] = (image1 << 31 << 1) | image0;
r->sig[1] = (image3 << 31 << 1) | image2;
}
lshift_significand (r, r, SIGNIFICAND_BITS - 113);
if (!fmt->qnan_msb_set)
r->sig[SIGSZ-1] ^= (SIG_MSB >> 1 | SIG_MSB >> 2);
}
else
{
r->class = rvc_inf;
r->sign = sign;
}
}
else
{
r->class = rvc_normal;
r->sign = sign;
r->exp = exp - 16383 + 1;
if (HOST_BITS_PER_LONG == 32)
{
r->sig[0] = image0;
r->sig[1] = image1;
r->sig[2] = image2;
r->sig[3] = image3;
}
else
{
r->sig[0] = (image1 << 31 << 1) | image0;
r->sig[1] = (image3 << 31 << 1) | image2;
}
lshift_significand (r, r, SIGNIFICAND_BITS - 113);
r->sig[SIGSZ-1] |= SIG_MSB;
}
}
const struct real_format ieee_quad_format =
{
encode_ieee_quad,
decode_ieee_quad,
2,
1,
113,
-16381,
16384,
true,
true,
true,
true,
true
};
/* Descriptions of VAX floating point formats can be found beginning at
http://www.openvms.compaq.com:8000/73final/4515/4515pro_013.html#f_floating_point_format
The thing to remember is that they're almost IEEE, except for word
order, exponent bias, and the lack of infinities, nans, and denormals.
We don't implement the H_floating format here, simply because neither
the VAX or Alpha ports use it. */
static void encode_vax_f PARAMS ((const struct real_format *fmt,
long *, const REAL_VALUE_TYPE *));
static void decode_vax_f PARAMS ((const struct real_format *,
REAL_VALUE_TYPE *, const long *));
static void encode_vax_d PARAMS ((const struct real_format *fmt,
long *, const REAL_VALUE_TYPE *));
static void decode_vax_d PARAMS ((const struct real_format *,
REAL_VALUE_TYPE *, const long *));
static void encode_vax_g PARAMS ((const struct real_format *fmt,
long *, const REAL_VALUE_TYPE *));
static void decode_vax_g PARAMS ((const struct real_format *,
REAL_VALUE_TYPE *, const long *));
static void
encode_vax_f (fmt, buf, r)
const struct real_format *fmt ATTRIBUTE_UNUSED;
long *buf;
const REAL_VALUE_TYPE *r;
{
unsigned long sign, exp, sig, image;
sign = r->sign << 15;
switch (r->class)
{
case rvc_zero:
image = 0;
break;
case rvc_inf:
case rvc_nan:
image = 0xffff7fff | sign;
break;
case rvc_normal:
sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 24)) & 0x7fffff;
exp = r->exp + 128;
image = (sig << 16) & 0xffff0000;
image |= sign;
image |= exp << 7;
image |= sig >> 16;
break;
default:
abort ();
}
buf[0] = image;
}
static void
decode_vax_f (fmt, r, buf)
const struct real_format *fmt ATTRIBUTE_UNUSED;
REAL_VALUE_TYPE *r;
const long *buf;
{
unsigned long image = buf[0] & 0xffffffff;
int exp = (image >> 7) & 0xff;
memset (r, 0, sizeof (*r));
if (exp != 0)
{
r->class = rvc_normal;
r->sign = (image >> 15) & 1;
r->exp = exp - 128;
image = ((image & 0x7f) << 16) | ((image >> 16) & 0xffff);
r->sig[SIGSZ-1] = (image << (HOST_BITS_PER_LONG - 24)) | SIG_MSB;
}
}
static void
encode_vax_d (fmt, buf, r)
const struct real_format *fmt ATTRIBUTE_UNUSED;
long *buf;
const REAL_VALUE_TYPE *r;
{
unsigned long image0, image1, sign = r->sign << 15;
switch (r->class)
{
case rvc_zero:
image0 = image1 = 0;
break;
case rvc_inf:
case rvc_nan:
image0 = 0xffff7fff | sign;
image1 = 0xffffffff;
break;
case rvc_normal:
/* Extract the significand into straight hi:lo. */
if (HOST_BITS_PER_LONG == 64)
{
image0 = r->sig[SIGSZ-1];
image1 = (image0 >> (64 - 56)) & 0xffffffff;
image0 = (image0 >> (64 - 56 + 1) >> 31) & 0x7fffff;
}
else
{
image0 = r->sig[SIGSZ-1];
image1 = r->sig[SIGSZ-2];
image1 = (image0 << 24) | (image1 >> 8);
image0 = (image0 >> 8) & 0xffffff;
}
/* Rearrange the half-words of the significand to match the
external format. */
image0 = ((image0 << 16) | (image0 >> 16)) & 0xffff007f;
image1 = ((image1 << 16) | (image1 >> 16)) & 0xffffffff;
/* Add the sign and exponent. */
image0 |= sign;
image0 |= (r->exp + 128) << 7;
break;
default:
abort ();
}
if (FLOAT_WORDS_BIG_ENDIAN)
buf[0] = image1, buf[1] = image0;
else
buf[0] = image0, buf[1] = image1;
}
static void
decode_vax_d (fmt, r, buf)
const struct real_format *fmt ATTRIBUTE_UNUSED;
REAL_VALUE_TYPE *r;
const long *buf;
{
unsigned long image0, image1;
int exp;
if (FLOAT_WORDS_BIG_ENDIAN)
image1 = buf[0], image0 = buf[1];
else
image0 = buf[0], image1 = buf[1];
image0 &= 0xffffffff;
image1 &= 0xffffffff;
exp = (image0 >> 7) & 0x7f;
memset (r, 0, sizeof (*r));
if (exp != 0)
{
r->class = rvc_normal;
r->sign = (image0 >> 15) & 1;
r->exp = exp - 128;
/* Rearrange the half-words of the external format into
proper ascending order. */
image0 = ((image0 & 0x7f) << 16) | ((image0 >> 16) & 0xffff);
image1 = ((image1 & 0xffff) << 16) | ((image1 >> 16) & 0xffff);
if (HOST_BITS_PER_LONG == 64)
{
image0 = (image0 << 31 << 1) | image1;
image0 <<= 64 - 56;
image0 |= SIG_MSB;
r->sig[SIGSZ-1] = image0;
}
else
{
r->sig[SIGSZ-1] = image0;
r->sig[SIGSZ-2] = image1;
lshift_significand (r, r, 2*HOST_BITS_PER_LONG - 56);
r->sig[SIGSZ-1] |= SIG_MSB;
}
}
}
static void
encode_vax_g (fmt, buf, r)
const struct real_format *fmt ATTRIBUTE_UNUSED;
long *buf;
const REAL_VALUE_TYPE *r;
{
unsigned long image0, image1, sign = r->sign << 15;
switch (r->class)
{
case rvc_zero:
image0 = image1 = 0;
break;
case rvc_inf:
case rvc_nan:
image0 = 0xffff7fff | sign;
image1 = 0xffffffff;
break;
case rvc_normal:
/* Extract the significand into straight hi:lo. */
if (HOST_BITS_PER_LONG == 64)
{
image0 = r->sig[SIGSZ-1];
image1 = (image0 >> (64 - 53)) & 0xffffffff;
image0 = (image0 >> (64 - 53 + 1) >> 31) & 0xfffff;
}
else
{
image0 = r->sig[SIGSZ-1];
image1 = r->sig[SIGSZ-2];
image1 = (image0 << 21) | (image1 >> 11);
image0 = (image0 >> 11) & 0xfffff;
}
/* Rearrange the half-words of the significand to match the
external format. */
image0 = ((image0 << 16) | (image0 >> 16)) & 0xffff000f;
image1 = ((image1 << 16) | (image1 >> 16)) & 0xffffffff;
/* Add the sign and exponent. */
image0 |= sign;
image0 |= (r->exp + 1024) << 4;
break;
default:
abort ();
}
if (FLOAT_WORDS_BIG_ENDIAN)
buf[0] = image1, buf[1] = image0;
else
buf[0] = image0, buf[1] = image1;
}
static void
decode_vax_g (fmt, r, buf)
const struct real_format *fmt ATTRIBUTE_UNUSED;
REAL_VALUE_TYPE *r;
const long *buf;
{
unsigned long image0, image1;
int exp;
if (FLOAT_WORDS_BIG_ENDIAN)
image1 = buf[0], image0 = buf[1];
else
image0 = buf[0], image1 = buf[1];
image0 &= 0xffffffff;
image1 &= 0xffffffff;
exp = (image0 >> 4) & 0x7ff;
memset (r, 0, sizeof (*r));
if (exp != 0)
{
r->class = rvc_normal;
r->sign = (image0 >> 15) & 1;
r->exp = exp - 1024;
/* Rearrange the half-words of the external format into
proper ascending order. */
image0 = ((image0 & 0xf) << 16) | ((image0 >> 16) & 0xffff);
image1 = ((image1 & 0xffff) << 16) | ((image1 >> 16) & 0xffff);
if (HOST_BITS_PER_LONG == 64)
{
image0 = (image0 << 31 << 1) | image1;
image0 <<= 64 - 53;
image0 |= SIG_MSB;
r->sig[SIGSZ-1] = image0;
}
else
{
r->sig[SIGSZ-1] = image0;
r->sig[SIGSZ-2] = image1;
lshift_significand (r, r, 64 - 53);
r->sig[SIGSZ-1] |= SIG_MSB;
}
}
}
const struct real_format vax_f_format =
{
encode_vax_f,
decode_vax_f,
2,
1,
24,
-127,
127,
false,
false,
false,
false,
false
};
const struct real_format vax_d_format =
{
encode_vax_d,
decode_vax_d,
2,
1,
56,
-127,
127,
false,
false,
false,
false,
false
};
const struct real_format vax_g_format =
{
encode_vax_g,
decode_vax_g,
2,
1,
53,
-1023,
1023,
false,
false,
false,
false,
false
};
/* A good reference for these can be found in chapter 9 of
"ESA/390 Principles of Operation", IBM document number SA22-7201-01.
An on-line version can be found here:
http://publibz.boulder.ibm.com/cgi-bin/bookmgr_OS390/BOOKS/DZ9AR001/9.1?DT=19930923083613
*/
static void encode_i370_single PARAMS ((const struct real_format *fmt,
long *, const REAL_VALUE_TYPE *));
static void decode_i370_single PARAMS ((const struct real_format *,
REAL_VALUE_TYPE *, const long *));
static void encode_i370_double PARAMS ((const struct real_format *fmt,
long *, const REAL_VALUE_TYPE *));
static void decode_i370_double PARAMS ((const struct real_format *,
REAL_VALUE_TYPE *, const long *));
static void
encode_i370_single (fmt, buf, r)
const struct real_format *fmt ATTRIBUTE_UNUSED;
long *buf;
const REAL_VALUE_TYPE *r;
{
unsigned long sign, exp, sig, image;
sign = r->sign << 31;
switch (r->class)
{
case rvc_zero:
image = 0;
break;
case rvc_inf:
case rvc_nan:
image = 0x7fffffff | sign;
break;
case rvc_normal:
sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 24)) & 0xffffff;
exp = ((r->exp / 4) + 64) << 24;
image = sign | exp | sig;
break;
default:
abort ();
}
buf[0] = image;
}
static void
decode_i370_single (fmt, r, buf)
const struct real_format *fmt ATTRIBUTE_UNUSED;
REAL_VALUE_TYPE *r;
const long *buf;
{
unsigned long sign, sig, image = buf[0];
int exp;
sign = (image >> 31) & 1;
exp = (image >> 24) & 0x7f;
sig = image & 0xffffff;
memset (r, 0, sizeof (*r));
if (exp || sig)
{
r->class = rvc_normal;
r->sign = sign;
r->exp = (exp - 64) * 4;
r->sig[SIGSZ-1] = sig << (HOST_BITS_PER_LONG - 24);
normalize (r);
}
}
static void
encode_i370_double (fmt, buf, r)
const struct real_format *fmt ATTRIBUTE_UNUSED;
long *buf;
const REAL_VALUE_TYPE *r;
{
unsigned long sign, exp, image_hi, image_lo;
sign = r->sign << 31;
switch (r->class)
{
case rvc_zero:
image_hi = image_lo = 0;
break;
case rvc_inf:
case rvc_nan:
image_hi = 0x7fffffff | sign;
image_lo = 0xffffffff;
break;
case rvc_normal:
if (HOST_BITS_PER_LONG == 64)
{
image_hi = r->sig[SIGSZ-1];
image_lo = (image_hi >> (64 - 56)) & 0xffffffff;
image_hi = (image_hi >> (64 - 56 + 1) >> 31) & 0xffffff;
}
else
{
image_hi = r->sig[SIGSZ-1];
image_lo = r->sig[SIGSZ-2];
image_lo = (image_lo >> 8) | (image_hi << 24);
image_hi >>= 8;
}
exp = ((r->exp / 4) + 64) << 24;
image_hi |= sign | exp;
break;
default:
abort ();
}
if (FLOAT_WORDS_BIG_ENDIAN)
buf[0] = image_hi, buf[1] = image_lo;
else
buf[0] = image_lo, buf[1] = image_hi;
}
static void
decode_i370_double (fmt, r, buf)
const struct real_format *fmt ATTRIBUTE_UNUSED;
REAL_VALUE_TYPE *r;
const long *buf;
{
unsigned long sign, image_hi, image_lo;
int exp;
if (FLOAT_WORDS_BIG_ENDIAN)
image_hi = buf[0], image_lo = buf[1];
else
image_lo = buf[0], image_hi = buf[1];
sign = (image_hi >> 31) & 1;
exp = (image_hi >> 24) & 0x7f;
image_hi &= 0xffffff;
image_lo &= 0xffffffff;
memset (r, 0, sizeof (*r));
if (exp || image_hi || image_lo)
{
r->class = rvc_normal;
r->sign = sign;
r->exp = (exp - 64) * 4 + (SIGNIFICAND_BITS - 56);
if (HOST_BITS_PER_LONG == 32)
{
r->sig[0] = image_lo;
r->sig[1] = image_hi;
}
else
r->sig[0] = image_lo | (image_hi << 31 << 1);
normalize (r);
}
}
const struct real_format i370_single_format =
{
encode_i370_single,
decode_i370_single,
16,
4,
6,
-64,
63,
false,
false,
false, /* ??? The encoding does allow for "unnormals". */
false, /* ??? The encoding does allow for "unnormals". */
false
};
const struct real_format i370_double_format =
{
encode_i370_double,
decode_i370_double,
16,
4,
14,
-64,
63,
false,
false,
false, /* ??? The encoding does allow for "unnormals". */
false, /* ??? The encoding does allow for "unnormals". */
false
};
/* The "twos-complement" c4x format is officially defined as
x = s(~s).f * 2**e
This is rather misleading. One must remember that F is signed.
A better description would be
x = -1**s * ((s + 1 + .f) * 2**e
So if we have a (4 bit) fraction of .1000 with a sign bit of 1,
that's -1 * (1+1+(-.5)) == -1.5. I think.
The constructions here are taken from Tables 5-1 and 5-2 of the
TMS320C4x User's Guide wherein step-by-step instructions for
conversion from IEEE are presented. That's close enough to our
internal representation so as to make things easy.
See http://www-s.ti.com/sc/psheets/spru063c/spru063c.pdf */
static void encode_c4x_single PARAMS ((const struct real_format *fmt,
long *, const REAL_VALUE_TYPE *));
static void decode_c4x_single PARAMS ((const struct real_format *,
REAL_VALUE_TYPE *, const long *));
static void encode_c4x_extended PARAMS ((const struct real_format *fmt,
long *, const REAL_VALUE_TYPE *));
static void decode_c4x_extended PARAMS ((const struct real_format *,
REAL_VALUE_TYPE *, const long *));
static void
encode_c4x_single (fmt, buf, r)
const struct real_format *fmt ATTRIBUTE_UNUSED;
long *buf;
const REAL_VALUE_TYPE *r;
{
unsigned long image, exp, sig;
switch (r->class)
{
case rvc_zero:
exp = -128;
sig = 0;
break;
case rvc_inf:
case rvc_nan:
exp = 127;
sig = 0x800000 - r->sign;
break;
case rvc_normal:
exp = r->exp - 1;
sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 24)) & 0x7fffff;
if (r->sign)
{
if (sig)
sig = -sig;
else
exp--;
sig |= 0x800000;
}
break;
default:
abort ();
}
image = ((exp & 0xff) << 24) | (sig & 0xffffff);
buf[0] = image;
}
static void
decode_c4x_single (fmt, r, buf)
const struct real_format *fmt ATTRIBUTE_UNUSED;
REAL_VALUE_TYPE *r;
const long *buf;
{
unsigned long image = buf[0];
unsigned long sig;
int exp, sf;
exp = (((image >> 24) & 0xff) ^ 0x80) - 0x80;
sf = ((image & 0xffffff) ^ 0x800000) - 0x800000;
memset (r, 0, sizeof (*r));
if (exp != -128)
{
r->class = rvc_normal;
sig = sf & 0x7fffff;
if (sf < 0)
{
r->sign = 1;
if (sig)
sig = -sig;
else
exp++;
}
sig = (sig << (HOST_BITS_PER_LONG - 24)) | SIG_MSB;
r->exp = exp + 1;
r->sig[SIGSZ-1] = sig;
}
}
static void
encode_c4x_extended (fmt, buf, r)
const struct real_format *fmt ATTRIBUTE_UNUSED;
long *buf;
const REAL_VALUE_TYPE *r;
{
unsigned long exp, sig;
switch (r->class)
{
case rvc_zero:
exp = -128;
sig = 0;
break;
case rvc_inf:
case rvc_nan:
exp = 127;
sig = 0x80000000 - r->sign;
break;
case rvc_normal:
exp = r->exp - 1;
sig = r->sig[SIGSZ-1];
if (HOST_BITS_PER_LONG == 64)
sig = sig >> 1 >> 31;
sig &= 0x7fffffff;
if (r->sign)
{
if (sig)
sig = -sig;
else
exp--;
sig |= 0x80000000;
}
break;
default:
abort ();
}
exp = (exp & 0xff) << 24;
sig &= 0xffffffff;
if (FLOAT_WORDS_BIG_ENDIAN)
buf[0] = exp, buf[1] = sig;
else
buf[0] = sig, buf[0] = exp;
}
static void
decode_c4x_extended (fmt, r, buf)
const struct real_format *fmt ATTRIBUTE_UNUSED;
REAL_VALUE_TYPE *r;
const long *buf;
{
unsigned long sig;
int exp, sf;
if (FLOAT_WORDS_BIG_ENDIAN)
exp = buf[0], sf = buf[1];
else
sf = buf[0], exp = buf[1];
exp = (((exp >> 24) & 0xff) & 0x80) - 0x80;
sf = ((sf & 0xffffffff) ^ 0x80000000) - 0x80000000;
memset (r, 0, sizeof (*r));
if (exp != -128)
{
r->class = rvc_normal;
sig = sf & 0x7fffffff;
if (sf < 0)
{
r->sign = 1;
if (sig)
sig = -sig;
else
exp++;
}
if (HOST_BITS_PER_LONG == 64)
sig = sig << 1 << 31;
sig |= SIG_MSB;
r->exp = exp + 1;
r->sig[SIGSZ-1] = sig;
}
}
const struct real_format c4x_single_format =
{
encode_c4x_single,
decode_c4x_single,
2,
1,
24,
-126,
128,
false,
false,
false,
false,
false
};
const struct real_format c4x_extended_format =
{
encode_c4x_extended,
decode_c4x_extended,
2,
1,
32,
-126,
128,
false,
false,
false,
false,
false
};
/* Set up default mode to format mapping for IEEE. Everyone else has
to set these values in OVERRIDE_OPTIONS. */
const struct real_format *real_format_for_mode[TFmode - QFmode + 1] =
{
NULL, /* QFmode */
NULL, /* HFmode */
NULL, /* TQFmode */
&ieee_single_format, /* SFmode */
&ieee_double_format, /* DFmode */
/* We explicitly don't handle XFmode. There are two formats,
pretty much equally common. Choose one in OVERRIDE_OPTIONS. */
NULL, /* XFmode */
&ieee_quad_format /* TFmode */
};
|