diff --git a/d3d12/crates/leonida-formats/src/bcpack.rs b/d3d12/crates/leonida-formats/src/bcpack.rs index 0b9edf6317..4c59101adb 100644 --- a/d3d12/crates/leonida-formats/src/bcpack.rs +++ b/d3d12/crates/leonida-formats/src/bcpack.rs @@ -492,10 +492,24 @@ fn references(s: &[u8], paired: bool) -> (usize, usize) { /// Two 4-bit symbols, the first on the even bits. fn interleave(a: u8, b: u8) -> u8 { - let spread = |v: u8| (0..4).fold(0, |acc, k| acc | (v >> k & 1) << (2 * k)); - spread(a) | spread(b) << 1 + SPREAD[usize::from(a & 15)] | SPREAD[usize::from(b & 15)] << 1 } +/// A nibble's bits on the even bits of a byte. +const SPREAD: [u8; 16] = { + let mut t = [0; 16]; + let mut v = 0; + while v < 16 { + let mut k = 0; + while k < 4 { + t[v] |= ((v >> k & 1) << (2 * k)) as u8; + k += 1; + } + v += 1; + } + t +}; + /// Block `i`'s reference `r`: 0 none, else `r` blocks back, before the first /// block into the dictionary. fn pick<'a, T>(i: usize, r: usize, history: &'a [T], dictionary: &'a [T], zero: &'a T) -> &'a T { @@ -512,21 +526,33 @@ fn zz(v: u8, bits: u32) -> u8 { ((v >> 1) ^ if v & 1 != 0 { mask } else { 0 }) & mask } -/// Element widths of the plain formats' endpoints and indices. +/// Element widths of the plain formats' endpoints and indices, as the masks +/// of their bits: the eight endpoints' in a word, the 32 indices' in four +/// (a byte each). struct Widths { - ep: [u8; 8], - ix: [u8; 32], + ep: u64, + ix: [u64; 4], paired: bool, } const fn widths(ep: [u8; 8], ix3: usize, ix2: usize, paired: bool) -> Widths { - let mut ix = [0; 32]; + let mut mask = 0; + let mut k = 0; + while k < 8 { + mask |= (((1u16 << ep[k]) - 1) as u8 as u64) << (8 * k); + k += 1; + } + let mut ix = [0; 4]; let mut k = 0; while k < ix3 + ix2 { - ix[k] = if k < ix3 { 3 } else { 2 }; + ix[k / 8] |= (if k < ix3 { 7u64 } else { 3 }) << (8 * (k % 8)); k += 1; } - Widths { ep, ix, paired } + Widths { + ep: mask, + ix, + paired, + } } const BC1: Widths = widths([5, 6, 5, 5, 6, 5, 0, 0], 0, 16, false); @@ -549,11 +575,19 @@ fn plain<'a>( let ep_ref = &pick(i, ep_ref, &history, dictionary, &zero).ep; let ix_ref = &pick(i, ix_ref, &history, dictionary, &zero).ix; let mut v = residual(s); - for ((v, r), bits) in v.ep.iter_mut().zip(ep_ref).zip(w.ep) { - *v = v.wrapping_add(*r) & ((1u16 << bits) - 1) as u8; - } - for ((v, r), bits) in v.ix.iter_mut().zip(ix_ref).zip(w.ix) { - *v = v.wrapping_add(*r) & ((1u8 << bits) - 1); + // Each element is summed with its reference modulo its width, eight + // bytes at a time (no carry leaves a byte: the top bits are added + // apart). + const HIGH: u64 = 0x8080_8080_8080_8080; + let (a, b) = (u64::from_le_bytes(v.ep), u64::from_le_bytes(*ep_ref)); + v.ep = ((((a & !HIGH) + (b & !HIGH)) ^ ((a ^ b) & HIGH)) & w.ep).to_le_bytes(); + // (Indices are at most seven bits wide: the masked sums stay in + // their bytes.) + let (rows, _) = v.ix.as_chunks_mut::<8>(); + let (refs, _) = ix_ref.as_chunks::<8>(); + for ((row, r), m) in rows.iter_mut().zip(refs).zip(w.ix) { + let sum = (u64::from_le_bytes(*row) & m) + (u64::from_le_bytes(*r) & m); + *row = (sum & m).to_le_bytes(); } history[i % HISTORY] = v; pack(&v, block); @@ -561,23 +595,53 @@ fn plain<'a>( Ok(()) } -/// 2-bit index residuals: one symbol per 2x2 quad. +/// 2-bit index residuals: one symbol per 2x2 quad, four of them for the 16 +/// pixels of a block (`ix` takes all 16, a byte each). fn quads(rank: &[u8; 256], s: &[u8], ix: &mut [u8]) { - for (q, &sym) in s.iter().enumerate() { - let v = rank[usize::from(sym)]; - let base = (q >> 1) * 8 + (q & 1) * 2; - for (k, p) in [0, 1, 4, 5].into_iter().enumerate() { - ix[base + p] = v >> (2 * k) & 3; - } + // A row is four pixels, a word: a quad's top pair is the low half of its + // row's word if the quad is a left one, the high half if a right one; + // its bottom pair is the same in the row below. + let mut words = [0u32; 4]; + for (q, &sym) in s.iter().take(4).enumerate() { + let pixels = QUAD_PIXELS[usize::from(rank[usize::from(sym)])]; + let (row, shift) = ((q >> 1) * 2, 16 * (q & 1)); + words[row] |= (pixels & 0xffff) << shift; + words[row + 1] |= (pixels >> 16) << shift; + } + let (rows, _) = ix.as_chunks_mut::<4>(); + for (row, word) in rows.iter_mut().zip(words) { + *row = word.to_le_bytes(); } } -/// 3-bit index residuals: one symbol per two pixels in raster order. +/// A quad's symbol (2 bits for each of the quad's pixels 0, 1, 4 and 5) as +/// the four bytes they are. +const QUAD_PIXELS: [u32; 256] = { + let mut t = [0; 256]; + let mut v = 0; + while v < 256 { + let mut k = 0; + while k < 4 { + t[v] |= ((v >> (2 * k) & 3) as u32) << (8 * k); + k += 1; + } + v += 1; + } + t +}; + +/// 3-bit index residuals: one symbol per two pixels in raster order (`ix` +/// takes both, a byte each). fn pairs(rank: &[u8; 64], s: &[u8], ix: &mut [u8]) { - for (k, &sym) in s.iter().enumerate() { - let v = rank[usize::from(sym & 63)]; - ix[2 * k] = v & 7; - ix[2 * k + 1] = v >> 4 & 7; + let (s, _) = s.as_chunks::<4>(); + let (ix, _) = ix.as_chunks_mut::<8>(); + for (s, ix) in s.iter().zip(ix) { + let mut word = 0u64; + for (k, &sym) in s.iter().enumerate() { + let v = u64::from(rank[usize::from(sym & 63)]); + word |= ((v & 7) | (v & 0x70) << 4) << (16 * k); + } + *ix = word.to_le_bytes(); } } @@ -626,16 +690,55 @@ fn bc5_residual(t: &Tables, s: &[u8]) -> Plain { v } -/// Indices along the endpoint line -> BC1 and BC4 codes. -const BC1_CODE: [u32; 4] = [0, 2, 3, 1]; +/// Indices along the endpoint line -> BC4 codes (`bc4_codes` is this, eight +/// at a time). +#[cfg(test)] const BC4_CODE: [u64; 8] = [0, 2, 3, 4, 5, 6, 7, 1]; +/// Eight indices (a byte each, the low two bits count) as BC1 codes, which +/// are 0, 2, 3, 1 for 0, 1, 2, 3: the high bit of a code is the index's bits +/// xored, the low one the index's high bit. +fn bc1_codes(ix: u64) -> u64 { + const LOW: u64 = 0x0101_0101_0101_0101; + let (b0, b1) = (ix & LOW, ix >> 1 & LOW); + (b0 ^ b1) << 1 | b1 +} + +/// Eight indices (a byte each, the low three bits count) as BC4 codes, +/// which are 0, 2, 3, 4, 5, 6, 7, 1: the index plus one, but for the two +/// ends of the line (whose bits are all alike), which are 0 and 1. +fn bc4_codes(ix: u64) -> u64 { + const LOW: u64 = 0x0101_0101_0101_0101; + const THREE: u64 = 3 * LOW; + const SEVEN: u64 = 7 * LOW; + let x = ix & SEVEN; + let d = (x ^ x >> 1) & THREE; + let ends = !(d | d >> 1) & LOW; + ((x + LOW) & SEVEN) ^ ends +} + +/// Eight bytes of two bits each (the rest must be zero) as 16 bits, the +/// first byte lowest. +fn pack2(x: u64) -> u64 { + let x = (x | x >> 6) & 0x000f_000f_000f_000f; + let x = (x | x >> 12) & 0x0000_00ff_0000_00ff; + (x | x >> 24) & 0xffff +} + +/// Eight bytes of three bits each (the rest must be zero) as 24 bits, the +/// first byte lowest. +fn pack3(x: u64) -> u64 { + let x = (x | x >> 5) & 0x003f_003f_003f_003f; + let x = (x | x >> 10) & 0x0000_0fff_0000_0fff; + (x | x >> 20) & 0x00ff_ffff +} + fn bc1_color(ep: &[u8], ix: &[u8], out: &mut [u8]) { let c0 = u16::from(ep[0]) << 11 | u16::from(ep[1]) << 5 | u16::from(ep[2]); let c1 = u16::from(ep[3]) << 11 | u16::from(ep[4]) << 5 | u16::from(ep[5]); - let bits = (0..16).fold(0, |acc, p| { - acc | BC1_CODE[usize::from(ix[p] & 3)] << (2 * p) - }); + let (rows, _) = ix.as_chunks::<8>(); + let codes = |row: &[u8; 8]| pack2(bc1_codes(u64::from_le_bytes(*row) & 0x0303_0303_0303_0303)); + let bits = (codes(&rows[0]) | codes(&rows[1]) << 16) as u32; out[..2].copy_from_slice(&c0.to_le_bytes()); out[2..4].copy_from_slice(&c1.to_le_bytes()); out[4..8].copy_from_slice(&bits.to_le_bytes()); @@ -643,10 +746,10 @@ fn bc1_color(ep: &[u8], ix: &[u8], out: &mut [u8]) { /// BC4 half: endpoints are kept 128 below their stored value. fn bc4_alpha(e0: u8, e1: u8, ix: &[u8], out: &mut [u8]) { - let bits = (0..16).fold(0u64, |acc, p| { - acc | BC4_CODE[usize::from(ix[p] & 7)] << (16 + 3 * p) - }); - let v = u64::from(e0 ^ 0x80) | u64::from(e1 ^ 0x80) << 8 | bits; + let (rows, _) = ix.as_chunks::<8>(); + let codes = |row: &[u8; 8]| pack3(bc4_codes(u64::from_le_bytes(*row))); + let bits = codes(&rows[0]) | codes(&rows[1]) << 24; + let v = u64::from(e0 ^ 0x80) | u64::from(e1 ^ 0x80) << 8 | bits << 16; out[..8].copy_from_slice(&v.to_le_bytes()); } @@ -1583,6 +1686,374 @@ mod tests { } } + /// The plain formats' (BC1, BC3, BC4, BC5) block loops as they were before + /// their masks, quads and packing were made cheaper. + mod reference_plain { + use super::*; + + const BC1_CODE: [u32; 4] = [0, 2, 3, 1]; + const BC4_CODE: [u64; 8] = [0, 2, 3, 4, 5, 6, 7, 1]; + + pub struct Widths { + ep: [u8; 8], + ix: [u8; 32], + paired: bool, + } + + const fn widths(ep: [u8; 8], ix3: usize, ix2: usize, paired: bool) -> Widths { + let mut ix = [0; 32]; + let mut k = 0; + while k < ix3 + ix2 { + ix[k] = if k < ix3 { 3 } else { 2 }; + k += 1; + } + Widths { ep, ix, paired } + } + + const BC1: Widths = widths([5, 6, 5, 5, 6, 5, 0, 0], 0, 16, false); + const BC3: Widths = widths([5, 6, 5, 5, 6, 5, 8, 8], 16, 16, true); + const BC4: Widths = widths([8, 8, 0, 0, 0, 0, 0, 0], 16, 0, false); + const BC5: Widths = widths([8, 8, 8, 8, 0, 0, 0, 0], 32, 0, true); + + fn interleave(a: u8, b: u8) -> u8 { + let spread = |v: u8| (0..4).fold(0, |acc, k| acc | (v >> k & 1) << (2 * k)); + spread(a) | spread(b) << 1 + } + + fn references(s: &[u8], paired: bool) -> (usize, usize) { + let (d, ix) = if paired { + (interleave(s[0], s[1]), interleave(s[2], s[3])) + } else { + (s[0], s[1]) + }; + let delta = (d >> 1) ^ (d & 1).wrapping_neg(); + (usize::from(ix.wrapping_add(delta)), usize::from(ix)) + } + + fn zz(v: u8, bits: u32) -> u8 { + let mask = ((1u32 << bits) - 1) as u8; + ((v >> 1) ^ if v & 1 != 0 { mask } else { 0 }) & mask + } + + pub fn plain<'a>( + records: impl Iterator, + out: impl Iterator, + dictionary: &[Plain], + w: &Widths, + residual: impl Fn(&[u8]) -> Plain, + pack: fn(&Plain, &mut [u8]), + ) -> Result<(), Error> { + let mut history = vec![Plain::default(); HISTORY]; + let zero = Plain::default(); + for (i, (s, block)) in records.zip(out).enumerate() { + let (ep_ref, ix_ref) = references(s, w.paired); + let ep_ref = &pick(i, ep_ref, &history, dictionary, &zero).ep; + let ix_ref = &pick(i, ix_ref, &history, dictionary, &zero).ix; + let mut v = residual(s); + for ((v, r), bits) in v.ep.iter_mut().zip(ep_ref).zip(w.ep) { + *v = v.wrapping_add(*r) & ((1u16 << bits) - 1) as u8; + } + for ((v, r), bits) in v.ix.iter_mut().zip(ix_ref).zip(w.ix) { + *v = v.wrapping_add(*r) & ((1u8 << bits) - 1); + } + history[i % HISTORY] = v; + pack(&v, block); + } + Ok(()) + } + + fn quads(rank: &[u8; 256], s: &[u8], ix: &mut [u8]) { + for (q, &sym) in s.iter().enumerate() { + let v = rank[usize::from(sym)]; + let base = (q >> 1) * 8 + (q & 1) * 2; + for (k, p) in [0, 1, 4, 5].into_iter().enumerate() { + ix[base + p] = v >> (2 * k) & 3; + } + } + } + + fn pairs(rank: &[u8; 64], s: &[u8], ix: &mut [u8]) { + for (k, &sym) in s.iter().enumerate() { + let v = rank[usize::from(sym & 63)]; + ix[2 * k] = v & 7; + ix[2 * k + 1] = v >> 4 & 7; + } + } + + pub fn bc1_residual(t: &Tables, s: &[u8]) -> Plain { + let mut v = Plain::default(); + for (k, bits) in [5, 6, 5, 5, 6, 5].into_iter().enumerate() { + v.ep[k] = zz(s[2 + k], bits); + } + quads(&t.rank1, &s[8..12], &mut v.ix); + v + } + + pub fn bc3_residual(t: &Tables, s: &[u8]) -> Plain { + let mut v = Plain { + ep: [ + zz(s[14], 5), + zz(s[16], 6), + zz(s[18], 5), + zz(s[15], 5), + zz(s[17], 6), + zz(s[19], 5), + zz(s[4], 8), + zz(s[5], 8), + ], + ..Default::default() + }; + pairs(&t.rank3a, &s[6..14], &mut v.ix[..16]); + quads(&t.rank3c, &s[20..24], &mut v.ix[16..]); + v + } + + pub fn bc4_residual(t: &Tables, s: &[u8]) -> Plain { + let mut v = Plain::default(); + v.ep[..2].copy_from_slice(&[zz(s[2], 8), zz(s[3], 8)]); + pairs(&t.rank4, &s[4..12], &mut v.ix[..16]); + v + } + + pub fn bc5_residual(t: &Tables, s: &[u8]) -> Plain { + let mut v = Plain::default(); + for k in 0..4 { + v.ep[k] = zz(s[4 + k], 8); + } + pairs(&t.rank5, &s[8..24], &mut v.ix); + v + } + + fn bc1_color(ep: &[u8], ix: &[u8], out: &mut [u8]) { + let c0 = u16::from(ep[0]) << 11 | u16::from(ep[1]) << 5 | u16::from(ep[2]); + let c1 = u16::from(ep[3]) << 11 | u16::from(ep[4]) << 5 | u16::from(ep[5]); + let bits = (0..16).fold(0, |acc, p| { + acc | BC1_CODE[usize::from(ix[p] & 3)] << (2 * p) + }); + out[..2].copy_from_slice(&c0.to_le_bytes()); + out[2..4].copy_from_slice(&c1.to_le_bytes()); + out[4..8].copy_from_slice(&bits.to_le_bytes()); + } + + fn bc4_alpha(e0: u8, e1: u8, ix: &[u8], out: &mut [u8]) { + let bits = (0..16).fold(0u64, |acc, p| { + acc | BC4_CODE[usize::from(ix[p] & 7)] << (16 + 3 * p) + }); + let v = u64::from(e0 ^ 0x80) | u64::from(e1 ^ 0x80) << 8 | bits; + out[..8].copy_from_slice(&v.to_le_bytes()); + } + + pub fn bc1_pack(v: &Plain, out: &mut [u8]) { + bc1_color(&v.ep, &v.ix, out); + } + + pub fn bc3_pack(v: &Plain, out: &mut [u8]) { + bc4_alpha(v.ep[6], v.ep[7], &v.ix[..16], &mut out[..8]); + bc1_color(&v.ep, &v.ix[16..], &mut out[8..]); + } + + pub fn bc4_pack(v: &Plain, out: &mut [u8]) { + bc4_alpha(v.ep[0], v.ep[1], &v.ix, out); + } + + pub fn bc5_pack(v: &Plain, out: &mut [u8]) { + bc4_alpha(v.ep[0], v.ep[1], &v.ix[..16], &mut out[..8]); + bc4_alpha(v.ep[2], v.ep[3], &v.ix[16..], &mut out[8..]); + } + + /// What `decode` does after the lanes, for `bc` (1, 3, 4 or 5). + pub fn post(bc: u8, t: &Tables, records: &[u8], out: &mut [u8]) -> Result<(), Error> { + let (stride, block) = post_sizes(bc); + let records = records.chunks_exact(stride); + let out = out.chunks_exact_mut(block); + match bc { + 1 => plain(records, out, &t.bc1, &BC1, |s| bc1_residual(t, s), bc1_pack), + 3 => plain(records, out, &t.bc3, &BC3, |s| bc3_residual(t, s), bc3_pack), + 4 => plain(records, out, &t.bc4, &BC4, |s| bc4_residual(t, s), bc4_pack), + _ => plain(records, out, &t.bc5, &BC5, |s| bc5_residual(t, s), bc5_pack), + } + } + } + + /// Record and block sizes of the plain formats. + fn post_sizes(bc: u8) -> (usize, usize) { + match bc { + 1 => (12, 8), + 3 => (24, 16), + 4 => (12, 8), + _ => (24, 16), + } + } + + /// What `decode` does after the lanes, for `bc` (1, 3, 4 or 5), through the + /// code under test. + fn plain_post(bc: u8, t: &Tables, records: &[u8], out: &mut [u8]) -> Result<(), Error> { + let (stride, block) = post_sizes(bc); + let records = records.chunks_exact(stride); + let out = out.chunks_exact_mut(block); + match bc { + 1 => plain(records, out, &t.bc1, &BC1, |s| bc1_residual(t, s), bc1_pack), + 3 => plain(records, out, &t.bc3, &BC3, |s| bc3_residual(t, s), bc3_pack), + 4 => plain(records, out, &t.bc4, &BC4, |s| bc4_residual(t, s), bc4_pack), + _ => plain(records, out, &t.bc5, &BC5, |s| bc5_residual(t, s), bc5_pack), + } + } + + /// A small, fixed-seed generator for the block tests. + struct PostRng(u64); + + impl PostRng { + fn next(&mut self) -> u64 { + self.0 ^= self.0 << 13; + self.0 ^= self.0 >> 7; + self.0 ^= self.0 << 17; + self.0 + } + } + + /// The shipped tables, or tables whose rank tables and dictionaries hold + /// any byte. + fn wild_plain_tables(rng: &mut PostRng) -> Tables { + let mut t = Tables::builtin().unwrap(); + for v in t + .rank1 + .iter_mut() + .chain(&mut t.rank3a) + .chain(&mut t.rank3c) + .chain(&mut t.rank4) + .chain(&mut t.rank5) + { + *v = rng.next() as u8; + } + for dictionary in [&mut t.bc1, &mut t.bc3, &mut t.bc4, &mut t.bc5] { + for p in dictionary.iter_mut() { + for v in p.ep.iter_mut().chain(&mut p.ix) { + *v = rng.next() as u8; + } + } + } + t + } + + /// The plain formats' block loops give the reference's bytes for the + /// shipped tables and for tables of arbitrary ranks and dictionaries + /// (history wraps: more blocks than the 256 it keeps). + #[test] + fn plain_blocks_match_the_reference() { + let mut rng = PostRng(0x9e37_79b9_7f4a_7c15); + for round in 0..16 { + let t = if round == 0 { + Tables::builtin().unwrap() + } else { + wild_plain_tables(&mut rng) + }; + for bc in [1, 3, 4, 5] { + let (stride, block) = post_sizes(bc); + let blocks = 700; + let records: Vec = (0..blocks * stride).map(|_| rng.next() as u8).collect(); + let mut got = vec![0u8; blocks * block]; + let mut want = vec![0u8; blocks * block]; + plain_post(bc, &t, &records, &mut got).unwrap(); + reference_plain::post(bc, &t, &records, &mut want).unwrap(); + assert!(got == want, "BC{bc}, round {round}"); + } + } + } + + /// The plain formats' block loops against the reference on the shipped + /// tables: + /// `cargo test --release -p leonida-formats plain_post_throughput -- --ignored --nocapture`. + #[test] + #[ignore = "benchmark"] + fn plain_post_throughput() { + let t = Tables::builtin().unwrap(); + let mut rng = PostRng(0x1234_5678_9abc_def1); + let blocks = 1 << 14; + for bc in [1, 3, 4, 5] { + let (stride, block) = post_sizes(bc); + let random: Vec = (0..blocks * stride).map(|_| rng.next() as u8).collect(); + // Random bytes pick random references: the branches that pick + // them are never right. Without references they always are. + let mut plain = random.clone(); + for record in plain.chunks_exact_mut(stride) { + record[..if bc == 3 || bc == 5 { 4 } else { 2 }].fill(0); + } + for (what, records) in [("random references", &random), ("no references", &plain)] { + let mut dst = vec![0u8; blocks * block]; + let mut want = vec![0u8; blocks * block]; + reference_plain::post(bc, &t, records, &mut want).unwrap(); + plain_post(bc, &t, records, &mut dst).unwrap(); + assert!(dst == want, "BC{bc}, {what}"); + // Alternating batches, the fastest of each (the machine is not ours alone). + let (mut old, mut new) = (f64::MAX, f64::MAX); + for _ in 0..40 { + let started = std::time::Instant::now(); + for _ in 0..3 { + reference_plain::post(bc, &t, std::hint::black_box(records), &mut dst) + .unwrap(); + } + old = old.min(started.elapsed().as_secs_f64() / 3.0); + let started = std::time::Instant::now(); + for _ in 0..3 { + plain_post(bc, &t, std::hint::black_box(records), &mut dst).unwrap(); + } + new = new.min(started.elapsed().as_secs_f64() / 3.0); + } + let (old, new) = (old * 1e9 / blocks as f64, new * 1e9 / blocks as f64); + println!( + "BC{bc} blocks, {what}: reference {old:.1} ns/block, now {new:.1} ns/block ({:.2}x)", + old / new + ); + } + } + } + + /// The eight-at-a-time index codes are the code tables' entries, for + /// every byte at every position (the bytes beside it any). + #[test] + fn index_codes_are_the_tables() { + const BC1_CODE: [u8; 4] = [0, 2, 3, 1]; + let mut rng = PostRng(0x0123_4567_89ab_cdef); + for x in 0..=255u8 { + for at in 0..8 { + let mut bytes = rng.next().to_le_bytes(); + bytes[at] = x; + let word = u64::from_le_bytes(bytes); + let one = bc1_codes(word & 0x0303_0303_0303_0303).to_le_bytes(); + let four = bc4_codes(word).to_le_bytes(); + for k in 0..8 { + assert_eq!( + one[k], + BC1_CODE[usize::from(bytes[k] & 3)], + "BC1 {bytes:?} at {k}" + ); + assert_eq!( + u64::from(four[k]), + BC4_CODE[usize::from(bytes[k] & 7)], + "BC4 {bytes:?} at {k}" + ); + } + } + } + } + + /// The packers put a byte's field where its position says. + #[test] + fn index_codes_pack_by_position() { + let mut rng = PostRng(0xfeed_face_cafe_beef); + for _ in 0..20_000 { + let word = rng.next(); + let (two, three) = (word & 0x0303_0303_0303_0303, word & 0x0707_0707_0707_0707); + let bytes = word.to_le_bytes(); + let want2: u64 = (0..8).map(|k| u64::from(bytes[k] & 3) << (2 * k)).sum(); + let want3: u64 = (0..8).map(|k| u64::from(bytes[k] & 7) << (3 * k)).sum(); + assert_eq!(pack2(two), want2); + assert_eq!(pack3(three), want3); + } + assert_eq!(pack2(0x0303_0303_0303_0303), 0xffff); + assert_eq!(pack3(0x0707_0707_0707_0707), 0xff_ffff); + } + /// Compares with reference outputs made outside the repository: /// `LEONIDA_BCPACK_TABLES` (the table file) and `LEONIDA_BCPACK_CASES`, a /// directory of `.bc` packed chunks with `.out` beside them.