use alloc::vec;
use alloc::vec::Vec;
use core::ptr;
pub(crate) const REP_NUM: u32 = 3;
pub(crate) const MINMATCH: u32 = 3;
pub(crate) const WILDCOPY_OVERLENGTH: usize = 32;
pub(crate) const MAX_LL: u8 = 35;
pub(crate) const MAX_ML: u8 = 52;
pub(crate) const MAX_OFF: u8 = 31;
pub(crate) const STREAM_ACCUMULATOR_MIN: u32 = if usize::BITS == 32 { 25 } else { 57 };
pub(crate) const REP_START_VALUE: [u32; 3] = [1, 4, 8];
pub(crate) const REPCODE1_TO_OFFBASE: u32 = 1;
#[inline(always)]
pub(crate) const fn repcode_to_offbase(r: u32) -> u32 {
debug_assert!(r >= 1 && r <= REP_NUM);
r
}
#[inline(always)]
pub(crate) const fn offset_to_offbase(o: u32) -> u32 {
debug_assert!(o > 0);
o + REP_NUM
}
#[inline(always)]
pub(crate) const fn offbase_is_offset(o: u32) -> bool {
o > REP_NUM
}
#[inline(always)]
pub(crate) const fn offbase_is_repcode(o: u32) -> bool {
1 <= o && o <= REP_NUM
}
#[inline(always)]
pub(crate) const fn offbase_to_offset(o: u32) -> u32 {
debug_assert!(offbase_is_offset(o));
o - REP_NUM
}
#[inline]
pub(crate) fn update_rep(rep: &mut [u32; 3], off_base: u32, ll0: bool) {
if offbase_is_offset(off_base) {
rep[2] = rep[1];
rep[1] = rep[0];
rep[0] = offbase_to_offset(off_base);
} else {
let rep_code = off_base - 1 + ll0 as u32;
if rep_code > 0 {
let current_offset = if rep_code == REP_NUM {
rep[0].wrapping_sub(1)
} else {
rep[rep_code as usize]
};
rep[2] = if rep_code >= 2 { rep[1] } else { rep[2] };
rep[1] = rep[0];
rep[0] = current_offset;
}
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub(crate) struct SeqDef {
pub(crate) off_base: u32,
pub(crate) lit_length: u16,
pub(crate) ml_base: u16,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum LongLengthType {
None,
LiteralLength,
MatchLength,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) struct SequenceLength {
pub(crate) lit_length: u32,
pub(crate) match_length: u32,
}
#[derive(Debug)]
pub(crate) struct SeqStore {
pub(crate) sequences: Vec<SeqDef>,
pub(crate) nb_seq: usize,
pub(crate) lit_buf: Vec<u8>,
pub(crate) lit_len: usize,
pub(crate) ll_code: Vec<u8>,
pub(crate) ml_code: Vec<u8>,
pub(crate) of_code: Vec<u8>,
pub(crate) max_nb_seq: usize,
pub(crate) max_nb_lit: usize,
pub(crate) long_length_type: LongLengthType,
pub(crate) long_length_pos: u32,
}
pub(crate) const fn max_nb_seq(block_size: usize, min_match: u32) -> usize {
let divider = if min_match == 3 { 3 } else { 4 };
block_size / divider
}
impl SeqStore {
pub(crate) fn new(block_size: usize, min_match: u32) -> Self {
let max_nb_seq = max_nb_seq(block_size, min_match);
Self {
sequences: vec![SeqDef::default(); max_nb_seq],
nb_seq: 0,
lit_buf: vec![0; block_size + WILDCOPY_OVERLENGTH],
lit_len: 0,
ll_code: vec![0; max_nb_seq],
ml_code: vec![0; max_nb_seq],
of_code: vec![0; max_nb_seq],
max_nb_seq,
max_nb_lit: block_size,
long_length_type: LongLengthType::None,
long_length_pos: 0,
}
}
#[inline]
pub(crate) fn reset(&mut self) {
self.lit_len = 0;
self.nb_seq = 0;
self.long_length_type = LongLengthType::None;
}
#[inline]
pub(crate) fn sequences(&self) -> &[SeqDef] {
&self.sequences[..self.nb_seq]
}
#[inline]
pub(crate) fn literals(&self) -> &[u8] {
&self.lit_buf[..self.lit_len]
}
pub(crate) fn sequence_length(&self, idx: usize) -> SequenceLength {
let seq = self.sequences[idx];
let mut len = SequenceLength {
lit_length: u32::from(seq.lit_length),
match_length: u32::from(seq.ml_base) + MINMATCH,
};
if self.long_length_pos as usize == idx {
match self.long_length_type {
LongLengthType::LiteralLength => len.lit_length += 0x10000,
LongLengthType::MatchLength => len.match_length += 0x10000,
LongLengthType::None => {}
}
}
len
}
#[inline(always)]
pub(crate) fn store_seq_only(&mut self, lit_length: usize, off_base: u32, match_length: usize) {
let n = self.nb_seq;
assert!(n < self.max_nb_seq);
debug_assert!(lit_length <= crate::enc::params::BLOCKSIZE_MAX);
if lit_length > 0xFFFF {
debug_assert!(self.long_length_type == LongLengthType::None);
self.long_length_type = LongLengthType::LiteralLength;
self.long_length_pos = n as u32;
}
debug_assert!(match_length <= crate::enc::params::BLOCKSIZE_MAX);
debug_assert!(match_length >= MINMATCH as usize);
let ml_base = match_length - MINMATCH as usize;
if ml_base > 0xFFFF {
debug_assert!(self.long_length_type == LongLengthType::None);
self.long_length_type = LongLengthType::MatchLength;
self.long_length_pos = n as u32;
}
self.sequences[n] = SeqDef {
off_base,
lit_length: lit_length as u16,
ml_base: ml_base as u16,
};
self.nb_seq = n + 1;
}
#[inline]
pub(crate) fn reserve(&self, len: usize) {
assert!(self.max_nb_seq <= self.sequences.len());
assert!(self.nb_seq + len / 4 <= self.max_nb_seq);
assert!(self.max_nb_lit + WILDCOPY_OVERLENGTH <= self.lit_buf.len());
assert!(self.lit_len + len <= self.max_nb_lit);
}
#[allow(unsafe_code)]
#[inline(always)]
pub(crate) unsafe fn store_seq_unchecked(
&mut self,
src: &[u8],
lit_start: usize,
lit_length: usize,
lit_limit: usize,
off_base: u32,
match_length: usize,
) {
debug_assert!(lit_start + lit_length <= lit_limit && lit_limit <= src.len());
debug_assert!(self.lit_len + lit_length <= self.max_nb_lit);
debug_assert!(self.nb_seq < self.max_nb_seq && self.max_nb_seq <= self.sequences.len());
debug_assert!(match_length >= 4);
unsafe {
let ip = src.as_ptr().add(lit_start);
let op = self.lit_buf.as_mut_ptr().add(self.lit_len);
if lit_start + lit_length + WILDCOPY_OVERLENGTH <= lit_limit {
ptr::copy_nonoverlapping(ip, op, 16);
let mut i = 16;
while i < lit_length {
ptr::copy_nonoverlapping(ip.add(i), op.add(i), 16);
i += 16;
}
} else {
ptr::copy_nonoverlapping(ip, op, lit_length);
}
}
self.lit_len += lit_length;
let n = self.nb_seq;
if lit_length > 0xFFFF {
debug_assert!(self.long_length_type == LongLengthType::None);
self.long_length_type = LongLengthType::LiteralLength;
self.long_length_pos = n as u32;
}
let ml_base = match_length - MINMATCH as usize;
if ml_base > 0xFFFF {
debug_assert!(self.long_length_type == LongLengthType::None);
self.long_length_type = LongLengthType::MatchLength;
self.long_length_pos = n as u32;
}
unsafe {
*self.sequences.as_mut_ptr().add(n) = SeqDef {
off_base,
lit_length: lit_length as u16,
ml_base: ml_base as u16,
};
}
self.nb_seq = n + 1;
}
#[inline(always)]
pub(crate) fn store_seq(
&mut self,
src: &[u8],
lit_start: usize,
lit_length: usize,
lit_limit: usize,
off_base: u32,
match_length: usize,
) {
assert!(lit_start + lit_length <= lit_limit && lit_limit <= src.len());
assert!(self.lit_len + lit_length <= self.max_nb_lit);
#[allow(unsafe_code)]
unsafe {
let ip = src.as_ptr().add(lit_start);
let op = self.lit_buf.as_mut_ptr().add(self.lit_len);
if lit_start + lit_length + WILDCOPY_OVERLENGTH <= lit_limit {
ptr::copy_nonoverlapping(ip, op, 16);
let mut i = 16;
while i < lit_length {
ptr::copy_nonoverlapping(ip.add(i), op.add(i), 16);
i += 16;
}
} else {
ptr::copy_nonoverlapping(ip, op, lit_length);
}
}
self.lit_len += lit_length;
self.store_seq_only(lit_length, off_base, match_length);
}
#[inline]
pub(crate) fn store_last_literals(&mut self, literals: &[u8]) {
let end = self.lit_len + literals.len();
self.lit_buf[self.lit_len..end].copy_from_slice(literals);
self.lit_len = end;
}
}
#[inline(always)]
pub(crate) const fn highbit32(v: u32) -> u32 {
debug_assert!(v != 0);
31 - v.leading_zeros()
}
#[inline(always)]
pub(crate) fn ll_code(lit_length: u32) -> u32 {
#[rustfmt::skip]
const LL_CODE: [u8; 64] = [
0, 1, 2, 3, 4, 5, 6, 7,
8, 9, 10, 11, 12, 13, 14, 15,
16, 16, 17, 17, 18, 18, 19, 19,
20, 20, 20, 20, 21, 21, 21, 21,
22, 22, 22, 22, 22, 22, 22, 22,
23, 23, 23, 23, 23, 23, 23, 23,
24, 24, 24, 24, 24, 24, 24, 24,
24, 24, 24, 24, 24, 24, 24, 24,
];
const LL_DELTA_CODE: u32 = 19;
if lit_length > 63 {
highbit32(lit_length) + LL_DELTA_CODE
} else {
u32::from(LL_CODE[lit_length as usize])
}
}
#[inline(always)]
pub(crate) fn ml_code(ml_base: u32) -> u32 {
#[rustfmt::skip]
const ML_CODE: [u8; 128] = [
0, 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, 32, 33, 33, 34, 34, 35, 35, 36, 36, 36, 36, 37, 37, 37, 37,
38, 38, 38, 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, 39, 39, 39,
40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40,
41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41,
42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42,
42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42,
];
const ML_DELTA_CODE: u32 = 36;
if ml_base > 127 {
highbit32(ml_base) + ML_DELTA_CODE
} else {
u32::from(ML_CODE[ml_base as usize])
}
}
#[inline]
pub(crate) fn fill_codes(seqs: &[SeqDef], ll: &mut [u8], of: &mut [u8], ml: &mut [u8]) -> bool {
#[cfg(all(target_arch = "x86_64", not(feature = "portable")))]
if crate::cpu::Features::detect().bmi2 {
#[allow(unsafe_code)]
return unsafe { fill_codes_lzcnt(seqs, ll, of, ml) };
}
fill_codes_body(seqs, ll, of, ml)
}
#[cfg(all(target_arch = "x86_64", not(feature = "portable")))]
#[target_feature(enable = "bmi1,bmi2,lzcnt")]
fn fill_codes_lzcnt(seqs: &[SeqDef], ll: &mut [u8], of: &mut [u8], ml: &mut [u8]) -> bool {
fill_codes_body(seqs, ll, of, ml)
}
#[inline(always)]
fn fill_codes_body(seqs: &[SeqDef], ll: &mut [u8], of: &mut [u8], ml: &mut [u8]) -> bool {
debug_assert!(ll.len() == seqs.len() && of.len() == seqs.len() && ml.len() == seqs.len());
let mut long_offsets = false;
for (((s, ll), of), ml) in seqs.iter().zip(ll).zip(of).zip(ml) {
let of_code = highbit32(s.off_base);
*ll = ll_code(u32::from(s.lit_length)) as u8;
*of = of_code as u8;
*ml = ml_code(u32::from(s.ml_base)) as u8;
debug_assert!(!(usize::BITS == 64 && of_code >= STREAM_ACCUMULATOR_MIN));
if usize::BITS == 32 && of_code >= STREAM_ACCUMULATOR_MIN {
long_offsets = true;
}
}
long_offsets
}
pub(crate) fn seq_to_codes(ss: &mut SeqStore) -> bool {
let nb_seq = ss.nb_seq;
debug_assert!(nb_seq <= ss.max_nb_seq);
let long_offsets = fill_codes(
&ss.sequences[..nb_seq],
&mut ss.ll_code[..nb_seq],
&mut ss.of_code[..nb_seq],
&mut ss.ml_code[..nb_seq],
);
match ss.long_length_type {
LongLengthType::LiteralLength => ss.ll_code[ss.long_length_pos as usize] = MAX_LL,
LongLengthType::MatchLength => ss.ml_code[ss.long_length_pos as usize] = MAX_ML,
LongLengthType::None => {}
}
long_offsets
}
#[allow(unsafe_code)]
#[inline(always)]
pub(crate) unsafe fn read32(p: *const u8) -> u32 {
unsafe { ptr::read_unaligned(p.cast::<u32>()) }
}
#[allow(unsafe_code)]
#[inline(always)]
pub(crate) unsafe fn read_le32(p: *const u8) -> u32 {
u32::from_le(unsafe { read32(p) })
}
#[allow(unsafe_code)]
#[inline(always)]
pub(crate) unsafe fn read_le64(p: *const u8) -> u64 {
u64::from_le(unsafe { ptr::read_unaligned(p.cast::<u64>()) })
}
#[allow(unsafe_code)]
#[inline(always)]
unsafe fn read_le_word(p: *const u8) -> usize {
usize::from_le(unsafe { ptr::read_unaligned(p.cast::<usize>()) })
}
#[allow(unsafe_code)]
#[inline(always)]
pub(crate) unsafe fn count(p_in: *const u8, p_match: *const u8, p_in_limit: *const u8) -> usize {
const W: usize = size_of::<usize>();
let len = (p_in_limit as usize).saturating_sub(p_in as usize);
let mut i = 0;
unsafe {
while i + W <= len {
let diff = read_le_word(p_match.add(i)) ^ read_le_word(p_in.add(i));
if diff != 0 {
return i + (diff.trailing_zeros() >> 3) as usize;
}
i += W;
}
if W == 8 && i + 4 <= len && read32(p_match.add(i)) == read32(p_in.add(i)) {
i += 4;
}
if i + 2 <= len
&& ptr::read_unaligned(p_match.add(i).cast::<u16>())
== ptr::read_unaligned(p_in.add(i).cast::<u16>())
{
i += 2;
}
if i < len && *p_match.add(i) == *p_in.add(i) {
i += 1;
}
}
i
}
#[allow(unsafe_code)]
#[inline(always)]
pub(crate) unsafe fn count_2segments(
ip: *const u8,
mtch: *const u8,
i_end: *const u8,
m_end: *const u8,
i_start: *const u8,
) -> usize {
let v_end = ip.wrapping_add(m_end as usize - mtch as usize);
let v_end = if (v_end as usize) < (i_end as usize) {
v_end
} else {
i_end
};
unsafe {
let match_length = count(ip, mtch, v_end);
if mtch.wrapping_add(match_length) != m_end {
return match_length;
}
match_length + count(ip.add(match_length), i_start, i_end)
}
}
pub(crate) const PRIME3BYTES: u32 = 506832829;
pub(crate) const PRIME4BYTES: u32 = 2654435761;
pub(crate) const PRIME5BYTES: u64 = 889523592379;
pub(crate) const PRIME6BYTES: u64 = 227718039650203;
pub(crate) const PRIME7BYTES: u64 = 58295818150454627;
pub(crate) const PRIME8BYTES: u64 = 0xCF1BBCDCB7A56463;
#[inline(always)]
pub(crate) const fn hash3(u: u32, h: u32) -> usize {
((u << (32 - 24)).wrapping_mul(PRIME3BYTES) >> (32 - h)) as usize
}
#[inline(always)]
pub(crate) const fn hash4(u: u32, h: u32) -> usize {
(u.wrapping_mul(PRIME4BYTES) >> (32 - h)) as usize
}
#[inline(always)]
pub(crate) const fn hash5(u: u64, h: u32) -> usize {
((u << (64 - 40)).wrapping_mul(PRIME5BYTES) >> (64 - h)) as usize
}
#[inline(always)]
pub(crate) const fn hash6(u: u64, h: u32) -> usize {
((u << (64 - 48)).wrapping_mul(PRIME6BYTES) >> (64 - h)) as usize
}
#[inline(always)]
pub(crate) const fn hash7(u: u64, h: u32) -> usize {
((u << (64 - 56)).wrapping_mul(PRIME7BYTES) >> (64 - h)) as usize
}
#[inline(always)]
pub(crate) const fn hash8(u: u64, h: u32) -> usize {
(u.wrapping_mul(PRIME8BYTES) >> (64 - h)) as usize
}
#[allow(unsafe_code)]
#[inline(always)]
pub(crate) unsafe fn hash3_ptr(p: *const u8, h: u32) -> usize {
hash3(unsafe { read_le32(p) }, h)
}
#[allow(unsafe_code)]
#[inline(always)]
pub(crate) unsafe fn hash4_ptr(p: *const u8, h: u32) -> usize {
hash4(unsafe { read_le32(p) }, h)
}
#[allow(unsafe_code)]
#[inline(always)]
pub(crate) unsafe fn hash5_ptr(p: *const u8, h: u32) -> usize {
hash5(unsafe { read_le64(p) }, h)
}
#[allow(unsafe_code)]
#[inline(always)]
pub(crate) unsafe fn hash6_ptr(p: *const u8, h: u32) -> usize {
hash6(unsafe { read_le64(p) }, h)
}
#[allow(unsafe_code)]
#[inline(always)]
pub(crate) unsafe fn hash7_ptr(p: *const u8, h: u32) -> usize {
hash7(unsafe { read_le64(p) }, h)
}
#[allow(unsafe_code)]
#[inline(always)]
pub(crate) unsafe fn hash8_ptr(p: *const u8, h: u32) -> usize {
hash8(unsafe { read_le64(p) }, h)
}
#[allow(unsafe_code)]
#[inline(always)]
pub(crate) unsafe fn hash_ptr(p: *const u8, h_bits: u32, mls: u32) -> usize {
debug_assert!(h_bits <= 32);
unsafe {
match mls {
5 => hash5_ptr(p, h_bits),
6 => hash6_ptr(p, h_bits),
7 => hash7_ptr(p, h_bits),
8 => hash8_ptr(p, h_bits),
_ => hash4_ptr(p, h_bits),
}
}
}
pub(crate) fn hash_at(src: &[u8], pos: usize, h_bits: u32, mls: u32) -> usize {
assert!(pos + 8 <= src.len());
#[allow(unsafe_code)]
unsafe {
hash_ptr(src.as_ptr().add(pos), h_bits, mls)
}
}
pub(crate) fn count_at(src: &[u8], ip: usize, mtch: usize, limit: usize) -> usize {
assert!(mtch <= ip && limit <= src.len());
#[allow(unsafe_code)]
unsafe {
let p = src.as_ptr();
count(p.add(ip.min(limit)), p.add(mtch.min(limit)), p.add(limit))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn codes() {
assert_eq!(ll_code(0), 0);
assert_eq!(ll_code(15), 15);
assert_eq!(ll_code(16), 16);
assert_eq!(ll_code(17), 16);
assert_eq!(ll_code(63), 24);
assert_eq!(ll_code(64), 25); assert_eq!(ll_code(65535), 34);
assert_eq!(ll_code(65536), 35);
assert_eq!(ml_code(0), 0);
assert_eq!(ml_code(31), 31);
assert_eq!(ml_code(32), 32);
assert_eq!(ml_code(127), 42);
assert_eq!(ml_code(128), 43); assert_eq!(ml_code(65535), 51);
assert_eq!(ml_code(65536), 52);
}
#[test]
fn offbase() {
assert_eq!(offset_to_offbase(1), 4);
assert_eq!(repcode_to_offbase(2), 2);
assert!(offbase_is_repcode(REPCODE1_TO_OFFBASE));
assert!(offbase_is_offset(4));
assert_eq!(offbase_to_offset(10), 7);
let mut rep = REP_START_VALUE;
update_rep(&mut rep, offset_to_offbase(100), false);
assert_eq!(rep, [100, 1, 4]);
update_rep(&mut rep, 1, false); assert_eq!(rep, [100, 1, 4]);
update_rep(&mut rep, 1, true); assert_eq!(rep, [1, 100, 4]);
update_rep(&mut rep, 3, true); assert_eq!(rep, [0, 1, 100]);
update_rep(&mut rep, 2, false); assert_eq!(rep, [1, 0, 100]);
}
#[test]
fn store_and_codes() {
let src: Vec<u8> = (0..200u8).collect();
let mut ss = SeqStore::new(1 << 17, 4);
ss.store_seq(&src, 0, 10, 200, offset_to_offbase(1), 4);
ss.store_seq(&src, 10, 0, 200, REPCODE1_TO_OFFBASE, 3 + 130);
ss.store_seq(&src, 180, 15, 200, offset_to_offbase(1000), 5);
ss.store_last_literals(&src[195..]);
assert_eq!(ss.nb_seq, 3);
let mut lits = src[..10].to_vec();
lits.extend_from_slice(&src[180..]);
assert_eq!(ss.literals(), &lits[..]);
assert!(!seq_to_codes(&mut ss));
assert_eq!(&ss.ll_code[..3], &[10, 0, 15]);
assert_eq!(&ss.ml_code[..3], &[1, 43, 2]);
assert_eq!(&ss.of_code[..3], &[2, 0, 9]);
assert_eq!(ss.sequence_length(1).match_length, 133);
ss.reset();
assert_eq!(ss.nb_seq, 0);
assert_eq!(ss.lit_len, 0);
}
#[test]
fn long_lengths() {
let src = vec![7u8; 140_000];
let mut ss = SeqStore::new(1 << 17, 4);
ss.store_seq(&src, 0, 5, src.len(), offset_to_offbase(8), 600);
ss.store_seq(&src, 0, 0x10005, src.len(), offset_to_offbase(8), 4);
assert_eq!(ss.long_length_type, LongLengthType::LiteralLength);
assert_eq!(ss.long_length_pos, 1);
assert_eq!(ss.sequence_length(1).lit_length, 0x10005);
assert_eq!(ss.sequences[1].lit_length, 5);
assert_eq!(&ss.literals()[5..5 + 0x10005], &src[..0x10005]);
seq_to_codes(&mut ss);
assert_eq!(ss.ll_code[1], MAX_LL);
assert_eq!(ss.ll_code[0], 5);
assert_eq!(ss.ml_code[0], ml_code(600 - 3) as u8);
let mut ss = SeqStore::new(1 << 17, 4);
ss.store_seq(&src, 0, 1, src.len(), offset_to_offbase(8), 70_000);
assert_eq!(ss.long_length_type, LongLengthType::MatchLength);
assert_eq!(ss.sequence_length(0).match_length, 70_000);
seq_to_codes(&mut ss);
assert_eq!(ss.ml_code[0], MAX_ML);
}
#[test]
fn counting() {
let mut a = vec![0u8; 64];
for (i, b) in a.iter_mut().enumerate() {
*b = (i % 7) as u8;
}
for ip in 7..64 {
for limit in ip..=64 {
assert_eq!(count_at(&a, ip, ip - 7, limit), limit - ip, "{ip} {limit}");
}
}
a[40] = 99;
assert_eq!(count_at(&a, 20, 13, 64), 20);
assert_eq!(count_at(&a, 20, 19, 64), 0);
assert_eq!(count_at(&a, 64, 10, 64), 0);
}
#[test]
fn count_two_segments() {
let dict = b"xxxxabcdefgh";
let prefix = b"ijklmnopabcdefghijklmnopq";
#[allow(unsafe_code)]
let n = unsafe {
count_2segments(
prefix.as_ptr().add(8),
dict.as_ptr().add(4),
prefix.as_ptr().add(prefix.len()),
dict.as_ptr().add(dict.len()),
prefix.as_ptr(),
)
};
assert_eq!(n, 16);
}
#[test]
fn hashes() {
let data = *b"\x01\x02\x03\x04\x05\x06\x07\x08";
let u32v = u32::from_le_bytes([1, 2, 3, 4]);
let u64v = u64::from_le_bytes(data);
assert_eq!(
hash_at(&data, 0, 17, 4),
(u32v.wrapping_mul(2654435761) >> 15) as usize
);
assert_eq!(hash_at(&data, 0, 17, 3), hash_at(&data, 0, 17, 4));
let h5 = ((u64v << 24).wrapping_mul(889523592379) >> 47) as usize;
assert_eq!(hash_at(&data, 0, 17, 5), h5);
let h8 = (u64v.wrapping_mul(0xCF1BBCDCB7A56463) >> 47) as usize;
assert_eq!(hash_at(&data, 0, 17, 8), h8);
let mut d2 = data;
d2[6] = 0xFF;
assert_eq!(hash_at(&data, 0, 20, 5), hash_at(&d2, 0, 20, 5));
assert_eq!(hash_at(&data, 0, 20, 6), hash_at(&d2, 0, 20, 6));
assert_ne!(hash_at(&data, 0, 20, 7), hash_at(&d2, 0, 20, 7));
for mls in 4..=8 {
assert!(hash_at(&data, 0, 10, mls) < 1024);
}
}
}