File: ikarus-numerics.c

package info (click to toggle)
ikarus 0.0.3+bzr.2010.01.26-4
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, sid
  • size: 39,868 kB
  • ctags: 9,284
  • sloc: lisp: 47,954; ansic: 13,247; sh: 4,595; java: 641; asm: 366; makefile: 264; awk: 186; perl: 66
file content (2148 lines) | stat: -rw-r--r-- 63,255 bytes parent folder | download | duplicates (2)
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
/*
 *  Ikarus Scheme -- A compiler for R6RS Scheme.
 *  Copyright (C) 2006,2007,2008  Abdulaziz Ghuloum
 *  
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 3 as
 *  published by the Free Software Foundation.
 *  
 *  This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 */



#include "ikarus-data.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <gmp.h>

#define bnfst_limb_count(x) \
  (((unsigned long int)(x)) >> bignum_length_shift)
#define bnfst_negative(x) \
  (((unsigned long int)(x)) & bignum_sign_mask)
    

#define most_positive_fixnum  \
  (((unsigned long int)-1) >> (fx_shift+1))
#define most_negative_fixnum  (most_positive_fixnum+1)
// #define most_positive_fixnum 0x1FFFFFFF
// #define most_negative_fixnum 0x20000000

#define max_digits_per_limb ((wordsize==4)?10:20)

#ifdef NDEBUG
#define verify_bignum(x,caller) (x)
#else
static ikptr
verify_bignum(ikptr x, char* caller){
  if(tagof(x) != vector_tag){
    fprintf(stderr, "Error in (%s) invalid primary tag 0x%016lx\n", caller, x);
    exit(-1);
  }
  ikptr fst = ref(x, -vector_tag);
  long int limb_count = ((unsigned long int) fst) >> bignum_length_shift;
  if(limb_count <= 0){
    fprintf(stderr, 
        "Error in (%s) invalid limb count in fst=0x%016lx\n",
        caller, (long int)fst);
    exit(-1);
  }
  int pos;
  if((long int)fst & bignum_sign_mask){
    pos = 0;
  } else {
    pos = 1;
  }
  mp_limb_t last_limb = 
    (mp_limb_t) ref(x, off_bignum_data + (limb_count - 1) * wordsize);
  if(last_limb == 0){
    fprintf(stderr, 
        "Error in (%s) invalid last limb = 0x%016lx", caller, last_limb);
    exit(-1);
  }
  if(limb_count == 1){
    if(pos){
      if(last_limb <= most_positive_fixnum){
        fprintf(stderr, 
                "Error in (%s) should be a positive fixnum: 0x%016lx\n", 
                caller, last_limb);
        exit(-1);
      }
    } else {
      if(last_limb <= most_negative_fixnum){
        fprintf(stderr, 
                "Error in (%s) should be a negative fixnum: 0x%016lx\n", 
                caller, last_limb);
        exit(-1);
      }
    }
  }
  /* ok */
  return x;
}
#endif

#define BN(x) verify_bignum(x,"BN")

#if 0
ikptr 
ikrt_isbignum(ikptr x){
  if(tagof(x) == vector_tag){
    ikptr fst = ref(x, -vector_tag);
    if (bignum_tag == (bignum_mask & (int)fst)){
      return true_object;
    }
  }
  return false_object;
}
#endif

ikptr
ikrt_positive_bn(ikptr x){
  ikptr fst = ref(x, -vector_tag);
  if(bnfst_negative(fst)){
    return false_object;
  } else {
    return true_object;
  }
}

ikptr 
ikrt_even_bn(ikptr x){
  long int fst = (long int)ref(x, wordsize-vector_tag);
  if(fst & 1){
    return false_object;
  } else {
    return true_object;
  }
}



ikptr 
ikrt_fxfxplus(ikptr x, ikptr y, ikpcb* pcb){
  long int n1 = unfix(x);
  long int n2 = unfix(y);
  long int r = n1 + n2;
  ikptr q = fix(r);
  if(r == unfix(q)){
    return q;
  } 
  else {
    ikptr bn = ik_safe_alloc(pcb, align(disp_bignum_data + wordsize));
    if(r > 0){
      ref(bn, 0) = (ikptr)(bignum_tag | (1 << bignum_length_shift)); 
      ref(bn, disp_bignum_data) = (ikptr)r;
    }
    else {
      ref(bn, 0) = 
        (ikptr)(bignum_tag | 
              (1 << bignum_length_shift) | 
              (1 << bignum_sign_shift));
      ref(bn, disp_bignum_data) = (ikptr)-r;
    }
    return verify_bignum(bn+vector_tag, "fxfx+");
  }
}

ikptr 
ikrt_fxbnplus(ikptr x, ikptr y, ikpcb* pcb){
  if(x == 0){ return y ; }
  ikptr fst = ref(y, -vector_tag);
  long int limb_count = bnfst_limb_count(fst);
  long int intx = unfix(x);
  if(intx > 0){
    if(!bnfst_negative(fst)){
      /* positive fx + positive bn = even bigger positive */
      pcb->root0 = &y;
      ikptr r = ik_safe_alloc(pcb, align(disp_bignum_data+(limb_count+1)*wordsize));
      pcb->root0 = 0;
      mp_limb_t carry =
        mpn_add_1((mp_limb_t*)(long)(r+disp_bignum_data), 
                  (mp_limb_t*)(long)(y - vector_tag + disp_bignum_data),
                  limb_count,
                  intx);
      if(carry){
        ref(r, disp_bignum_data + limb_count*wordsize) = (ikptr)1;
        ref(r, 0) = (ikptr)
             (((limb_count + 1) << bignum_length_shift) |
              (0 << bignum_sign_shift) |
              bignum_tag);
        return verify_bignum(r+vector_tag, "fxbn+1");
      } else {
        ref(r, 0) = (ikptr)
          ((limb_count << bignum_length_shift) |
           (0 << bignum_sign_shift) |
           bignum_tag);
        return verify_bignum(r+vector_tag, "fxbn+2");
      }
    }
    else {
      /* positive fx + negative bn = smaller negative bn */
      pcb->root0 = &y;
      ikptr r = ik_safe_alloc(pcb, align(disp_bignum_data+limb_count*wordsize));
      pcb->root0 = 0;
      mp_limb_t borrow =
        mpn_sub_1((mp_limb_t*)(long)(r+disp_bignum_data), 
                  (mp_limb_t*)(long)(y - vector_tag + disp_bignum_data),
                  limb_count,
                  intx);
      if(borrow){
        fprintf(stderr, "Error: BUG in borrow1 %ld\n", borrow);
        exit(-1);
      }
      long int result_size = 
        (ref(r, disp_bignum_data + (limb_count-1)*wordsize)) 
        ? limb_count
        : (limb_count - 1);
      if(result_size == 0){ 
        return 0;
      }
      if(result_size == 1){
        mp_limb_t last = 
          (mp_limb_t) ref(r, disp_bignum_data + (result_size-1)*wordsize);
        if(last <= most_negative_fixnum){
          return fix(-(long int)last);
        }
      }
      ref(r, 0) = (ikptr)
        ((result_size << bignum_length_shift) |
         (1 << bignum_sign_shift) |
         bignum_tag);
      return verify_bignum(r+vector_tag, "fxbn+3");
    }
  }
  else {
    if(! bnfst_negative(fst)){
      /* negative fx + positive bn = smaller positive */
      pcb->root0 = &y;
      ikptr r = ik_safe_alloc(pcb, align(disp_bignum_data+limb_count*wordsize));
      pcb->root0 = 0;
      mp_limb_t borrow =
        mpn_sub_1((mp_limb_t*)(long)(r+disp_bignum_data), 
                  (mp_limb_t*)(long)(y - vector_tag + disp_bignum_data),
                  limb_count,
                  - intx);
      if(borrow){
        fprintf(stderr, "Error: BUG in borrow2\n");
        exit(-1);
      }
      long int result_size = 
        (ref(r, disp_bignum_data + (limb_count-1)*wordsize) == 0) 
        ? (limb_count - 1)
        : limb_count;
      if(result_size == 0){
        return 0;
      }
      if(result_size == 1){
        mp_limb_t last = 
          (mp_limb_t) ref(r, disp_bignum_data + (result_size-1)*wordsize);
        if(last <= most_positive_fixnum){
          return fix((long int)last);
        }
      }
      ref(r, 0) = (ikptr)
        ((result_size << bignum_length_shift) |
         (0 << bignum_sign_shift) |
         bignum_tag);
      return verify_bignum(r+vector_tag, "fxbn+4");
    } else {
      /* negative fx + negative bn = larger negative */
      pcb->root0 = &y;
      ikptr r = ik_safe_alloc(pcb, align(disp_bignum_data+(limb_count+1)*wordsize));
      pcb->root0 = 0;
      mp_limb_t carry =
        mpn_add_1((mp_limb_t*)(long)(r+disp_bignum_data), 
                  (mp_limb_t*)(long)(y - vector_tag + disp_bignum_data),
                  limb_count,
                  -intx);
      if(carry){
        ref(r, disp_bignum_data + limb_count*wordsize) = (ikptr)1;
        ref(r, 0) = (ikptr)
             (((limb_count + 1) << bignum_length_shift) |
              (1 << bignum_sign_shift) |
              bignum_tag);
        return verify_bignum(r+vector_tag, "fxbn+5");
      } else {
        ref(r, 0) = (ikptr)
          ((limb_count << bignum_length_shift) |
           (1 << bignum_sign_shift) |
           bignum_tag);
        return verify_bignum(r+vector_tag, "fxbn+5");
      }
    }
  }
}




