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
// Copyright 2016 Jeremy Mason
//
// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
// http://opensource.org/licenses/MIT>, at your option. This file may not be
// copied, modified, or distributed except according to those terms.

//! `Unimodal` encoding of integer arrays. Intended for cases where information
//! about the probability distribution of the entries is not known, and the
//! presence of outliers reduces the compression ratio of the other types.
//! Implemented for all primitive integer types.
//!
//! This type approaches general probability distributions with outliers by
//! performing several initial transformations to reduce the minimum necessary
//! bit width. Specifically, the median value is subtracted from the entries,
//! the entries are mapped to the unsigned integers by the zig-zag encoding,
//! and the most significant bits of any outliers are removed and stored
//! separately. The result is that the compression effectively depends only on
//! the standard deviation of the probability distribution of the block
//! entries.
//!
//! # Examples
//!
//! ```
//! use mayda::{Access, Encode, Unimodal};
//!
//! let input: Vec<u32> = vec![1, 4, 2, 8, 5, 7];
//! let mut bits = Unimodal::new();
//! bits.encode(&input).unwrap();
//!
//! let length = bits.len();
//! assert_eq!(length, 6);
//!
//! let output = bits.decode();
//! assert_eq!(input, output);
//!
//! let value = bits.access(4);
//! assert_eq!(value, 5);
//!
//! let range = bits.access(1..4);
//! assert_eq!(range, vec![4, 2, 8]); 
//! ```

use std::marker::PhantomData;
use std::{mem, ops, ptr, usize};

use mayda_codec;
use utility::{self, Bits, Encode, Access, AccessInto};

const E_WIDTH: u32 = 0x0000007f;
const E_COUNT: u32 = 0x00007f80;
const X_WIDTH: u32 = 0x003f8000;
const X_COUNT: u32 = 0x1fc00000;
const I_WIDTH: u32 = 0xe0000000;

/// The type of a unimodal encoded integer array. Designed for moderate
/// compression and efficient decoding through the `Encode` trait, and
/// efficient random access through the `Access` and `AccessInto` traits.
///
/// Support is provided for arrays with as many as (2^37 - 2^7) entries, or
/// about 512 GiB of `u32`s. If your application requires more than that, you
/// should probably be designing your own data structure anyway.
///
/// # Examples
///
/// ```
/// use mayda::{Access, Encode, Unimodal};
///
/// let input: Vec<u32> = vec![1, 4, 2, 8, 5, 7];
/// let mut bits = Unimodal::new();
/// bits.encode(&input).unwrap();
///
/// let length = bits.len();
/// assert_eq!(length, 6);
///
/// let output = bits.decode();
/// assert_eq!(input, output);
///
/// let value = bits.access(4);
/// assert_eq!(value, 5);
///
/// let range = bits.access(1..4);
/// assert_eq!(range, vec![4, 2, 8]); 
/// ```
///
/// # Performance
///
/// Decoding does not allocate except for the return value, and decodes around
/// 6 GiB/s of decoded integers on difficult inputs. Encoding allocates `O(n)`
/// memory (`n` in the length of the array), and encodes around 250 MiB/s of
/// decoded integers. Around three-fourths of the encoding runtime is due to
/// the algorithm `utility::select_m` used to find the median of a block. Run
/// `cargo bench --bench unimodal` for performance numbers on your setup.
///
/// The performance (speed and compression) degrades gradually as the number of
/// entries falls below 128.
///
/// # Safety
///
/// As a general rule, you **should not** decode or access `Unimodal` objects
/// from untrusted sources.
///
/// A `Unimodal` object performs unsafe pointer operations during encoding and
/// decoding. Changing the header information with `mut_storage` can cause data
/// to be written to or read from arbitrary addresses in memory.
///
/// That said, the situation is the same for any of the data structures in the
/// standard library (consider the `set_len` method of a `Vec`).
#[derive(Clone, Debug, Default, Hash, PartialEq, PartialOrd)]
pub struct Unimodal<B> {
  storage: Box<[u32]>,
  phantom: PhantomData<B>
}

impl<B: Bits> Unimodal<B> {
  /// Creates an empty `Unimodal` object.
  ///
  /// # Examples
  /// ```
  /// use mayda::{Encode, Unimodal};
  ///
  /// let input: Vec<u32> = vec![1, 4, 2, 8, 5, 7];
  /// let mut bits = Unimodal::new();
  /// bits.encode(&input).unwrap();
  ///
  /// let bytes = std::mem::size_of_val(bits.storage());
  /// assert_eq!(bytes, 16);
  /// ```
  #[inline]
  pub fn new() -> Self {
    Unimodal {
      storage: Box::new([0; 0]),
      phantom: PhantomData
    }
  }

  /// Creates a `Unimodal` object that encodes the slice.
  ///
  /// # Examples
  /// ```
  /// use mayda::{Encode, Unimodal};
  ///
  /// let input: Vec<u32> = vec![1, 5, 7, 15, 20, 27];
  /// let bits = Unimodal::from_slice(&input).unwrap();
  ///
  /// let output = bits.decode();
  /// assert_eq!(input, output);
  /// ```
  #[inline]
  pub fn from_slice(slice: &[B]) -> Result<Self, super::Error> {
    let mut bits = Self::new();
    match bits.encode(slice) {
      Err(error) => Err(error),
      Ok(()) => Ok(bits)
    }
  }

  /// Returns true when there are no encoded entries.
  ///
  /// # Examples
  /// ```
  /// use mayda::Unimodal;
  ///
  /// let mut bits = Unimodal::<u32>::new();
  /// assert_eq!(bits.is_empty(), true);
  /// ```
  #[inline]
  pub fn is_empty(&self) -> bool {
    self.storage.is_empty()
  }

  /// Returns the number of encoded entries. Note that since the length has to
  /// be calculated, `Unimodal::len()` is more expensive than `Slice::len()`.
  ///
  /// # Examples
  /// ```
  /// use mayda::{Encode, Unimodal};
  ///
  /// let input: Vec<u32> = vec![1, 4, 2, 8, 5, 7];
  /// let mut bits = Unimodal::new();
  /// bits.encode(&input).unwrap();
  ///
  /// assert_eq!(bits.len(), 6);
  /// ```
  pub fn len(&self) -> usize {
    if self.storage.is_empty() { return 0 }

    let s_ptr: *const u32 = self.storage.as_ptr();
    let n_blks: usize = unsafe { (*s_ptr >> 2) as usize };

    let ty_wd: u32 = B::width();
    let wrd_to_blk: usize = words_to_block(n_blks, n_blks, ty_wd, s_ptr);
    let left: usize = unsafe {
      ((*s_ptr.offset(wrd_to_blk as isize) & E_COUNT) >> 7) as usize
    };

    (n_blks << 7) + left
  }

  /// Exposes the word storage of the `Unimodal` object.
  ///
  /// # Examples
  /// ```
  /// use mayda::{Encode, Unimodal};
  ///
  /// let input: Vec<u32> = vec![1, 4, 2, 8, 5, 7];
  /// let mut bits = Unimodal::new();
  /// bits.encode(&input).unwrap();
  ///
  /// let storage = bits.storage();
  /// assert_eq!(storage.len(), 4);
  /// ```
  #[inline]
  pub fn storage(&self) -> &[u32] {
    &self.storage
  }

  /// Exposes the mutable word storage of the `Unimodal` object.
  ///
  /// # Safety
  ///
  /// A `Unimodal` object performs unsafe pointer operations during encoding and
  /// decoding. Changing the header information can cause data to be written to
  /// or read from arbitrary addresses in memory. 
  #[inline]
  pub unsafe fn mut_storage(&mut self) -> &mut Box<[u32]> {
    &mut self.storage
  }

  /// Returns the width of the encoded integer type.
  ///
  /// # Examples
  /// ```
  /// use mayda::{Encode, Unimodal};
  ///
  /// let input: Vec<u32> = vec![1, 4, 2, 8, 5, 7];
  /// let mut bits = Unimodal::new();
  /// bits.encode(&input).unwrap();
  ///
  /// assert_eq!(bits.required_width(), 32);
  /// ```
  #[inline]
  pub fn required_width(&self) -> u32 {
    B::width()
  }
}

////////////////////////////////////////////////////////////////////////////////
// HIC SUNT LEONES
////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////
// Implementations of Encode
////////////////////////////////////////////////////////////////////////////////

impl<B: Bits> Encode<B> for Unimodal<B> {
  fn encode(&mut self, input: &[B]) -> Result<(), super::Error> {
    let storage: Vec<u32> = unsafe { try!(Unimodal::<B>::_encode(input)) };
    self.storage = storage.into_boxed_slice();
    Ok(())
  }

  fn decode(&self) -> Vec<B> {
    // Nothing to do
    if self.storage.is_empty() { return Vec::new() }

    let s_ptr: *const u32 = self.storage.as_ptr();
    let n_blks: usize = unsafe { (*s_ptr >> 2) as usize };
    let len_bnd: usize = (n_blks + 1) << 7;
    let mut output: Vec<B> = Vec::with_capacity(len_bnd);

    unsafe {
      output.set_len(len_bnd);
      let length: usize = Unimodal::<B>::_decode(&*self.storage, n_blks, &mut *output);
      output.set_len(length);
    }

    output
  }

  fn decode_into(&self, output: &mut [B]) -> usize {
    if self.storage.is_empty() {
      return 0
    }

    let s_ptr: *const u32 = self.storage.as_ptr();
    let n_blks: usize = unsafe { (*s_ptr >> 2) as usize };
    let len_bnd: usize = n_blks << 7;
    if output.len() <= len_bnd {
      panic!(
        format!("source length is > {} but slice length is {}", len_bnd, output.len())
      );
    }

    unsafe { Unimodal::<B>::_decode(&*self.storage, n_blks, output) }
  }
}

/// The private interface of an `Encode` type. Allows the implementation to
/// be shared for different types.
trait EncodePrivate<B: Bits> {
  /// Encodes a slice.
  unsafe fn _encode(&[B]) -> Result<Vec<u32>, super::Error>;

  /// Decodes a slice.
  unsafe fn _decode(&[u32], usize, &mut [B]) -> usize;

  /// Encodes a block with 128 or fewer elements. Returns pointer to storage.
  unsafe fn _encode_tail(_: *const B, _: *mut u32, usize, u32) -> *mut u32;

  /// Decodes a block with 128 or fewer elements. Returns pointer to storage.
  unsafe fn _decode_tail(_: *const u32, _: *mut B, usize, u32) -> *const u32;
}

/// Default is only to catch unimplemented types. Should not be reachable.
impl<B: Bits> EncodePrivate<B> for Unimodal<B> {
  default unsafe fn _encode(_: &[B]) -> Result<Vec<u32>, super::Error> {
    Err(super::Error::new("Encode not implemented for this type"))
  }

