pub const CHUNK: usize = 1024;
const LANES: usize = 16;
#[inline]
pub const fn packed_bytes(width: u32, count: usize) -> usize {
(count * width as usize).div_ceil(64) * 8
}
pub fn pack(values: &[u64], width: u32, out: &mut Vec<u8>) {
assert!(values.len() <= CHUNK);
assert!(width <= 64);
if width == 0 {
return;
}
let mask = if width == 64 {
u64::MAX
} else {
(1u64 << width) - 1
};
if width > 1 && width < 64 && packed_bytes(width, values.len()) == 128 * width as usize {
pack_transposed(values, width, mask, out);
return;
}
let mut acc = 0u64;
let mut bits = 0u32;
for &raw in values {
let v = raw & mask;
acc |= v << bits;
bits += width;
if bits >= 64 {
out.extend_from_slice(&acc.to_le_bytes());
bits -= 64;
acc = if bits == 0 { 0 } else { v >> (width - bits) };
}
}
if bits > 0 {
out.extend_from_slice(&acc.to_le_bytes());
}
}
fn pack_transposed(values: &[u64], width: u32, mask: u64, out: &mut Vec<u8>) {
let w = width as usize;
let mut words = [0u64; LANES * 63];
let words = &mut words[..LANES * w];
for (i, &raw) in values.iter().enumerate() {
let bit = i / LANES * w;
let (r, s) = (bit >> 6, (bit & 63) as u32);
let v = raw & mask;
words[LANES * r + i % LANES] |= v << s;
if s + width > 64 {
words[LANES * (r + 1) + i % LANES] |= v >> (64 - s);
}
}
for &word in words.iter() {
out.extend_from_slice(&word.to_le_bytes());
}
}
pub fn unpack(packed: &[u8], width: u32, out: &mut [u64]) {
assert_eq!(out.len(), CHUNK);
assert!(width <= 64);
assert_eq!(packed.len() % 8, 0);
if width == 0 {
out.fill(0);
return;
}
if width == 64 {
let n = (packed.len() / 8).min(CHUNK);
for (chunk, slot) in packed.as_chunks::<8>().0.iter().zip(out.iter_mut()) {
*slot = u64::from_le_bytes(*chunk);
}
out[n..].fill(0);
return;
}
let full = width > 1 && packed.len() == 128 * width as usize;
macro_rules! dispatch {
($($w:literal)*) => {
match width {
$($w if full => unpack_transposed::<$w>(packed, out),)*
$($w => unpack_const::<$w>(packed, out),)*
_ => unreachable!(),
}
};
}
dispatch!(
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
);
}
fn unpack_transposed<const W: u32>(packed: &[u8], out: &mut [u64]) {
let mask: u64 = (1u64 << W) - 1;
let mut words = [0u64; LANES * 63];
for (slot, chunk) in words[..LANES * W as usize]
.iter_mut()
.zip(packed.as_chunks::<8>().0)
{
*slot = u64::from_le_bytes(*chunk);
}
for (t, out16) in out.as_chunks_mut::<LANES>().0.iter_mut().enumerate() {
let bit = t as u32 * W;
let r = (bit >> 6) as usize;
let s = bit & 63;
let lo = &words[LANES * r..LANES * (r + 1)];
if s + W > 64 {
let hi = &words[LANES * (r + 1)..LANES * (r + 2)];
for ((o, &l), &h) in out16.iter_mut().zip(lo).zip(hi) {
*o = ((l >> s) | (h << (64 - s))) & mask;
}
} else {
for (o, &l) in out16.iter_mut().zip(lo) {
*o = (l >> s) & mask;
}
}
}
}
fn unpack_const<const W: u32>(packed: &[u8], out: &mut [u64]) {
let mask: u64 = (1u64 << W) - 1;
let mut words = [0u64; 16 * 63];
for (slot, chunk) in words[..16 * W as usize]
.iter_mut()
.zip(packed.as_chunks::<8>().0)
{
*slot = u64::from_le_bytes(*chunk);
}
for (block, out_block) in out.as_chunks_mut::<64>().0.iter_mut().enumerate() {
let base = block * W as usize;
for (i, slot) in out_block.iter_mut().enumerate() {
let bit = i as u32 * W;
let w = base + (bit >> 6) as usize;
let s = bit & 63;
let lo = words[w] >> s;
*slot = if s + W > 64 {
(lo | (words[w + 1] << (64 - s))) & mask
} else {
lo & mask
};
}
}
}
#[cfg(test)]
mod tests {
use super::*;
fn xorshift(state: &mut u64) -> u64 {
*state ^= *state << 13;
*state ^= *state >> 7;
*state ^= *state << 17;
*state
}
#[test]
fn roundtrip_every_width() {
let mut rng = 0x2545F4914F6CDD1Du64;
for width in 0..=64u32 {
let mask = if width == 64 {
u64::MAX
} else {
(1u64 << width) - 1
};
let values: Vec<u64> = (0..CHUNK).map(|_| xorshift(&mut rng) & mask).collect();
let mut packed = Vec::new();
pack(&values, width, &mut packed);
assert_eq!(packed.len(), packed_bytes(width, CHUNK));
let mut out = vec![0u64; CHUNK];
unpack(&packed, width, &mut out);
assert_eq!(values, out, "width {width}");
}
}
#[test]
fn short_tail_packs_tight() {
let values = [7u64, 7, 7];
let mut packed = Vec::new();
pack(&values, 3, &mut packed);
assert_eq!(packed.len(), packed_bytes(3, 3));
assert_eq!(packed.len(), 8);
let mut out = vec![1u64; CHUNK];
unpack(&packed, 3, &mut out);
assert_eq!(&out[..3], &values);
assert!(out[3..].iter().all(|&v| v == 0));
}
#[test]
fn tail_boundaries_every_width() {
let mut rng = 0xDEADBEEFu64;
for width in 1..=64u32 {
let mask = if width == 64 {
u64::MAX
} else {
(1u64 << width) - 1
};
for len in [1usize, 63, 64, 65, 1000, 1023, CHUNK] {
let values: Vec<u64> = (0..len).map(|_| xorshift(&mut rng) & mask).collect();
let mut packed = Vec::new();
pack(&values, width, &mut packed);
assert_eq!(packed.len(), packed_bytes(width, len), "w{width} n{len}");
let mut out = vec![1u64; CHUNK];
unpack(&packed, width, &mut out);
assert_eq!(&out[..len], values.as_slice(), "w{width} n{len}");
assert!(out[len..].iter().all(|&v| v == 0), "w{width} n{len} pad");
}
}
}
}