ikptr 
ikrt_bnbnplus(ikptr x, ikptr y, ikpcb* pcb){
  unsigned long int xfst = (unsigned long int)ref(x, -vector_tag);
  unsigned long int yfst = (unsigned long int)ref(y, -vector_tag);
  long int xsign = xfst & bignum_sign_mask;
  long int ysign = yfst & bignum_sign_mask;
  long int xlimbs = xfst >> bignum_length_shift;
  long int ylimbs = yfst >> bignum_length_shift;
  if(xsign == ysign){
    long int n1,n2;
    ikptr s1,s2;
    if(xlimbs > ylimbs){
      n1 = xlimbs; n2 = ylimbs; s1 = x; s2 = y;
    } else {
      n1 = ylimbs; n2 = xlimbs; s1 = y; s2 = x;
    }
    pcb->root0 = &s1;
    pcb->root1 = &s2;
    ikptr res = ik_safe_alloc(pcb, align(disp_bignum_data + (n1+1)*wordsize));
    pcb->root0 = 0;
    pcb->root1 = 0;
    mp_limb_t carry =
      mpn_add((mp_limb_t*)(long)(res+disp_bignum_data),
              (mp_limb_t*)(long)(s1-vector_tag+disp_bignum_data),
              n1,
              (mp_limb_t*)(long)(s2-vector_tag+disp_bignum_data),
              n2);
    if(carry){
      ref(res, disp_vector_data + xlimbs*wordsize) = (ikptr)1;
      ref(res, 0) = (ikptr)
                    (((n1+1) << bignum_length_shift) |
                     xsign |
                     bignum_tag);
      return verify_bignum(res+vector_tag, "bnbn+1");
    } else {
      ref(res, 0) = (ikptr)
                    ((n1 << bignum_length_shift) |
                     xsign |
                     bignum_tag);
      return verify_bignum(res+vector_tag, "bnbn+2");
    }
  }
  else {
    ikptr s1=x, s2=y;
    long int n1=xlimbs, n2=ylimbs;
    long int result_sign = xsign;
    while((xlimbs == ylimbs) && 
          (ref(x, -vector_tag+disp_bignum_data+(xlimbs-1)*wordsize) == 
           ref(y, -vector_tag+disp_bignum_data+(xlimbs-1)*wordsize))){
      xlimbs -= 1;
      ylimbs -= 1;
      if(xlimbs == 0){ return 0; }
    }
    /* |x| != |y| */
    if(xlimbs <= ylimbs){
      if(xlimbs == ylimbs){
        if((ref(y, -vector_tag+disp_bignum_data+(xlimbs-1)*wordsize) > 
            ref(x, -vector_tag+disp_bignum_data+(xlimbs-1)*wordsize))){
          s1 = y; n1 = ylimbs;
          s2 = x; n2 = xlimbs;
          result_sign = ysign;
        }
      } else {
        s1 = y; n1 = ylimbs;
        s2 = x; n2 = xlimbs;
        result_sign = ysign;
      }
    }
    /* |s1| > |s2| */
    pcb->root0 = &s1;
    pcb->root1 = &s2;
    ikptr res = ik_safe_alloc(pcb, align(disp_bignum_data + n1 * wordsize));
    pcb->root0 = 0;
    pcb->root1 = 0;
    mp_limb_t burrow =
      mpn_sub((mp_limb_t*)(long)(res + disp_bignum_data),
              (mp_limb_t*)(long)(s1 - vector_tag + disp_bignum_data),
              n1,
              (mp_limb_t*)(long)(s2 - vector_tag + disp_bignum_data),
              n2);
    if(burrow){
      fprintf(stderr, "BUG: Burrow error in bnbn+\n");
      exit(-1);
    }
    long int len = n1;
    while(ref(res, disp_bignum_data + (len-1)*wordsize) == 0){
      len--;
      if(len == 0){
        return 0;
      }
    }
    if(result_sign == 0){
      /* positive result */
      if(len == 1){
        mp_limb_t fst_limb = (mp_limb_t) ref(res, disp_bignum_data);
        if(fst_limb <= most_positive_fixnum){
          return fix((long int)fst_limb);
        }
      }
      ref(res, 0) = (ikptr)
                    ((len << bignum_length_shift) |
                     result_sign |
                     bignum_tag);
      return verify_bignum(res+vector_tag, "bnbn+3");
    } else {
      /* negative result */
      if(len == 1){
        mp_limb_t fst_limb = (mp_limb_t) ref(res, disp_bignum_data);
        if(fst_limb <= most_negative_fixnum){
          return fix(-(long int)fst_limb);
        }
      }
      ref(res, 0) = (ikptr)
                    ((len << bignum_length_shift) |
                     result_sign |
                     bignum_tag);
      return verify_bignum(res+vector_tag, "bnbn+4");
    }
  }
}




ikptr 
ikrt_fxfxminus(ikptr x, ikptr y, ikpcb* pcb){
  long int n1 = unfix(x);
  long int n2 = unfix(y);
  long int r = n1 - n2;
  if(r >= 0){
    if(((unsigned long int)r) <= most_positive_fixnum){
      return fix(r);
    } else {
      ikptr bn = ik_safe_alloc(pcb, align(disp_bignum_data + wordsize));
      ref(bn, 0) = (ikptr) (bignum_tag | (1 << bignum_length_shift));
      ref(bn, disp_bignum_data) = (ikptr)r;
      return verify_bignum(bn+vector_tag,"fxfx-1");
    }
  } else {
    ikptr fxr = fix(r);
    if(unfix(fxr) == r){
      return fxr;
    } else {
      ikptr bn = ik_safe_alloc(pcb, align(disp_bignum_data + wordsize));
      ref(bn, 0) = (ikptr) 
        (bignum_tag | 
         (1 << bignum_sign_shift) |
         (1 << bignum_length_shift));
      ref(bn, disp_bignum_data) = (ikptr)(-r);
      return verify_bignum(bn+vector_tag, "fxfx-2");
    }
  }
}


ikptr 
ikrt_bnnegate(ikptr x, ikpcb* pcb){
  ikptr fst = ref(x, -vector_tag);
  long int limb_count = bnfst_limb_count(fst);
  if(limb_count == 1){
    if(! bnfst_negative(fst)){
      /* positive bignum */
      mp_limb_t limb =
        (mp_limb_t) ref(x, disp_bignum_data - vector_tag);
      if(limb == (most_positive_fixnum + 1)){
        return fix(-(long int)limb);
      }
    }
  }
  pcb->root0 = &x;
  ikptr bn = ik_safe_alloc(pcb, align(disp_bignum_data + limb_count * wordsize));
  pcb->root0 = 0;
  memcpy((char*)(long)bn+disp_bignum_data,
         (char*)(long)x-vector_tag+disp_bignum_data,
         limb_count*wordsize);
  ref(bn, 0) = (ikptr)
    (bignum_tag |
     ((1 << bignum_sign_shift) - (bignum_sign_mask & (long int)fst)) |
     (limb_count << bignum_length_shift));
  return verify_bignum(bn+vector_tag, "bnneg");
}

ikptr 
ikrt_fxbnminus(ikptr x, ikptr y, ikpcb* pcb){
  if(x == 0){ return ikrt_bnnegate(y, pcb) ; }
  ikptr fst = ref(y, -vector_tag);
  long int limb_count = bnfst_limb_count(fst);
  long int intx = unfix(x);
  if(intx > 0){
    if(bnfst_negative(fst)){
      /* positive fx - negative bn = positive bn */
      pcb->root0 = &y;
      ikptr r = ik_safe_alloc(pcb, align(disp_bignum_data+(limb_count+1)*wordsize));
      pcb->root0 = 0;
      long int carry =
        mpn_add_1((mp_limb_t*)(long)(r+disp_bignum_data), 
                  (mp_limb_t*)(long)(y - vector_tag + disp_bignum_data),
                  limb_count,
                  intx);
      if(carry){
        ref(r, disp_bignum_data + limb_count*wordsize) = (ikptr)1;
        ref(r, 0) = (ikptr)
             (((limb_count + 1) << bignum_length_shift) |
              (0 << bignum_sign_shift) |
              bignum_tag);
        return verify_bignum(r+vector_tag, "fxbn-1");
      } else {
        ref(r, 0) = (ikptr)
          ((limb_count << bignum_length_shift) |
           (0 << bignum_sign_shift) |
           bignum_tag);
        return verify_bignum(r+vector_tag, "fxbn-2");
      }
    }
    else {
      /* positive fx - positive bn = smaller negative bn/fx */
      pcb->root0 = &y;
      ikptr r = ik_safe_alloc(pcb, align(disp_bignum_data+limb_count*wordsize));
      pcb->root0 = 0;
      long int borrow =
        mpn_sub_1((mp_limb_t*)(long)(r+disp_bignum_data), 
                  (mp_limb_t*)(long)(y - vector_tag + disp_bignum_data),
                  limb_count,
                  intx);
      if(borrow){
        fprintf(stderr, "Error: BUG in borrow3\n");
        exit(-1);
      }
      long int result_size = 
        (ref(r, disp_bignum_data + (limb_count-1)*wordsize)) 
        ? limb_count
        : (limb_count - 1);
      if(result_size == 0){ 
        return 0;
      }
      if(result_size == 1){
        unsigned long int last = 
          (unsigned long int) ref(r, disp_bignum_data + (result_size-1)*wordsize);
        if(last <= most_negative_fixnum){
          return fix(-(long int)last);
        }
      }
      ref(r, 0) = (ikptr)
        ((result_size << bignum_length_shift) |
         (1 << bignum_sign_shift) |
         bignum_tag);
      return verify_bignum(r+vector_tag, "fxbn-");
    }
  }
  else {
    if(bnfst_negative(fst)){
      /* negative fx - negative bn = smaller positive */
      pcb->root0 = &y;
      ikptr r = ik_safe_alloc(pcb, align(disp_bignum_data+limb_count*wordsize));
      pcb->root0 = 0;
      long int borrow =
        mpn_sub_1((mp_limb_t*)(long)(r+disp_bignum_data), 
                  (mp_limb_t*)(long)(y - vector_tag + disp_bignum_data),
                  limb_count,
                  - intx);
      if(borrow){
        fprintf(stderr, "Error: BUG in borrow4\n");
        exit(-1);
      }
      long int result_size = 
        (ref(r, disp_bignum_data + (limb_count-1)*wordsize) == 0) 
        ? (limb_count - 1)
        : limb_count;
      if(result_size == 0){
        return 0;
      }
      if(result_size == 1){
        unsigned long int last = 
          (unsigned long int) ref(r, disp_bignum_data + (result_size-1)*wordsize);
        if(last <= most_positive_fixnum){
          return fix((long int)last);
        }
      }
      ref(r, 0) = (ikptr)
        ((result_size << bignum_length_shift) |
         (0 << bignum_sign_shift) |
         bignum_tag);
      return verify_bignum(r+vector_tag,"fxbn-");
    } else {
      /* negative fx - positive bn = larger negative */
      pcb->root0 = &y;
      ikptr r = ik_safe_alloc(pcb, align(disp_bignum_data+(limb_count+1)*wordsize));
      pcb->root0 = 0;
      long int carry = 
        mpn_add_1((mp_limb_t*)(long)(r+disp_bignum_data), 
                  (mp_limb_t*)(long)(y - vector_tag + disp_bignum_data),
                  limb_count,
                  -intx);
      if(carry){
        ref(r, disp_bignum_data + limb_count*wordsize) = (ikptr)1;
        ref(r, 0) = (ikptr)
             (((limb_count + 1) << bignum_length_shift) |
              (1 << bignum_sign_shift) |
              bignum_tag);
        return verify_bignum(r+vector_tag, "fxbn-");
      } else {
        ref(r, 0) = (ikptr)
          ((limb_count << bignum_length_shift) |
           (1 << bignum_sign_shift) |
           bignum_tag);
        return verify_bignum(r+vector_tag, "fxbn-");
      }
    }
  }
}