  default unsafe fn _decode(_: &[u32], _: usize, _: &mut [B]) -> usize {
    panic!("Encode not implemented for this type")
  }

  default unsafe fn _encode_tail(_: *const B, _: *mut u32, _: usize, _: u32) -> *mut u32 {
    panic!("Encode not implemented for this type");
  }

  default unsafe fn _decode_tail(_: *const u32, _: *mut B, _: usize, _: u32) -> *const u32 {
    panic!("Encode not implemented for this type");
  }
}

macro_rules! encodable_unsigned {
  ($(($ty: ident: $step: expr,
      $enc: ident, $dec: ident,
      $enc_simd: ident, $dec_simd: ident,
      $enc_zz: ident))*) => ($(
    impl EncodePrivate<$ty> for Unimodal<$ty> {
      unsafe fn _encode(input: &[$ty]) -> Result<Vec<u32>, super::Error> {
        // Nothing to do
        if input.is_empty() { return Ok(Vec::new()) }

        // Allow arrays of 2^37 entries (512 GiB of u32)
        let n_blks: usize = (input.len() - 1) >> 7;
        let n_lvls: usize = n_blks.bits() as usize;
        if n_lvls > 30 {
          return Err(super::Error::new("exceeded internal capacity"))
        }
        let mut e_counts: Vec<usize> = vec![128; n_blks + 1];
        e_counts[n_blks] = input.len() - (n_blks << 7);

        // Internal representation of ty
        let ty_wd: u32 = $ty::width();
        let ty_wrd: usize = utility::words_for_bits(ty_wd);
        let flag: u32 = match ty_wd {
          8 => utility::U8_FLAG,
          16 => utility::U16_FLAG,
          32 => utility::U32_FLAG,
          64 => utility::U64_FLAG,
          _ => unreachable!()
        };

        let mut scratch: Vec<$ty> = input.to_vec();
        let mut shifts: Vec<$ty> = Vec::with_capacity(n_blks + 1);
        let mut c_ptr: *mut $ty = scratch.as_mut_ptr();
        let shift_ptr: *mut $ty = shifts.as_mut_ptr();

        // Construct shifts
        let mut lwr: usize = 0;
        for (a, &e_cnt) in e_counts.iter().enumerate() {
          let slice: &mut [$ty] = &mut scratch[lwr..(lwr + e_cnt)];
          *shift_ptr.offset(a as isize) = utility::select_m(slice, e_cnt >> 1);
          lwr += e_cnt;
        }
        scratch.copy_from_slice(input);

        // Apply shifts
        for (a, &e_cnt) in e_counts.iter().enumerate() {
          mayda_codec::$enc_zz(c_ptr, e_cnt, *shift_ptr.offset(a as isize));
          c_ptr = c_ptr.offset(e_cnt as isize);
        }
        c_ptr = scratch.as_mut_ptr();

        // Quantities used for the headers
        let mut e_widths: Vec<u32> = Vec::with_capacity(n_blks + 1);
        let mut x_widths: Vec<u32> = Vec::with_capacity(n_blks + 1);
        let mut i_widths: Vec<u32> = Vec::with_capacity(n_blks + 1);
        let mut words: Vec<u64> = Vec::with_capacity(n_blks + 1);

        let e_wd_ptr: *mut u32 = e_widths.as_mut_ptr();
        let x_wd_ptr: *mut u32 = x_widths.as_mut_ptr();
        let i_wd_ptr: *mut u32 = i_widths.as_mut_ptr();
        let wrd_ptr: *mut u64 = words.as_mut_ptr();

        // Construct block header information
        let mut bit_dist: Vec<u32> = Vec::with_capacity(ty_wd as usize + 1);
        let mut e_wd_words: Vec<usize> = Vec::with_capacity(ty_wd as usize + 1);

        let bit_ptr: *mut u32 = bit_dist.as_mut_ptr();
        let eww_ptr: *mut usize = e_wd_words.as_mut_ptr();

        bit_dist.set_len(ty_wd as usize + 1);
        for (a, &e_cnt) in e_counts.iter().enumerate() {
          // Find distribution of bits
          ptr::write_bytes(bit_ptr, 0u8, ty_wd as usize + 1);
          for b in 0..(e_cnt as isize) {
            *bit_ptr.offset((*c_ptr.offset(b)).bits() as isize) += 1;
          }
          // Should not fail since block contains at least one entry
          let max_e_wd: u32 = bit_dist.iter()
            .rposition(|&x| x != 0).unwrap() as u32;

          // Find e_wd and x_wd
          let mut acc: u32 = 0;
          for e_wd in 0..(max_e_wd + 1) {
            acc += *bit_ptr.offset(e_wd as isize);
            let x_cnt: u32 = e_cnt as u32 - acc;
            *eww_ptr.offset(e_wd as isize) =
              utility::words_for_bits(e_wd * e_cnt as u32) +
              utility::words_for_bits(acc.bits() * x_cnt) +
              utility::words_for_bits((max_e_wd - e_wd) * x_cnt);
          }
          e_wd_words.set_len(max_e_wd as usize + 1);
          let (e_wd, _) = e_wd_words.iter().enumerate().fold(
            (0, !0), |(a, b), (c, &d)| if d <= b { (c, d) } else { (a, b) }
          );
          let e_wd: u32 = e_wd as u32;
          let x_wd: u32 = max_e_wd - e_wd;

          // Find i_wd and x_cnt. The values of the indices and exceptions are
          // not stored since the required memory is not reasonably bounded.
          let mask: $ty = { if e_wd > 0 { !0 >> (ty_wd - e_wd) } else { 0 } };
          let mut idx_max: usize = 0;
          let mut prev: usize = 0;
          let mut x_cnt: u32 = 0;
          for b in 0..e_cnt {
            if *c_ptr > mask {
              idx_max |= b - prev;
              prev = b;
              x_cnt += 1;
            }
            c_ptr = c_ptr.offset(1);
          }
          let i_wd: u32 = idx_max.bits();

          let wrd: usize =
            utility::words_for_bits(e_wd * e_cnt as u32) +
            utility::words_for_bits(i_wd * x_cnt) +
            utility::words_for_bits(x_wd * x_cnt);

          *e_wd_ptr.offset(a as isize) = e_wd;
          *x_wd_ptr.offset(a as isize) = x_wd;
          *i_wd_ptr.offset(a as isize) = i_wd;
          *wrd_ptr.offset(a as isize) = wrd as u64;
        }
        words.set_len(n_blks + 1);
        c_ptr = scratch.as_mut_ptr();

        // Construct index header
        let mut lvls: Vec<Vec<u64>> = Vec::with_capacity(n_lvls);
        for a in 0..(n_lvls as isize) {
          let length: usize = (n_blks + (1 << a)) >> (a + 1);
          let mut lvl: Vec<u64> = Vec::with_capacity(length);
          for b in (0..(length as isize)).map(|x| x << (a + 1)) {
            let mut acc: u64 = 0;
            for c in 0..(1 << a) {
              acc += *wrd_ptr.offset(b + c);
            }
            lvl.push(acc);
          }
          lvls.push(lvl);
        }

        // Lengths of index header and blocks
        let base_wd: u32 = ty_wd.bits() + 2;
        let mut h_words: usize = 0;
        for (a, x) in lvls.iter().enumerate() {
          let bits: u32 = (base_wd + a as u32) * x.len() as u32;
          h_words += utility::words_for_bits(bits);
        }
        let b_words: usize = (n_blks + 1) * (1 + ty_wrd) +
          words.iter().sum::<u64>() as usize;

        // Construct storage
        let s_len: usize = 1 + h_words + b_words;
        let mut storage: Vec<u32> = Vec::with_capacity(s_len);
        let mut s_ptr: *mut u32 = storage.as_mut_ptr();

        // Write Unimodal header
        *s_ptr =
          (n_blks as u32) << 2 |
          flag;
        s_ptr = s_ptr.offset(1);

        // Write index header
        for (a, lvl) in lvls.iter().enumerate() {
          let l_wd: u32 = base_wd + a as u32;
          let l_blks: usize = (lvl.len() - 1) >> 7;

          let mut l_ptr: *const u64 = lvl.as_ptr();
          for _ in 0..l_blks {
            mayda_codec::ENCODE_SIMD_U64[l_wd as usize](l_ptr, s_ptr);
            l_ptr = l_ptr.offset(128);
            s_ptr = s_ptr.offset((l_wd << 2) as isize);
          }

          let l_left: usize = lvl.len() - (l_blks << 7);
          s_ptr = Unimodal::<u64>::_encode_tail(l_ptr, s_ptr, l_left, l_wd);
        }

        // Write the input
        let mut exceptions: [$ty; 127] = [0; 127];
        let mut indices: [u8; 127] = [0; 127];
        let exc_ptr: *mut $ty = exceptions.as_mut_ptr();
        let idx_ptr: *mut u8 = indices.as_mut_ptr();

        for (a, &e_cnt) in e_counts.iter().enumerate() {
          let e_wd: u32 = *e_wd_ptr.offset(a as isize);
          let x_wd: u32 = *x_wd_ptr.offset(a as isize);
          let i_wd: u32 = *i_wd_ptr.offset(a as isize);

          // Find exceptions and indices
          let mask: $ty = { if e_wd > 0 { !0 >> (ty_wd - e_wd) } else { 0 } };
          let mut prev: isize = 0;
          let mut x_cnt: usize = 0;
          for b in 0..(e_cnt as isize) {
            if *c_ptr.offset(b) > mask {
              *exc_ptr.offset(x_cnt as isize) = *c_ptr.offset(b) >> e_wd;
              *c_ptr.offset(b) &= mask;
              *idx_ptr.offset(x_cnt as isize) = (b - prev) as u8;
              prev = b;
              x_cnt += 1;
            }
          }
          
          // Write block header
          *s_ptr = 
            i_wd << 29 |
            (x_cnt as u32) << 22 |
            x_wd << 15 |
            (e_cnt as u32) << 7 |
            e_wd;
          s_ptr = s_ptr.offset(1);

          // Write the block
          s_ptr = Unimodal::<$ty>::_encode_tail(c_ptr, s_ptr, e_cnt, e_wd);
          c_ptr = c_ptr.offset(e_cnt as isize);
          if x_cnt > 0 {
            s_ptr = Unimodal::<u8>::_encode_tail(idx_ptr, s_ptr, x_cnt, i_wd);
            s_ptr = Unimodal::<$ty>::_encode_tail(exc_ptr, s_ptr, x_cnt, x_wd);
          }

          // Write the shift. Notice that some bits can be left unitialized.
          *(s_ptr as *mut $ty) = *shift_ptr.offset(a as isize);
          s_ptr = s_ptr.offset(ty_wrd as isize);
        }

        // Set storage length AFTER everything is initialized
        storage.set_len(s_len);
        Ok(storage)
      }

      unsafe fn _decode(storage: &[u32], n_blks: usize, output: &mut [$ty]) -> usize {
        // Internal representation of ty
        let ty_wd: u32 = $ty::width();
        let ty_wrd: usize = utility::words_for_bits(ty_wd);

        // Length of index header
        let n_lvls: u32 = n_blks.bits();
        let base_wd: u32  = ty_wd.bits() + 2;
        let mut h_words: usize = 0;
        for a in 0..n_lvls {
          let len: usize = (n_blks + (1 << a)) >> (a + 1);
          h_words += utility::words_for_bits((base_wd + a) * len as u32);
        }

        // Avoid memory initialization, bounds checking, etc.
        let mut s_ptr: *const u32 = storage.as_ptr();
        s_ptr = s_ptr.offset(1 + h_words as isize);
        let mut o_ptr: *mut $ty = output.as_mut_ptr();

        let mut exceptions: [$ty; 127] = [0; 127];
        let mut indices: [u8; 127] = [0; 127];
        let exp_ptr: *mut $ty = exceptions.as_mut_ptr();
        let idx_ptr: *mut u8 = indices.as_mut_ptr();

        // Block size is known for all but the final block
        for _ in 0..n_blks {
          // Find the width of the block
          let e_wd: u32 = *s_ptr & E_WIDTH;
          let x_wd: u32 = (*s_ptr & X_WIDTH) >> 15;
          let x_cnt: usize = ((*s_ptr & X_COUNT) >> 22) as usize;
          let i_wd: u32 = (*s_ptr & I_WIDTH) >> 29;
          s_ptr = s_ptr.offset(1);

          // Decode the block
          mayda_codec::$dec_simd[e_wd as usize](s_ptr, o_ptr);
          s_ptr = s_ptr.offset((e_wd << 2) as isize);
          if x_cnt > 0 {
            s_ptr = Unimodal::<u8>::_decode_tail(s_ptr, idx_ptr, x_cnt, i_wd);
            s_ptr = Unimodal::<$ty>::_decode_tail(s_ptr, exp_ptr, x_cnt, x_wd);
            let mut idx: u8 = 0;
            for a in 0..(x_cnt as isize) {
              idx += *idx_ptr.offset(a);
              *o_ptr.offset(idx as isize) |= *exp_ptr.offset(a) << e_wd;
            }
          }

          let shift: $ty = *(s_ptr as *const $ty);
          s_ptr = s_ptr.offset(ty_wrd as isize);
          for a in 0..128 {
            let val = *o_ptr.offset(a);
            let xor = (0 as $ty).wrapping_sub(val & 1);
            *o_ptr.offset(a) = ((val >> 1) ^ xor).wrapping_add(shift);
          }

          o_ptr = o_ptr.offset(128);
        }

        // Final block
        let e_wd: u32 = *s_ptr & E_WIDTH;
        let left: usize = ((*s_ptr & E_COUNT) >> 7) as usize;
        let x_wd: u32 = (*s_ptr & X_WIDTH) >> 15;
        let x_cnt: usize = ((*s_ptr & X_COUNT) >> 22) as usize;
        let i_wd: u32 = (*s_ptr & I_WIDTH) >> 29;
        s_ptr = s_ptr.offset(1);

        let length: usize = (n_blks << 7) + left;
        if output.len() < length {
          panic!(
            format!("source length is {} but slice length is {}", length, output.len())
          );
        }

        s_ptr = Unimodal::<$ty>::_decode_tail(s_ptr, o_ptr, left, e_wd);
        if x_cnt > 0 {
          s_ptr = Unimodal::<u8>::_decode_tail(s_ptr, idx_ptr, x_cnt, i_wd);
          s_ptr = Unimodal::<$ty>::_decode_tail(s_ptr, exp_ptr, x_cnt, x_wd);
          let mut idx: u8 = 0;
          for a in 0..(x_cnt as isize) {
            idx += *idx_ptr.offset(a);
            *o_ptr.offset(idx as isize) |= *exp_ptr.offset(a) << e_wd;
          }
        }

        let shift: $ty = *(s_ptr as *const $ty);
        for a in 0..(left as isize) {
          let val = *o_ptr.offset(a);
          let xor = (0 as $ty).wrapping_sub(val & 1);
          *o_ptr.offset(a) = ((val >> 1) ^ xor).wrapping_add(shift);
        }

        length
      }

      unsafe fn _encode_tail(mut c_ptr: *const $ty,
                             mut s_ptr: *mut u32,
                             e_cnt: usize,
                             e_wd: u32) -> *mut u32 {
        // Encode a run of 128 integers and return
        if e_cnt == 128 {
          mayda_codec::$enc_simd[e_wd as usize](c_ptr, s_ptr);
          return s_ptr.offset(4 * e_wd as isize)
        }

        // Encode a short run and return
        if e_cnt < $step {
          *s_ptr = 0;
          let mut s_bits: u32 = 32;
          let mut i_bits: u32;
          for a in 0..e_cnt {
            i_bits = e_wd;

            // Encode in the available space
            let lsft: u32 = 32 - s_bits;
            *s_ptr |= (*c_ptr as u32) << lsft;

            // While the available space is not enough...
            while s_bits < i_bits {
              i_bits -= s_bits;
              s_ptr = s_ptr.offset(1);
              *s_ptr = (*c_ptr >> (e_wd - i_bits)) as u32;
              s_bits = 32;
            }
            s_bits -= i_bits;

            if a < e_cnt - 1 {
              c_ptr = c_ptr.offset(1);
              if s_bits == 0 {
                s_ptr = s_ptr.offset(1);
                *s_ptr = 0;
                s_bits = 32;
              }
            }
          }
          return { if s_bits < 32 { s_ptr.offset(1) } else { s_ptr } }
        }

        // Encode any runs of 32 integers
        for _ in 0..(e_cnt >> 5) {
          mayda_codec::$enc[e_wd as usize][32 / $step - 1](c_ptr, s_ptr);
          c_ptr = c_ptr.offset(32);
          s_ptr = s_ptr.offset(e_wd as isize);
        }
        let mut e_cnt: usize = e_cnt & 31;

        // Encode any runs of step integers
        let mut s_bits: u32 = 32;
        if e_cnt >= $step {
          let part: usize = e_cnt - e_cnt % $step;
          let bits: u32 = part as u32 * e_wd;
          mayda_codec::$enc[e_wd as usize][(part / $step - 1) as usize](c_ptr, s_ptr);
          c_ptr = c_ptr.offset(part as isize);
          s_ptr = s_ptr.offset((bits >> 5) as isize);
          s_bits -= bits & 31;
          e_cnt -= part;
        }

        // Encode any leftover integers one by one
        if e_cnt > 0 {
          if s_bits == 32 { *s_ptr = 0; }
          let mut i_bits: u32;
          for a in 0..e_cnt {
            i_bits = e_wd;

            // Encode in the available space
            let lsft: u32 = 32 - s_bits;
            *s_ptr |= (*c_ptr as u32) << lsft;

            // While the available space is not enough...
            while s_bits < i_bits {
              i_bits -= s_bits;
              s_ptr = s_ptr.offset(1);
              *s_ptr = (*c_ptr >> (e_wd - i_bits)) as u32;
              s_bits = 32;
            }
            s_bits -= i_bits;

            if a < e_cnt - 1 {
              c_ptr = c_ptr.offset(1);
              if s_bits == 0 {
                s_ptr = s_ptr.offset(1);
                *s_ptr = 0;
                s_bits = 32;
              }
            }
          }
        }
        if s_bits < 32 { s_ptr.offset(1) } else { s_ptr }
      }

      unsafe fn _decode_tail(mut s_ptr: *const u32,
                             mut o_ptr: *mut $ty,
                             e_cnt: usize,
                             e_wd: u32) -> *const u32 {
        // Decode a run of 128 integers and return
        if e_cnt == 128 {
          mayda_codec::$dec_simd[e_wd as usize](s_ptr, o_ptr);
          return s_ptr.offset(4 * e_wd as isize)
        }

        // Decode a short run and return
        if e_cnt < $step {
          let mut s_bits: u32 = 32;
          let mut o_bits: u32;
          let mask: $ty = {
            if e_wd > 0 { !0 >> ($ty::width() - e_wd) } else { 0 }
          };

          // Decode any remaining integers one by one
          for _ in 0..(e_cnt - 1) {
            o_bits = e_wd;

            // Decode anything in the available space
            let rsft: u32 = 32 - s_bits;
            *o_ptr = (*s_ptr >> rsft) as $ty;

            // While the available space is not enough...
            while o_bits > s_bits {
              o_bits -= s_bits;
              s_ptr = s_ptr.offset(1);
              *o_ptr |= (*s_ptr as $ty) << (e_wd - o_bits);
              s_bits = 32;
            }
            s_bits -= o_bits;

            *o_ptr &= mask;
            o_ptr = o_ptr.offset(1);
            if s_bits == 0 {
              s_ptr = s_ptr.offset(1);
              s_bits = 32;
            }
          }
          // Final iteration moved out of loop to avoid branching
          o_bits = e_wd;

          // Decode anything in the available space
          let rsft: u32 = 32 - s_bits;
          *o_ptr = (*s_ptr >> rsft) as $ty;

          // While the available space is not enough...
          while o_bits > s_bits {
            o_bits -= s_bits;
            s_ptr = s_ptr.offset(1);
            *o_ptr |= (*s_ptr as $ty) << (e_wd - o_bits);
            s_bits = 32;
          }
          s_bits -= o_bits;

          *o_ptr &= mask;
          return { if s_bits < 32 { s_ptr.offset(1) } else { s_ptr } }
        }

        // Decode any runs of 32 integers
        for _ in 0..(e_cnt >> 5) {
          mayda_codec::$dec[e_wd as usize][32 / $step - 1](s_ptr, o_ptr);
          s_ptr = s_ptr.offset(e_wd as isize);
          o_ptr = o_ptr.offset(32);
        }
        let mut e_cnt: usize = e_cnt & 31;

        // Decode any runs of step integers
        let mut s_bits: u32 = 32;
        if e_cnt >= $step {
          let part: usize = e_cnt - e_cnt % $step;
          let bits: u32 = part as u32 * e_wd;
          mayda_codec::$dec[e_wd as usize][(part / $step - 1) as usize](s_ptr, o_ptr);
          s_ptr = s_ptr.offset((bits >> 5) as isize);
          o_ptr = o_ptr.offset(part as isize);
          s_bits -= bits & 31;
          e_cnt -= part;
        }

        // Decode any leftover integers one by one
        if e_cnt > 0 {
          let mut o_bits: u32;
          let mask: $ty = {
            if e_wd > 0 { !0 >> ($ty::width() - e_wd) } else { 0 }
          };

          // Decode any remaining integers one by one
          for _ in 0..(e_cnt - 1) {
            o_bits = e_wd;

            // Decode anything in the available space
            let rsft: u32 = 32 - s_bits;
            *o_ptr = (*s_ptr >> rsft) as $ty;

            // While the available space is not enough...
            while o_bits > s_bits {
              o_bits -= s_bits;
              s_ptr = s_ptr.offset(1);
              *o_ptr |= (*s_ptr as $ty) << (e_wd - o_bits);
              s_bits = 32;
            }
            s_bits -= o_bits;

            *o_ptr &= mask;
            o_ptr = o_ptr.offset(1);
            if s_bits == 0 {
              s_ptr = s_ptr.offset(1);
              s_bits = 32;
            }
          }
          // Final iteration moved out of loop to avoid branching
          o_bits = e_wd;

          // Decode anything in the available space
          let rsft: u32 = 32 - s_bits;
          *o_ptr = (*s_ptr >> rsft) as $ty;

          // While the available space is not enough...
          while o_bits > s_bits {
            o_bits -= s_bits;
            s_ptr = s_ptr.offset(1);
            *o_ptr |= (*s_ptr as $ty) << (e_wd - o_bits);
            s_bits = 32;
          }
          s_bits -= o_bits;

          *o_ptr &= mask;
        }
        if s_bits < 32 { s_ptr.offset(1) } else { s_ptr }
      }
    }
  )*)
}

