File: int-inf0.sml

package info (click to toggle)
mlton 20210117%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 58,464 kB
  • sloc: ansic: 27,682; sh: 4,455; asm: 3,569; lisp: 2,879; makefile: 2,347; perl: 1,169; python: 191; pascal: 68; javascript: 7
file content (1626 lines) | stat: -rw-r--r-- 70,498 bytes parent folder | download | duplicates (3)
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
(* Copyright (C) 2013-2014,2016-2017 Matthew Fluet.
 * Copyright (C) 1999-2008 Henry Cejtin, Matthew Fluet, Suresh
 *    Jagannathan, and Stephen Weeks.
 * Copyright (C) 1997-2000 NEC Research Institute.
 *
 * MLton is released under a HPND-style license.
 * See the file MLton-LICENSE for details.
 *)

signature PRIM_INT_INF =
   sig
      eqtype int
      type t = int

      val precision: Primitive.Int32.int option

      val maxInt: int option
      val minInt: int option

      val zero: int
      val one: int
      val negOne: int

      datatype rep =
         Big of C_MPLimb.word vector
       | Small of ObjptrInt.int
      val rep: int -> rep
      val fromRep: rep -> int option

      val isSmall: int -> bool
      val areSmall: int * int -> bool

      val abs: int -> int
      val +! : int * int -> int
      val + : int * int -> int
      val divMod: int * int -> int * int
      val div: int * int -> int
      val gcd: int * int -> int
      val mod: int * int -> int
      val *! : int * int -> int
      val * : int * int -> int
      val ~! : int -> int
      val ~ : int -> int
      val quotRem: int * int -> int * int
      val quot: int * int -> int
      val rem: int * int -> int
      val -! : int * int -> int
      val - : int * int -> int

      val < : int * int -> bool
      val <= : int * int -> bool
      val > : int * int -> bool
      val >= : int * int -> bool
      val compare: int * int -> Primitive.Order.order
      val min: int * int -> int
      val max: int * int -> int
      val ltu: int * int -> bool
      val leu: int * int -> bool
      val gtu: int * int -> bool
      val geu: int * int -> bool

      val andb: int * int -> int
      val <<? : int * Primitive.Word32.word -> int
      val << : int * Primitive.Word32.word -> int
      val notb: int -> int
      val orb: int * int -> int
      val ~>>? : int * Primitive.Word32.word -> int
      val ~>> : int * Primitive.Word32.word -> int
      val xorb: int * int -> int

      val mkCvt: ({base: Primitive.Int32.int,
                   smallCvt: ObjptrInt.int -> Primitive.String8.string} 
                  -> int -> Primitive.String8.string)
      val mkLog2: ({fromSmall: {smallLog2: Primitive.Int32.int} -> 'a,
                    fromLarge: {mostSigLimbLog2: Primitive.Int32.int,
                                numLimbsMinusOne: SeqIndex.int} -> 'a}
                   -> int -> 'a)

      val zextdFromInt8: Primitive.Int8.int -> int
      val zextdFromInt16: Primitive.Int16.int -> int
      val zextdFromInt32: Primitive.Int32.int -> int
      val zextdFromInt64: Primitive.Int64.int -> int
      val zextdFromIntInf: Primitive.IntInf.int -> int
      val zextdFromWord8: Primitive.Word8.word -> int
      val zextdFromWord16: Primitive.Word16.word -> int
      val zextdFromWord32: Primitive.Word32.word -> int
      val zextdFromWord64: Primitive.Word64.word -> int
      val zextdToInt8: int -> Primitive.Int8.int
      val zextdToInt16: int -> Primitive.Int16.int
      val zextdToInt32: int -> Primitive.Int32.int
      val zextdToInt64: int -> Primitive.Int64.int
      val zextdToIntInf: int -> Primitive.IntInf.int
      val zextdToWord8: int -> Primitive.Word8.word
      val zextdToWord16: int -> Primitive.Word16.word
      val zextdToWord32: int -> Primitive.Word32.word
      val zextdToWord64: int -> Primitive.Word64.word

      val sextdFromInt8: Primitive.Int8.int -> int
      val sextdFromInt16: Primitive.Int16.int -> int
      val sextdFromInt32: Primitive.Int32.int -> int
      val sextdFromInt64: Primitive.Int64.int -> int
      val sextdFromIntInf: Primitive.IntInf.int -> int
      val sextdFromWord8: Primitive.Word8.word -> int
      val sextdFromWord16: Primitive.Word16.word -> int
      val sextdFromWord32: Primitive.Word32.word -> int
      val sextdFromWord64: Primitive.Word64.word -> int
      val sextdToInt8: int -> Primitive.Int8.int
      val sextdToInt16: int -> Primitive.Int16.int
      val sextdToInt32: int -> Primitive.Int32.int
      val sextdToInt64: int -> Primitive.Int64.int
      val sextdToIntInf: int -> Primitive.IntInf.int
      val sextdToWord8: int -> Primitive.Word8.word
      val sextdToWord16: int -> Primitive.Word16.word
      val sextdToWord32: int -> Primitive.Word32.word
      val sextdToWord64: int -> Primitive.Word64.word

      val castFromInt8: Primitive.Int8.int -> int
      val castFromInt16: Primitive.Int16.int -> int
      val castFromInt32: Primitive.Int32.int -> int
      val castFromInt64: Primitive.Int64.int -> int
      val castFromIntInf: Primitive.IntInf.int -> int
      val castFromWord8: Primitive.Word8.word -> int
      val castFromWord16: Primitive.Word16.word -> int
      val castFromWord32: Primitive.Word32.word -> int
      val castFromWord64: Primitive.Word64.word -> int
      val castToInt8: int -> Primitive.Int8.int
      val castToInt16: int -> Primitive.Int16.int
      val castToInt32: int -> Primitive.Int32.int
      val castToInt64: int -> Primitive.Int64.int
      val castToIntInf: int -> Primitive.IntInf.int
      val castToWord8: int -> Primitive.Word8.word
      val castToWord16: int -> Primitive.Word16.word
      val castToWord32: int -> Primitive.Word32.word
      val castToWord64: int -> Primitive.Word64.word

      val zchckFromInt8: Primitive.Int8.int -> int
      val zchckFromInt16: Primitive.Int16.int -> int
      val zchckFromInt32: Primitive.Int32.int -> int
      val zchckFromInt64: Primitive.Int64.int -> int
      val zchckFromIntInf: Primitive.IntInf.int -> int
      val zchckFromWord8: Primitive.Word8.word -> int
      val zchckFromWord16: Primitive.Word16.word -> int
      val zchckFromWord32: Primitive.Word32.word -> int
      val zchckFromWord64: Primitive.Word64.word -> int
      val zchckToInt8: int -> Primitive.Int8.int
      val zchckToInt16: int -> Primitive.Int16.int
      val zchckToInt32: int -> Primitive.Int32.int
      val zchckToInt64: int -> Primitive.Int64.int
      val zchckToIntInf: int -> Primitive.IntInf.int
      val zchckToWord8: int -> Primitive.Word8.word
      val zchckToWord16: int -> Primitive.Word16.word
      val zchckToWord32: int -> Primitive.Word32.word
      val zchckToWord64: int -> Primitive.Word64.word

      val schckFromInt8: Primitive.Int8.int -> int
      val schckFromInt16: Primitive.Int16.int -> int
      val schckFromInt32: Primitive.Int32.int -> int
      val schckFromInt64: Primitive.Int64.int -> int
      val schckFromIntInf: Primitive.IntInf.int -> int
      val schckFromWord8: Primitive.Word8.word -> int
      val schckFromWord16: Primitive.Word16.word -> int
      val schckFromWord32: Primitive.Word32.word -> int
      val schckFromWord64: Primitive.Word64.word -> int
      val schckToInt8: int -> Primitive.Int8.int
      val schckToInt16: int -> Primitive.Int16.int
      val schckToInt32: int -> Primitive.Int32.int
      val schckToInt64: int -> Primitive.Int64.int
      val schckToIntInf: int -> Primitive.IntInf.int
      val schckToWord8: int -> Primitive.Word8.word
      val schckToWord16: int -> Primitive.Word16.word
      val schckToWord32: int -> Primitive.Word32.word
      val schckToWord64: int -> Primitive.Word64.word
   end

signature PRIM_INTWORD_CONV =
   sig
      include PRIM_INTWORD_CONV

      val idFromIntInfToIntInf: Primitive.IntInf.int -> Primitive.IntInf.int

      val zextdFromInt8ToIntInf: Primitive.Int8.int -> Primitive.IntInf.int
      val zextdFromInt16ToIntInf: Primitive.Int16.int -> Primitive.IntInf.int
      val zextdFromInt32ToIntInf: Primitive.Int32.int -> Primitive.IntInf.int
      val zextdFromInt64ToIntInf: Primitive.Int64.int -> Primitive.IntInf.int
      val zextdFromWord8ToIntInf: Primitive.Word8.word -> Primitive.IntInf.int
      val zextdFromWord16ToIntInf: Primitive.Word16.word -> Primitive.IntInf.int
      val zextdFromWord32ToIntInf: Primitive.Word32.word -> Primitive.IntInf.int
      val zextdFromWord64ToIntInf: Primitive.Word64.word -> Primitive.IntInf.int

      val zextdFromIntInfToInt8: Primitive.IntInf.int -> Primitive.Int8.int
      val zextdFromIntInfToInt16: Primitive.IntInf.int -> Primitive.Int16.int
      val zextdFromIntInfToInt32: Primitive.IntInf.int -> Primitive.Int32.int
      val zextdFromIntInfToInt64: Primitive.IntInf.int -> Primitive.Int64.int
      val zextdFromIntInfToIntInf: Primitive.IntInf.int -> Primitive.IntInf.int
      val zextdFromIntInfToWord8: Primitive.IntInf.int -> Primitive.Word8.word
      val zextdFromIntInfToWord16: Primitive.IntInf.int -> Primitive.Word16.word
      val zextdFromIntInfToWord32: Primitive.IntInf.int -> Primitive.Word32.word
      val zextdFromIntInfToWord64: Primitive.IntInf.int -> Primitive.Word64.word


      val sextdFromInt8ToIntInf: Primitive.Int8.int -> Primitive.IntInf.int
      val sextdFromInt16ToIntInf: Primitive.Int16.int -> Primitive.IntInf.int
      val sextdFromInt32ToIntInf: Primitive.Int32.int -> Primitive.IntInf.int
      val sextdFromInt64ToIntInf: Primitive.Int64.int -> Primitive.IntInf.int
      val sextdFromWord8ToIntInf: Primitive.Word8.word -> Primitive.IntInf.int
      val sextdFromWord16ToIntInf: Primitive.Word16.word -> Primitive.IntInf.int
      val sextdFromWord32ToIntInf: Primitive.Word32.word -> Primitive.IntInf.int
      val sextdFromWord64ToIntInf: Primitive.Word64.word -> Primitive.IntInf.int

      val sextdFromIntInfToInt8: Primitive.IntInf.int -> Primitive.Int8.int
      val sextdFromIntInfToInt16: Primitive.IntInf.int -> Primitive.Int16.int
      val sextdFromIntInfToInt32: Primitive.IntInf.int -> Primitive.Int32.int
      val sextdFromIntInfToInt64: Primitive.IntInf.int -> Primitive.Int64.int
      val sextdFromIntInfToIntInf: Primitive.IntInf.int -> Primitive.IntInf.int
      val sextdFromIntInfToWord8: Primitive.IntInf.int -> Primitive.Word8.word
      val sextdFromIntInfToWord16: Primitive.IntInf.int -> Primitive.Word16.word
      val sextdFromIntInfToWord32: Primitive.IntInf.int -> Primitive.Word32.word
      val sextdFromIntInfToWord64: Primitive.IntInf.int -> Primitive.Word64.word


      val castFromInt8ToIntInf: Primitive.Int8.int -> Primitive.IntInf.int
      val castFromInt16ToIntInf: Primitive.Int16.int -> Primitive.IntInf.int
      val castFromInt32ToIntInf: Primitive.Int32.int -> Primitive.IntInf.int
      val castFromInt64ToIntInf: Primitive.Int64.int -> Primitive.IntInf.int
      val castFromWord8ToIntInf: Primitive.Word8.word -> Primitive.IntInf.int
      val castFromWord16ToIntInf: Primitive.Word16.word -> Primitive.IntInf.int
      val castFromWord32ToIntInf: Primitive.Word32.word -> Primitive.IntInf.int
      val castFromWord64ToIntInf: Primitive.Word64.word -> Primitive.IntInf.int

      val castFromIntInfToInt8: Primitive.IntInf.int -> Primitive.Int8.int
      val castFromIntInfToInt16: Primitive.IntInf.int -> Primitive.Int16.int
      val castFromIntInfToInt32: Primitive.IntInf.int -> Primitive.Int32.int
      val castFromIntInfToInt64: Primitive.IntInf.int -> Primitive.Int64.int
      val castFromIntInfToIntInf: Primitive.IntInf.int -> Primitive.IntInf.int
      val castFromIntInfToWord8: Primitive.IntInf.int -> Primitive.Word8.word
      val castFromIntInfToWord16: Primitive.IntInf.int -> Primitive.Word16.word
      val castFromIntInfToWord32: Primitive.IntInf.int -> Primitive.Word32.word
      val castFromIntInfToWord64: Primitive.IntInf.int -> Primitive.Word64.word


      val zchckFromInt8ToIntInf: Primitive.Int8.int -> Primitive.IntInf.int
      val zchckFromInt16ToIntInf: Primitive.Int16.int -> Primitive.IntInf.int
      val zchckFromInt32ToIntInf: Primitive.Int32.int -> Primitive.IntInf.int
      val zchckFromInt64ToIntInf: Primitive.Int64.int -> Primitive.IntInf.int
      val zchckFromWord8ToIntInf: Primitive.Word8.word -> Primitive.IntInf.int
      val zchckFromWord16ToIntInf: Primitive.Word16.word -> Primitive.IntInf.int
      val zchckFromWord32ToIntInf: Primitive.Word32.word -> Primitive.IntInf.int
      val zchckFromWord64ToIntInf: Primitive.Word64.word -> Primitive.IntInf.int

      val zchckFromIntInfToInt8: Primitive.IntInf.int -> Primitive.Int8.int
      val zchckFromIntInfToInt16: Primitive.IntInf.int -> Primitive.Int16.int
      val zchckFromIntInfToInt32: Primitive.IntInf.int -> Primitive.Int32.int
      val zchckFromIntInfToInt64: Primitive.IntInf.int -> Primitive.Int64.int
      val zchckFromIntInfToIntInf: Primitive.IntInf.int -> Primitive.IntInf.int
      val zchckFromIntInfToWord8: Primitive.IntInf.int -> Primitive.Word8.word
      val zchckFromIntInfToWord16: Primitive.IntInf.int -> Primitive.Word16.word
      val zchckFromIntInfToWord32: Primitive.IntInf.int -> Primitive.Word32.word
      val zchckFromIntInfToWord64: Primitive.IntInf.int -> Primitive.Word64.word


      val schckFromInt8ToIntInf: Primitive.Int8.int -> Primitive.IntInf.int
      val schckFromInt16ToIntInf: Primitive.Int16.int -> Primitive.IntInf.int
      val schckFromInt32ToIntInf: Primitive.Int32.int -> Primitive.IntInf.int
      val schckFromInt64ToIntInf: Primitive.Int64.int -> Primitive.IntInf.int
      val schckFromWord8ToIntInf: Primitive.Word8.word -> Primitive.IntInf.int
      val schckFromWord16ToIntInf: Primitive.Word16.word -> Primitive.IntInf.int
      val schckFromWord32ToIntInf: Primitive.Word32.word -> Primitive.IntInf.int
      val schckFromWord64ToIntInf: Primitive.Word64.word -> Primitive.IntInf.int

      val schckFromIntInfToInt8: Primitive.IntInf.int -> Primitive.Int8.int
      val schckFromIntInfToInt16: Primitive.IntInf.int -> Primitive.Int16.int
      val schckFromIntInfToInt32: Primitive.IntInf.int -> Primitive.Int32.int
      val schckFromIntInfToInt64: Primitive.IntInf.int -> Primitive.Int64.int
      val schckFromIntInfToIntInf: Primitive.IntInf.int -> Primitive.IntInf.int
      val schckFromIntInfToWord8: Primitive.IntInf.int -> Primitive.Word8.word
      val schckFromIntInfToWord16: Primitive.IntInf.int -> Primitive.Word16.word
      val schckFromIntInfToWord32: Primitive.IntInf.int -> Primitive.Word32.word
      val schckFromIntInfToWord64: Primitive.IntInf.int -> Primitive.Word64.word
   end
signature PRIM_INTEGER =
   sig
      include PRIM_INTEGER

      val zextdFromIntInf: Primitive.IntInf.int -> int
      val zextdToIntInf: int -> Primitive.IntInf.int

      val sextdFromIntInf: Primitive.IntInf.int -> int
      val sextdToIntInf: int -> Primitive.IntInf.int

      val castFromIntInf: Primitive.IntInf.int -> int
      val castToIntInf: int -> Primitive.IntInf.int

      val zchckFromIntInf: Primitive.IntInf.int -> int
      val zchckToIntInf: int -> Primitive.IntInf.int

      val schckFromIntInf: Primitive.IntInf.int -> int
      val schckToIntInf: int -> Primitive.IntInf.int
   end
signature PRIM_WORD =
   sig
      include PRIM_WORD

      val zextdFromIntInf: Primitive.IntInf.int -> word
      val zextdToIntInf: word -> Primitive.IntInf.int

      val sextdFromIntInf: Primitive.IntInf.int -> word
      val sextdToIntInf: word -> Primitive.IntInf.int

      val castFromIntInf: Primitive.IntInf.int -> word
      val castToIntInf: word -> Primitive.IntInf.int

      val zchckFromIntInf: Primitive.IntInf.int -> word
      val zchckToIntInf: word -> Primitive.IntInf.int

      val schckFromIntInf: Primitive.IntInf.int -> word
      val schckToIntInf: word -> Primitive.IntInf.int
   end

structure Primitive = struct

open Primitive

structure IntInf =
   struct
      structure Prim = Primitive.IntInf
      structure MLton = Primitive.MLton

      structure A = Primitive.Array
      structure V = Primitive.Vector
      structure S = SeqIndex
      structure ObjptrWord = struct
                                open ObjptrWord
                                local
                                   structure S =
                                      ObjptrInt_ChooseIntN
                                      (type 'a t = 'a -> ObjptrWord.word
                                       val fInt8 = ObjptrWord.zextdFromInt8
                                       val fInt16 = ObjptrWord.zextdFromInt16
                                       val fInt32 = ObjptrWord.zextdFromInt32
                                       val fInt64 = ObjptrWord.zextdFromInt64)
                                in
                                   val idFromObjptrInt = S.f
                                end
                                local
                                   structure S =
                                      ObjptrInt_ChooseIntN
                                      (type 'a t = ObjptrWord.word -> 'a
                                       val fInt8 = ObjptrWord.zextdToInt8
                                       val fInt16 = ObjptrWord.zextdToInt16
                                       val fInt32 = ObjptrWord.zextdToInt32
                                       val fInt64 = ObjptrWord.zextdToInt64)
                                in
                                   val idToObjptrInt = S.f
                                end
                                local
                                   structure S =
                                      C_MPLimb_ChooseWordN
                                      (type 'a t = 'a -> ObjptrWord.word
                                       val fWord8 = ObjptrWord.castFromWord8
                                       val fWord16 = ObjptrWord.castFromWord16
                                       val fWord32 = ObjptrWord.castFromWord32
                                       val fWord64 = ObjptrWord.castFromWord64)
                                in
                                   val castFromMPLimb = S.f
                                end
                                local
                                   structure S =
                                      C_MPLimb_ChooseWordN
                                      (type 'a t = ObjptrWord.word -> 'a
                                       val fWord8 = ObjptrWord.castToWord8
                                       val fWord16 = ObjptrWord.castToWord16
                                       val fWord32 = ObjptrWord.castToWord32
                                       val fWord64 = ObjptrWord.castToWord64)
                                in
                                   val castToMPLimb = S.f
                                end
                    end
      structure W = ObjptrWord
      structure I = ObjptrInt
      structure MPLimb = C_MPLimb
      structure Sz = struct 
                        open C_Size
                        local
                           structure S =
                              SeqIndex_ChooseIntN
                              (type 'a t = 'a -> C_Size.word
                               val fInt8 = C_Size.zextdFromInt8
                               val fInt16 = C_Size.zextdFromInt16
                               val fInt32 = C_Size.zextdFromInt32
                               val fInt64 = C_Size.zextdFromInt64)
                        in
                           val zextdFromSeqIndex = S.f
                        end
                     end

      type bigInt = Prim.int

      val zero: bigInt = 0
      val one: bigInt = 1
      val negOne: bigInt = ~1

      (* Check if an IntInf.int is small (i.e., a fixnum). *)
      fun isSmall (i: bigInt): bool =
         0w0 <> W.andb (Prim.toWord i, 0w1)

      (* Check if two IntInf.int's are both small (i.e., fixnums). *)
      fun areSmall (i: bigInt, i': bigInt): bool =
         0w0 <> W.andb (W.andb (Prim.toWord i, Prim.toWord i'), 0w1)

      (* Return the number of `limbs' in a bigInt. *)
      fun bigNumLimbs i = S.- (V.length (Prim.toVector i), 1)
      fun numLimbs i = 
         if isSmall i
            then 1
            else bigNumLimbs i

      fun dropTag (w: W.word): W.word = W.~>>? (w, 0w1)
      fun dropTagCoerce (i: bigInt): W.word = dropTag (Prim.toWord i)
      fun dropTagCoerceInt (i: bigInt): I.int = W.idToObjptrInt (dropTagCoerce i)
      fun addTag (w: W.word): W.word = W.orb (W.<<? (w, 0w1), 0w1)
      fun addTagCoerce (w: W.word): bigInt = Prim.fromWord (addTag w)
      fun addTagCoerceInt (i: I.int): bigInt = addTagCoerce (W.idFromObjptrInt i)
      fun zeroTag (w: W.word): W.word = W.andb (w, W.notb 0w1)
      fun oneTag (w: W.word): W.word = W.orb (w, 0w1)
      fun oneTagCoerce (w: W.word): bigInt = Prim.fromWord (oneTag w)


      datatype rep =
         Big of MPLimb.t V.vector
       | Small of ObjptrInt.int

      fun rep i =
         if isSmall i
            then Small (dropTagCoerceInt i)
            else Big (Prim.toVector i)

      fun fromRep r =
         case r of
            Big v =>
               let
                  val limbsPerObjptr =
                     if Int32.>= (MPLimb.sizeInBits, ObjptrWord.sizeInBits)
                        then 1
                     else S.sextdFromInt32 (Int32.quot (ObjptrWord.sizeInBits, MPLimb.sizeInBits))

                  val l = V.length v
                  val ok =
                     (* sign limb + magnitude limb(s) *)
                     S.>= (l, 2) andalso
                     (* sign limb is 0w0 (positive) or 0w1 (negative) *)
                     MPLimb.<= (V.unsafeSub (v, 0), 0w1) andalso
                     (* most-significant magnitude limb is non-zero *)
                     MPLimb.> (V.unsafeSub (v, S.- (l, 1)), 0w0) andalso
                     (* value exceeds Small representation;
                      * if positive, then mag in [1, 2^(ObjptrWord.sizeInBits - 2)].
                      * if negative, then mag in [0, 2^(ObjptrWord.sizeInBits - 2) - 1].
                      *)
                     (S.> (l, S.+ (1, limbsPerObjptr)) orelse
                      if Int32.<= (ObjptrWord.sizeInBits, MPLimb.sizeInBits)
                         then let
                                 val mag = V.unsafeSub (v, 1)
                              in
                                 MPLimb.>=
                                 (if MPLimb.>= (V.unsafeSub (v, 0), 0w1) then MPLimb.- (mag, 0w1) else mag,
                                  MPLimb.<<? (0w1, Word32.- (ObjptrWord.sizeInBitsWord, 0w2)))
                              end
                      else let
                              fun loop (i, mag) =
                                 if S.< (i, l)
                                    then ObjptrWord.andb
                                         (ObjptrWord.<<? (mag, MPLimb.sizeInBitsWord),
                                          W.castFromMPLimb (V.unsafeSub (v, i)))
                                 else mag
                              val mag = loop (2, W.castFromMPLimb (V.unsafeSub (v, 1)))
                           in
                              ObjptrWord.>=
                              (if MPLimb.>= (V.unsafeSub (v, 0), 0w1) then ObjptrWord.- (mag, 0w1) else mag,
                               ObjptrWord.<<? (0w1, Word32.- (ObjptrWord.sizeInBitsWord, 0w2)))
                           end)
               in
                  if ok
                     then SOME (Prim.fromVector v)
                  else NONE
               end
          | Small i =>
               let
                  val w = ObjptrWord.idFromObjptrInt i
                  val wt = addTag w
                  val ok = w = dropTag wt
               in
                  if ok
                     then SOME (Prim.fromWord wt)
                  else NONE
               end


      local
         fun 'a make {zextdToMPLimb: 'a -> MPLimb.word,
                      zextdToObjptrWord: 'a -> ObjptrWord.word,
                      sextdToObjptrWord: 'a -> ObjptrWord.word,
                      other : {sizeInBits: Int32.int,
                               zero: 'a,
                               eq: 'a * 'a -> bool,
                               isNeg: 'a -> bool,
                               neg: 'a -> 'a,
                               notb: 'a -> 'a,
                               rashift: 'a * Word32.word -> 'a,
                               rshift: 'a * Word32.word -> 'a}}
                     (sextd, w) =
            if Int32.> (ObjptrWord.sizeInBits, #sizeInBits other)
               orelse let
                         val shift = Word32.- (ObjptrWord.sizeInBitsWord, 0w2)
                         val upperBits = (#rashift other) (w, shift)
                         val zeroBits = #zero other
                         val oneBits = (#notb other) zeroBits
                      in
                         (#eq other) (upperBits, zeroBits)
                         orelse
                         (sextd andalso (#eq other) (upperBits, oneBits))
                      end
               then if sextd
                       then Prim.fromWord (addTag (sextdToObjptrWord w))
                       else Prim.fromWord (addTag (zextdToObjptrWord w))
               else let
                       fun loop (w, i, acc) =
                          if (#eq other) (w, (#zero other))
                             then (i, acc)
                             else 
                                let
                                   val limb = zextdToMPLimb w
                                   val w = 
                                      (#rshift other) 
                                      (w, MPLimb.sizeInBitsWord)
                                in
                                   loop (w, S.+ (i, 1), (i, limb) :: acc)
                                end
                       val (n, acc) = 
                          if sextd andalso (#isNeg other) w
                             then loop ((#neg other) w, 1, [(0,0w1)])
                             else loop (w, 1, [(0,0w0)])
                       val a = A.unsafeAlloc n
                       fun loop acc =
                          case acc of
                             [] => ()
                           | (i, v) :: acc => (A.unsafeUpdate (a, i, v)
                                               ; loop acc)
                       val () = loop acc
                    in
                       Prim.fromVector (V.unsafeFromArray a)
                    end
      in
         fun extdFromWord8 (sextd, w) =
            make {zextdToMPLimb = MPLimb.zextdFromWord8,
                  zextdToObjptrWord = ObjptrWord.zextdFromWord8,
                  sextdToObjptrWord = ObjptrWord.sextdFromWord8,
                  other = {sizeInBits = Word8.sizeInBits,
                           zero = Word8.zero,
                           eq = ((op =) : Word8.word * Word8.word -> bool),
                           isNeg = fn w => Int8.< (IntWordConv.idFromWord8ToInt8 w, 0),
                           neg = Word8.~,
                           notb = Word8.notb,
                           rashift = Word8.~>>?,
                           rshift = Word8.>>?}}
                 (sextd, w)
         fun zextdFromWord8 w = extdFromWord8 (false, w)
         fun zextdFromInt8 i = zextdFromWord8 (IntWordConv.idFromInt8ToWord8 i)
         fun sextdFromWord8 w = extdFromWord8 (true, w)
         fun sextdFromInt8 i = sextdFromWord8 (IntWordConv.idFromInt8ToWord8 i)
         val castFromInt8 = sextdFromInt8
         val castFromWord8 = zextdFromWord8
         val zchckFromInt8 = zextdFromInt8
         val zchckFromWord8 = zextdFromWord8
         val schckFromInt8 = sextdFromInt8
         val schckFromWord8 = sextdFromWord8

         fun extdFromWord16 (sextd, w) =
            make {zextdToMPLimb = MPLimb.zextdFromWord16,
                  zextdToObjptrWord = ObjptrWord.zextdFromWord16,
                  sextdToObjptrWord = ObjptrWord.sextdFromWord16,
                  other = {sizeInBits = Word16.sizeInBits,
                           zero = Word16.zero,
                           eq = ((op =) : Word16.word * Word16.word -> bool),
                           isNeg = fn w => Int16.< (IntWordConv.idFromWord16ToInt16 w, 0),
                           neg = Word16.~,
                           notb = Word16.notb,
                           rashift = Word16.~>>?,
                           rshift = Word16.>>?}}
                 (sextd, w)
         fun zextdFromWord16 w = extdFromWord16 (false, w)
         fun zextdFromInt16 i = zextdFromWord16 (IntWordConv.idFromInt16ToWord16 i)
         fun sextdFromWord16 w = extdFromWord16 (true, w)
         fun sextdFromInt16 i = sextdFromWord16 (IntWordConv.idFromInt16ToWord16 i)
         val castFromInt16 = sextdFromInt16
         val castFromWord16 = zextdFromWord16
         val zchckFromInt16 = zextdFromInt16
         val zchckFromWord16 = zextdFromWord16
         val schckFromInt16 = sextdFromInt16
         val schckFromWord16 = sextdFromWord16

         fun extdFromWord32 (sextd, w) =
            make {zextdToMPLimb = MPLimb.zextdFromWord32,
                  zextdToObjptrWord = ObjptrWord.zextdFromWord32,
                  sextdToObjptrWord = ObjptrWord.sextdFromWord32,
                  other = {sizeInBits = Word32.sizeInBits,
                           zero = Word32.zero,
                           eq = ((op =) : Word32.word * Word32.word -> bool),
                           isNeg = fn w => Int32.< (IntWordConv.idFromWord32ToInt32 w, 0),
                           neg = Word32.~,
                           notb = Word32.notb,
                           rashift = Word32.~>>?,
                           rshift = Word32.>>?}}
                 (sextd, w)
         fun zextdFromWord32 w = extdFromWord32 (false, w)
         fun zextdFromInt32 i = zextdFromWord32 (IntWordConv.idFromInt32ToWord32 i)
         fun sextdFromWord32 w = extdFromWord32 (true, w)
         fun sextdFromInt32 i = sextdFromWord32 (IntWordConv.idFromInt32ToWord32 i)
         val castFromInt32 = sextdFromInt32
         val castFromWord32 = zextdFromWord32
         val zchckFromInt32 = zextdFromInt32
         val zchckFromWord32 = zextdFromWord32
         val schckFromInt32 = sextdFromInt32
         val schckFromWord32 = sextdFromWord32

         fun extdFromWord64 (sextd, w) =
            make {zextdToMPLimb = MPLimb.zextdFromWord64,
                  zextdToObjptrWord = ObjptrWord.zextdFromWord64,
                  sextdToObjptrWord = ObjptrWord.sextdFromWord64,
                  other = {sizeInBits = Word64.sizeInBits,
                           zero = Word64.zero,
                           eq = ((op =) : Word64.word * Word64.word -> bool),
                           isNeg = fn w => Int64.< (IntWordConv.idFromWord64ToInt64 w, 0),
                           neg = Word64.~,
                           notb = Word64.notb,
                           rashift = Word64.~>>?,
                           rshift = Word64.>>?}}
                 (sextd, w)
         fun zextdFromWord64 w = extdFromWord64 (false, w)
         fun zextdFromInt64 i = zextdFromWord64 (IntWordConv.idFromInt64ToWord64 i)
         fun sextdFromWord64 w = extdFromWord64 (true, w)
         fun sextdFromInt64 i = sextdFromWord64 (IntWordConv.idFromInt64ToWord64 i)
         val castFromInt64 = sextdFromInt64
         val castFromWord64 = zextdFromWord64
         val zchckFromInt64 = zextdFromInt64
         val zchckFromWord64 = zextdFromWord64
         val schckFromInt64 = sextdFromInt64
         val schckFromWord64 = sextdFromWord64

         fun zextdFromIntInf ii = ii
         fun sextdFromIntInf ii = ii
         fun castFromIntInf ii = ii
         fun zchckFromIntInf ii = ii
         fun schckFromIntInf ii = ii
      end

      local
         structure S =
            ObjptrInt_ChooseIntN
            (type 'a t = 'a -> bigInt
             val fInt8 = sextdFromInt8
             val fInt16 = sextdFromInt16
             val fInt32 = sextdFromInt32
             val fInt64 = sextdFromInt64)
      in
         val sextdFromObjptrInt = S.f
      end

      local
         datatype 'a ans =
            Big of bool * bool * 'a
          | Small of ObjptrWord.word
         fun 'a make {zextdFromMPLimb: MPLimb.word -> 'a,
                      other : {sizeInBits: Int32.int,
                               sizeInBitsWord: Word32.word,
                               zero: 'a,
                               lshift: 'a * Word32.word -> 'a,
                               orb: 'a * 'a -> 'a}} i =
            if isSmall i
               then Small (dropTagCoerce i)
               else let
                       val v = Prim.toVector i
                       val n = V.length v
                       val isneg = V.unsafeSub (v, 0) <> 0w0
                    in
                       if Int32.>= (MPLimb.sizeInBits, #sizeInBits other) 
                          then let
                                  val limbsPer : S.t = 1
                                  val limb = V.unsafeSub (v, 1)
                                  val extra =
                                     S.> (n, S.+ (limbsPer, 1))
                                     orelse
                                     (MPLimb.>>? (limb, #sizeInBitsWord other)) <> 0w0
                                  val ans = zextdFromMPLimb limb
                               in
                                  Big (isneg, extra, ans)
                               end
                          else let
                                  val limbsPer =
                                     S.sextdFromInt32
                                     (Int32.quot (#sizeInBits other,
                                                  MPLimb.sizeInBits))
                                  val extra = S.> (n, S.+ (limbsPer, 1))
                                  val ans =
                                     let
                                        fun loop (i, ans) =
                                           if S.> (i, 0)
                                              then let
                                                      val limb = V.unsafeSub (v, i)
                                                      val ans = 
                                                         (#orb other) 
                                                         ((#lshift other) 
                                                          (ans, MPLimb.sizeInBitsWord),
                                                          zextdFromMPLimb limb)
                                                   in
                                                      loop (S.- (i, 1), ans)
                                                   end
                                              else ans
                                     in
                                        loop (S.min (S.- (n, 1), limbsPer), #zero other)
                                     end
                               in
                                  Big (isneg, extra, ans)
                               end
                    end
      in
         val chckToWord8Aux =
            make {zextdFromMPLimb = MPLimb.zextdToWord8,
                  other = {sizeInBits = Word8.sizeInBits,
                           sizeInBitsWord = Word8.sizeInBitsWord,
                           zero = Word8.zero,
                           lshift = Word8.<<?,
                           orb = Word8.orb}}
         fun sextdToWord8 i =
            case chckToWord8Aux i of
               Small w => ObjptrWord.sextdToWord8 w
             | Big (isneg, _, ans) => if isneg then Word8.~ ans else ans
         fun sextdToInt8 i = IntWordConv.idFromWord8ToInt8 (sextdToWord8 i)
         val zextdToWord8 = sextdToWord8
         fun zextdToInt8 i = IntWordConv.idFromWord8ToInt8 (zextdToWord8 i)
         val castToWord8 = sextdToWord8
         val castToInt8 = sextdToInt8
         fun schckToWord8 i =
            if not Primitive.Controls.detectOverflow
               then sextdToWord8 i
            else
            case chckToWord8Aux i of
               Small w => ObjptrWord.schckToWord8 w
             | Big (isneg, extra, ans) => 
                  if extra
                     then raise Overflow
                  else if isneg
                     then let
                             val ans = Word8.~ ans
                             val ans' = IntWordConv.idFromWord8ToInt8 ans
                          in 
                             if Int8.> (ans', 0)
                                then raise Overflow
                                else ans
                          end
                  else let
                          val ans' = IntWordConv.idFromWord8ToInt8 ans
                       in
                          if Int8.< (ans', 0)
                             then raise Overflow
                             else ans
                       end
         fun schckToInt8 i = IntWordConv.idFromWord8ToInt8 (schckToWord8 i)
         fun zchckToWord8 i =
            if not Primitive.Controls.detectOverflow
               then zextdToWord8 i
            else
            case chckToWord8Aux i of
               Small w => ObjptrWord.schckToWord8 w
             | Big (isneg, extra, ans) => 
                  if isneg orelse extra
                     then raise Overflow
                  else ans
         fun zchckToInt8 i = IntWordConv.idFromWord8ToInt8 (zchckToWord8 i)

         val chckToWord16Aux =
            make {zextdFromMPLimb = MPLimb.zextdToWord16,
                  other = {sizeInBits = Word16.sizeInBits,
                           sizeInBitsWord = Word16.sizeInBitsWord,
                           zero = Word16.zero,
                           lshift = Word16.<<?,
                           orb = Word16.orb}}
         fun sextdToWord16 i =
            case chckToWord16Aux i of
               Small w => ObjptrWord.sextdToWord16 w
             | Big (isneg, _, ans) => if isneg then Word16.~ ans else ans
         fun sextdToInt16 i = IntWordConv.idFromWord16ToInt16 (sextdToWord16 i)
         val zextdToWord16 = sextdToWord16
         fun zextdToInt16 i = IntWordConv.idFromWord16ToInt16 (zextdToWord16 i)
         val castToWord16 = sextdToWord16
         val castToInt16 = sextdToInt16
         fun schckToWord16 i =
            if not Primitive.Controls.detectOverflow
               then sextdToWord16 i
            else
            case chckToWord16Aux i of
               Small w => ObjptrWord.schckToWord16 w
             | Big (isneg, extra, ans) => 
                  if extra
                     then raise Overflow
                  else if isneg
                     then let
                             val ans = Word16.~ ans
                             val ans' = IntWordConv.idFromWord16ToInt16 ans
                          in 
                             if Int16.> (ans', 0)
                                then raise Overflow
                                else ans
                          end
                  else let
                          val ans' = IntWordConv.idFromWord16ToInt16 ans
                       in
                          if Int16.< (ans', 0)
                             then raise Overflow
                             else ans
                       end
         fun schckToInt16 i = IntWordConv.idFromWord16ToInt16 (schckToWord16 i)
         fun zchckToWord16 i =
            if not Primitive.Controls.detectOverflow
               then zextdToWord16 i
            else
            case chckToWord16Aux i of
               Small w => ObjptrWord.schckToWord16 w
             | Big (isneg, extra, ans) => 
                  if isneg orelse extra
                     then raise Overflow
                  else ans
         fun zchckToInt16 i = IntWordConv.idFromWord16ToInt16 (zchckToWord16 i)

         val chckToWord32Aux =
            make {zextdFromMPLimb = MPLimb.zextdToWord32,
                  other = {sizeInBits = Word32.sizeInBits,
                           sizeInBitsWord = Word32.sizeInBitsWord,
                           zero = Word32.zero,
                           lshift = Word32.<<?,
                           orb = Word32.orb}}
         fun sextdToWord32 i =
            case chckToWord32Aux i of
               Small w => ObjptrWord.sextdToWord32 w
             | Big (isneg, _, ans) => if isneg then Word32.~ ans else ans
         fun sextdToInt32 i = IntWordConv.idFromWord32ToInt32 (sextdToWord32 i)
         val zextdToWord32 = sextdToWord32
         fun zextdToInt32 i = IntWordConv.idFromWord32ToInt32 (zextdToWord32 i)
         val castToWord32 = sextdToWord32
         val castToInt32 = sextdToInt32
         fun schckToWord32 i =
            if not Primitive.Controls.detectOverflow
               then sextdToWord32 i
            else
            case chckToWord32Aux i of
               Small w => ObjptrWord.schckToWord32 w
             | Big (isneg, extra, ans) => 
                  if extra
                     then raise Overflow
                  else if isneg
                     then let
                             val ans = Word32.~ ans
                             val ans' = IntWordConv.idFromWord32ToInt32 ans
                          in 
                             if Int32.> (ans', 0)
                                then raise Overflow
                                else ans
                          end
                  else let
                          val ans' = IntWordConv.idFromWord32ToInt32 ans
                       in
                          if Int32.< (ans', 0)
                             then raise Overflow
                             else ans
                       end
         fun schckToInt32 i = IntWordConv.idFromWord32ToInt32 (schckToWord32 i)
         fun zchckToWord32 i =
            if not Primitive.Controls.detectOverflow
               then zextdToWord32 i
            else
            case chckToWord32Aux i of
               Small w => ObjptrWord.schckToWord32 w
             | Big (isneg, extra, ans) => 
                  if isneg orelse extra
                     then raise Overflow
                  else ans
         fun zchckToInt32 i = IntWordConv.idFromWord32ToInt32 (zchckToWord32 i)

         val chckToWord64Aux =
            make {zextdFromMPLimb = MPLimb.zextdToWord64,
                  other = {sizeInBits = Word64.sizeInBits,
                           sizeInBitsWord = Word64.sizeInBitsWord,
                           zero = Word64.zero,
                           lshift = Word64.<<?,
                           orb = Word64.orb}}
         fun sextdToWord64 i =
            case chckToWord64Aux i of
               Small w => ObjptrWord.sextdToWord64 w
             | Big (isneg, _, ans) => if isneg then Word64.~ ans else ans
         fun sextdToInt64 i = IntWordConv.idFromWord64ToInt64 (sextdToWord64 i)
         val zextdToWord64 = sextdToWord64
         fun zextdToInt64 i = IntWordConv.idFromWord64ToInt64 (zextdToWord64 i)
         val castToWord64 = sextdToWord64
         val castToInt64 = sextdToInt64
         fun schckToWord64 i =
            if not Primitive.Controls.detectOverflow
               then sextdToWord64 i
            else
            case chckToWord64Aux i of
               Small w => ObjptrWord.schckToWord64 w
             | Big (isneg, extra, ans) => 
                  if extra
                     then raise Overflow
                  else if isneg
                     then let
                             val ans = Word64.~ ans
                             val ans' = IntWordConv.idFromWord64ToInt64 ans
                          in 
                             if Int64.> (ans', 0)
                                then raise Overflow
                                else ans
                          end
                  else let
                          val ans' = IntWordConv.idFromWord64ToInt64 ans
                       in
                          if Int64.< (ans', 0)
                             then raise Overflow
                             else ans
                       end
         fun schckToInt64 i = IntWordConv.idFromWord64ToInt64 (schckToWord64 i)
         fun zchckToWord64 i =
            if not Primitive.Controls.detectOverflow
               then zextdToWord64 i
            else
            case chckToWord64Aux i of
               Small w => ObjptrWord.schckToWord64 w
             | Big (isneg, extra, ans) => 
                  if isneg orelse extra
                     then raise Overflow
                  else ans
         fun zchckToInt64 i = IntWordConv.idFromWord64ToInt64 (zchckToWord64 i)

         fun zextdToIntInf ii = ii
         fun sextdToIntInf ii = ii
         fun castToIntInf ii = ii
         fun zchckToIntInf ii = ii
         fun schckToIntInf ii = ii
      end

      local
         val bytesPerMPLimb = Sz.zextdFromInt32 (Int32.quot (MPLimb.sizeInBits, 8))
      in
         val bytesPerSequenceMetaData = Sz.zextdFromInt32 SequenceMetaDataSize.bytes
         (* Reserve heap space for a large IntInf.int with room for num + extra
          * `limbs'.  The reason for splitting this up is that extra is intended
          * to be a constant, and so can be combined at compile time.
          *)
         fun reserve (num: S.int, extra: S.int) =
            Sz.+ (Sz.* (bytesPerMPLimb, Sz.zextdFromSeqIndex num),
            Sz.+ (Sz.* (bytesPerMPLimb, Sz.zextdFromSeqIndex extra),
            Sz.+ (bytesPerMPLimb, (* isneg Field *)
            Sz.+ (bytesPerSequenceMetaData, (* Sequence MetaData *)
                  case MLton.Align.align of (* alignment *)
                     MLton.Align.Align4 => 0w3
                   | MLton.Align.Align8 => 0w7
            ))))
      end

      (* badObjptr{Int,Word}{,Tagged} is the fixnum IntInf.int whose 
       * negation and absolute values are not fixnums. 
       * negBadIntInf is the negation (and absolute value) of that IntInf.int.
       *)
      val badObjptrInt: I.int = I.~>>? (I.minInt', 0w1)
      val badObjptrWord: W.word = W.idFromObjptrInt badObjptrInt
      val badObjptrWordTagged: W.word = addTag badObjptrWord
      (* val badObjptrIntTagged: I.int = W.idToObjptrInt badObjptrWordTagged *)
      val negBadIntInf: bigInt = sextdFromObjptrInt (I.~ badObjptrInt)

      (* Given two ObjptrWord.word's, check if they have the same 'high'/'sign' bit.
       *)
      fun sameSignBit (lhs: W.word, rhs: W.word): bool =
         I.>= (W.idToObjptrInt (W.xorb (lhs, rhs)), 0)

      (* Given a bignum bigint, test if it is (strictly) negative.
       *)
      fun bigIsNeg (arg: bigInt): bool =
         V.unsafeSub (Prim.toVector arg, 0) <> 0w0

      local
         fun make (smallOp, bigOp, limbsFn, extra)
                  (lhs: bigInt, rhs: bigInt): bigInt =
            let
               val res =
                  if areSmall (lhs, rhs)
                     then let
                             val lhsw = dropTagCoerce lhs
                             val lhsi = W.idToObjptrInt lhsw
                             val rhsw = dropTagCoerce rhs
                             val rhsi = W.idToObjptrInt rhsw
                             val ansi = smallOp (lhsi, rhsi)
                             val answ = W.idFromObjptrInt ansi
                             val ans = addTag answ
                          in
                             if sameSignBit (ans, answ)
                                then SOME (Prim.fromWord ans)
                                else NONE
                          end handle Overflow => NONE
                     else NONE
            in
               case res of
                  NONE => bigOp (lhs, rhs, 
                                 reserve (limbsFn (numLimbs lhs, numLimbs rhs), extra))
                | SOME i => i
            end
      in
         val bigAdd = make (I.+$, Prim.+, S.max, 1)
         val bigSub = make (I.-$, Prim.-, S.max, 1)
         val bigMul = make (I.*$, Prim.*, S.+, 0)
      end

      fun bigNeg (arg: bigInt): bigInt =
         if isSmall arg
            then let 
                    val argw = Prim.toWord arg
                 in 
                    if argw = badObjptrWordTagged
                       then negBadIntInf
                       else Prim.fromWord (W.- (0w2, argw))
                 end 
            else Prim.~ (arg, reserve (numLimbs arg, 1))


      fun bigQuot (num: bigInt, den: bigInt): bigInt =
         if areSmall (num, den)
            then let
                    val numw = dropTagCoerce num
                    val numi = W.idToObjptrInt numw
                    val denw = dropTagCoerce den
                    val deni = W.idToObjptrInt denw
                 in
                    if numw = badObjptrWord 
                       andalso deni = ~1
                       then negBadIntInf
                       else let
                               val ansi = I.quot (numi, deni)
                               val answ = W.idFromObjptrInt ansi
                               val ans = addTag answ
                            in 
                               Prim.fromWord ans
                            end
                 end
            else let
                    val nlimbs = numLimbs num
                    val dlimbs = numLimbs den
                 in
                    if S.< (nlimbs, dlimbs)
                       then zero
                       else if den = zero
                               then raise Div
                               else Prim.quot (num, den, 
                                               reserve (S.- (nlimbs, dlimbs), 2))
                 end

      fun bigRem (num: bigInt, den: bigInt): bigInt =
         if areSmall (num, den)
            then let 
                    val numw = dropTagCoerce num
                    val numi = W.idToObjptrInt numw
                    val denw = dropTagCoerce den
                    val deni = W.idToObjptrInt denw
                    val ansi = I.rem (numi, deni)
                    val answ = W.idFromObjptrInt ansi
                    val ans = addTag answ
                 in 
                    Prim.fromWord ans
                 end
            else let 
                    val nlimbs = numLimbs num
                    val dlimbs = numLimbs den
                 in 
                    if S.< (nlimbs, dlimbs)
                       then num
                       else if den = zero
                               then raise Div
                               else Prim.rem (num, den, 
                                              reserve (dlimbs, 1))
                 end

      (* Based on code from PolySpace. *)
      local
         open I

         fun mod2 x = I.andb (x, 1)
         fun div2 x = I.>>? (x, 0w1)

         fun smallGcd (a, b, acc) =
            case (a, b) of
               (0, _) => b * acc
             | (_, 0) => a * acc
             | (_, 1) => acc
             | (1, _) => acc
             | (_ : I.t * I.t) =>
                  if a = b
                     then a * acc
                     else
                        let
                           val a_2 = div2 a
                           val a_r2 = mod2 a
                           val b_2 = div2 b
                           val b_r2 = mod2 b
                        in
                           if 0 = a_r2
                              then
                                 if 0 = b_r2
                                    then smallGcd (a_2, b_2, acc + acc)
                                    else smallGcd (a_2, b, acc)
                              else
                                 if 0 = b_r2
                                    then smallGcd (a, b_2, acc)
                                    else
                                       if a >= b
                                          then smallGcd (div2 (a - b), b, acc)
                                          else smallGcd (a, div2 (b - a), acc)
                        end
      in
         fun bigGcd (lhs: bigInt, rhs: bigInt): bigInt =
            if areSmall (lhs, rhs)
               then addTagCoerceInt 
                    (smallGcd (I.abs (dropTagCoerceInt lhs),
                               I.abs (dropTagCoerceInt rhs),
                               1))
               else Prim.gcd 
                    (lhs, rhs, reserve (S.max (numLimbs lhs, numLimbs rhs), 0))
      end

      local
         fun make (smallTest: I.int * I.int -> 'a, 
                   int32Test: Int32.int * Int32.int -> 'a)
                  (lhs: bigInt, rhs: bigInt): 'a =
            if areSmall (lhs, rhs)
               then smallTest (W.idToObjptrInt (Prim.toWord lhs),
                               W.idToObjptrInt (Prim.toWord rhs))
               else int32Test (Prim.compare (lhs, rhs), 0)
      in
         val bigCompare = make (I.compare, Int32.compare)
         val bigLT = make (I.<, Int32.<)
         val bigLE = make (I.<=, Int32.<=)
         val bigGT = make (I.>, Int32.>)
         val bigGE = make (I.>=, Int32.>=)
      end

      fun bigAbs (arg: bigInt): bigInt =
         if isSmall arg
            then let 
                    val argw = Prim.toWord arg
                 in 
                    if argw = badObjptrWordTagged
                       then negBadIntInf
                       else if I.< (W.idToObjptrInt argw, 0)
                               then Prim.fromWord (W.- (0w2, argw))
                               else arg
                 end
            else if bigIsNeg arg
                    then Prim.~ (arg, reserve (numLimbs arg, 1))
                    else arg

      fun bigMin (lhs: bigInt, rhs: bigInt): bigInt =
         if bigLE (lhs, rhs) then lhs else rhs

      fun bigMax (lhs: bigInt, rhs: bigInt): bigInt =
         if bigLE (lhs, rhs) then rhs else lhs

      local
         fun bigLTU (lhs, rhs) =
            case (bigCompare (lhs, 0), bigCompare (rhs, 0)) of
               (LESS, LESS) => bigLT (lhs, rhs)
             | (LESS, GREATER) => false
             | (_, EQUAL) => false
             | (EQUAL, _) => true
             | (GREATER, LESS) => true
             | (GREATER, GREATER) => bigLT (lhs, rhs)
         structure S = IntegralComparisons(type t = bigInt
                                           val op < = bigLTU)
      in
         val bigLTU = S.<
         val bigLEU = S.<=
         val bigGTU = S.>
         val bigGEU = S.>=
      end

      local
         val op + = bigAdd
         val op - = bigSub
         val op > = bigGT
         val op >= = bigGE
         val op < = bigLT
         val quot = bigQuot
         val rem = bigRem
      in
         fun bigDiv (x, y) =
            if x >= zero
               then if y > zero
                       then quot (x, y)
                       else if y < zero
                               then if x = zero
                                       then zero
                                       else quot (x - one, y) - one
                               else raise Div
               else if y < zero
                       then quot (x, y)
                       else if y > zero
                               then quot (x + one, y) - one
                               else raise Div

         fun bigMod (x, y) =
            if x >= zero
               then if y > zero
                       then rem (x, y)
                       else if y < zero
                               then if x = zero
                                       then zero
                                       else rem (x - one, y) + (one + y)
                               else raise Div
               else if y < zero
                       then rem (x, y)
                       else if y > zero
                               then rem (x + one, y) + (y - one)
                               else raise Div

         fun bigDivMod (x, y) = (bigDiv (x, y), bigMod (x, y))
         fun bigQuotRem (x, y) = (bigQuot (x, y), bigRem (x, y))
      end

      local
         fun make (smallOp, bigOp) 
                  (lhs: bigInt, rhs: bigInt) =
            if areSmall (lhs, rhs)
               then
                  let
                     val answ = smallOp (Prim.toWord lhs, Prim.toWord rhs)
                     val ans = oneTagCoerce answ
                  in
                     ans
                  end
               else bigOp (lhs, rhs, 
                           reserve (S.max (numLimbs lhs, numLimbs rhs), 0))
      in
         val bigAndb = make (W.andb, Prim.andb)
         val bigOrb = make (W.orb, Prim.orb)
         val bigXorb = make (W.xorb, Prim.xorb)
      end

      fun bigNotb (arg: bigInt): bigInt =
         if isSmall arg
            then oneTagCoerce (W.notb (Prim.toWord arg))
            else Prim.notb (arg, reserve (numLimbs arg, 0))

      local
         val bitsPerLimb = MPLimb.sizeInBitsWord
         fun shiftSize shift = S.sextdFromWord32 (Word32.div (shift, bitsPerLimb))
      in
         fun bigLshift (arg: bigInt, shift: Word32.word): bigInt =
            if shift = 0wx0
               then arg
               else Prim.<< (arg, shift, 
                             reserve (S.+ (numLimbs arg, shiftSize shift), 1))
         fun bigRashift (arg: bigInt, shift: Word32.word): bigInt =
            if shift = 0wx0
               then arg
               else Prim.~>> (arg, shift,
                              reserve (S.max (0, S.- (numLimbs arg, shiftSize shift)), 1))
      end

      fun mkBigCvt {base: Int32.int,
                    smallCvt: I.int -> Primitive.String8.string}
                   (arg: bigInt)
                   : Primitive.String8.string =
         if isSmall arg
            then smallCvt (dropTagCoerceInt arg)
            else let
                    val bpd = Int32.log2 base
                    val bpl = MPLimb.sizeInBits
                    val dpl =
                       Int32.+ (Int32.quot (bpl, bpd),
                                if Int32.mod (bpl, bpd) = 0
                                   then 0 else 1)
                    val bytes =
                       Sz.+ (Sz.+ (bytesPerSequenceMetaData (* Sequence MetaData *),
                             Sz.+ (0w1 (* sign *),
                                   case MLton.Align.align of (* alignment *)
                                      MLton.Align.Align4 => 0w3
                                    | MLton.Align.Align8 => 0w7)),
                             Sz.* (Sz.zextdFromInt32 dpl, 
                                   Sz.zextdFromSeqIndex (numLimbs arg)))
                 in
                    Prim.toString (arg, base, bytes)
                 end

      fun mkBigLog2 {fromSmall: {smallLog2: Primitive.Int32.int} -> 'a,
                     fromLarge: {numLimbsMinusOne: SeqIndex.int,
                                 mostSigLimbLog2: Primitive.Int32.int} -> 'a}
                    (arg: bigInt) =
         if bigLE (arg, 0)
            then raise Domain
            else if isSmall arg
                    then fromSmall {smallLog2 = W.log2 (dropTagCoerce arg)}
                    else let
                            val v = Prim.toVector arg
                            val n = V.length v
                            val w = MPLimb.log2 (V.unsafeSub (v, S.- (n, 1)))
                         in
                            fromLarge {numLimbsMinusOne = S.- (n, 2),
                                       mostSigLimbLog2 = w}
                         end

      type int = bigInt
      type t = int

      val precision = NONE

      val maxInt = NONE
      val minInt = NONE

      val abs = bigAbs
      val op +! = bigAdd
      val op + = bigAdd
      val divMod = bigDivMod
      val op div = bigDiv
      val gcd = bigGcd
      val op mod = bigMod
      val op *! = bigMul
      val op * = bigMul
      val op ~! = bigNeg
      val op ~ = bigNeg
      val quotRem = bigQuotRem
      val quot = bigQuot
      val rem = bigRem
      val op -! = bigSub
      val op - = bigSub

      val op < = bigLT
      val op <= = bigLE
      val op > = bigGT
      val op >= = bigGE
      val compare = bigCompare
      val min = bigMin
      val max = bigMax
      val ltu = bigLTU
      val leu = bigLEU
      val gtu = bigGTU
      val geu = bigGEU

      val andb = bigAndb
      val <<? = bigLshift
      val << = bigLshift
      val notb = bigNotb
      val orb = bigOrb
      val ~>>? = bigRashift
      val ~>> = bigRashift
      val xorb = bigXorb

      val mkCvt = mkBigCvt
      val mkLog2 = mkBigLog2
end

structure IntWordConv : PRIM_INTWORD_CONV =
   struct
      open IntWordConv

      val idFromIntInfToIntInf = fn i => i


      val zextdFromInt8ToIntInf = IntInf.zextdFromInt8
      val zextdFromInt16ToIntInf = IntInf.zextdFromInt16
      val zextdFromInt32ToIntInf = IntInf.zextdFromInt32
      val zextdFromInt64ToIntInf = IntInf.zextdFromInt64
      val zextdFromWord8ToIntInf = IntInf.zextdFromWord8
      val zextdFromWord16ToIntInf = IntInf.zextdFromWord16
      val zextdFromWord32ToIntInf = IntInf.zextdFromWord32
      val zextdFromWord64ToIntInf = IntInf.zextdFromWord64

      val zextdFromIntInfToInt8 = IntInf.zextdToInt8
      val zextdFromIntInfToInt16 = IntInf.zextdToInt16
      val zextdFromIntInfToInt32 = IntInf.zextdToInt32
      val zextdFromIntInfToInt64 = IntInf.zextdToInt64
      val zextdFromIntInfToIntInf = IntInf.zextdToIntInf
      val zextdFromIntInfToWord8 = IntInf.zextdToWord8
      val zextdFromIntInfToWord16 = IntInf.zextdToWord16
      val zextdFromIntInfToWord32 = IntInf.zextdToWord32
      val zextdFromIntInfToWord64 = IntInf.zextdToWord64


      val sextdFromInt8ToIntInf = IntInf.sextdFromInt8
      val sextdFromInt16ToIntInf = IntInf.sextdFromInt16
      val sextdFromInt32ToIntInf = IntInf.sextdFromInt32
      val sextdFromInt64ToIntInf = IntInf.sextdFromInt64
      val sextdFromWord8ToIntInf = IntInf.sextdFromWord8
      val sextdFromWord16ToIntInf = IntInf.sextdFromWord16
      val sextdFromWord32ToIntInf = IntInf.sextdFromWord32
      val sextdFromWord64ToIntInf = IntInf.sextdFromWord64

      val sextdFromIntInfToInt8 = IntInf.sextdToInt8
      val sextdFromIntInfToInt16 = IntInf.sextdToInt16
      val sextdFromIntInfToInt32 = IntInf.sextdToInt32
      val sextdFromIntInfToInt64 = IntInf.sextdToInt64
      val sextdFromIntInfToIntInf = IntInf.sextdToIntInf
      val sextdFromIntInfToWord8 = IntInf.sextdToWord8
      val sextdFromIntInfToWord16 = IntInf.sextdToWord16
      val sextdFromIntInfToWord32 = IntInf.sextdToWord32
      val sextdFromIntInfToWord64 = IntInf.sextdToWord64


      val castFromInt8ToIntInf = IntInf.castFromInt8
      val castFromInt16ToIntInf = IntInf.castFromInt16
      val castFromInt32ToIntInf = IntInf.castFromInt32
      val castFromInt64ToIntInf = IntInf.castFromInt64
      val castFromWord8ToIntInf = IntInf.castFromWord8
      val castFromWord16ToIntInf = IntInf.castFromWord16
      val castFromWord32ToIntInf = IntInf.castFromWord32
      val castFromWord64ToIntInf = IntInf.castFromWord64

      val castFromIntInfToInt8 = IntInf.castToInt8
      val castFromIntInfToInt16 = IntInf.castToInt16
      val castFromIntInfToInt32 = IntInf.castToInt32
      val castFromIntInfToInt64 = IntInf.castToInt64
      val castFromIntInfToIntInf = IntInf.castToIntInf
      val castFromIntInfToWord8 = IntInf.castToWord8
      val castFromIntInfToWord16 = IntInf.castToWord16
      val castFromIntInfToWord32 = IntInf.castToWord32
      val castFromIntInfToWord64 = IntInf.castToWord64


      val zchckFromInt8ToIntInf = IntInf.zchckFromInt8
      val zchckFromInt16ToIntInf = IntInf.zchckFromInt16
      val zchckFromInt32ToIntInf = IntInf.zchckFromInt32
      val zchckFromInt64ToIntInf = IntInf.zchckFromInt64
      val zchckFromWord8ToIntInf = IntInf.zchckFromWord8
      val zchckFromWord16ToIntInf = IntInf.zchckFromWord16
      val zchckFromWord32ToIntInf = IntInf.zchckFromWord32
      val zchckFromWord64ToIntInf = IntInf.zchckFromWord64

      val zchckFromIntInfToInt8 = IntInf.zchckToInt8
      val zchckFromIntInfToInt16 = IntInf.zchckToInt16
      val zchckFromIntInfToInt32 = IntInf.zchckToInt32
      val zchckFromIntInfToInt64 = IntInf.zchckToInt64
      val zchckFromIntInfToIntInf = IntInf.zchckToIntInf
      val zchckFromIntInfToWord8 = IntInf.zchckToWord8
      val zchckFromIntInfToWord16 = IntInf.zchckToWord16
      val zchckFromIntInfToWord32 = IntInf.zchckToWord32
      val zchckFromIntInfToWord64 = IntInf.zchckToWord64


      val schckFromInt8ToIntInf = IntInf.schckFromInt8
      val schckFromInt16ToIntInf = IntInf.schckFromInt16
      val schckFromInt32ToIntInf = IntInf.schckFromInt32
      val schckFromInt64ToIntInf = IntInf.schckFromInt64
      val schckFromWord8ToIntInf = IntInf.schckFromWord8
      val schckFromWord16ToIntInf = IntInf.schckFromWord16
      val schckFromWord32ToIntInf = IntInf.schckFromWord32
      val schckFromWord64ToIntInf = IntInf.schckFromWord64

      val schckFromIntInfToInt8 = IntInf.schckToInt8
      val schckFromIntInfToInt16 = IntInf.schckToInt16
      val schckFromIntInfToInt32 = IntInf.schckToInt32
      val schckFromIntInfToInt64 = IntInf.schckToInt64
      val schckFromIntInfToIntInf = IntInf.schckToIntInf
      val schckFromIntInfToWord8 = IntInf.schckToWord8
      val schckFromIntInfToWord16 = IntInf.schckToWord16
      val schckFromIntInfToWord32 = IntInf.schckToWord32
      val schckFromIntInfToWord64 = IntInf.schckToWord64
   end

structure Int8 : PRIM_INTEGER =
   struct
      open Int8

      val zextdFromIntInf = IntWordConv.zextdFromIntInfToInt8
      val zextdToIntInf = IntWordConv.zextdFromInt8ToIntInf

      val sextdFromIntInf = IntWordConv.sextdFromIntInfToInt8
      val sextdToIntInf = IntWordConv.sextdFromInt8ToIntInf

      val castFromIntInf = IntWordConv.castFromIntInfToInt8
      val castToIntInf = IntWordConv.castFromInt8ToIntInf

      val zchckFromIntInf = IntWordConv.zchckFromIntInfToInt8
      val zchckToIntInf = IntWordConv.zchckFromInt8ToIntInf

      val schckFromIntInf = IntWordConv.schckFromIntInfToInt8
      val schckToIntInf = IntWordConv.schckFromInt8ToIntInf
   end
structure Int16 : PRIM_INTEGER =
   struct
      open Int16

      val zextdFromIntInf = IntWordConv.zextdFromIntInfToInt16
      val zextdToIntInf = IntWordConv.zextdFromInt16ToIntInf

      val sextdFromIntInf = IntWordConv.sextdFromIntInfToInt16
      val sextdToIntInf = IntWordConv.sextdFromInt16ToIntInf

      val castFromIntInf = IntWordConv.castFromIntInfToInt16
      val castToIntInf = IntWordConv.castFromInt16ToIntInf

      val zchckFromIntInf = IntWordConv.zchckFromIntInfToInt16
      val zchckToIntInf = IntWordConv.zchckFromInt16ToIntInf

      val schckFromIntInf = IntWordConv.schckFromIntInfToInt16
      val schckToIntInf = IntWordConv.schckFromInt16ToIntInf
   end
structure Int32 : PRIM_INTEGER =
   struct
      open Int32

      val zextdFromIntInf = IntWordConv.zextdFromIntInfToInt32
      val zextdToIntInf = IntWordConv.zextdFromInt32ToIntInf

      val sextdFromIntInf = IntWordConv.sextdFromIntInfToInt32
      val sextdToIntInf = IntWordConv.sextdFromInt32ToIntInf

      val castFromIntInf = IntWordConv.castFromIntInfToInt32
      val castToIntInf = IntWordConv.castFromInt32ToIntInf

      val zchckFromIntInf = IntWordConv.zchckFromIntInfToInt32
      val zchckToIntInf = IntWordConv.zchckFromInt32ToIntInf

      val schckFromIntInf = IntWordConv.schckFromIntInfToInt32
      val schckToIntInf = IntWordConv.schckFromInt32ToIntInf
   end
structure Int64 : PRIM_INTEGER =
   struct
      open Int64

      val zextdFromIntInf = IntWordConv.zextdFromIntInfToInt64
      val zextdToIntInf = IntWordConv.zextdFromInt64ToIntInf

      val sextdFromIntInf = IntWordConv.sextdFromIntInfToInt64
      val sextdToIntInf = IntWordConv.sextdFromInt64ToIntInf

      val castFromIntInf = IntWordConv.castFromIntInfToInt64
      val castToIntInf = IntWordConv.castFromInt64ToIntInf

      val zchckFromIntInf = IntWordConv.zchckFromIntInfToInt64
      val zchckToIntInf = IntWordConv.zchckFromInt64ToIntInf

      val schckFromIntInf = IntWordConv.schckFromIntInfToInt64
      val schckToIntInf = IntWordConv.schckFromInt64ToIntInf
   end
structure Word8 : PRIM_WORD =
   struct
      open Word8

      val zextdFromIntInf = IntWordConv.zextdFromIntInfToWord8
      val zextdToIntInf = IntWordConv.zextdFromWord8ToIntInf

      val sextdFromIntInf = IntWordConv.sextdFromIntInfToWord8
      val sextdToIntInf = IntWordConv.sextdFromWord8ToIntInf

      val castFromIntInf = IntWordConv.castFromIntInfToWord8
      val castToIntInf = IntWordConv.castFromWord8ToIntInf

      val zchckFromIntInf = IntWordConv.zchckFromIntInfToWord8
      val zchckToIntInf = IntWordConv.zchckFromWord8ToIntInf

      val schckFromIntInf = IntWordConv.schckFromIntInfToWord8
      val schckToIntInf = IntWordConv.schckFromWord8ToIntInf
   end
structure Word16 : PRIM_WORD =
   struct
      open Word16

      val zextdFromIntInf = IntWordConv.zextdFromIntInfToWord16
      val zextdToIntInf = IntWordConv.zextdFromWord16ToIntInf

      val sextdFromIntInf = IntWordConv.sextdFromIntInfToWord16
      val sextdToIntInf = IntWordConv.sextdFromWord16ToIntInf

      val castFromIntInf = IntWordConv.castFromIntInfToWord16
      val castToIntInf = IntWordConv.castFromWord16ToIntInf

      val zchckFromIntInf = IntWordConv.zchckFromIntInfToWord16
      val zchckToIntInf = IntWordConv.zchckFromWord16ToIntInf

      val schckFromIntInf = IntWordConv.schckFromIntInfToWord16
      val schckToIntInf = IntWordConv.schckFromWord16ToIntInf
   end
structure Word32 : PRIM_WORD =
   struct
      open Word32

      val zextdFromIntInf = IntWordConv.zextdFromIntInfToWord32
      val zextdToIntInf = IntWordConv.zextdFromWord32ToIntInf

      val sextdFromIntInf = IntWordConv.sextdFromIntInfToWord32
      val sextdToIntInf = IntWordConv.sextdFromWord32ToIntInf

      val castFromIntInf = IntWordConv.castFromIntInfToWord32
      val castToIntInf = IntWordConv.castFromWord32ToIntInf

      val zchckFromIntInf = IntWordConv.zchckFromIntInfToWord32
      val zchckToIntInf = IntWordConv.zchckFromWord32ToIntInf

      val schckFromIntInf = IntWordConv.schckFromIntInfToWord32
      val schckToIntInf = IntWordConv.schckFromWord32ToIntInf
   end
structure Word64 : PRIM_WORD =
   struct
      open Word64

      val zextdFromIntInf = IntWordConv.zextdFromIntInfToWord64
      val zextdToIntInf = IntWordConv.zextdFromWord64ToIntInf

      val sextdFromIntInf = IntWordConv.sextdFromIntInfToWord64
      val sextdToIntInf = IntWordConv.sextdFromWord64ToIntInf

      val castFromIntInf = IntWordConv.castFromIntInfToWord64
      val castToIntInf = IntWordConv.castFromWord64ToIntInf

      val zchckFromIntInf = IntWordConv.zchckFromIntInfToWord64
      val zchckToIntInf = IntWordConv.zchckFromWord64ToIntInf

      val schckFromIntInf = IntWordConv.schckFromIntInfToWord64
      val schckToIntInf = IntWordConv.schckFromWord64ToIntInf
   end

structure IntInf : PRIM_INT_INF = IntInf

end