ikptr 
ikrt_bnfxminus(ikptr x, ikptr y, ikpcb* pcb){
  if(y == 0){ return x; }
  ikptr fst = ref(x, -vector_tag);
  long int limb_count = bnfst_limb_count(fst);
  long int inty = unfix(y);
  if(inty < 0){
    if(!bnfst_negative(fst)){
      /* - negative fx + positive bn = positive bn */
      pcb->root0 = &x;
      ikptr r = ik_safe_alloc(pcb, align(disp_bignum_data+(limb_count+1)*wordsize));
      pcb->root0 = 0;
      long int carry = 
        mpn_add_1((mp_limb_t*)(long)(r+disp_bignum_data), 
                  (mp_limb_t*)(long)(x - vector_tag + disp_bignum_data),
                  limb_count,
                  -inty);
      if(carry){
        ref(r, disp_bignum_data + limb_count*wordsize) = (ikptr)1;
        ref(r, 0) = (ikptr)
             (((limb_count + 1) << bignum_length_shift) |
              (0 << bignum_sign_shift) |
              bignum_tag);
        return verify_bignum(r+vector_tag,"bnfx-");
      } else {
        ref(r, 0) = (ikptr)
          ((limb_count << bignum_length_shift) |
           (0 << bignum_sign_shift) |
           bignum_tag);
        return verify_bignum(r+vector_tag,"bnfx-");
      }
    }
    else {
      /* - negative fx + negative bn = smaller negative bn/fx */
      pcb->root0 = &x;
      ikptr r = ik_safe_alloc(pcb, align(disp_bignum_data+limb_count*wordsize));
      pcb->root0 = 0;
      long int borrow =
        mpn_sub_1((mp_limb_t*)(long)(r+disp_bignum_data), 
                  (mp_limb_t*)(long)(x - vector_tag + disp_bignum_data),
                  limb_count,
                  -inty);
      if(borrow){
        fprintf(stderr, "Error: BUG in borrow5\n");
        exit(-1);
      }
      long int result_size = 
        (ref(r, disp_bignum_data + (limb_count-1)*wordsize)) 
        ? limb_count
        : (limb_count - 1);
      if(result_size == 0){ 
        return 0;
      }
      if(result_size == 1){
        unsigned long int last = 
          (unsigned long int) ref(r, disp_bignum_data + (result_size-1)*wordsize);
        if(last <= most_negative_fixnum){
          return fix(-(long int)last);
        }
      }
      ref(r, 0) = (ikptr)
        ((result_size << bignum_length_shift) |
         (1 << bignum_sign_shift) |
         bignum_tag);
      return verify_bignum(r+vector_tag,"bnfx-");
    }
  }
  else {
    if((bignum_sign_mask & (long int)fst) == 0){
      /* - positive fx + positive bn = smaller positive */
      pcb->root0 = &x;
      ikptr r = ik_safe_alloc(pcb, align(disp_bignum_data+limb_count*wordsize));
      pcb->root0 = 0;
      long int borrow = 
        mpn_sub_1((mp_limb_t*)(long)(r+disp_bignum_data), 
                  (mp_limb_t*)(long)(x - vector_tag + disp_bignum_data),
                  limb_count,
                  inty);
      if(borrow){
        fprintf(stderr, "Error: BUG in borrow6\n");
        exit(-1);
      }
      long int result_size = 
        (ref(r, disp_bignum_data + (limb_count-1)*wordsize) == 0) 
        ? (limb_count - 1)
        : limb_count;
      if(result_size == 0){
        return 0;
      }
      if(result_size == 1){
        unsigned long int last = 
          (unsigned long int) ref(r, disp_bignum_data + (result_size-1)*wordsize);
        if(last <= most_positive_fixnum){
          return fix((long int)last);
        }
      }
      ref(r, 0) = (ikptr)
        ((result_size << bignum_length_shift) |
         (0 << bignum_sign_shift) |
         bignum_tag);
      return verify_bignum(r+vector_tag, "bnfx-");
    } else {
      /* - positive fx + negative bn = larger negative */
      pcb->root0 = &x;
      ikptr r = ik_safe_alloc(pcb, align(disp_bignum_data+(limb_count+1)*wordsize));
      pcb->root0 = 0;
      long int carry = 
        mpn_add_1((mp_limb_t*)(long)(r+disp_bignum_data), 
                  (mp_limb_t*)(long)(x - vector_tag + disp_bignum_data),
                  limb_count,
                  inty);
      if(carry){
        ref(r, disp_bignum_data + limb_count*wordsize) = (ikptr)1;
        ref(r, 0) = (ikptr)
             (((limb_count + 1) << bignum_length_shift) |
              (1 << bignum_sign_shift) |
              bignum_tag);
        return verify_bignum(r+vector_tag, "bnfx-");
      } else {
        ref(r, 0) = (ikptr)
          ((limb_count << bignum_length_shift) |
           (1 << bignum_sign_shift) |
           bignum_tag);
        return verify_bignum(r+vector_tag, "bnfx-");
      }
    }
  }
}



ikptr 
ikrt_bnbnminus(ikptr x, ikptr y, ikpcb* pcb){
  if(x == y) { return 0; }
  unsigned long int xfst = (unsigned long int)ref(x, -vector_tag);
  unsigned long int yfst = (unsigned long int)ref(y, -vector_tag);
  long int xsign = xfst & bignum_sign_mask;
  long int ysign = yfst & bignum_sign_mask;
  long int xlimbs = xfst >> bignum_length_shift;
  long int ylimbs = yfst >> bignum_length_shift;
  if(xsign != ysign){
    long int n1,n2;
    ikptr s1,s2;
    if(xlimbs >= ylimbs){
      n1 = xlimbs; n2 = ylimbs; s1 = x; s2 = y;
    } else {
      n1 = ylimbs; n2 = xlimbs; s1 = y; s2 = x;
    }
    pcb->root0 = &s1;
    pcb->root1 = &s2;
    ikptr res = ik_safe_alloc(pcb, align(disp_bignum_data + (n1+1)*wordsize));
    pcb->root0 = 0;
    pcb->root1 = 0;
    mp_limb_t carry = 
      mpn_add((mp_limb_t*)(long)(res+disp_bignum_data),
              (mp_limb_t*)(long)(s1-vector_tag+disp_bignum_data),
              n1,
              (mp_limb_t*)(long)(s2-vector_tag+disp_bignum_data),
              n2);
    if(carry){
      ref(res, disp_vector_data + xlimbs*wordsize) = (ikptr)1;
      ref(res, 0) = (ikptr)
                    (((n1+1) << bignum_length_shift) |
                     xsign |
                     bignum_tag);
      return verify_bignum(res+vector_tag, "bnbn-");
    } else {
      ref(res, 0) = (ikptr)
                    ((n1 << bignum_length_shift) |
                     xsign |
                     bignum_tag);
      return verify_bignum(res+vector_tag, "bnbn-");
    }
  }
  else {
    /* same sign */
    if(xlimbs == ylimbs){
      while((ref(x, -vector_tag+disp_bignum_data+(xlimbs-1)*wordsize) == 
             ref(y, -vector_tag+disp_bignum_data+(xlimbs-1)*wordsize))){
        xlimbs -= 1;
        if(xlimbs == 0){ return 0; }
      }
      ylimbs = xlimbs;
    }
    ikptr s1=x, s2=y;
    long int n1=xlimbs, n2=ylimbs;
    long int result_sign = xsign;
    /* |x| != |y| */
    if(xlimbs <= ylimbs){
      if(xlimbs == ylimbs){
        if((ref(y, -vector_tag+disp_bignum_data+(xlimbs-1)*wordsize) > 
            ref(x, -vector_tag+disp_bignum_data+(xlimbs-1)*wordsize))){
          s1 = y; n1 = ylimbs;
          s2 = x; n2 = xlimbs;
          result_sign = (1 << bignum_sign_shift) - ysign;
        }
      } else {
        s1 = y; n1 = ylimbs;
        s2 = x; n2 = xlimbs;
        result_sign = (1 << bignum_sign_shift) - ysign;
      }
    }
    /* |s1| > |s2| */
    pcb->root0 = &s1;
    pcb->root1 = &s2;
    ikptr res = ik_safe_alloc(pcb, align(disp_bignum_data + n1 * wordsize));
    bzero((void*)(res+disp_bignum_data), n1*wordsize);
    pcb->root0 = 0;
    pcb->root1 = 0;
    long int burrow = 
      mpn_sub((mp_limb_t*)(long)(res + disp_bignum_data),
              (mp_limb_t*)(long)(s1 - vector_tag + disp_bignum_data),
              n1,
              (mp_limb_t*)(long)(s2 - vector_tag + disp_bignum_data),
              n2);
    if(burrow){
      fprintf(stderr, "BUG: Burrow error in bnbn-\n");
      exit(-1);
    }
    long int len = n1;
    while(ref(res, disp_bignum_data + (len-1)*wordsize) == 0){
      len--;
      if(len == 0){
        return 0;
      }
    }
    if(result_sign == 0){
      /* positive result */
      if(len == 1){
        unsigned long int fst_limb = 
          (unsigned long int) ref(res, disp_bignum_data);
        if(fst_limb <= most_positive_fixnum){
          return fix((long int)fst_limb);
        }
      }
      ref(res, 0) = (ikptr)
                    ((len << bignum_length_shift) |
                     result_sign |
                     bignum_tag);
      return verify_bignum(res+vector_tag, "bnbn-");
    } else {
      /* negative result */
      if(len == 1){
        unsigned long int fst_limb = 
          (unsigned long int) ref(res, disp_bignum_data);
        if(fst_limb <= most_negative_fixnum){
          return fix(-(long int)fst_limb);
        }
      }
      ref(res, 0) = (ikptr)
                    ((len << bignum_length_shift) |
                     result_sign |
                     bignum_tag);
      return verify_bignum(res+vector_tag, "bnbn-");
    }
  }
}