encodable_unsigned!{
  (u8: 8,
   ENCODE_U8, DECODE_U8,
   ENCODE_SIMD_U8, DECODE_SIMD_U8,
   encode_zz_shift_u8)
  (u16: 8,
   ENCODE_U16, DECODE_U16,
   ENCODE_SIMD_U16, DECODE_SIMD_U16,
   encode_zz_shift_u16)
  (u32: 8,
   ENCODE_U32, DECODE_U32,
   ENCODE_SIMD_U32, DECODE_SIMD_U32,
   encode_zz_shift_u32)
  (u64: 8,
   ENCODE_U64, DECODE_U64,
   ENCODE_SIMD_U64, DECODE_SIMD_U64,
   encode_zz_shift_u64)
}

#[cfg(target_pointer_width = "16")]
impl EncodePrivate<usize> for Unimodal<usize> {
  #[inline]
  unsafe fn _encode(storage: &[usize]) -> Result<Vec<u32>, super::Error> {
    Unimodal::<u16>::_encode(mem::transmute(storage))
  }

  #[inline]
  unsafe fn _decode(storage: &[u32], n_blks: usize, output: &mut [usize]) -> usize {
    Unimodal::<u16>::_decode(storage, n_blks, mem::transmute(output))
  }
}

#[cfg(target_pointer_width = "32")]
impl EncodePrivate<usize> for Unimodal<usize> {
  #[inline]
  unsafe fn _encode(storage: &[usize]) -> Result<Vec<u32>, super::Error> {
    Unimodal::<u32>::_encode(mem::transmute(storage))
  }