ikptr 
ikrt_fxfxmult(ikptr x, ikptr y, ikpcb* pcb){
  long int n1 = unfix(x);
  long int n2 = unfix(y);
  mp_limb_t lo = 0;
  mp_limb_t s1 = n1;
  mp_limb_t s2 = n2;
  long int sign = 0;
  if(n1 < 0){
    s1 = -n1;
    sign = 1 - sign;
  } 
  if(n2 < 0){
    s2 = -n2;
    sign = 1 - sign;
  } 
  mp_limb_t hi = mpn_mul_1(&lo, &s1, 1, s2);
  if(hi == 0){
    if(sign){
      if(lo <= most_negative_fixnum){
        return fix(-((long int)lo));
      } 
    } else {
      if(lo <= most_positive_fixnum){
        return fix((long int)lo);
      }
    }
    ikptr r = ik_safe_alloc(pcb, disp_bignum_data + wordsize);
    ref(r, 0) = (ikptr)
      (bignum_tag |
       (sign << bignum_sign_shift) |
       (1 << bignum_length_shift));
    ref(r, disp_bignum_data) = (ikptr)lo;
    return BN(r+vector_tag);
  } else {
    ikptr r = ik_safe_alloc(pcb, align(disp_bignum_data + 2*wordsize));
    ref(r, 0) = (ikptr)
      (bignum_tag | 
       (sign << bignum_sign_shift) |
       (2 << bignum_length_shift));
    ref(r, disp_bignum_data) = (ikptr)lo;
    ref(r, disp_bignum_data+wordsize) = (ikptr)hi;
    return BN(r+vector_tag);
  }
}

ikptr
normalize_bignum(long int limbs, int sign, ikptr r){
  while(ref(r, disp_bignum_data + (limbs-1)*wordsize) == 0){
    limbs--;
    if(limbs == 0){ return 0;}
  }
  if(limbs == 1){
    mp_limb_t last = (mp_limb_t) ref(r, disp_bignum_data);
    if(sign == 0){
      if(last <= most_positive_fixnum){
        return fix(last);
      }
    } else {
      if(last <= most_negative_fixnum){
        return fix(-(last));
      }
    }
  }
  ref(r, 0) = (ikptr) (bignum_tag | sign | (limbs << bignum_length_shift));
  return BN(r+vector_tag);
}


ikptr 
ikrt_fxbnmult(ikptr x, ikptr y, ikpcb* pcb){
  long int n2 = unfix(x);
  if(n2 == 0) { return 0; }
  mp_limb_t s2 = (n2>0) ? n2 : (- n2);
  ikptr fst = ref(y, -vector_tag);
  long int limb_count = bnfst_limb_count(fst);
  pcb->root0 = &y;
  ikptr r = ik_safe_alloc(pcb, align(disp_bignum_data + (limb_count+1)*wordsize));
  pcb->root0 = 0;
  mp_limb_t hi = mpn_mul_1((mp_limb_t*)(long)(r+disp_bignum_data),
                           (mp_limb_t*)(long)(y-vector_tag+disp_bignum_data),
                           limb_count,
                           s2);
  ref(r, disp_bignum_data + limb_count * wordsize) = (ikptr)hi;
  long int sign = 
    ((n2 > 0) ?
     (bignum_sign_mask & (long int)fst) : 
     ((1 << bignum_sign_shift) - (bignum_sign_mask&(long int)fst)));
  return normalize_bignum(limb_count+1, sign, r);
}

ikptr 
ikrt_bnbnmult(ikptr x, ikptr y, ikpcb* pcb){
  long int f1 = (long int)ref(x, -vector_tag);
  long int f2 = (long int)ref(y, -vector_tag);
  long int n1 = bnfst_limb_count(f1);
  long int n2 = bnfst_limb_count(f2);
  long int nr = n1 + n2;
  pcb->root0 = &x;
  pcb->root1 = &y;
  ikptr bn = ik_safe_alloc(pcb, align(disp_bignum_data + nr*wordsize));
  pcb->root0 = 0;
  pcb->root1 = 0;
  mp_limb_t r;
  if(n1 >= n2){
    r = mpn_mul((mp_limb_t*)(long)(bn+disp_bignum_data),
                (mp_limb_t*)(long)(x-vector_tag+disp_bignum_data),
                n1,
                (mp_limb_t*)(long)(y-vector_tag+disp_bignum_data),
                n2);
  } else {
    r = mpn_mul((mp_limb_t*)(long)(bn+disp_bignum_data),
                (mp_limb_t*)(long)(y-vector_tag+disp_bignum_data),
                n2,
                (mp_limb_t*)(long)(x-vector_tag+disp_bignum_data),
                n1);
  } 
  long int sign = 
    ((bignum_sign_mask & f1) ?
     ((1 << bignum_sign_shift) - (bignum_sign_mask & f2)) :
     (bignum_sign_mask & f2));
  return normalize_bignum(nr, sign, bn);
}




ikptr
ikrt_bnbncomp(ikptr bn1, ikptr bn2){
  ikptr f1 = ref(bn1, -vector_tag);
  ikptr f2 = ref(bn2, -vector_tag);
  if(bnfst_negative(f1)){
    if(bnfst_negative(f2)){
      /* both negative */
      long int n1 = ((mp_limb_t) f1) >> bignum_length_shift;
      long int n2 = ((mp_limb_t) f2) >> bignum_length_shift;
      if(n1 < n2) {
        return fix(1);
      } else if(n1 > n2){
        return fix(-1);
      } else {
        long int i;
        for(i=(n1-1); i>=0; i--){
          mp_limb_t t1 = 
            (mp_limb_t) ref(bn1,disp_bignum_data-vector_tag+i*wordsize);
          mp_limb_t t2 = 
            (mp_limb_t) ref(bn2,disp_bignum_data-vector_tag+i*wordsize);
          if(t1 < t2){
            return fix(1);
          } else if(t1 > t2){
            return fix(-1);
          }
        }
      }
      return 0;
    } else {
      /* n1 negative, n2 positive */
      return fix(-1);
    } 
  } else {
    if(bnfst_negative(f2)){
      /* n1 positive, n2 negative */
      return fix(1);
    } else {
      /* both positive */
      long int n1 = ((mp_limb_t) f1) >> bignum_length_shift;
      long int n2 = ((mp_limb_t) f2) >> bignum_length_shift;
      if(n1 < n2) {
        return fix(-1);
      } else if(n1 > n2){
        return fix(1);
      } else {
        long int i;
        for(i=(n1-1); i>=0; i--){
          mp_limb_t t1 = 
           (mp_limb_t) ref(bn1,disp_bignum_data-vector_tag+i*wordsize);
          mp_limb_t t2 = 
            (mp_limb_t) ref(bn2,disp_bignum_data-vector_tag+i*wordsize);
          if(t1 < t2){
            return fix(-1);
          } else if(t1 > t2){
            return fix(1);
          }
        }
      }
      return 0;
    }
  }
}


static inline int
count_leading_ffs(int n, mp_limb_t* x){
  int idx;
  for(idx=0; idx<n; idx++){
    if(x[idx] != (mp_limb_t)-1){
      return idx;
    }
  }
  return n;
}


static void
copy_limbs(mp_limb_t* src, mp_limb_t* dst, int n1, int n2){
  while(n1 < n2){
    dst[n1] = src[n1];
    n1++;
  }
}

static void
bits_compliment(mp_limb_t* src, mp_limb_t* dst, long int n){
  mp_limb_t carry = 1;
  long int i;
  for(i=0; i<n; i++){
    mp_limb_t d = src[i];
    mp_limb_t c = carry + ~ d;
    dst[i] = c;
    carry = (carry && ! d);
  }
}

static void
bits_compliment2(mp_limb_t* src, mp_limb_t* dst, int n1, int n2){
  mp_limb_t carry = 1;
  int i;
  for(i=0; i<n1; i++){
    mp_limb_t d = src[i];
    mp_limb_t c = carry + ~ d;
    dst[i] = c;
    carry = (carry && ! d);
  }
  for(i=n1; i<n2; i++){
    mp_limb_t d = 0;
    mp_limb_t c = carry + ~ d;
    dst[i] = c;
    carry = (carry && ! d);
  }
}