  #[inline]
  unsafe fn _decode(storage: &[u32], n_blks: usize, output: &mut [usize]) -> usize {
    Unimodal::<u32>::_decode(storage, n_blks, mem::transmute(output))
  }
}

#[cfg(target_pointer_width = "64")]
impl EncodePrivate<usize> for Unimodal<usize> {
  #[inline]
  unsafe fn _encode(storage: &[usize]) -> Result<Vec<u32>, super::Error> {
    Unimodal::<u64>::_encode(mem::transmute(storage))
  }

  #[inline]
  unsafe fn _decode(storage: &[u32], n_blks: usize, output: &mut [usize]) -> usize {
    Unimodal::<u64>::_decode(storage, n_blks, mem::transmute(output))
  }
}

macro_rules! encodable_signed {
  ($(($it: ident: $ut: ident, $enc_zz: ident))*) => ($(
    impl EncodePrivate<$it> for Unimodal<$it> {
      unsafe fn _encode(input: &[$it]) -> Result<Vec<u32>, super::Error> {
        // Nothing to do
        if input.is_empty() { return Ok(Vec::new()) }

        // Allow arrays of 2^37 entries (128 GB of u8)
        let n_blks: usize = (input.len() - 1) >> 7;
        let n_lvls: usize = n_blks.bits() as usize;
        if n_lvls > 30 {
          return Err(super::Error::new("exceeded internal capacity"))
        }
        let mut e_counts: Vec<usize> = vec![128; n_blks + 1];
        e_counts[n_blks] = input.len() - (n_blks << 7);

        // Internal representation of ty
        let ty_wd: u32 = $ut::width();
        let ty_wrd: usize = utility::words_for_bits(ty_wd);
        let flag: u32 = match ty_wd {
          8 => utility::U8_FLAG,
          16 => utility::U16_FLAG,
          32 => utility::U32_FLAG,
          64 => utility::U64_FLAG,
          _ => unreachable!()
        };

        let mut scratch: Vec<$it> = input.to_vec();
        let mut shifts: Vec<$it> = Vec::with_capacity(n_blks + 1);
        let shift_ptr: *mut $it = shifts.as_mut_ptr();

        // Construct shifts
        let mut lwr: usize = 0;
        for (a, &e_cnt) in e_counts.iter().enumerate() {
          let slice: &mut [$it] = &mut scratch[lwr..(lwr + e_cnt)];
          *shift_ptr.offset(a as isize) = utility::select_m(slice, e_cnt >> 1);
          lwr += e_cnt;
        }
        scratch.copy_from_slice(input);

        // Transmute to unsigned. This and the use of the signed type for the 
        // calculation of the shifts is the only difference with the unsigned
        // implementation. Hope to eventually share the following parts.
        let mut scratch: Vec<$ut> = mem::transmute(scratch);
        let shift_ptr: *mut $ut = shift_ptr as *mut $ut;
        
        // Apply shifts
        let mut c_ptr: *mut $ut = scratch.as_mut_ptr();
        for (a, &e_cnt) in e_counts.iter().enumerate() {
          mayda_codec::$enc_zz(c_ptr, e_cnt, *shift_ptr.offset(a as isize));
          c_ptr = c_ptr.offset(e_cnt as isize);
        }
        c_ptr = scratch.as_mut_ptr();

        // Quantities used for the headers
        let mut e_widths: Vec<u32> = Vec::with_capacity(n_blks + 1);
        let mut x_widths: Vec<u32> = Vec::with_capacity(n_blks + 1);
        let mut i_widths: Vec<u32> = Vec::with_capacity(n_blks + 1);
        let mut words: Vec<u64> = Vec::with_capacity(n_blks + 1);

        let e_wd_ptr: *mut u32 = e_widths.as_mut_ptr();
        let x_wd_ptr: *mut u32 = x_widths.as_mut_ptr();
        let i_wd_ptr: *mut u32 = i_widths.as_mut_ptr();
        let wrd_ptr: *mut u64 = words.as_mut_ptr();

        // Construct block header information
        let mut bit_dist: Vec<u32> = Vec::with_capacity(ty_wd as usize + 1);
        let mut e_wd_words: Vec<usize> = Vec::with_capacity(ty_wd as usize + 1);

        let bit_ptr: *mut u32 = bit_dist.as_mut_ptr();
        let eww_ptr: *mut usize = e_wd_words.as_mut_ptr();

        bit_dist.set_len(ty_wd as usize + 1);
        for (a, &e_cnt) in e_counts.iter().enumerate() {
          // Find distribution of bits
          ptr::write_bytes(bit_ptr, 0u8, ty_wd as usize + 1);
          for b in 0..(e_cnt as isize) {
            *bit_ptr.offset((*c_ptr.offset(b)).bits() as isize) += 1;
          }
          // Should not fail since block contains at least one entry
          let max_e_wd: u32 = bit_dist.iter()
            .rposition(|x| *x != 0)
            .unwrap() as u32;

          // Find e_wd and x_wd
          let mut acc: u32 = 0;
          for e_wd in 0..(max_e_wd + 1) {
            acc += *bit_ptr.offset(e_wd as isize);
            let x_cnt: u32 = e_cnt as u32 - acc;
            *eww_ptr.offset(e_wd as isize) =
              utility::words_for_bits(e_wd * e_cnt as u32) +
              utility::words_for_bits(acc.bits() * x_cnt) +
              utility::words_for_bits((max_e_wd - e_wd) * x_cnt);
          }
          e_wd_words.set_len(max_e_wd as usize + 1);
          let (e_wd, _) = e_wd_words.iter().enumerate().fold(
            (0, !0), |(a, b), (c, &d)| if d <= b { (c, d) } else { (a, b) }
          );
          let e_wd: u32 = e_wd as u32;
          let x_wd: u32 = max_e_wd - e_wd;

          // Find i_wd and x_cnt. The values of the indices and exceptions are
          // not stored since the required memory is not reasonably bounded.
          let mask: $ut = { if e_wd > 0 { !0 >> (ty_wd - e_wd) } else { 0 } };

          let mut idx_max: usize = 0;
          let mut prev: usize = 0;
          let mut x_cnt: u32 = 0;
          for b in 0..e_cnt {
            if *c_ptr > mask {
              idx_max |= b - prev;
              prev = b;
              x_cnt += 1;
            }
            c_ptr = c_ptr.offset(1);
          }
          let i_wd: u32 = idx_max.bits();

          let wrd: u64 = (
            utility::words_for_bits(e_wd * e_cnt as u32) +
            utility::words_for_bits(i_wd * x_cnt) +
            utility::words_for_bits(x_wd * x_cnt)) as u64;

          *e_wd_ptr.offset(a as isize) = e_wd;
          *x_wd_ptr.offset(a as isize) = x_wd;
          *i_wd_ptr.offset(a as isize) = i_wd;
          *wrd_ptr.offset(a as isize) = wrd as u64;
        }
        words.set_len(n_blks + 1);
        c_ptr = scratch.as_mut_ptr();

        // Construct index header
        let mut lvls: Vec<Vec<u64>> = Vec::with_capacity(n_lvls);
        for a in 0..(n_lvls as isize) {
          let length: usize = (n_blks + (1 << a)) >> (a + 1);
          let mut lvl: Vec<u64> = Vec::with_capacity(length);
          for b in (0..(length as isize)).map(|x| x << (a + 1)) {
            let mut acc: u64 = 0;
            for c in 0..(1 << a) {
              acc += *wrd_ptr.offset(b + c);
            }
            lvl.push(acc);
          }
          lvls.push(lvl);
        }

        // Lengths of index header and blocks
        let base_wd: u32 = ty_wd.bits() + 2;
        let mut h_words: usize = 0;
        for (a, x) in lvls.iter().enumerate() {
          let bits: u32 = (base_wd + a as u32) * x.len() as u32;
          h_words += utility::words_for_bits(bits);
        }
        let b_words: usize = words.iter().sum::<u64>() as usize +
          (n_blks + 1) * (1 + utility::words_for_bits(ty_wd));

        // Construct storage
        let s_len: usize = 1 + h_words + b_words;
        let mut storage: Vec<u32> = Vec::with_capacity(s_len);
        let mut s_ptr: *mut u32 = storage.as_mut_ptr();

        // Write Unimodal header
        *s_ptr =
          (n_blks as u32) << 2 |
          flag;
        s_ptr = s_ptr.offset(1);

        // Write index header
        for (a, lvl) in lvls.iter().enumerate() {
          let l_wd: u32 = base_wd + a as u32;
          let l_blks: usize = (lvl.len() - 1) >> 7;

          let mut l_ptr: *const u64 = lvl.as_ptr();
          for _ in 0..l_blks {
            mayda_codec::ENCODE_SIMD_U64[l_wd as usize](l_ptr, s_ptr);
            l_ptr = l_ptr.offset(128);
            s_ptr = s_ptr.offset((l_wd << 2) as isize);
          }

          let l_left: usize = lvl.len() - (l_blks << 7);
          s_ptr = Unimodal::<u64>::_encode_tail(l_ptr, s_ptr, l_left, l_wd);
        }

        // Write the input
        let mut exceptions: [$ut; 127] = [0; 127];
        let mut indices: [u8; 127] = [0; 127];
        let exc_ptr: *mut $ut = exceptions.as_mut_ptr();
        let idx_ptr: *mut u8 = indices.as_mut_ptr();

        for (a, &e_cnt) in e_counts.iter().enumerate() {
          let e_wd: u32 = *e_wd_ptr.offset(a as isize);
          let x_wd: u32 = *x_wd_ptr.offset(a as isize);
          let i_wd: u32 = *i_wd_ptr.offset(a as isize);

          // Find exceptions and indices
          let mask: $ut = { if e_wd > 0 { !0 >> (ty_wd - e_wd) } else { 0 } };
          let mut prev: isize = 0;
          let mut x_cnt: usize = 0;
          for b in 0..(e_cnt as isize) {
            if *c_ptr.offset(b) > mask {
              *exc_ptr.offset(x_cnt as isize) = *c_ptr.offset(b) >> e_wd;
              *c_ptr.offset(b) &= mask;
              *idx_ptr.offset(x_cnt as isize) = (b - prev) as u8;
              prev = b;
              x_cnt += 1;
            }
          }
          
          // Write block header
          *s_ptr = 
            i_wd << 29 |
            (x_cnt as u32) << 22 |
            x_wd << 15 |
            (e_cnt as u32) << 7 |
            e_wd;
          s_ptr = s_ptr.offset(1);

          // Write the block
          s_ptr = Unimodal::<$ut>::_encode_tail(c_ptr, s_ptr, e_cnt, e_wd);
          c_ptr = c_ptr.offset(128);
          if x_cnt > 0 {
            s_ptr = Unimodal::<u8>::_encode_tail(idx_ptr, s_ptr, x_cnt, i_wd);
            s_ptr = Unimodal::<$ut>::_encode_tail(exc_ptr, s_ptr, x_cnt, x_wd);
          }

          // Write the shift. Notice that some bits can be left unitialized.
          *(s_ptr as *mut $ut) = *shift_ptr.offset(a as isize);
          s_ptr = s_ptr.offset(ty_wrd as isize);
        }

        // Set storage length AFTER everything is initialized
        storage.set_len(s_len);
        Ok(storage)
      }

      #[inline]
      unsafe fn _decode(storage: &[u32], n_blks: usize, output: &mut [$it]) -> usize {
        Unimodal::<$ut>::_decode(storage, n_blks, mem::transmute(output))
      }
    }
  )*)
}

encodable_signed!{
  (i8: u8, encode_zz_shift_u8)
  (i16: u16, encode_zz_shift_u16)
  (i32: u32, encode_zz_shift_u32)
  (i64: u64, encode_zz_shift_u64)
}

#[cfg(target_pointer_width = "16")]
impl EncodePrivate<isize> for Unimodal<isize> {
  #[inline]
  unsafe fn _encode(storage: &[isize]) -> Result<Vec<u32>, super::Error> {
    Unimodal::<i16>::_encode(mem::transmute(storage))
  }

  #[inline]
  unsafe fn _decode(storage: &[u32], n_blks: usize, output: &mut [isize]) -> usize {
    Unimodal::<u16>::_decode(storage, n_blks, left, mem::transmute(output))
  }
}

#[cfg(target_pointer_width = "32")]
impl EncodePrivate<isize> for Unimodal<isize> {
  #[inline]
  unsafe fn _encode(storage: &[isize]) -> Result<Vec<u32>, super::Error> {
    Unimodal::<i32>::_encode(mem::transmute(storage))
  }

  #[inline]
  unsafe fn _decode(storage: &[u32], n_blks: usize, output: &mut [isize]) -> usize {
    Unimodal::<u32>::_decode(storage, n_blks, left, mem::transmute(output))
  }
}

#[cfg(target_pointer_width = "64")]
impl EncodePrivate<isize> for Unimodal<isize> {
  #[inline]
  unsafe fn _encode(storage: &[isize]) -> Result<Vec<u32>, super::Error> {
    Unimodal::<i64>::_encode(mem::transmute(storage))
  }

  #[inline]
  unsafe fn _decode(storage: &[u32], n_blks: usize, output: &mut [isize]) -> usize {
    Unimodal::<u64>::_decode(storage, n_blks, mem::transmute(output))
  }
}

////////////////////////////////////////////////////////////////////////////////
// Implementations of Access
////////////////////////////////////////////////////////////////////////////////

// Private traits

trait AccessOne<B: Bits> {
  /// The method for the range access operation.
  unsafe fn _access_one(&[u32], usize) -> B;
}

trait AccessMany<B: Bits, Range> {
  /// The method for the range access operation.
  unsafe fn _access_many(&[u32], usize, Range, &mut [B]) -> usize;
}

// Defaults

impl<B: Bits> AccessOne<B> for Unimodal<B> {
  default unsafe fn _access_one(_: &[u32], _: usize) -> B {
    panic!("Access not implemented for this type");
  }
}

impl<B: Bits> AccessMany<B, ops::Range<usize>> for Unimodal<B> {
  default unsafe fn _access_many(_: &[u32],
                                 _: usize,
                                 _: ops::Range<usize>,
                                 _: &mut [B]) -> usize {
    panic!("Access not implemented for this type");
  }
}

impl<B: Bits> AccessMany<B, ops::RangeFrom<usize>> for Unimodal<B> {
  default unsafe fn _access_many(_: &[u32],
                                 _: usize,
                                 _: ops::RangeFrom<usize>,
                                 _: &mut [B]) -> usize {
    panic!("Access not implemented for this type");
  }
}

// External interface

impl<B: Bits> Access<usize> for Unimodal<B> {
  type Output = B;

  #[inline]
  fn access(&self, index: usize) -> B {
    unsafe { Unimodal::<B>::_access_one(&*self.storage, index) }
  }
}

impl<B: Bits> Access<ops::Range<usize>> for Unimodal<B> {
  type Output = Vec<B>;

  fn access(&self, range: ops::Range<usize>) -> Vec<B> {
    if range.end < range.start {
      panic!(format!("range start is {} but range end is {}", range.start, range.end))
    }
    if self.storage.is_empty() {
      if range.start > 0 {
        panic!(format!("range start is {} but length is 0", range.start))
      }
      if range.end > 0 {
        panic!(format!("range end is {} but length is 0", range.end))
      }
      return Vec::new();
    }

    let s_ptr: *const u32 = self.storage.as_ptr();
    let n_blks: usize = unsafe { (*s_ptr >> 2) as usize };
    let o_length: usize = range.end - range.start;
    let mut output: Vec<B> = Vec::with_capacity(o_length);

    unsafe {
      output.set_len(o_length);
      Unimodal::<B>::_access_many(&*self.storage, n_blks, range, &mut *output);
    }

    output
  }
}

impl<B: Bits> Access<ops::RangeFrom<usize>> for Unimodal<B> {
  type Output = Vec<B>;

  fn access(&self, range: ops::RangeFrom<usize>) -> Vec<B> {
    if self.storage.is_empty() {
      if range.start > 0 {
        panic!(format!("range start is {} but length is 0", range.start))
      }
      return Vec::new();
    }

    let s_ptr: *const u32 = self.storage.as_ptr();
    let n_blks: usize = unsafe { (*s_ptr >> 2) as usize };
    let len_bnd: usize = (n_blks + 1) << 7;
    let mut output: Vec<B> = Vec::with_capacity(len_bnd);

    unsafe {
      output.set_len(len_bnd);
      let length: usize = Unimodal::<B>::_access_many(
        &*self.storage, n_blks, range, &mut *output
      );
      output.set_len(length);
    }

    output
  }
}

impl<B: Bits> Access<ops::RangeTo<usize>> for Unimodal<B> {
  type Output = Vec<B>;

  #[inline]
  fn access(&self, range: ops::RangeTo<usize>) -> Vec<B> {
    self.access(0..range.end)
  }
}

impl<B: Bits> Access<ops::RangeFull> for Unimodal<B> {
  type Output = Vec<B>;

  #[inline]
  fn access(&self, _: ops::RangeFull) -> Vec<B> {
    self.decode()
  }
}

impl<B: Bits> Access<ops::RangeInclusive<usize>> for Unimodal<B> {
  type Output = Vec<B>;

  #[inline]
  fn access(&self, range: ops::RangeInclusive<usize>) -> Vec<B> {
    match range {
      ops::RangeInclusive::Empty { .. } => Vec::new(),
      ops::RangeInclusive::NonEmpty { end, .. } if end == usize::MAX =>
        panic!("attempted to index slice up to maximum usize"),
      ops::RangeInclusive::NonEmpty { start, end } =>
        self.access(start..(end + 1))
    }
  }
}

impl<B: Bits> Access<ops::RangeToInclusive<usize>> for Unimodal<B> {
  type Output = Vec<B>;

  #[inline]
  fn access(&self, range: ops::RangeToInclusive<usize>) -> Vec<B> {
    self.access(0...range.end)
  }
}