static int
bits_compliment_carry(mp_limb_t* src, mp_limb_t* dst, int n1, int n2, mp_limb_t carry){
  int i;
  for(i=n1; i<n2; i++){
    mp_limb_t d = src[i];
    mp_limb_t c = carry + ~ d;
    dst[i] = c;
    carry = (carry && ! d);
  }
  return carry;
}




static void
bits_compliment_with_carry(mp_limb_t* src, mp_limb_t* dst, long int n, long int carry){
  long int i;
  for(i=0; i<n; i++){
    mp_limb_t d = src[i];
    mp_limb_t c = carry + ~ d;
    dst[i] = c;
    carry = (carry && ! d);
  }
}

static void
bits_compliment_logand(mp_limb_t* s1, mp_limb_t* s2, mp_limb_t* dst, int n){
  int carry = 1;
  int i;
  for(i=0; i<n; i++){
    mp_limb_t d = s1[i];
    mp_limb_t c = carry + ~ d;
    dst[i] = c & s2[i];
    carry = (carry && ! d);
  }
}



static int
bits_compliment_logor(mp_limb_t* s1, mp_limb_t* s2, mp_limb_t* dst, int n){
  int carry = 1;
  int i;
  for(i=0; i<n; i++){
    mp_limb_t d = s1[i];
    mp_limb_t c = carry + ~ d;
    dst[i] = c | s2[i];
    carry = (carry && ! d);
  }
  return carry;
}


static long int
bits_carry(mp_limb_t* s,  int n){
  /*
  int carry = 1;
  int i;
  for(i=0; i<n; i++){
    mp_limb_t d = s[i];
    carry = (carry && ! d);
  }
  return carry;
  */
  int i;
  for(i=0; i<n; i++){
    if (s[i] != 0){
      return 0;
    }
  }
  return 1;
}

ikptr
ikrt_bnlognot(ikptr x, ikpcb* pcb){
  ikptr fst = ref(x, -vector_tag);
  long int n = bnfst_limb_count(fst);
  if(bnfst_negative(fst)){
    /* negative */
    pcb->root0 = &x;
    ikptr r = ik_safe_alloc(pcb, align(disp_bignum_data + n*wordsize));
    pcb->root0 = 0;
    mp_limb_t* s1 = (mp_limb_t*)(long)(x+disp_bignum_data-vector_tag);
    mp_limb_t* rd = (mp_limb_t*)(long)(r+disp_bignum_data);
    int i;
    for(i=0; (i<n) && (s1[i] == 0); i++) {
      rd[i] = -1;
    }
    rd[i] = s1[i] - 1;
    for(i++; i<n; i++){
      rd[i] = s1[i];
    }
    return normalize_bignum(n, 0, r);
  } else {
    /* positive */
    long int i;
    mp_limb_t* s1 = (mp_limb_t*)(long)(x+disp_bignum_data-vector_tag);
    for(i=0; (i<n) && (s1[i] == (mp_limb_t)-1); i++) {/*nothing*/}
    if(i==n){
      pcb->root0 = &x;
      ikptr r = ik_safe_alloc(pcb, align(disp_bignum_data + (n+1)*wordsize));
      pcb->root0 = 0;
      bzero((char*)(long)r+disp_bignum_data, n*wordsize);
      ((mp_limb_t*)(long)(r+disp_bignum_data))[n] = 1;
      ref(r, 0) = (ikptr)
        (bignum_tag | (1<<bignum_sign_shift) | ((n+1) << bignum_length_shift));
      return r+vector_tag;
    } else {
      pcb->root0 = &x;
      ikptr r = ik_safe_alloc(pcb, align(disp_bignum_data + n*wordsize));
      pcb->root0 = 0;
      mp_limb_t* s1 = (mp_limb_t*)(long)(x+disp_bignum_data-vector_tag);
      mp_limb_t* rd = (mp_limb_t*)(long)(r+disp_bignum_data);
      int j;
      for(j=0; j<i; j++){ rd[j] = 0; }
      rd[i] = s1[i] + 1;
      for(j=i+1; j<n; j++){ rd[j] = s1[j]; }
      ref(r, 0) = (ikptr)
        (bignum_tag | (1<<bignum_sign_shift) | (n << bignum_length_shift));
      return r+vector_tag;
    }
  }
}


ikptr
ikrt_fxbnlogand(ikptr x, ikptr y, ikpcb* pcb){
  long int n1 = unfix(x);
  ikptr fst = ref(y, -vector_tag);
  if(n1 >= 0){
    /* x is positive */
    if(bnfst_negative(fst)){
      /* y is negative */
      return fix(n1 & (1+~(long int)ref(y, disp_vector_data-vector_tag))); 
    } else {
      /* y is positive */
      return fix(n1 & (long int)ref(y, disp_vector_data-vector_tag)); 
    }
  } else {
    /* x is negative */
    if(n1 == -1){ return y; }
    if(bnfst_negative(fst)){
      /* y is negative */
      long int len = bnfst_limb_count(fst);
      pcb->root0 = &y;
      ikptr r = ik_safe_alloc(pcb, align(disp_bignum_data + (len+1)*wordsize));
      pcb->root0 = 0;
      mp_limb_t* s2 = (mp_limb_t*)(long)(y+disp_bignum_data-vector_tag);
      mp_limb_t* s = (mp_limb_t*)(long)(r+disp_bignum_data);
      bits_compliment2(s2, s, len, len+1);
      s[0] = s[0] & n1;
      bits_compliment2(s, s, len+1, len+1);
      return normalize_bignum(len+1, 1<<bignum_sign_shift, r);
    } else {
      /* y is positive */
      long int len = bnfst_limb_count(fst);
      pcb->root0 = &y;
      ikptr r = ik_safe_alloc(pcb, align(disp_bignum_data + len * wordsize));
      pcb->root0 = 0;
      ref(r, 0) = fst;
      ref(r, disp_bignum_data) = (ikptr)
        (((long int)ref(y, disp_bignum_data - vector_tag)) & n1);
      int i;
      for(i=1; i<len; i++){
        ref(r, disp_bignum_data+i*wordsize) = 
          ref(y, disp_bignum_data-vector_tag+i*wordsize);
      }
      return BN(r+vector_tag);
    }
  }
}

ikptr
ikrt_bnbnlogand(ikptr x, ikptr y, ikpcb* pcb){
  ikptr xfst = ref(x, -vector_tag);
  ikptr yfst = ref(y, -vector_tag);
  long int n1 = bnfst_limb_count(xfst);
  long int n2 = bnfst_limb_count(yfst);
  if(bnfst_negative(xfst)){
    if(bnfst_negative(yfst)){
      if(n1 >= n2){
        pcb->root0 = &x;
        pcb->root1 = &y;
        ikptr r = ik_safe_alloc(pcb, align(disp_bignum_data + (n1+1)*wordsize));
        pcb->root0 = 0;
        pcb->root1 = 0;
        mp_limb_t* s1 = (mp_limb_t*)(long)(x+disp_bignum_data-vector_tag);
        mp_limb_t* s2 = (mp_limb_t*)(long)(y+disp_bignum_data-vector_tag);
        mp_limb_t* s = (mp_limb_t*)(long)(r+disp_bignum_data);
        bits_compliment2(s1, s, n1, n1+1);
        bits_compliment_logand(s2, s, s, n2);
        bits_compliment2(s, s, n1+1, n1+1);
        return normalize_bignum(n1+1, 1<<bignum_sign_shift, r);
      } else {
        return ikrt_bnbnlogand(y,x,pcb);
      }
    } else {
      return ikrt_bnbnlogand(y,x,pcb);
    }
  } else {
    if(bnfst_negative(yfst)){
      /* x positive, y negative */
      /*  the result is at most n1 words long */
      pcb->root0 = &x;
      pcb->root1 = &y;
      ikptr r = ik_safe_alloc(pcb, align(disp_bignum_data + n1*wordsize));
      pcb->root0 = 0;
      pcb->root1 = 0;
      mp_limb_t* s1 = (mp_limb_t*)(long)(x+disp_bignum_data-vector_tag);
      mp_limb_t* s2 = (mp_limb_t*)(long)(y+disp_bignum_data-vector_tag);
      mp_limb_t* s = (mp_limb_t*)(long)(r+disp_bignum_data);
      if(n1 <= n2){
        bits_compliment_logand(s2, s1, s, n1);
      } else {
        bits_compliment_logand(s2, s1, s, n2);
        copy_limbs(s1, s, n2, n1);
      }
      return normalize_bignum(n1, 0, r);
    } else {
      /* both positive */
      int n = (n1<n2)?n1:n2;
      long int i;
      for(i=n-1; i>=0; i--){
        long int l1 = 
          (long int) ref(x, disp_bignum_data-vector_tag+i*wordsize);
        long int l2 = 
          (long int) ref(y, disp_bignum_data-vector_tag+i*wordsize);
        unsigned long int last = l1 & l2;
        if(last){
          if((i == 0) && (last < most_positive_fixnum)){
            return fix(last);
          }
          pcb->root0 = &x;
          pcb->root1 = &y;
          ikptr r = ik_safe_alloc(pcb, align(disp_bignum_data+(i+1)*wordsize));
          pcb->root0 = 0;
          pcb->root1 = 0;
          ref(r, 0) = (ikptr) (bignum_tag | ((i+1)<<bignum_length_shift));
          ref(r, disp_bignum_data + i*wordsize) = (ikptr)last;
          int j;
          for(j=0; j<i; j++){
            ref(r, disp_bignum_data + j*wordsize) = (ikptr)
              (((long int)ref(x, disp_bignum_data-vector_tag+j*wordsize))
               &
               ((long int)ref(y, disp_bignum_data-vector_tag+j*wordsize)));
          }
          return r+vector_tag;
        }
      }
      return 0;
    }
  }
}


ikptr
ikrt_fxbnlogor(ikptr x, ikptr y, ikpcb* pcb){
  long int n1 = unfix(x);
  ikptr fst = ref(y, -vector_tag);
  if(n1 < 0){
    /* x is negative */
    if(bnfst_negative(fst)){
      /* y is negative */
      return fix(n1 | (1+~(long int)ref(y, disp_vector_data-vector_tag))); 
    } else {
      /* y is positive */
      return fix(n1 | (long int)ref(y, disp_vector_data-vector_tag)); 
    }
  } else {
    /* x is non negative */
    if(n1 == 0){ return y; }
    /* x is positive */
    if(bnfst_negative(fst)){
      /* y is negative */
      long int len = bnfst_limb_count(fst);
      pcb->root0 = &y;
      ikptr r = ik_safe_alloc(pcb, align(disp_bignum_data + (len+1)*wordsize));
      pcb->root0 = 0;
      mp_limb_t* s2 = (mp_limb_t*)(long)(y+disp_bignum_data-vector_tag);
      mp_limb_t* s = (mp_limb_t*)(long)(r+disp_bignum_data);
      bits_compliment2(s2, s, len, len+1);
      s[0] = s[0] | n1;
      bits_compliment2(s, s, len+1, len+1);
      return normalize_bignum(len+1, 1<<bignum_sign_shift, r);
    } else {
      /* y is positive */
      long int len = bnfst_limb_count(fst);
      pcb->root0 = &y;
      ikptr r = ik_safe_alloc(pcb, align(disp_bignum_data + len * wordsize));
      pcb->root0 = 0;
      ref(r, 0) = fst;
      ref(r, disp_bignum_data) = (ikptr)
        (((long int)ref(y, disp_bignum_data - vector_tag)) | n1);
      int i;
      for(i=1; i<len; i++){
        ref(r, disp_bignum_data+i*wordsize) = 
          ref(y, disp_bignum_data-vector_tag+i*wordsize);
      }
      return BN(r+vector_tag);
    }
  }
}

ikptr
ikrt_bnbnlogor(ikptr x, ikptr y, ikpcb* pcb){
  ikptr xfst = ref(x, -vector_tag);
  ikptr yfst = ref(y, -vector_tag);
  long int n1 = bnfst_limb_count(xfst);
  long int n2 = bnfst_limb_count(yfst);
  if(bnfst_negative(xfst)){
    if(bnfst_negative(yfst)){
      if(n1 >= n2){
        pcb->root0 = &x;
        pcb->root1 = &y;
        ikptr r = ik_safe_alloc(pcb, align(disp_bignum_data + n1*wordsize));
        pcb->root0 = 0;
        pcb->root1 = 0;
        mp_limb_t* s1 = (mp_limb_t*)(long)(x+disp_bignum_data-vector_tag);
        mp_limb_t* s2 = (mp_limb_t*)(long)(y+disp_bignum_data-vector_tag);
        mp_limb_t* s = (mp_limb_t*)(long)(r+disp_bignum_data);
        bits_compliment2(s2, s, n2, n1);
        int carry = bits_compliment_logor(s1, s, s, n1);
        bits_compliment_carry(s,s,n1,n1,carry);
        bits_compliment2(s, s, n1, n1);
        return normalize_bignum(n1, 1<<bignum_sign_shift, r);
      } else {
        return ikrt_bnbnlogor(y,x,pcb);
      }
    } else {
      return ikrt_bnbnlogor(y,x,pcb);
    }
  } else {
    if(bnfst_negative(yfst)){
      /* x positive, y negative */
      /*  the result is at most n2 words long */
      pcb->root0 = &x;
      pcb->root1 = &y;
      ikptr r = ik_safe_alloc(pcb, align(disp_bignum_data + n2*wordsize));
      pcb->root0 = 0;
      pcb->root1 = 0;
      mp_limb_t* s1 = (mp_limb_t*)(long)(x+disp_bignum_data-vector_tag);
      mp_limb_t* s2 = (mp_limb_t*)(long)(y+disp_bignum_data-vector_tag);
      mp_limb_t* s = (mp_limb_t*)(long)(r+disp_bignum_data);
      if(n2 <= n1){
        bits_compliment_logor(s2, s1, s, n2);
        bits_compliment2(s, s, n2, n2);
      } else {
        int carry = bits_compliment_logor(s2, s1, s, n1);
        bits_compliment_carry(s2, s, n1, n2, carry);
        bits_compliment_carry(s, s, 0, n2, 1);
      }
      return normalize_bignum(n2, 1<<bignum_sign_shift, r);
    } else {
      /* both positive */
      int n = (n1>n2)?n1:n2;
      pcb->root0 = &x;
      pcb->root1 = &y;
      ikptr r = ik_safe_alloc(pcb, align(disp_bignum_data+n*wordsize));
      mp_limb_t* s = (mp_limb_t*)(long)(r+disp_bignum_data);
      mp_limb_t* s1 = (mp_limb_t*)(long)(x+disp_bignum_data-vector_tag);
      mp_limb_t* s2 = (mp_limb_t*)(long)(y+disp_bignum_data-vector_tag);
      pcb->root0 = 0;
      pcb->root1 = 0;
      long int i;
      if(n == n1){
        for(i=0; i<n2; i++){
          s[i] = s1[i] | s2[i];
        }
        for(i=n2; i<n1; i++){
          s[i] = s1[i];
        }
      } else {
        for(i=0; i<n1; i++){
          s[i] = s1[i] | s2[i];
        }
        for(i=n1; i<n2; i++){
          s[i] = s2[i];
        }
      }
      return normalize_bignum(n, 0, r);
    }
  }
}

static void
copy_bits_shifting_right(mp_limb_t* src, mp_limb_t* dst, int n, int m){
  mp_limb_t carry = src[0] >> m;
  int i;
  for(i=1; i<n; i++){
    mp_limb_t b = src[i];
    dst[i-1] = (b << (mp_bits_per_limb-m)) | carry;
    carry = b >> m;
  }
  dst[n-1] = carry;
}

static void
copy_bits_shifting_left(mp_limb_t* src, mp_limb_t* dst, int n, int m){
  mp_limb_t carry = 0;
  int i;
  for(i=0; i<n; i++){
    mp_limb_t b = src[i];
    dst[i] = (b << m) | carry;
    carry = b >> (mp_bits_per_limb-m);
  }
  dst[n] = carry;
}





ikptr
ikrt_bignum_shift_right(ikptr x, ikptr y, ikpcb* pcb){
  int limb_shift = (wordsize == 4 ? 5 : 6);
  long int m = unfix(y);
  ikptr fst = ref(x, -vector_tag);
  long int n = bnfst_limb_count(fst);
  long int whole_limb_shift = m >> limb_shift;
  long int bit_shift = m & (mp_bits_per_limb-1);
  long int new_limb_count = n - whole_limb_shift;
  if(bnfst_negative(fst)){
    if(new_limb_count <= 0){
      return fix(-1);
    }
    if(bit_shift == 0){
      pcb->root0 = &x;
      ikptr r = ik_safe_alloc(pcb, align(disp_bignum_data + new_limb_count * wordsize));
      pcb->root0 = 0;
      bits_compliment_with_carry(
          (mp_limb_t*)(long)(x+off_bignum_data+whole_limb_shift*wordsize),
          (mp_limb_t*)(long)(r+disp_bignum_data),
          new_limb_count,
          bits_carry((mp_limb_t*)(long)(x+off_bignum_data), whole_limb_shift));
      bits_compliment(
          (mp_limb_t*)(long)(r+disp_bignum_data),
          (mp_limb_t*)(long)(r+disp_bignum_data),
          new_limb_count);
      return normalize_bignum(new_limb_count, 1 << bignum_sign_shift, r);
    } else {
      pcb->root0 = &x;
      ikptr r = ik_safe_alloc(pcb, align(disp_bignum_data + new_limb_count * wordsize));
      pcb->root0 = 0;
      bits_compliment_with_carry(
          (mp_limb_t*)(long)(x+off_bignum_data+whole_limb_shift*wordsize),
          (mp_limb_t*)(long)(r+disp_bignum_data),
          new_limb_count,
          bits_carry((mp_limb_t*)(long)(x+off_bignum_data), whole_limb_shift));
      copy_bits_shifting_right(
          (mp_limb_t*)(long)(r+disp_bignum_data),
          (mp_limb_t*)(long)(r+disp_bignum_data),
          new_limb_count,
          bit_shift);
      *((mp_limb_t*)(r+disp_bignum_data+(new_limb_count-1)*wordsize))
          |= (-1L << (mp_bits_per_limb - bit_shift));
      bits_compliment(
          (mp_limb_t*)(long)(r+disp_bignum_data),
          (mp_limb_t*)(long)(r+disp_bignum_data),
          new_limb_count);
      return normalize_bignum(new_limb_count, 1 << bignum_sign_shift, r);
      fprintf(stderr, "not yet for negative bignum_shift\n");
      exit(-1);
    }
  } else {
    if(new_limb_count <= 0){
      return 0;
    }
    pcb->root0 = &x;
    ikptr r = ik_safe_alloc(pcb, align(disp_bignum_data + new_limb_count * wordsize));
    pcb->root0 = 0;
    if(bit_shift == 0){
      memcpy((char*)(long)r+disp_bignum_data,
             (char*)(long)x+off_bignum_data+whole_limb_shift*wordsize,
             new_limb_count * wordsize);
      return normalize_bignum(new_limb_count, 0, r);
    } else {
      copy_bits_shifting_right(
          (mp_limb_t*)(long)(x+off_bignum_data+whole_limb_shift*wordsize),
          (mp_limb_t*)(long)(r+disp_bignum_data),
          new_limb_count,
          bit_shift);
      return normalize_bignum(new_limb_count, 0, r);
    }
  }
}