impl<B: Bits> AccessInto<ops::Range<usize>, B> for Unimodal<B> {
  fn access_into(&self, range: ops::Range<usize>, output: &mut [B]) -> usize {
    if range.end < range.start {
      panic!(format!("range start is {} but range end is {}", range.start, range.end))
    }
    if self.storage.is_empty() {
      if range.start > 0 {
        panic!(format!("range start is {} but length is 0", range.start))
      }
      if range.end > 0 {
        panic!(format!("range end is {} but length is 0", range.end))
      }
      return 0;
    }

    let s_ptr: *const u32 = self.storage.as_ptr();
    unsafe {
      let n_blks: usize = (*s_ptr >> 2) as usize;
      Unimodal::<B>::_access_many(&*self.storage, n_blks, range, output)
    }
  }
}

impl<B: Bits> AccessInto<ops::RangeFrom<usize>, B> for Unimodal<B> {
  fn access_into(&self, range: ops::RangeFrom<usize>, output: &mut [B]) -> usize {
    if self.storage.is_empty() {
      if range.start > 0 {
        panic!(format!("range start is {} but length is 0", range.start))
      }
      return 0;
    }

    let s_ptr: *const u32 = self.storage.as_ptr();
    unsafe {
      let n_blks: usize = (*s_ptr >> 2) as usize;
      Unimodal::<B>::_access_many(&*self.storage, n_blks, range, &mut *output)
    }
  }
}

impl<B: Bits> AccessInto<ops::RangeTo<usize>, B> for Unimodal<B> {
  #[inline]
  fn access_into(&self, range: ops::RangeTo<usize>, output: &mut [B]) -> usize {
    self.access_into(0..range.end, output)
  }
}

impl<B: Bits> AccessInto<ops::RangeFull, B> for Unimodal<B> {
  #[inline]
  fn access_into(&self, _: ops::RangeFull, output: &mut [B]) -> usize {
    self.decode_into(output)
  }
}

impl<B: Bits> AccessInto<ops::RangeInclusive<usize>, B> for Unimodal<B> {
  #[inline]
  fn access_into(&self, range: ops::RangeInclusive<usize>, output: &mut [B]) -> usize {
    match range {
      ops::RangeInclusive::Empty { .. } => 0,
      ops::RangeInclusive::NonEmpty { end, .. } if end == usize::MAX =>
        panic!("attempted to index slice up to maximum usize"),
      ops::RangeInclusive::NonEmpty { start, end } =>
        self.access_into(start..(end + 1), output)
    }
  }
}

impl<B: Bits> AccessInto<ops::RangeToInclusive<usize>, B> for Unimodal<B> {
  #[inline]
  fn access_into(&self, range: ops::RangeToInclusive<usize>, output: &mut [B]) -> usize {
    self.access_into(0...range.end, output)
  }
}

// Actual implementations

/// Calculates the offset in words to the start of the block. Not intended to
/// be used outside the implementation of `Access`.
fn words_to_block(n_blks: usize, blk: usize, ty_wd: u32, s_head: *const u32) -> usize {
  let ty_wrd: usize = utility::words_for_bits(ty_wd);

  let base_wd: u32 = ty_wd.bits() + 2;
  let mut lvl: u32 = 0;
  let mut lvl_head: usize = 1;
  let mut wrd_to_blk: usize = 0;
  let mut s_ptr: *const u32;

  if blk > 0 {
    let mut w_idx: usize = blk;
    let mut output: u64;

    // Initial iteration moved out of loop to avoid branching
    let shift: u32 = w_idx.trailing_zeros() + 1;
    for _ in 1..shift {
      let l_wd: u32 = base_wd + lvl;
      let len: usize = (n_blks + (1 << lvl)) >> (lvl + 1);
      lvl_head += utility::words_for_bits(l_wd * len as u32);
      lvl += 1;
    }
    w_idx >>= shift;

    let l_wd: u32 = base_wd + lvl;
    let len: usize = (n_blks + (1 << lvl)) >> (lvl + 1);
    unsafe {
      s_ptr = s_head.offset(lvl_head as isize);
      if (w_idx & !127) + 128 <= len {
        // Width encoded using SIMD
        let l_bits: u32 = (w_idx as u32 >> 1) * l_wd;
        let w_bits: u32 = 64 - (l_bits & 63);

        let mut w_ptr: *const u64 = s_ptr as *const u64;
        let w_sft: u32 = ((l_bits >> 5) & !1) | (w_idx as u32 & 1); 
        w_ptr = w_ptr.offset(w_sft as isize);

        output = *w_ptr >> (l_bits & 63);
        if l_wd > w_bits {
          output |= *w_ptr.offset(2) << w_bits;
        }
      } else {
        // Width encoded using u32
        let l_bits: u32 = w_idx as u32 * l_wd;
        let mut s_bits: u32 = 32 - (l_bits & 31);
        let mut o_bits: u32 = l_wd;

        s_ptr = s_ptr.offset((l_bits >> 5) as isize);

        output = (*s_ptr >> (l_bits & 31)) as u64;
        while o_bits > s_bits {
          o_bits -= s_bits;
          s_ptr = s_ptr.offset(1);
          output |= (*s_ptr as u64) << (l_wd - o_bits);
          s_bits = 32;
        }
      }
    }
    wrd_to_blk += (output & (!0 >> (64 - l_wd))) as usize;

    // Decode widths for all other levels
    for _ in 0..w_idx.count_ones() {
      let shift: u32 = w_idx.trailing_zeros() + 1;
      for _ in 0..shift {
        let l_wd: u32 = base_wd + lvl;
        let len: usize = (n_blks + (1 << lvl)) >> (lvl + 1);
        lvl_head += utility::words_for_bits(l_wd * len as u32);
        lvl += 1;
      }
      w_idx >>= shift;

      let l_wd: u32 = base_wd + lvl;
      let len: usize = (n_blks + (1 << lvl)) >> (lvl + 1);
      unsafe {
        s_ptr = s_head.offset(lvl_head as isize);
        if (w_idx & !127) + 128 <= len {
          // Width encoded using SIMD
          let l_bits: u32 = (w_idx as u32 >> 1) * l_wd;
          let w_bits: u32 = 64 - (l_bits & 63);

          let mut w_ptr: *const u64 = s_ptr as *const u64;
          let w_sft: u32 = ((l_bits >> 5) & !1) | (w_idx as u32 & 1); 
          w_ptr = w_ptr.offset(w_sft as isize);

          output = *w_ptr >> (l_bits & 63);
          if l_wd > w_bits {
            output |= *w_ptr.offset(2) << w_bits;
          }
        } else {
          // Width encoded using u32
          let l_bits: u32 = w_idx as u32 * l_wd;
          let mut s_bits: u32 = 32 - (l_bits & 31);
          let mut o_bits: u32 = l_wd;

          s_ptr = s_ptr.offset((l_bits >> 5) as isize);

          output = (*s_ptr >> (l_bits & 31)) as u64;
          while o_bits > s_bits {
            o_bits -= s_bits;
            s_ptr = s_ptr.offset(1);
            output |= (*s_ptr as u64) << (l_wd - o_bits);
            s_bits = 32;
          }
        }
      }
      wrd_to_blk += (output & (!0 >> (64 - l_wd))) as usize;
    }
    wrd_to_blk += blk * (1 + ty_wrd);
  }

  // Include the header words
  wrd_to_blk += lvl_head;
  for a in lvl..n_blks.bits() {
    let l_wd: u32 = base_wd + a;
    let len: usize = (n_blks + (1 << a)) >> (a + 1);
    wrd_to_blk += utility::words_for_bits(l_wd * len as u32);
  }

  wrd_to_blk
}

macro_rules! access_unsigned {
  ($(($ty: ident: $step: expr, $dec: ident, $dec_simd: ident))*) => ($(
    impl AccessOne<$ty> for Unimodal<$ty> {
      unsafe fn _access_one(storage: &[u32], index: usize) -> $ty {
        if storage.is_empty() {
          panic!(format!("index is {} but length is 0", index))
        }

        let mut s_ptr: *const u32 = storage.as_ptr();
        let n_blks: usize = (*s_ptr >> 2) as usize;
        let blk: usize = index >> 7;
        if blk > n_blks {
          let len_bnd: usize = (n_blks + 1) << 7;
          panic!(format!("index is {} but length < {}", index, len_bnd))
        }

        // Find the block containing the range start
        let ty_wd: u32 = $ty::width();
        let wrd_to_blk: usize = words_to_block(n_blks, blk, ty_wd, s_ptr);

        // Block found, decode the value
        s_ptr = s_ptr.offset(wrd_to_blk as isize);

        let e_wd: u32 = *s_ptr & E_WIDTH;
        let left: usize = ((*s_ptr & E_COUNT) >> 7) as usize;
        let x_wd: u32 = (*s_ptr & X_WIDTH) >> 15;
        let x_cnt: usize = ((*s_ptr & X_COUNT) >> 22) as usize;
        let i_wd: u32 = (*s_ptr & I_WIDTH) >> 29;
        s_ptr = s_ptr.offset(1);

        let idx: u8 = (index & 127) as u8;
        if idx as usize >= left {
          let len: usize = index - idx as usize + left;
          panic!(format!("index is {} but length is {}", index, len))
        }

        let mut output: $ty;
        if left == 128 {
          // Value encoded using SIMD
          let lanes: u32 = 128 / ty_wd;
          let l_bits: u32 = (idx as u32 / lanes) * e_wd;
          let mut w_bits: u32 = ty_wd - l_bits % ty_wd;
          let mut o_bits: u32 = e_wd;

          let mut w_ptr: *const $ty = s_ptr as *const $ty;
          w_ptr = w_ptr.offset((idx as u32 % lanes + (l_bits / ty_wd) * lanes) as isize);

          output = *w_ptr >> (ty_wd - w_bits);
          while o_bits > w_bits {
            o_bits -= w_bits;
            w_ptr = w_ptr.offset(lanes as isize);
            output |= *w_ptr << (e_wd - o_bits);
            w_bits = ty_wd;
          }

          s_ptr = s_ptr.offset((e_wd << 2) as isize);
        } else {
          // Value encoded using u32
          let l_bits: u32 = idx as u32 * e_wd;
          let mut s_bits: u32 = 32 - (l_bits & 31);
          let mut o_bits: u32 = e_wd;

          let mut v_ptr: *const u32 = s_ptr.offset((l_bits >> 5) as isize);

          output = (*v_ptr >> (32 - s_bits)) as $ty;
          while o_bits > s_bits {
            o_bits -= s_bits;
            v_ptr = v_ptr.offset(1);
            output |= (*v_ptr as $ty) << (e_wd - o_bits);
            s_bits = 32;
          }

          s_ptr = s_ptr.offset(utility::words_for_bits(e_wd * left as u32) as isize);
        }
        output = { if e_wd > 0 { output & !0 >> (ty_wd - e_wd) } else { 0 } };

        if x_cnt > 0 {
          let mut indices: Vec<u8> = Vec::with_capacity(x_cnt);
          let idx_ptr: *mut u8 = indices.as_mut_ptr();
          s_ptr = Unimodal::<u8>::_decode_tail(s_ptr, idx_ptr, x_cnt, i_wd);

          let mut acc: u8 = 0;
          for a in 0..(x_cnt as isize) {
            acc += *idx_ptr.offset(a);
            if acc > idx { break }
            if acc == idx {
              let mut exception: $ty;

              let x_bits: u32 = a as u32 * x_wd;
              let mut s_bits: u32 = 32 - (x_bits & 31);
              let mut o_bits: u32 = x_wd;

              let mut x_ptr: *const u32 = s_ptr.offset((x_bits >> 5) as isize);
              exception = (*x_ptr >> (32 - s_bits)) as $ty;
              while o_bits > s_bits {
                o_bits -= s_bits;
                x_ptr = x_ptr.offset(1);
                exception |= (*x_ptr as $ty) << (x_wd - o_bits);
                s_bits = 32;
              }
              exception &= !0 >> (ty_wd - x_wd);
              output |= exception << e_wd;
              break
            }
          }
          s_ptr = s_ptr.offset(utility::words_for_bits(x_wd * x_cnt as u32) as isize);
        }

        let shift: $ty = *(s_ptr as *const $ty);
        let xor: $ty = (0 as $ty).wrapping_sub(output & 1);
        ((output >> 1) ^ xor).wrapping_add(shift)
      }
    }

    impl AccessMany<$ty, ops::Range<usize>> for Unimodal<$ty> {
      unsafe fn _access_many(storage: &[u32], n_blks: usize,
                             range: ops::Range<usize>, output: &mut [$ty]) -> usize {
        let s_blk: usize = range.start >> 7;
        if s_blk > n_blks {
          let len_bnd: usize = (n_blks + 1) << 7;
          panic!(format!("range start is {} but length < {}", range.start, len_bnd))
        }
        let e_blk: usize = range.end.saturating_sub(1) >> 7;
        if e_blk > n_blks {
          let len_bnd: usize = (n_blks + 1) << 7;
          panic!(format!("range end is {} but length < {}", range.end, len_bnd))
        }
        let o_length: usize = range.end - range.start;
        if output.len() < o_length {
          panic!(
            format!("range length is {} but slice length is {}", o_length, output.len())
          );
        }
        let lwr: usize = range.start - (s_blk << 7);
        let upr: usize = range.end - (e_blk << 7);

        let ty_wd: u32 = $ty::width();
        let ty_wrd: usize = utility::words_for_bits(ty_wd);

        // Find the block containing the range start
        let mut s_ptr: *const u32 = storage.as_ptr();
        let wrd_to_blk: usize = words_to_block(n_blks, s_blk, ty_wd, s_ptr);
        s_ptr = s_ptr.offset(wrd_to_blk as isize);

        // Prepare return variable
        let mut scratch: [$ty; 128] = [0; 128];
        let c_ptr: *mut $ty = scratch.as_mut_ptr();
        let mut o_ptr: *mut $ty = output.as_mut_ptr();

        // Start block known, decode the range
        let mut exceptions: [$ty; 127] = [0; 127];
        let mut indices: [u8; 127] = [0; 127];
        let exp_ptr: *mut $ty = exceptions.as_mut_ptr();
        let idx_ptr: *mut u8 = indices.as_mut_ptr();

        if s_blk == e_blk {
          // Find the width of the block
          let e_wd: u32 = *s_ptr & E_WIDTH;
          let left: usize = ((*s_ptr & E_COUNT) >> 7) as usize;
          let x_wd: u32 = (*s_ptr & X_WIDTH) >> 15;
          let x_cnt: usize = ((*s_ptr & X_COUNT) >> 22) as usize;
          let i_wd: u32 = (*s_ptr & I_WIDTH) >> 29;
          s_ptr = s_ptr.offset(1);

          // Checks a lower bound on the length
          let len_bnd: usize = (e_blk << 7) + left;
          if range.start > len_bnd {
            panic!(format!("range start is {} but length is {}", range.start, len_bnd))
          }
          if range.end > len_bnd {
            panic!(format!("range end is {} but length is {}", range.end, len_bnd))
          }
          if range.start == range.end {
            return 0;
          }

          // Decode the block
          s_ptr = Unimodal::<$ty>::_decode_tail(s_ptr, c_ptr, left, e_wd);
          if x_cnt > 0 {
            s_ptr = Unimodal::<u8>::_decode_tail(s_ptr, idx_ptr, x_cnt, i_wd);
            s_ptr = Unimodal::<$ty>::_decode_tail(s_ptr, exp_ptr, x_cnt, x_wd);
            let mut idx: u8 = 0;
            for a in 0..(x_cnt as isize) {
              idx += *idx_ptr.offset(a);
              *c_ptr.offset(idx as isize) |= *exp_ptr.offset(a) << e_wd;
            }
          }

          let shift: $ty = *(s_ptr as *const $ty);
          for a in (lwr as isize)..(upr as isize) {
            let val = *c_ptr.offset(a);
            let xor = (0 as $ty).wrapping_sub(val & 1);
            *c_ptr.offset(a) = ((val >> 1) ^ xor).wrapping_add(shift);
          }

          ptr::copy_nonoverlapping(c_ptr.offset(lwr as isize), o_ptr, o_length);

          o_length
        } else {
          // Initial block
          let e_wd: u32 = *s_ptr & E_WIDTH;
          let x_wd: u32 = (*s_ptr & X_WIDTH) >> 15;
          let x_cnt: usize = ((*s_ptr & X_COUNT) >> 22) as usize;
          let i_wd: u32 = (*s_ptr & I_WIDTH) >> 29;
          s_ptr = s_ptr.offset(1);

          // Decode initial block
          mayda_codec::$dec_simd[e_wd as usize](s_ptr, c_ptr);
          s_ptr = s_ptr.offset((e_wd << 2) as isize);
          
          if x_cnt > 0 {
            s_ptr = Unimodal::<u8>::_decode_tail(s_ptr, idx_ptr, x_cnt, i_wd);
            s_ptr = Unimodal::<$ty>::_decode_tail(s_ptr, exp_ptr, x_cnt, x_wd);
            let mut idx: u8 = 0;
            for a in 0..(x_cnt as isize) {
              idx += *idx_ptr.offset(a);
              *c_ptr.offset(idx as isize) |= *exp_ptr.offset(a) << e_wd;
            }
          }

          let shift: $ty = *(s_ptr as *const $ty);
          s_ptr = s_ptr.offset(ty_wrd as isize);
          for a in (lwr as isize)..128 {
            let val = *c_ptr.offset(a);
            let xor = (0 as $ty).wrapping_sub(val & 1);
            *c_ptr.offset(a) = ((val >> 1) ^ xor).wrapping_add(shift);
          }

          let count: usize = 128 - lwr;
          ptr::copy_nonoverlapping(c_ptr.offset(lwr as isize), o_ptr, count);
          o_ptr = o_ptr.offset(count as isize);

          // Block size is known for all but the final block
          for _ in 0..(e_blk - s_blk - 1) {
            // Find the width of the block
            let e_wd: u32 = *s_ptr & E_WIDTH;
            let x_wd: u32 = (*s_ptr & X_WIDTH) >> 15;
            let x_cnt: usize = ((*s_ptr & X_COUNT) >> 22) as usize;
            let i_wd: u32 = (*s_ptr & I_WIDTH) >> 29;
            s_ptr = s_ptr.offset(1);

            // Decode the block
            mayda_codec::$dec_simd[e_wd as usize](s_ptr, o_ptr);
            s_ptr = s_ptr.offset((e_wd << 2) as isize);
            if x_cnt > 0 {
              s_ptr = Unimodal::<u8>::_decode_tail(s_ptr, idx_ptr, x_cnt, i_wd);
              s_ptr = Unimodal::<$ty>::_decode_tail(s_ptr, exp_ptr, x_cnt, x_wd);
              let mut idx: u8 = 0;
              for a in 0..(x_cnt as isize) {
                idx += *idx_ptr.offset(a);
                *o_ptr.offset(idx as isize) |= *exp_ptr.offset(a) << e_wd;
              }
            }

            let shift: $ty = *(s_ptr as *const $ty);
            s_ptr = s_ptr.offset(ty_wrd as isize);
            for a in 0..128 {
              let val = *o_ptr.offset(a);
              let xor = (0 as $ty).wrapping_sub(val & 1);
              *o_ptr.offset(a) = ((val >> 1) ^ xor).wrapping_add(shift);
            }

            o_ptr = o_ptr.offset(128);
          }

          // Final block
          let e_wd: u32 = *s_ptr & E_WIDTH;
          let left: usize = ((*s_ptr & E_COUNT) >> 7) as usize;
          let x_wd: u32 = (*s_ptr & X_WIDTH) >> 15;
          let x_cnt: usize = ((*s_ptr & X_COUNT) >> 22) as usize;
          let i_wd: u32 = (*s_ptr & I_WIDTH) >> 29;
          s_ptr = s_ptr.offset(1);

          // Checks a lower bound on the length
          let len_bnd: usize = (e_blk << 7) + left;
          if range.end > len_bnd {
            panic!(format!("range end is {} but length is {}", range.end, len_bnd))
          }

          // Decode final block
          s_ptr = Unimodal::<$ty>::_decode_tail(s_ptr, c_ptr, left, e_wd);
          if x_cnt > 0 {
            s_ptr = Unimodal::<u8>::_decode_tail(s_ptr, idx_ptr, x_cnt, i_wd);
            s_ptr = Unimodal::<$ty>::_decode_tail(s_ptr, exp_ptr, x_cnt, x_wd);
            let mut idx: u8 = 0;
            for a in 0..(x_cnt as isize) {
              idx += *idx_ptr.offset(a);
              *c_ptr.offset(idx as isize) |= *exp_ptr.offset(a) << e_wd;
            }
          }

          let shift: $ty = *(s_ptr as *const $ty);
          for a in 0..(upr as isize) {
            let val = *c_ptr.offset(a);
            let xor = (0 as $ty).wrapping_sub(val & 1);
            *c_ptr.offset(a) = ((val >> 1) ^ xor).wrapping_add(shift);
          }

          ptr::copy_nonoverlapping(c_ptr, o_ptr, upr);

          o_length
        }
      }
    }

    impl AccessMany<$ty, ops::RangeFrom<usize>> for Unimodal<$ty> {
      unsafe fn _access_many(storage: &[u32], n_blks: usize,
                             range: ops::RangeFrom<usize>, output: &mut [$ty]) -> usize {
        let s_blk: usize = range.start >> 7;
        if s_blk > n_blks {
          let len_bnd: usize = (n_blks + 1) << 7;
          panic!(format!("range start is {} but length < {}", range.start, len_bnd))
        }
        let lwr: usize = range.start - (s_blk << 7);

        let ty_wd: u32 = $ty::width();
        let ty_wrd: usize = utility::words_for_bits(ty_wd);

        // Find the block containing the range start
        let mut s_ptr: *const u32 = storage.as_ptr();
        let wrd_to_blk: usize = words_to_block(n_blks, s_blk, ty_wd, s_ptr);
        s_ptr = s_ptr.offset(wrd_to_blk as isize);

        // Prepare return variable
        let mut scratch: [$ty; 128] = [0; 128];
        let c_ptr: *mut $ty = scratch.as_mut_ptr();
        let mut o_ptr: *mut $ty = output.as_mut_ptr();

        // Start block known, decode the range
        let mut exceptions: [$ty; 127] = [0; 127];
        let mut indices: [u8; 127] = [0; 127];
        let exp_ptr: *mut $ty = exceptions.as_mut_ptr();
        let idx_ptr: *mut u8 = indices.as_mut_ptr();

        if s_blk == n_blks {
          // Find the width of the block
          let e_wd: u32 = *s_ptr & E_WIDTH;
          let left: usize = ((*s_ptr & E_COUNT) >> 7) as usize;
          let x_wd: u32 = (*s_ptr & X_WIDTH) >> 15;
          let x_cnt: usize = ((*s_ptr & X_COUNT) >> 22) as usize;
          let i_wd: u32 = (*s_ptr & I_WIDTH) >> 29;
          s_ptr = s_ptr.offset(1);

          // Checks the length of storage
          let s_length: usize = (n_blks << 7) + left;
          if range.start > s_length {
            panic!(format!("range start is {} but length is {}", range.start, s_length))
          }
          let o_length: usize = s_length - range.start;
          if output.len() < o_length {
            panic!(
              format!("range length is {} but slice length is {}", o_length, output.len())
            );
          }
          if range.start == s_length {
            return 0;
          }

          // Decode the block
          s_ptr = Unimodal::<$ty>::_decode_tail(s_ptr, c_ptr, left, e_wd);
          if x_cnt > 0 {
            s_ptr = Unimodal::<u8>::_decode_tail(s_ptr, idx_ptr, x_cnt, i_wd);
            s_ptr = Unimodal::<$ty>::_decode_tail(s_ptr, exp_ptr, x_cnt, x_wd);
            let mut idx: u8 = 0;
            for a in 0..(x_cnt as isize) {
              idx += *idx_ptr.offset(a);
              *c_ptr.offset(idx as isize) |= *exp_ptr.offset(a) << e_wd;
            }
          }

          let shift: $ty = *(s_ptr as *const $ty);
          for a in (lwr as isize)..(left as isize) {
            let val = *c_ptr.offset(a);
            let xor = (0 as $ty).wrapping_sub(val & 1);
            *c_ptr.offset(a) = ((val >> 1) ^ xor).wrapping_add(shift);
          }

          ptr::copy_nonoverlapping(c_ptr.offset(lwr as isize), o_ptr, o_length);

          o_length
        } else {
          // Checks the length of storage
          let len_bnd: usize = (n_blks << 7) - range.start;
          if output.len() < len_bnd {
            panic!(
              format!("range length is > {} but slice length is {}", len_bnd, output.len())
            );
          }

          // Initial block
          let e_wd: u32 = *s_ptr & E_WIDTH;
          let x_wd: u32 = (*s_ptr & X_WIDTH) >> 15;
          let x_cnt: usize = ((*s_ptr & X_COUNT) >> 22) as usize;
          let i_wd: u32 = (*s_ptr & I_WIDTH) >> 29;
          s_ptr = s_ptr.offset(1);

          // Decode initial block
          mayda_codec::$dec_simd[e_wd as usize](s_ptr, c_ptr);
          s_ptr = s_ptr.offset((e_wd << 2) as isize);
          if x_cnt > 0 {
            s_ptr = Unimodal::<u8>::_decode_tail(s_ptr, idx_ptr, x_cnt, i_wd);
            s_ptr = Unimodal::<$ty>::_decode_tail(s_ptr, exp_ptr, x_cnt, x_wd);
            let mut idx: u8 = 0;
            for a in 0..(x_cnt as isize) {
              idx += *idx_ptr.offset(a);
              *c_ptr.offset(idx as isize) |= *exp_ptr.offset(a) << e_wd;
            }
          }

          let shift: $ty = *(s_ptr as *const $ty);
          s_ptr = s_ptr.offset(ty_wrd as isize);
          for a in (lwr as isize)..128 {
            let val = *c_ptr.offset(a);
            let xor = (0 as $ty).wrapping_sub(val & 1);
            *c_ptr.offset(a) = ((val >> 1) ^ xor).wrapping_add(shift);
          }

          let count: usize = 128 - lwr;
          ptr::copy_nonoverlapping(c_ptr.offset(lwr as isize), o_ptr, count);
          o_ptr = o_ptr.offset(count as isize);

          // Block size is known for all but the final block
          for _ in 0..(n_blks - s_blk - 1) {
            // Find the width of the block
            let e_wd: u32 = *s_ptr & E_WIDTH;
            let x_wd: u32 = (*s_ptr & X_WIDTH) >> 15;
            let x_cnt: usize = ((*s_ptr & X_COUNT) >> 22) as usize;
            let i_wd: u32 = (*s_ptr & I_WIDTH) >> 29;
            s_ptr = s_ptr.offset(1);

            // Decode the block
            mayda_codec::$dec_simd[e_wd as usize](s_ptr, o_ptr);
            s_ptr = s_ptr.offset((e_wd << 2) as isize);
            if x_cnt > 0 {
              s_ptr = Unimodal::<u8>::_decode_tail(s_ptr, idx_ptr, x_cnt, i_wd);
              s_ptr = Unimodal::<$ty>::_decode_tail(s_ptr, exp_ptr, x_cnt, x_wd);
              let mut idx: u8 = 0;
              for a in 0..(x_cnt as isize) {
                idx += *idx_ptr.offset(a);
                *o_ptr.offset(idx as isize) |= *exp_ptr.offset(a) << e_wd;
              }
            }

            let shift: $ty = *(s_ptr as *const $ty);
            s_ptr = s_ptr.offset(ty_wrd as isize);
            for a in 0..128 {
              let val = *o_ptr.offset(a);
              let xor = (0 as $ty).wrapping_sub(val & 1);
              *o_ptr.offset(a) = ((val >> 1) ^ xor).wrapping_add(shift);
            }

            o_ptr = o_ptr.offset(128);
          }

          // Final block
          let e_wd: u32 = *s_ptr & E_WIDTH;
          let left: usize = ((*s_ptr & E_COUNT) >> 7) as usize;
          let x_wd: u32 = (*s_ptr & X_WIDTH) >> 15;
          let x_cnt: usize = ((*s_ptr & X_COUNT) >> 22) as usize;
          let i_wd: u32 = (*s_ptr & I_WIDTH) >> 29;
          s_ptr = s_ptr.offset(1);

          // Checks the length of storage
          let o_length: usize = (n_blks << 7) + left - range.start;
          if output.len() < o_length {
            panic!(
              format!("range length is {} but slice length is {}", o_length, output.len())
            );
          }

          // Decode final block
          s_ptr = Unimodal::<$ty>::_decode_tail(s_ptr, c_ptr, left, e_wd);
          if x_cnt > 0 {
            s_ptr = Unimodal::<u8>::_decode_tail(s_ptr, idx_ptr, x_cnt, i_wd);
            s_ptr = Unimodal::<$ty>::_decode_tail(s_ptr, exp_ptr, x_cnt, x_wd);
            let mut idx: u8 = 0;
            for a in 0..(x_cnt as isize) {
              idx += *idx_ptr.offset(a);
              *c_ptr.offset(idx as isize) |= *exp_ptr.offset(a) << e_wd;
            }
          }

          let shift: $ty = *(s_ptr as *const $ty);
          for a in 0..(left as isize) {
            let val = *c_ptr.offset(a);
            let xor = (0 as $ty).wrapping_sub(val & 1);
            *c_ptr.offset(a) = ((val >> 1) ^ xor).wrapping_add(shift);
          }

          ptr::copy_nonoverlapping(c_ptr, o_ptr, left);

          o_length
        }
      }
    }
  )*)
}