ikptr
ikrt_fixnum_shift_left(ikptr x, ikptr y, ikpcb* pcb){
  int limb_shift = (wordsize == 4 ? 5 : 6);
  long int m = unfix(y);
  long int n = unfix(x);
  long int limb_count = (m >> limb_shift) + 2; 
  long int bit_shift = m & (mp_bits_per_limb-1);
  ikptr r = ik_safe_alloc(pcb, align(disp_bignum_data + limb_count * wordsize));
  unsigned long int* s = (unsigned long int*)(long)(r+disp_bignum_data);
  bzero(s, limb_count * wordsize);
  if(n >= 0){
    if(bit_shift){
      s[limb_count-1] = n >> (mp_bits_per_limb - bit_shift);
    }
    s[limb_count-2] = n << bit_shift;
  } else {
    if(bit_shift){
      s[limb_count-1] = (-n) >> (mp_bits_per_limb - bit_shift);
    }
    s[limb_count-2] = (-n) << bit_shift;
  }
  return normalize_bignum(limb_count, (n>=0)?(0):(1<<bignum_sign_shift), r);
}


ikptr
ikrt_bignum_shift_left(ikptr x, ikptr y, ikpcb* pcb){
  int limb_shift = (wordsize == 4 ? 5 : 6);
  long int m = unfix(y);
  ikptr fst = ref(x, -vector_tag);
  long int n = bnfst_limb_count(fst);
  long int whole_limb_shift = m >> limb_shift; 
  long int bit_shift = m & (mp_bits_per_limb-1);
  if(bit_shift == 0){
    long int limb_count = n + whole_limb_shift;
    pcb->root0 = &x;
    ikptr r = ik_safe_alloc(pcb, align(disp_bignum_data + limb_count * wordsize));
    pcb->root0 = 0;
    unsigned int* s = (unsigned int*)(long)(r+disp_bignum_data);
    bzero(s, whole_limb_shift*wordsize);
    memcpy(((char*)s) + whole_limb_shift*wordsize, 
           ((char*)x) + off_bignum_data, 
           n*wordsize);
    return normalize_bignum(limb_count, bnfst_negative(fst), r);
  } else {
    int limb_count = n + whole_limb_shift + 1;
    pcb->root0 = &x;
    ikptr r = ik_safe_alloc(pcb, align(disp_bignum_data + limb_count * wordsize));
    pcb->root0 = 0;
    mp_limb_t* s = (mp_limb_t*)(r+disp_bignum_data);
    bzero(s, whole_limb_shift*wordsize);
    copy_bits_shifting_left(
        (mp_limb_t*)(x+off_bignum_data),
        (mp_limb_t*)(s+whole_limb_shift),
        n,
        bit_shift);
    return normalize_bignum(limb_count, bnfst_negative(fst), r);
  }
}


#if 0
From TFM:
void
mpn_tdiv_qr (
  mp limb t *qp,        /* quotient placed here */
  mp limb t *rp,        /* remainder placed here */
  mp size t qxn,        /* must be zero! */ 
  const mp limb t *np,  /* first number  */
  mp size t nn,         /* its length    */
  const mp limb t *dp,  /* second number */
  mp size t dn          /* its length    */
)

Divide {np, nn} by {dp, dn} and put the quotient at {qp,nn-dn+1}
and the remainder at {rp, dn}. The quotient is rounded towards 0.
No overlap is permitted between arguments. nn must be greater than
or equal to dn. The most significant limb of dp must be non-zero.
The qxn operand must be zero. 
#endif

ikptr
ikrt_bnbndivrem(ikptr x, ikptr y, ikpcb* pcb){
  ikptr xfst = ref(x, -vector_tag);
  ikptr yfst = ref(y, -vector_tag);
  mp_size_t xn = bnfst_limb_count(xfst);
  mp_size_t yn = bnfst_limb_count(yfst);
  if(xn < yn){
    /* quotient is zero, remainder is x */
    pcb->root0 = &x;
    pcb->root1 = &y;
    ikptr rv = ik_safe_alloc(pcb, pair_size);
    pcb->root0 = 0;
    pcb->root1 = 0;
    ref(rv, disp_car) = 0;
    ref(rv, disp_cdr) = x;
    return rv+pair_tag;
  }
  mp_size_t qn = xn - yn + 1;
  mp_size_t rn = yn;
  /*
  ikptr q = ik_unsafe_alloc(pcb, align(disp_bignum_data + qn*wordsize));
  ikptr r = ik_unsafe_alloc(pcb, align(disp_bignum_data + rn*wordsize));
  */
  pcb->root0 = &x;
  pcb->root1 = &y;
  ikptr q = ik_safe_alloc(pcb, 
            align(disp_bignum_data + qn*wordsize) + 
            align(disp_bignum_data + rn*wordsize));
  ikptr r = q + align(disp_bignum_data + qn*wordsize);
  pcb->root0 = 0;
  pcb->root1 = 0;
  mpn_tdiv_qr (
      (mp_limb_t*)(long)(q+disp_bignum_data),
      (mp_limb_t*)(long)(r+disp_bignum_data),
      0,
      (mp_limb_t*)(long)(x+off_bignum_data),
      xn,
      (mp_limb_t*)(long)(y+off_bignum_data),
      yn);

  if(bnfst_negative(xfst)){
    /* x is negative => remainder is negative */
    r = normalize_bignum(rn, 1 << bignum_sign_shift, r);
  } else {
    r = normalize_bignum(rn, 0, r);
  }

  if(bnfst_negative(yfst)){
    /* y is negative => quotient is opposite of x */
    long int sign = bignum_sign_mask - bnfst_negative(xfst);
    q = normalize_bignum(qn, sign, q);
  } else {
    /* y is positive => quotient is same as x */
    long int sign = bnfst_negative(xfst);
    q = normalize_bignum(qn, sign, q);
  }
  pcb->root0 = &q;
  pcb->root1 = &r;
  ikptr rv = ik_safe_alloc(pcb, pair_size);
  pcb->root0 = 0;
  pcb->root1 = 0;
  ref(rv, disp_car) = q;
  ref(rv, disp_cdr) = r;
  return rv+pair_tag;
}


#if 0
[Function]

mp_limb_t
mpn_divrem_1 (
  mp limb t *r1p,
  mp size t qxn, 
  mp limb t *s2p,
  mp size t s2n,
  mp limb t s3limb 
) 

Divide {s2p, s2n} by s3limb, and write the quotient at r1p. Return the remainder. 
The integer quotient is written to {r1p+qxn, s2n} and in addition qxn fraction limbs are 
developed and written to {r1p, qxn}. Either or both s2n and qxn can be zero. For most 
usages, qxn will be zero. 
#endif

ikptr
ikrt_bnfxdivrem(ikptr x, ikptr y, ikpcb* pcb){
  long int yint = unfix(y);
  ikptr fst = ref(x, -vector_tag);
  mp_size_t s2n = bnfst_limb_count(fst);
  pcb->root0 = &x;
  ikptr quot = ik_safe_alloc(pcb, align(s2n*wordsize + disp_bignum_data));
  pcb->root0 = 0;
  mp_limb_t* s2p = (mp_limb_t*)(long)(x+off_bignum_data);
  mp_limb_t rv = mpn_divrem_1(
      (mp_limb_t*)(long)(quot+disp_bignum_data),
      0,
      s2p,
      s2n,
      labs(yint));

  ikptr rem;

  if(yint < 0){
    /* y is negative => quotient is opposite of x */
    long int sign = bignum_sign_mask - bnfst_negative(fst);
    quot = normalize_bignum(s2n, sign, quot);
  } else {
    /* y is positive => quotient is same as x */
    long int sign = bnfst_negative(fst);
    quot = normalize_bignum(s2n, sign, quot);
  }

  /* the remainder is always less than |y|, so it will
     always be a fixnum.  (if y == most_negative_fixnum, 
     then |remainder| will be at most most_positive_fixnum). */
  if(bnfst_negative(fst)){
    /* x is negative => remainder is negative */
    rem = (ikptr) -(rv << fx_shift);
  } else {
    rem = fix(rv);
  }
  pcb->root0 = &quot;
  pcb->root1 = &rem;
  ikptr p = ik_safe_alloc(pcb, pair_size);
  pcb->root0 = 0;
  pcb->root1 = 0;
  ref(p, disp_car) = quot;
  ref(p, disp_cdr) = rem;
  return p+pair_tag;
}

ikptr
ikrt_bnfx_modulo(ikptr x, ikptr y /*, ikpcb* pcb */){
  long int yint = unfix(y);
  mp_limb_t* s2p = (mp_limb_t*)(long)(x+off_bignum_data);
  ikptr fst = ref(x, -vector_tag);
  mp_size_t s2n = bnfst_limb_count(fst);
  if(yint < 0){
    if(bnfst_negative(fst)){
      /* x negative, y negative */
      mp_limb_t m = mpn_mod_1(s2p, s2n, -yint);
      return fix(-m);
    } else {
      /* x positive, y negative */
      mp_limb_t m = mpn_mod_1(s2p, s2n, -yint);
      return fix(yint+m);
    }
  } else {
    if(bnfst_negative(fst)){
      /* x negative, y positive */
      mp_limb_t m = mpn_mod_1(s2p, s2n, yint);
      return fix(yint-m);
    } else {
      /* x positive, y positive */
      mp_limb_t m = mpn_mod_1(s2p, s2n, yint);
      return fix(m);
    }
  }
}


static int
limb_length(unsigned long int n){
  int i=0;
  while(n != 0){
    n = n >> 1;
    i++;
  } 
  return i;
}


ikptr
ikrt_bignum_length(ikptr x){
  ikptr fst = ref(x, -vector_tag);
  mp_limb_t* sp = (mp_limb_t*)(long)(x+off_bignum_data);
  mp_size_t sn = bnfst_limb_count(fst);
  mp_limb_t last = sp[sn-1];
  int n0 = limb_length(last);
  if(((unsigned long int) fst) & bignum_sign_mask){
    /* negative */
    if (last == (mp_limb_t)(1L<<(n0-1))){
      /* single bit set in last limb */
      int i;
      for(i=0; i<(sn-1); i++){
        if(sp[i] != 0){
          /* another bit set */
          return fix((sn-1)*mp_bits_per_limb + n0);
        }
      }
      /* number is - #b100000000000000000000000000 */
      /* fxnot(n) =  #b011111111111111111111111111 */
      /* so, subtract 1. */
      return fix((sn-1)*mp_bits_per_limb + n0 - 1);
    } else {
      return fix((sn-1)*mp_bits_per_limb + n0);
    }
  } else {
    return fix((sn-1)*mp_bits_per_limb + n0);
  }
}


ikptr
ikrt_bignum_to_bytevector(ikptr x, ikpcb* pcb){
  /* FIXME: avoid calling malloc, instead, use the heap pointer itself
   * as a buffer to hold the temporary data after ensuring that it has enough
   * space */
  ikptr fst = ref(x, -vector_tag);
  long int limb_count = bnfst_limb_count(fst);
  if(limb_count <= 0){
    fprintf(stderr, "BUG: nbtostring: invalid length %ld\n", limb_count);
    exit(-1);
  }
  long int sign_bit = bignum_sign_mask & (long int) fst;
  long int nbsize = limb_count * sizeof(mp_limb_t);
  long int strsize = limb_count * max_digits_per_limb;
  long int mem_req = nbsize + strsize + 1;
  unsigned char* mem = malloc(mem_req);
  if(! mem){
    fprintf(stderr, "Error allocating space for bignum\n");
    exit(-1);
  }
  memcpy((char*)(long)mem,
         (char*)(long)x - vector_tag + disp_bignum_data,
         nbsize);
  mp_size_t bytes = 
    mpn_get_str(mem+nbsize,       /* output string */ 
                10,               /* base */
                (mp_limb_t*) mem, /* limb */
                limb_count        /* number of limbs */
        );
  unsigned char* string_start = mem + nbsize;
  while(*string_start == 0){
    string_start++;
    bytes--;
  }
  ikptr bv = ik_safe_alloc(pcb, align(bytes + disp_bytevector_data + (sign_bit?1:0)));
  ref(bv, 0) = fix(bytes + (sign_bit?1:0));
  char* dest = (char*)(long)(bv + disp_bytevector_data);
  if(sign_bit){
    *dest = '-';
    dest++;
  }
  { 
    long int i = 0;
    while(i < bytes){
      dest[i] = string_start[i] + '0';
      i++;
    }
    dest[bytes] = 0;
  }
  free(mem);
  return bv + bytevector_tag;
}


ikptr 
ikrt_fxrandom(ikptr x){
  long int mask = 1; 
  long int n = unfix(x); 
  {
    while(mask < n){
      mask = (mask << 1) | 1;
    }
  }
  while(1){
    long r = rand() & mask;
    if(r < n){
      return fix(r);
    }
  }
}

static int 
limb_size(mp_limb_t x){
  int i = 0;
  while(x){
    i++;
    x = x>>1;
  }
  return i;
}

static int 
all_zeros(mp_limb_t* start, mp_limb_t* end){
  while(start <= end){
    if(*end) return 0; 
    end--;
  }
  return 1;
}

#define PRECISION 53

static ikptr 
ikrt_bignum_to_flonum64(ikptr bn, ikptr more_bits, ikptr fl){
  ikptr fst = ref(bn, -vector_tag);
  long int limb_count = bnfst_limb_count(fst);
  mp_limb_t* sp = (mp_limb_t*)(long)(bn+off_bignum_data);
  double pos_result;
  if(limb_count == 1){
    pos_result = sp[0];
  } else {
    mp_limb_t hi = sp[limb_count-1];
    int bc = limb_size(hi);
    if(bc < 64){
      mp_limb_t mi = sp[limb_count-2];
      hi = (hi << (64-bc)) | (mi >> bc);
    }
    /* now hi has 64 full bits */
    mp_limb_t mask = ((1L<<(64-PRECISION)) - 1);
    if((hi & mask) == ((mask+1)>>1)){
      /* exactly at break point */
      if(((sp[limb_count-2] << (64-bc)) == 0) &&
          all_zeros(sp, sp+limb_count-3) &&
          (more_bits == 0)){
        if(hi & (1L<<(64-PRECISION))){
          /* odd number, round to even */
          hi = hi | mask;
        }
      } else {
        /* round up */
        hi = hi | mask;
      }
    } else if ((hi & mask) > ((mask+1)>>1)){
      /* also round up */
      hi = hi | mask;
    } else {
      /* keep it to round down */
    }
    pos_result = hi;
    int bignum_bits = bc + (mp_bits_per_limb * (limb_count-1));
    int exponent = bignum_bits - mp_bits_per_limb;
    while(exponent){
      pos_result *= 2.0;
      exponent -= 1;
    }
  }
  if(bnfst_negative(fst)){
    flonum_data(fl)  = - pos_result;
  } else {
    flonum_data(fl) = pos_result;
  }
  return fl;
}

ikptr
ikrt_bignum_to_flonum(ikptr bn, ikptr more_bits, ikptr fl){
  if(mp_bits_per_limb == 64){
    return ikrt_bignum_to_flonum64(bn, more_bits, fl);
  }
  ikptr fst = ref(bn, -vector_tag);
  long int limb_count = bnfst_limb_count(fst);
  mp_limb_t* sp = (mp_limb_t*)(long)(bn+off_bignum_data);
  double pos_result;
  if(limb_count == 1){
    pos_result = sp[0];
  } else if (limb_count == 2){
    mp_limb_t lo = sp[0];
    mp_limb_t hi = sp[1];
    pos_result = hi;
    pos_result = pos_result * 4294967296.0;
    pos_result = pos_result + lo;
  } else {
    mp_limb_t hi = sp[limb_count-1];
    mp_limb_t mi = sp[limb_count-2];
    int bc = limb_size(hi);
    if(bc < 32){
      mp_limb_t lo = sp[limb_count-3];
      hi = (hi << (32-bc)) | (mi >> bc);
      mi = (mi << (32-bc)) | (lo >> bc);
    }
    /* now hi has 32 full bits, and mi has 32 full bits */
    mp_limb_t mask = ((1<<(64-PRECISION)) - 1);
    if((mi & mask) == ((mask+1)>>1)){
      /* exactly at break point */
      if(((sp[limb_count-3] << (32-bc)) == 0) &&
          all_zeros(sp, sp+limb_count-4) &&
          (more_bits == 0)){
        if(mi & (1<<(64-PRECISION))){
          /* odd number, round to even */
          mi = mi | mask;
        }
      } else {
        /* round up */
        mi = mi | mask;
      }
    } else if ((mi & mask) > ((mask+1)>>1)){
      /* also round up */
      mi = mi | mask;
    } else {
      /* keep it to round down */
    }
    pos_result = hi;
    pos_result = pos_result * 4294967296.0;
    pos_result = pos_result + mi;
    int bignum_bits = bc + (mp_bits_per_limb * (limb_count-1));
    int exponent = bignum_bits - (2 * mp_bits_per_limb);
    while(exponent){
      pos_result *= 2.0;
      exponent -= 1;
    }
  }
  if(bnfst_negative(fst)){
    flonum_data(fl)  = - pos_result;
  } else {
    flonum_data(fl) = pos_result;
  }
  return fl;
}

ikptr 
ikrt_exact_fixnum_sqrt(ikptr fx /*, ikpcb* pcb*/){
  mp_limb_t x = unfix(fx);
  mp_limb_t s;
  mp_limb_t r;
  mpn_sqrtrem(&s, &r, &x, 1);
  return fix(s);
}

ikptr
ikrt_exact_bignum_sqrt(ikptr bn, ikpcb* pcb){
  ikptr fst = ref(bn, -vector_tag);
  long int limb_count = bnfst_limb_count(fst);
  long int result_limb_count = (limb_count + 1)/2;
  pcb->root0 = &bn;
  ikptr s = ik_safe_alloc(pcb,
            align(disp_bignum_data+result_limb_count*wordsize))
          + vector_tag;
  ref(s, -vector_tag) = 
    (ikptr) (bignum_tag | (result_limb_count << bignum_length_shift));
  pcb->root1 = &s;
  ikptr r = ik_safe_alloc(pcb,
              align(disp_bignum_data+limb_count*wordsize)) 
          + vector_tag;
  ref(r, -vector_tag) = 
    (ikptr) (bignum_tag | (limb_count << bignum_length_shift));
  pcb->root0 = &r;
  ikptr pair = ik_safe_alloc(pcb, pair_size) + pair_tag;
  pcb->root0 = 0;
  pcb->root1 = 0;
  mp_size_t r_actual_limbs = mpn_sqrtrem(
      (mp_limb_t*) (s+off_bignum_data),
      (mp_limb_t*) (r+off_bignum_data),
      (mp_limb_t*) (bn+off_bignum_data),
      limb_count);
  ref(pair, off_car) = normalize_bignum(result_limb_count, 0, s-vector_tag);
  if(r_actual_limbs == 0) {
    /* perfect square */
    ref(pair, off_cdr) = 0;
  } else {
    ref(pair, off_cdr) = normalize_bignum(r_actual_limbs, 0, r-vector_tag);
  }
  return pair;
}


ikptr
ikrt_flonum_hash(ikptr x /*, ikpcb* pcb */) {
  short* buf = (short*)(x+off_flonum_data);
  return fix(((long)buf[0]) ^ 
             ((long)buf[1] << 3) ^
             ((long)buf[3] << 7) ^
             ((long)buf[2] << 11));
}
ikptr
ikrt_bignum_hash(ikptr bn /*, ikpcb* pcb */) {
  ikptr fst = ref(bn, -vector_tag);
  long int limb_count = bnfst_limb_count(fst);
  long h = (long)fst;
  mp_limb_t* dat = (mp_limb_t*)(bn+off_bignum_data);
  long i;
  for (i=0; i<limb_count; i++){
    h = (h^dat[i]) << 3;
  }
  return fix(h);
}