access_unsigned!{
  (u8: 8, DECODE_U8, DECODE_SIMD_U8)
  (u16: 8, DECODE_U16, DECODE_SIMD_U16)
  (u32: 8, DECODE_U32, DECODE_SIMD_U32)
  (u64: 8, DECODE_U64, DECODE_SIMD_U64)
}

macro_rules! access_signed {
  ($(($it: ident, $ut: ident))*) => ($(
    impl AccessOne<$it> for Unimodal<$it> {
      #[inline]
      unsafe fn _access_one(storage: &[u32], index: usize) -> $it {
        Unimodal::<$ut>::_access_one(storage, index) as $it
      }
    }

    impl AccessMany<$it, ops::Range<usize>> for Unimodal<$it> {
      #[inline]
      unsafe fn _access_many(storage: &[u32], n_blks: usize,
                             range: ops::Range<usize>, output: &mut [$it]) -> usize {
        Unimodal::<$ut>::_access_many(storage, n_blks, range, mem::transmute(output))
      }
    }

    impl AccessMany<$it, ops::RangeFrom<usize>> for Unimodal<$it> {
      #[inline]
      unsafe fn _access_many(storage: &[u32], n_blks: usize,
                             range: ops::RangeFrom<usize>, output: &mut [$it]) -> usize {
        Unimodal::<$ut>::_access_many(storage, n_blks, range, mem::transmute(output))
      }
    }
  )*)
}

access_signed!{
  (i8, u8)
  (i16, u16)
  (i32, u32)
  (i64, u64)
}

#[cfg(target_pointer_width = "16")]
access_signed!{
  (usize, u16)
  (isize, u16)
}

#[cfg(target_pointer_width = "32")]
access_signed!{
  (usize, u32)
  (isize, u32)
}

#[cfg(target_pointer_width = "64")]
access_signed!{
  (usize, u64)
  (isize, u64)
}