use super::fast::{
DmsView, ExtWindow, K_SEARCH_STRENGTH, MatchState, hash_mls, index_overlap_check,
};
use super::params::Strategy;
use super::row::{self, ROW_HASH_CACHE_SIZE};
use super::seq_store::{
REP_NUM, REPCODE1_TO_OFFBASE, SeqStore, count, count_2segments, highbit32, read32,
};
pub(crate) const DUBT_UNSORTED_MARK: u32 = 1;
const LAZY_SKIPPING_STEP: usize = 8;
const OFFBASE_UNSET: u32 = 999_999_999;
pub(super) const SEARCH_HASH_CHAIN: u8 = 0;
pub(super) const SEARCH_BINARY_TREE: u8 = 1;
pub(super) const SEARCH_ROW_HASH: u8 = 2;
pub(super) struct Searcher {
pub(super) base: *const u8,
pub(super) hash_table: *mut u32,
pub(super) chain_table: *mut u32,
pub(super) tag_table: *mut u8,
pub(super) hash_log: u32,
pub(super) chain_log: u32,
pub(super) row_hash_log: u32,
pub(super) search_log: u32,
pub(super) window_log: u32,
pub(super) low_limit: u32,
pub(super) loaded_dict_end: u32,
pub(super) hash_salt: u64,
pub(super) next_to_update: u32,
pub(super) lazy_skipping: bool,
pub(super) hash_cache: [u32; ROW_HASH_CACHE_SIZE],
pub(super) hash_salt_entropy: u32,
pub(super) lowest: *const u8,
pub(super) iend: *const u8,
pub(super) dict_base: *const u8,
pub(super) dict_limit: u32,
pub(super) dict_lowest: *const u8,
pub(super) dict_end: *const u8,
pub(super) prefix_start: *const u8,
pub(super) dms: DmsSearch,
}
#[derive(Clone, Copy)]
pub(super) struct DmsSearch {
pub(super) hash_table: *const u32,
pub(super) chain_table: *const u32,
pub(super) tag_table: *const u8,
pub(super) hash_log: u32,
pub(super) chain_log: u32,
pub(super) row_hash_log: u32,
pub(super) dict_limit: u32,
pub(super) low_limit: u32,
pub(super) high_limit: u32,
}
impl DmsSearch {
pub(super) const NONE: DmsSearch = DmsSearch {
hash_table: core::ptr::null(),
chain_table: core::ptr::null(),
tag_table: core::ptr::null(),
hash_log: 0,
chain_log: 0,
row_hash_log: 0,
dict_limit: 0,
low_limit: 0,
high_limit: 0,
};
fn new(d: &DmsView) -> Self {
DmsSearch {
hash_table: d.hash_table,
chain_table: d.chain_table,
tag_table: d.tag_table,
hash_log: d.hash_log,
chain_log: d.chain_log,
row_hash_log: d.row_hash_log,
dict_limit: d.dict_limit,
low_limit: d.low_limit,
high_limit: d.high_limit,
}
}
}
impl Searcher {
#[inline(always)]
pub(super) fn idx(&self, p: *const u8) -> u32 {
(p as usize).wrapping_sub(self.base as usize) as u32
}
#[inline(always)]
pub(super) fn pos(&self, i: u32) -> *const u8 {
self.base.wrapping_add(i as usize)
}
#[inline(always)]
pub(super) fn chk(&self, p: *const u8, n: usize) {
let (p, e) = (p as usize, (p as usize).wrapping_add(n));
debug_assert!(
(p >= self.lowest as usize && e <= self.iend as usize)
|| (p >= self.dict_lowest as usize && e <= self.dict_end as usize),
"read out of the window"
);
}
#[allow(unsafe_code)]
#[inline(always)]
pub(super) unsafe fn tree_count<const EXT: bool>(
&self,
ip: *const u8,
in_dict: bool,
match_index: u32,
match_length: usize,
iend: *const u8,
) -> (usize, *const u8) {
unsafe {
if !EXT {
let m = self.pos(match_index);
self.chk(m, 0);
let ml = match_length + count(ip.add(match_length), m.add(match_length), iend);
return (ml, m.add(ml));
}
let in_prefix = match_index as usize + match_length >= self.dict_limit as usize;
if in_prefix || in_dict {
let mp = if in_prefix { self.base } else { self.dict_base }
.wrapping_add(match_index as usize + match_length);
self.chk(mp, 0);
let n = count(ip.add(match_length), mp, iend);
(match_length + n, mp.add(n))
} else {
let dm = self.dict_base.wrapping_add(match_index as usize);
let ml =
match_length + self.count_ext(ip.add(match_length), dm.add(match_length), iend);
let m_ml = if match_index as usize + ml >= self.dict_limit as usize {
self.base.wrapping_add(match_index as usize + ml)
} else {
dm.add(ml)
};
(ml, m_ml)
}
}
}
#[inline(always)]
pub(super) fn ext_pos(&self, i: u32) -> *const u8 {
if i < self.dict_limit {
self.dict_base.wrapping_add(i as usize)
} else {
self.base.wrapping_add(i as usize)
}
}
#[allow(unsafe_code)]
#[inline(always)]
pub(super) unsafe fn count_ext(&self, ip: *const u8, m: *const u8, ilimit: *const u8) -> usize {
self.chk(m, 0);
unsafe { count_2segments(ip, m, ilimit, self.dict_end, self.prefix_start) }
}
#[inline(always)]
pub(super) fn lowest_match_index(&self, curr: u32) -> u32 {
let max_distance = 1u32 << self.window_log;
let lowest_valid = self.low_limit;
let within_window = if curr.wrapping_sub(lowest_valid) > max_distance {
curr - max_distance
} else {
lowest_valid
};
if self.loaded_dict_end != 0 {
lowest_valid
} else {
within_window
}
}
}
#[allow(unsafe_code)]
#[inline(always)]
unsafe fn insert_and_find_first_index<const MLS: u32>(s: &mut Searcher, ip: *const u8) -> u32 {
let hash_log = s.hash_log;
let chain_mask = (1u32 << s.chain_log) - 1;
let target = s.idx(ip);
let mut idx = s.next_to_update;
unsafe {
while idx < target {
let p = s.pos(idx);
s.chk(p, 8);
let h = hash_mls::<MLS>(p, hash_log);
*s.chain_table.add((idx & chain_mask) as usize) = *s.hash_table.add(h);
*s.hash_table.add(h) = idx;
idx += 1;
if s.lazy_skipping {
break;
}
}
s.next_to_update = target;
s.chk(ip, 8);
*s.hash_table.add(hash_mls::<MLS>(ip, hash_log))
}
}
#[allow(unsafe_code)]
#[inline(always)]
unsafe fn hc_find_best_match<const MLS: u32, const EXT: bool, const DMS: bool>(
s: &mut Searcher,
ip: *const u8,
ilimit: *const u8,
offset_ptr: &mut u32,
) -> usize {
let chain_size = 1u32 << s.chain_log;
let chain_mask = chain_size - 1;
let curr = s.idx(ip);
let low_limit = s.lowest_match_index(curr);
let min_chain = curr.saturating_sub(chain_size);
let mut nb_attempts = 1u32 << s.search_log;
let mut ml = 4 - 1;
unsafe {
let mut match_index = insert_and_find_first_index::<MLS>(s, ip);
while match_index >= low_limit && nb_attempts > 0 {
let mut current_ml = 0;
debug_assert!(match_index < curr);
if !EXT || match_index >= s.dict_limit {
let m = s.pos(match_index);
s.chk(m.wrapping_add(ml - 3), 4);
if read32(m.add(ml - 3)) == read32(ip.add(ml - 3)) {
current_ml = count(ip, m, ilimit);
}
} else {
let m = s.dict_base.wrapping_add(match_index as usize);
s.chk(m, 4);
if read32(m) == read32(ip) {
current_ml = s.count_ext(ip.add(4), m.add(4), ilimit) + 4;
}
}
if current_ml > ml {
ml = current_ml;
*offset_ptr = curr - match_index + REP_NUM;
if ip.add(current_ml) == ilimit {
break; }
}
if match_index <= min_chain {
break;
}
match_index = *s.chain_table.add((match_index & chain_mask) as usize);
nb_attempts -= 1;
}
if DMS {
let d = s.dms;
let dms_chain_size = 1u32 << d.chain_log;
let dms_chain_mask = dms_chain_size - 1;
let dms_lowest_index = d.dict_limit;
let dms_base = s.dict_base;
let dms_size = d.high_limit;
let dms_index_delta = s.dict_limit - dms_size;
let dms_min_chain = dms_size.saturating_sub(dms_chain_size);
let mut match_index = *d.hash_table.add(hash_mls::<MLS>(ip, d.hash_log));
while match_index >= dms_lowest_index && nb_attempts > 0 {
let mut current_ml = 0;
let m = dms_base.wrapping_add(match_index as usize);
s.chk(m, 4);
if read32(m) == read32(ip) {
current_ml = s.count_ext(ip.add(4), m.add(4), ilimit) + 4;
}
if current_ml > ml {
ml = current_ml;
debug_assert!(curr > match_index + dms_index_delta);
*offset_ptr = curr - (match_index + dms_index_delta) + REP_NUM;
if ip.add(current_ml) == ilimit {
break; }
}
if match_index <= dms_min_chain {
break;
}
match_index = *d.chain_table.add((match_index & dms_chain_mask) as usize);
nb_attempts -= 1;
}
}
}
ml
}
#[allow(unsafe_code)]
#[inline(always)]
unsafe fn update_dubt<const MLS: u32>(s: &mut Searcher, ip: *const u8) {
let hash_log = s.hash_log;
let bt = s.chain_table;
let bt_mask = (1u32 << (s.chain_log - 1)) - 1;
let target = s.idx(ip);
unsafe {
for idx in s.next_to_update..target {
let p = s.pos(idx);
s.chk(p, 8);
let h = hash_mls::<MLS>(p, hash_log);
let match_index = *s.hash_table.add(h);
let next_candidate = bt.add(2 * (idx & bt_mask) as usize);
*s.hash_table.add(h) = idx; *next_candidate = match_index; *next_candidate.add(1) = DUBT_UNSORTED_MARK;
}
}
s.next_to_update = target;
}
#[allow(unsafe_code)]
#[inline(always)]
unsafe fn insert_dubt1<const EXT: bool>(
s: &Searcher,
curr: u32,
input_end: *const u8,
mut nb_compares: u32,
bt_low: u32,
) {
let bt = s.chain_table;
let bt_mask = (1u32 << (s.chain_log - 1)) - 1;
let mut common_length_smaller = 0usize;
let mut common_length_larger = 0usize;
let ip = if EXT { s.ext_pos(curr) } else { s.pos(curr) };
let iend = if !EXT || curr >= s.dict_limit {
input_end
} else {
s.dict_end
};
let mut dummy32 = 0u32;
let dummy: *mut u32 = &mut dummy32;
let window_valid = s.low_limit;
let max_distance = 1u32 << s.window_log;
let window_low = if curr.wrapping_sub(window_valid) > max_distance {
curr - max_distance
} else {
window_valid
};
debug_assert!(curr >= bt_low);
debug_assert!((ip as usize) < (iend as usize));
unsafe {
let mut smaller_ptr = bt.add(2 * (curr & bt_mask) as usize);
let mut larger_ptr = smaller_ptr.add(1);
let mut match_index = *smaller_ptr;
while nb_compares > 0 && match_index > window_low {
let next_ptr = bt.add(2 * (match_index & bt_mask) as usize);
let mut match_length = common_length_smaller.min(common_length_larger);
debug_assert!(match_index < curr);
let m_ml;
(match_length, m_ml) = s.tree_count::<EXT>(
ip,
EXT && curr < s.dict_limit,
match_index,
match_length,
iend,
);
if ip.add(match_length) == iend {
break;
}
if *m_ml < *ip.add(match_length) {
*smaller_ptr = match_index;
common_length_smaller = match_length;
if match_index <= bt_low {
smaller_ptr = dummy;
break;
}
smaller_ptr = next_ptr.add(1);
match_index = *next_ptr.add(1);
} else {
*larger_ptr = match_index;
common_length_larger = match_length;
if match_index <= bt_low {
larger_ptr = dummy;
break;
}
larger_ptr = next_ptr;
match_index = *next_ptr;
}
nb_compares -= 1;
}
*smaller_ptr = 0;
*larger_ptr = 0;
}
}
#[allow(unsafe_code)]
#[inline(always)]
unsafe fn dubt_find_best_match<const MLS: u32, const EXT: bool, const DMS: bool>(
s: &mut Searcher,
ip: *const u8,
iend: *const u8,
off_base_ptr: &mut u32,
) -> usize {
let bt = s.chain_table;
let bt_mask = (1u32 << (s.chain_log - 1)) - 1;
let curr = s.idx(ip);
let window_low = s.lowest_match_index(curr);
let bt_low = curr.saturating_sub(bt_mask);
let unsort_limit = bt_low.max(window_low);
let mut nb_compares = 1u32 << s.search_log;
let mut nb_candidates = nb_compares;
let mut previous_candidate = 0u32;
unsafe {
s.chk(ip, 8);
let h = hash_mls::<MLS>(ip, s.hash_log);
let mut match_index = *s.hash_table.add(h);
let mut next_candidate = bt.add(2 * (match_index & bt_mask) as usize);
let mut unsorted_mark = next_candidate.add(1);
while match_index > unsort_limit
&& *unsorted_mark == DUBT_UNSORTED_MARK
&& nb_candidates > 1
{
*unsorted_mark = previous_candidate;
previous_candidate = match_index;
match_index = *next_candidate;
next_candidate = bt.add(2 * (match_index & bt_mask) as usize);
unsorted_mark = next_candidate.add(1);
nb_candidates -= 1;
}
if match_index > unsort_limit && *unsorted_mark == DUBT_UNSORTED_MARK {
*next_candidate = 0;
*unsorted_mark = 0;
}
match_index = previous_candidate;
while match_index != 0 {
let next_candidate_idx = *bt.add(2 * (match_index & bt_mask) as usize + 1);
insert_dubt1::<EXT>(s, match_index, iend, nb_candidates, unsort_limit);
match_index = next_candidate_idx;
nb_candidates += 1;
}
let mut common_length_smaller = 0usize;
let mut common_length_larger = 0usize;
let mut smaller_ptr = bt.add(2 * (curr & bt_mask) as usize);
let mut larger_ptr = smaller_ptr.add(1);
let mut match_end_idx = curr + 8 + 1;
let mut dummy32 = 0u32;
let dummy: *mut u32 = &mut dummy32;
let mut best_length = 0usize;
match_index = *s.hash_table.add(h);
*s.hash_table.add(h) = curr;
while nb_compares > 0 && match_index > window_low {
let next_ptr = bt.add(2 * (match_index & bt_mask) as usize);
let mut match_length = common_length_smaller.min(common_length_larger);
debug_assert!(match_index < curr);
let m_ml;
(match_length, m_ml) = s.tree_count::<EXT>(ip, false, match_index, match_length, iend);
if match_length > best_length {
if match_length > (match_end_idx - match_index) as usize {
match_end_idx = match_index + match_length as u32;
}
if 4 * (match_length - best_length) as i32
> highbit32(curr - match_index + 1) as i32 - highbit32(*off_base_ptr) as i32
{
best_length = match_length;
*off_base_ptr = curr - match_index + REP_NUM;
}
if ip.add(match_length) == iend {
if DMS {
nb_compares = 0;
}
break;
}
}
if *m_ml < *ip.add(match_length) {
*smaller_ptr = match_index;
common_length_smaller = match_length;
if match_index <= bt_low {
smaller_ptr = dummy;
break;
}
smaller_ptr = next_ptr.add(1);
match_index = *next_ptr.add(1);
} else {
*larger_ptr = match_index;
common_length_larger = match_length;
if match_index <= bt_low {
larger_ptr = dummy;
break;
}
larger_ptr = next_ptr;
match_index = *next_ptr;
}
nb_compares -= 1;
}
*smaller_ptr = 0;
*larger_ptr = 0;
if DMS && nb_compares > 0 {
best_length = dubt_find_better_dict_match::<MLS>(
s,
ip,
iend,
off_base_ptr,
best_length,
nb_compares,
);
}
debug_assert!(match_end_idx > curr + 8);
s.next_to_update = match_end_idx - 8; best_length
}
}
#[allow(unsafe_code)]
#[inline(always)]
unsafe fn bt_find_best_match<const MLS: u32, const EXT: bool, const DMS: bool>(
s: &mut Searcher,
ip: *const u8,
iend: *const u8,
off_base_ptr: &mut u32,
) -> usize {
if (ip as usize) < (s.pos(s.next_to_update) as usize) {
return 0; }
unsafe {
update_dubt::<MLS>(s, ip);
dubt_find_best_match::<MLS, EXT, DMS>(s, ip, iend, off_base_ptr)
}
}
#[allow(unsafe_code)]
#[inline(always)]
unsafe fn dubt_find_better_dict_match<const MLS: u32>(
s: &Searcher,
ip: *const u8,
iend: *const u8,
offset_ptr: &mut u32,
mut best_length: usize,
mut nb_compares: u32,
) -> usize {
let d = s.dms;
let base = s.base;
let curr = s.idx(ip);
let dict_base = s.dict_base;
let dict_high_limit = d.high_limit;
let dict_low_limit = d.low_limit;
let dict_index_delta = s.low_limit.wrapping_sub(dict_high_limit);
let dict_bt = d.chain_table;
let bt_log = d.chain_log - 1;
let bt_mask = (1u32 << bt_log) - 1;
let bt_low = if bt_mask >= dict_high_limit - dict_low_limit {
dict_low_limit
} else {
dict_high_limit - bt_mask
};
let mut common_length_smaller = 0usize;
let mut common_length_larger = 0usize;
unsafe {
s.chk(ip, 8);
let h = hash_mls::<MLS>(ip, d.hash_log);
let mut dict_match_index = *d.hash_table.add(h);
while nb_compares > 0 && dict_match_index > dict_low_limit {
let next_ptr = dict_bt.add(2 * (dict_match_index & bt_mask) as usize);
let mut match_length = common_length_smaller.min(common_length_larger);
let mut m = dict_base.wrapping_add(dict_match_index as usize);
s.chk(m, 0);
if m.wrapping_add(match_length) as usize <= s.dict_end as usize {
match_length += s.count_ext(ip.add(match_length), m.add(match_length), iend);
}
if dict_match_index as usize + match_length >= dict_high_limit as usize {
m = base.wrapping_add(dict_match_index.wrapping_add(dict_index_delta) as usize);
}
if match_length > best_length {
let match_index = dict_match_index.wrapping_add(dict_index_delta);
if 4 * (match_length - best_length) as i32
> highbit32(curr - match_index + 1) as i32
- highbit32(offset_ptr.wrapping_add(1)) as i32
{
best_length = match_length;
*offset_ptr = curr - match_index + REP_NUM;
}
if ip.add(match_length) == iend {
break;
}
}
let mm = m.wrapping_add(match_length);
s.chk(mm, 1);
if *mm < *ip.add(match_length) {
if dict_match_index <= bt_low {
break; }
common_length_smaller = match_length;
dict_match_index = *next_ptr.add(1);
} else {
if dict_match_index <= bt_low {
break; }
common_length_larger = match_length;
dict_match_index = *next_ptr;
}
nb_compares -= 1;
}
}
best_length
}
#[allow(unsafe_code)]
#[inline(always)]
unsafe fn search_max<
const MLS: u32,
const METHOD: u8,
const ROW_LOG: u32,
const EXT: bool,
const DMS: bool,
const BMI2: bool,
>(
s: &mut Searcher,
ip: *const u8,
iend: *const u8,
offset_ptr: &mut u32,
) -> usize {
#[cfg(all(target_arch = "x86_64", not(feature = "portable")))]
if BMI2 && METHOD == SEARCH_ROW_HASH && ROW_LOG != 6 && !EXT && !DMS {
return unsafe { search_body::<MLS, METHOD, ROW_LOG, EXT, DMS>(s, ip, iend, offset_ptr) };
}
#[cfg(all(target_arch = "x86_64", not(feature = "portable")))]
if BMI2 && !(METHOD == SEARCH_ROW_HASH && ROW_LOG == 6) {
return unsafe { search_bmi2::<MLS, METHOD, ROW_LOG, EXT, DMS>(s, ip, iend, offset_ptr) };
}
unsafe { search_generic::<MLS, METHOD, ROW_LOG, EXT, DMS>(s, ip, iend, offset_ptr) }
}
#[allow(unsafe_code)]
#[inline(never)]
unsafe fn search_generic<
const MLS: u32,
const METHOD: u8,
const ROW_LOG: u32,
const EXT: bool,
const DMS: bool,
>(
s: &mut Searcher,
ip: *const u8,
iend: *const u8,
offset_ptr: &mut u32,
) -> usize {
unsafe { search_body::<MLS, METHOD, ROW_LOG, EXT, DMS>(s, ip, iend, offset_ptr) }
}
#[cfg(all(target_arch = "x86_64", not(feature = "portable")))]
#[allow(unsafe_code)]
#[inline(never)]
#[target_feature(enable = "bmi1,bmi2,lzcnt")]
unsafe fn search_bmi2<
const MLS: u32,
const METHOD: u8,
const ROW_LOG: u32,
const EXT: bool,
const DMS: bool,
>(
s: &mut Searcher,
ip: *const u8,
iend: *const u8,
offset_ptr: &mut u32,
) -> usize {
unsafe { search_body::<MLS, METHOD, ROW_LOG, EXT, DMS>(s, ip, iend, offset_ptr) }
}
#[allow(unsafe_code)]
#[inline(always)]
unsafe fn search_body<
const MLS: u32,
const METHOD: u8,
const ROW_LOG: u32,
const EXT: bool,
const DMS: bool,
>(
s: &mut Searcher,
ip: *const u8,
iend: *const u8,
offset_ptr: &mut u32,
) -> usize {
unsafe {
match METHOD {
SEARCH_HASH_CHAIN => hc_find_best_match::<MLS, EXT, DMS>(s, ip, iend, offset_ptr),
SEARCH_BINARY_TREE => bt_find_best_match::<MLS, EXT, DMS>(s, ip, iend, offset_ptr),
_ => row::row_find_best_match::<MLS, ROW_LOG, EXT, DMS>(s, ip, iend, offset_ptr),
}
}
}
#[cfg(all(target_arch = "x86_64", not(feature = "portable")))]
fn bmi2_pays(ms: &MatchState) -> bool {
ms.use_row_match_finder || ms.cparams.strategy == Strategy::BtLazy2
}
#[inline(never)]
pub(crate) fn compress_block_lazy(
ms: &mut MatchState,
ss: &mut SeqStore,
rep: &mut [u32; 3],
buf: &[u8],
start: usize,
end: usize,
) -> usize {
#[cfg(all(target_arch = "x86_64", not(feature = "portable")))]
if crate::cpu::Features::detect().bmi2 && bmi2_pays(ms) {
#[allow(unsafe_code)]
return unsafe { compress_block_lazy_bmi2(ms, ss, rep, buf, start, end) };
}
compress_block_lazy_impl::<false>(ms, ss, rep, buf, start, end)
}
#[cfg(all(target_arch = "x86_64", not(feature = "portable")))]
#[inline(never)]
#[target_feature(enable = "bmi1,bmi2,lzcnt")]
fn compress_block_lazy_bmi2(
ms: &mut MatchState,
ss: &mut SeqStore,
rep: &mut [u32; 3],
buf: &[u8],
start: usize,
end: usize,
) -> usize {
compress_block_lazy_impl::<true>(ms, ss, rep, buf, start, end)
}
#[inline(always)]
fn compress_block_lazy_impl<const BMI2: bool>(
ms: &mut MatchState,
ss: &mut SeqStore,
rep: &mut [u32; 3],
buf: &[u8],
start: usize,
end: usize,
) -> usize {
macro_rules! go {
($method:expr, $depth:literal) => {
match (
ms.cparams.min_match.clamp(4, 6),
ms.cparams.search_log.clamp(4, 6),
) {
(4, 4) => {
lazy_generic::<4, { $method }, 4, $depth, BMI2>(ms, ss, rep, buf, start, end)
}
(4, 5) => {
lazy_generic::<4, { $method }, 5, $depth, BMI2>(ms, ss, rep, buf, start, end)
}
(4, _) => {
lazy_generic::<4, { $method }, 6, $depth, BMI2>(ms, ss, rep, buf, start, end)
}
(5, 4) => {
lazy_generic::<5, { $method }, 4, $depth, BMI2>(ms, ss, rep, buf, start, end)
}
(5, 5) => {
lazy_generic::<5, { $method }, 5, $depth, BMI2>(ms, ss, rep, buf, start, end)
}
(5, _) => {
lazy_generic::<5, { $method }, 6, $depth, BMI2>(ms, ss, rep, buf, start, end)
}
(_, 4) => {
lazy_generic::<6, { $method }, 4, $depth, BMI2>(ms, ss, rep, buf, start, end)
}
(_, 5) => {
lazy_generic::<6, { $method }, 5, $depth, BMI2>(ms, ss, rep, buf, start, end)
}
(_, _) => {
lazy_generic::<6, { $method }, 6, $depth, BMI2>(ms, ss, rep, buf, start, end)
}
}
};
}
macro_rules! go_mls {
($method:expr, $depth:literal) => {
match ms.cparams.min_match.clamp(4, 6) {
4 => lazy_generic::<4, { $method }, 4, $depth, BMI2>(ms, ss, rep, buf, start, end),
5 => lazy_generic::<5, { $method }, 4, $depth, BMI2>(ms, ss, rep, buf, start, end),
_ => lazy_generic::<6, { $method }, 4, $depth, BMI2>(ms, ss, rep, buf, start, end),
}
};
}
let row = ms.use_row_match_finder;
match ms.cparams.strategy {
Strategy::Greedy if row => go!(SEARCH_ROW_HASH, 0),
Strategy::Greedy => go_mls!(SEARCH_HASH_CHAIN, 0),
Strategy::Lazy if row => go!(SEARCH_ROW_HASH, 1),
Strategy::Lazy => go_mls!(SEARCH_HASH_CHAIN, 1),
Strategy::Lazy2 if row => go!(SEARCH_ROW_HASH, 2),
Strategy::Lazy2 => go_mls!(SEARCH_HASH_CHAIN, 2),
Strategy::BtLazy2 => go_mls!(SEARCH_BINARY_TREE, 2),
s => unreachable!("not a lazy strategy: {s:?}"),
}
}
#[allow(unsafe_code)]
#[inline(always)]
fn lazy_generic<
const MLS: u32,
const METHOD: u8,
const ROW_LOG: u32,
const DEPTH: u32,
const BMI2: bool,
>(
ms: &mut MatchState,
ss: &mut SeqStore,
rep: &mut [u32; 3],
buf: &[u8],
start: usize,
end: usize,
) -> usize {
let cparams = ms.cparams;
assert!(start <= end && end <= buf.len());
assert!(cparams.hash_log <= 32 && ms.hash_table.len() == 1usize << cparams.hash_log);
match METHOD {
SEARCH_ROW_HASH => {
assert!(ms.use_row_match_finder);
assert!(ms.tag_table.len() == ms.hash_table.len());
assert!(ms.row_hash_log + ROW_LOG == cparams.hash_log);
assert!(ms.row_hash_log + row::ROW_HASH_TAG_BITS <= 32);
assert!(cparams.search_log.clamp(4, 6) == ROW_LOG);
}
SEARCH_BINARY_TREE => {
assert!(cparams.chain_log >= 1 && cparams.chain_log <= 32);
assert!(ms.chain_table.len() == 1usize << cparams.chain_log);
}
_ => {
assert!(cparams.chain_log <= 32);
assert!(ms.chain_table.len() == 1usize << cparams.chain_log);
}
}
assert!(cparams.search_log < 32);
assert!(!ms.window.has_ext_dict());
let window_start = ms.window.pos(ms.window.dict_limit);
assert!(window_start <= start, "window start is not in the buffer");
assert!(ms.window.pos(ms.next_to_update) >= window_start);
assert!(end.wrapping_sub(ms.window.base) <= u32::MAX as usize);
let bp = buf.as_ptr();
let base = bp.wrapping_add(ms.window.base);
let istart = bp.wrapping_add(start);
let iend = bp.wrapping_add(end);
let ilimit = if METHOD == SEARCH_ROW_HASH {
iend.wrapping_sub(8 + ROW_HASH_CACHE_SIZE)
} else {
iend.wrapping_sub(8)
};
let prefix_lowest_index = ms.window.dict_limit;
let prefix_lowest = base.wrapping_add(prefix_lowest_index as usize);
let lowest = bp.wrapping_add(window_start);
let mut s = Searcher {
base,
hash_table: ms.hash_table.as_mut_ptr(),
chain_table: ms.chain_table.as_mut_ptr(),
tag_table: ms.tag_table.as_mut_ptr(),
hash_log: cparams.hash_log,
chain_log: cparams.chain_log,
row_hash_log: ms.row_hash_log,
search_log: cparams.search_log,
window_log: cparams.window_log,
low_limit: ms.window.low_limit,
loaded_dict_end: ms.loaded_dict_end,
hash_salt: ms.hash_salt,
next_to_update: ms.next_to_update,
lazy_skipping: false,
hash_cache: ms.hash_cache,
hash_salt_entropy: ms.hash_salt_entropy,
lowest,
iend,
dict_base: base,
dict_limit: prefix_lowest_index,
dict_lowest: lowest,
dict_end: lowest,
prefix_start: prefix_lowest,
dms: DmsSearch::NONE,
};
let mut ip = istart;
let mut anchor = istart;
let mut offset_1 = rep[0];
let mut offset_2 = rep[1];
let mut offset_saved1 = 0u32;
let mut offset_saved2 = 0u32;
ip = ip.wrapping_add((ip == prefix_lowest) as usize);
{
let curr = s.idx(ip);
let window_low = ms.lowest_prefix_index(curr, cparams.window_log);
let max_rep = curr.wrapping_sub(window_low);
if offset_2 > max_rep {
offset_saved2 = offset_2;
offset_2 = 0;
}
if offset_1 > max_rep {
offset_saved1 = offset_1;
offset_1 = 0;
}
}
unsafe {
if METHOD == SEARCH_ROW_HASH {
let ntu = s.next_to_update;
row::fill_hash_cache::<MLS, ROW_LOG>(&mut s, ntu, ilimit);
}
while ip < ilimit {
let mut match_length = 0usize;
let mut off_base = REPCODE1_TO_OFFBASE;
let mut start = ip.add(1);
let mut store_now = false;
if offset_1 > 0 && {
let r = ip.add(1).wrapping_sub(offset_1 as usize);
s.chk(r, 4);
read32(r) == read32(ip.add(1))
} {
match_length = count(ip.add(1 + 4), ip.add(1 + 4).sub(offset_1 as usize), iend) + 4;
store_now = DEPTH == 0;
}
if !store_now {
{
let mut offbase_found = OFFBASE_UNSET;
let ml2 = search_max::<MLS, METHOD, ROW_LOG, false, false, BMI2>(
&mut s,
ip,
iend,
&mut offbase_found,
);
if ml2 > match_length {
match_length = ml2;
start = ip;
off_base = offbase_found;
}
}
if match_length < 4 {
let step = ((ip as usize - anchor as usize) >> K_SEARCH_STRENGTH) + 1;
ip = ip.add(step);
s.lazy_skipping = step > LAZY_SKIPPING_STEP;
continue;
}
if DEPTH >= 1 {
while ip < ilimit {
ip = ip.add(1);
if off_base != 0 && offset_1 > 0 && {
let r = ip.wrapping_sub(offset_1 as usize);
s.chk(r, 4);
read32(ip) == read32(r)
} {
let ml_rep =
count(ip.add(4), ip.add(4).sub(offset_1 as usize), iend) + 4;
let gain2 = (ml_rep * 3) as i64;
let gain1 = (match_length * 3) as i64 - highbit32(off_base) as i64 + 1;
if ml_rep >= 4 && gain2 > gain1 {
match_length = ml_rep;
off_base = REPCODE1_TO_OFFBASE;
start = ip;
}
}
{
let mut ofb_candidate = OFFBASE_UNSET;
let ml2 = search_max::<MLS, METHOD, ROW_LOG, false, false, BMI2>(
&mut s,
ip,
iend,
&mut ofb_candidate,
);
let gain2 = (ml2 * 4) as i64 - highbit32(ofb_candidate) as i64;
let gain1 = (match_length * 4) as i64 - highbit32(off_base) as i64 + 4;
if ml2 >= 4 && gain2 > gain1 {
match_length = ml2;
off_base = ofb_candidate;
start = ip;
continue; }
}
if DEPTH == 2 && ip < ilimit {
ip = ip.add(1);
if off_base != 0 && offset_1 > 0 && {
let r = ip.wrapping_sub(offset_1 as usize);
s.chk(r, 4);
read32(ip) == read32(r)
} {
let ml_rep =
count(ip.add(4), ip.add(4).sub(offset_1 as usize), iend) + 4;
let gain2 = (ml_rep * 4) as i64;
let gain1 =
(match_length * 4) as i64 - highbit32(off_base) as i64 + 1;
if ml_rep >= 4 && gain2 > gain1 {
match_length = ml_rep;
off_base = REPCODE1_TO_OFFBASE;
start = ip;
}
}
{
let mut ofb_candidate = OFFBASE_UNSET;
let ml2 = search_max::<MLS, METHOD, ROW_LOG, false, false, BMI2>(
&mut s,
ip,
iend,
&mut ofb_candidate,
);
let gain2 = (ml2 * 4) as i64 - highbit32(ofb_candidate) as i64;
let gain1 =
(match_length * 4) as i64 - highbit32(off_base) as i64 + 7;
if ml2 >= 4 && gain2 > gain1 {
match_length = ml2;
off_base = ofb_candidate;
start = ip;
continue;
}
}
}
break; }
}
if off_base > REP_NUM {
let offset = off_base - REP_NUM;
while (start > anchor)
&& (start.wrapping_sub(offset as usize) > prefix_lowest)
&& {
let m = start.wrapping_sub(offset as usize);
s.chk(m.wrapping_sub(1), 1);
*start.sub(1) == *m.sub(1)
}
{
start = start.sub(1);
match_length += 1;
}
offset_2 = offset_1;
offset_1 = offset;
}
}
{
let lit_length = start as usize - anchor as usize;
ss.store_seq(
buf,
anchor as usize - bp as usize,
lit_length,
end,
off_base,
match_length,
);
ip = start.add(match_length);
anchor = ip;
}
if s.lazy_skipping {
if METHOD == SEARCH_ROW_HASH {
let ntu = s.next_to_update;
row::fill_hash_cache::<MLS, ROW_LOG>(&mut s, ntu, ilimit);
}
s.lazy_skipping = false;
}
while ip <= ilimit && offset_2 > 0 && {
let r = ip.wrapping_sub(offset_2 as usize);
s.chk(r, 4);
read32(ip) == read32(r)
} {
match_length = count(ip.add(4), ip.add(4).sub(offset_2 as usize), iend) + 4;
core::mem::swap(&mut offset_1, &mut offset_2);
ss.store_seq(
buf,
anchor as usize - bp as usize,
0,
end,
REPCODE1_TO_OFFBASE,
match_length,
);
ip = ip.add(match_length);
anchor = ip;
}
}
}
ms.next_to_update = s.next_to_update;
ms.lazy_skipping = s.lazy_skipping;
ms.hash_cache = s.hash_cache;
ms.hash_salt_entropy = s.hash_salt_entropy;
if offset_saved1 != 0 && offset_1 != 0 {
offset_saved2 = offset_saved1;
}
rep[0] = if offset_1 != 0 {
offset_1
} else {
offset_saved1
};
rep[1] = if offset_2 != 0 {
offset_2
} else {
offset_saved2
};
iend as usize - anchor as usize
}
#[inline(never)]
pub(crate) fn compress_block_lazy_dict_match_state(
ms: &mut MatchState,
ss: &mut SeqStore,
rep: &mut [u32; 3],
buf: &[u8],
start: usize,
end: usize,
) -> usize {
#[cfg(all(target_arch = "x86_64", not(feature = "portable")))]
if crate::cpu::Features::detect().bmi2 && bmi2_pays(ms) {
#[allow(unsafe_code)]
return unsafe { compress_block_lazy_dict_match_state_bmi2(ms, ss, rep, buf, start, end) };
}
compress_block_lazy_dict_match_state_impl::<false>(ms, ss, rep, buf, start, end)
}
#[cfg(all(target_arch = "x86_64", not(feature = "portable")))]
#[inline(never)]
#[target_feature(enable = "bmi1,bmi2,lzcnt")]
fn compress_block_lazy_dict_match_state_bmi2(
ms: &mut MatchState,
ss: &mut SeqStore,
rep: &mut [u32; 3],
buf: &[u8],
start: usize,
end: usize,
) -> usize {
compress_block_lazy_dict_match_state_impl::<true>(ms, ss, rep, buf, start, end)
}
#[inline(always)]
fn compress_block_lazy_dict_match_state_impl<const BMI2: bool>(
ms: &mut MatchState,
ss: &mut SeqStore,
rep: &mut [u32; 3],
buf: &[u8],
start: usize,
end: usize,
) -> usize {
macro_rules! go {
($method:expr, $depth:literal) => {
match (
ms.cparams.min_match.clamp(4, 6),
ms.cparams.search_log.clamp(4, 6),
) {
(4, 4) => {
dms_generic::<4, { $method }, 4, $depth, BMI2>(ms, ss, rep, buf, start, end)
}
(4, 5) => {
dms_generic::<4, { $method }, 5, $depth, BMI2>(ms, ss, rep, buf, start, end)
}
(4, _) => {
dms_generic::<4, { $method }, 6, $depth, BMI2>(ms, ss, rep, buf, start, end)
}
(5, 4) => {
dms_generic::<5, { $method }, 4, $depth, BMI2>(ms, ss, rep, buf, start, end)
}
(5, 5) => {
dms_generic::<5, { $method }, 5, $depth, BMI2>(ms, ss, rep, buf, start, end)
}
(5, _) => {
dms_generic::<5, { $method }, 6, $depth, BMI2>(ms, ss, rep, buf, start, end)
}
(_, 4) => {
dms_generic::<6, { $method }, 4, $depth, BMI2>(ms, ss, rep, buf, start, end)
}
(_, 5) => {
dms_generic::<6, { $method }, 5, $depth, BMI2>(ms, ss, rep, buf, start, end)
}
(_, _) => {
dms_generic::<6, { $method }, 6, $depth, BMI2>(ms, ss, rep, buf, start, end)
}
}
};
}
macro_rules! go_mls {
($method:expr, $depth:literal) => {
match ms.cparams.min_match.clamp(4, 6) {
4 => dms_generic::<4, { $method }, 4, $depth, BMI2>(ms, ss, rep, buf, start, end),
5 => dms_generic::<5, { $method }, 4, $depth, BMI2>(ms, ss, rep, buf, start, end),
_ => dms_generic::<6, { $method }, 4, $depth, BMI2>(ms, ss, rep, buf, start, end),
}
};
}
let row = ms.use_row_match_finder;
match ms.cparams.strategy {
Strategy::Greedy if row => go!(SEARCH_ROW_HASH, 0),
Strategy::Greedy => go_mls!(SEARCH_HASH_CHAIN, 0),
Strategy::Lazy if row => go!(SEARCH_ROW_HASH, 1),
Strategy::Lazy => go_mls!(SEARCH_HASH_CHAIN, 1),
Strategy::Lazy2 if row => go!(SEARCH_ROW_HASH, 2),
Strategy::Lazy2 => go_mls!(SEARCH_HASH_CHAIN, 2),
Strategy::BtLazy2 => go_mls!(SEARCH_BINARY_TREE, 2),
s => unreachable!("not a lazy strategy: {s:?}"),
}
}
#[allow(unsafe_code)]
#[inline(always)]
fn dms_generic<
const MLS: u32,
const METHOD: u8,
const ROW_LOG: u32,
const DEPTH: u32,
const BMI2: bool,
>(
ms: &mut MatchState,
ss: &mut SeqStore,
rep: &mut [u32; 3],
buf: &[u8],
start: usize,
end: usize,
) -> usize {
let cparams = ms.cparams;
let cd = ms.dict.clone().expect("attached dictionary");
let d = DmsView::new(&cd);
assert!(start <= end && end <= buf.len());
assert!(cparams.hash_log <= 32 && ms.hash_table.len() == 1usize << cparams.hash_log);
assert!(d.hash_log <= 32);
match METHOD {
SEARCH_ROW_HASH => {
assert!(ms.use_row_match_finder && cd.ms.use_row_match_finder);
assert!(ms.tag_table.len() == ms.hash_table.len());
assert!(ms.row_hash_log + ROW_LOG == cparams.hash_log);
assert!(ms.row_hash_log + row::ROW_HASH_TAG_BITS <= 32);
assert!(cparams.search_log.clamp(4, 6) == ROW_LOG);
assert!(cd.ms.tag_table.len() == cd.ms.hash_table.len());
assert!(d.row_hash_log + ROW_LOG == d.hash_log);
}
SEARCH_BINARY_TREE => {
assert!(cparams.chain_log >= 1 && cparams.chain_log <= 32);
assert!(ms.chain_table.len() == 1usize << cparams.chain_log);
assert!(d.chain_log >= 1 && d.chain_log <= 32);
assert!(cd.ms.chain_table.len() == 1usize << d.chain_log);
}
_ => {
assert!(cparams.chain_log <= 32);
assert!(ms.chain_table.len() == 1usize << cparams.chain_log);
assert!(d.chain_log <= 32);
assert!(cd.ms.chain_table.len() == 1usize << d.chain_log);
}
}
assert!(cparams.search_log < 32);
assert!(!ms.window.has_ext_dict());
let window_start = ms.window.pos(ms.window.dict_limit);
assert!(window_start <= start, "window start is not in the buffer");
assert!(ms.window.pos(ms.next_to_update) >= window_start);
assert!(end.wrapping_sub(ms.window.base) <= u32::MAX as usize);
let bp = buf.as_ptr();
let base = bp.wrapping_add(ms.window.base);
let istart = bp.wrapping_add(start);
let iend = bp.wrapping_add(end);
let ilimit = if METHOD == SEARCH_ROW_HASH {
iend.wrapping_sub(8 + ROW_HASH_CACHE_SIZE)
} else {
iend.wrapping_sub(8)
};
let prefix_lowest_index = ms.window.dict_limit;
let prefix_lowest = base.wrapping_add(prefix_lowest_index as usize);
let dict_lowest_index = d.dict_limit;
let dict_base = d.base;
let dict_lowest = dict_base.wrapping_add(dict_lowest_index as usize);
let dict_end = d.end;
assert!(prefix_lowest_index >= d.high_limit);
let dict_index_delta = prefix_lowest_index - d.high_limit;
let dict_and_prefix_length = (start - window_start) as u32
+ (dict_end as usize).wrapping_sub(dict_lowest as usize) as u32;
let mut s = Searcher {
base,
hash_table: ms.hash_table.as_mut_ptr(),
chain_table: ms.chain_table.as_mut_ptr(),
tag_table: ms.tag_table.as_mut_ptr(),
hash_log: cparams.hash_log,
chain_log: cparams.chain_log,
row_hash_log: ms.row_hash_log,
search_log: cparams.search_log,
window_log: cparams.window_log,
low_limit: ms.window.low_limit,
loaded_dict_end: ms.loaded_dict_end,
hash_salt: ms.hash_salt,
next_to_update: ms.next_to_update,
lazy_skipping: false,
hash_cache: ms.hash_cache,
hash_salt_entropy: ms.hash_salt_entropy,
lowest: bp.wrapping_add(window_start),
iend,
dict_base,
dict_limit: prefix_lowest_index,
dict_lowest: d.start,
dict_end,
prefix_start: prefix_lowest,
dms: DmsSearch::new(&d),
};
let mut ip = istart;
let mut anchor = istart;
let mut offset_1 = rep[0];
let mut offset_2 = rep[1];
ip = ip.wrapping_add((dict_and_prefix_length == 0) as usize);
assert!(offset_1 <= dict_and_prefix_length && offset_2 <= dict_and_prefix_length);
let rep_pos = |i: u32| {
if i < prefix_lowest_index {
dict_base.wrapping_add(i.wrapping_sub(dict_index_delta) as usize)
} else {
base.wrapping_add(i as usize)
}
};
let rep_len = |s: &Searcher, ip: *const u8, rep_index: u32| -> usize {
let rep_match = rep_pos(rep_index);
if index_overlap_check(prefix_lowest_index, rep_index) && {
s.chk(rep_match, 4);
unsafe { read32(rep_match) == read32(ip) }
} {
let rep_match_end = if rep_index < prefix_lowest_index {
dict_end
} else {
iend
};
unsafe {
count_2segments(
ip.add(4),
rep_match.add(4),
iend,
rep_match_end,
prefix_lowest,
) + 4
}
} else {
0
}
};
unsafe {
if METHOD == SEARCH_ROW_HASH {
let ntu = s.next_to_update;
row::fill_hash_cache::<MLS, ROW_LOG>(&mut s, ntu, ilimit);
}
while ip < ilimit {
let mut match_length = 0usize;
let mut off_base = REPCODE1_TO_OFFBASE;
let mut start = ip.add(1);
let mut store_now = false;
{
let rep_index = s.idx(ip).wrapping_add(1).wrapping_sub(offset_1);
let ml = rep_len(&s, ip.add(1), rep_index);
if ml != 0 {
match_length = ml;
store_now = DEPTH == 0;
}
}
if !store_now {
{
let mut offbase_found = OFFBASE_UNSET;
let ml2 = search_max::<MLS, METHOD, ROW_LOG, false, true, BMI2>(
&mut s,
ip,
iend,
&mut offbase_found,
);
if ml2 > match_length {
match_length = ml2;
start = ip;
off_base = offbase_found;
}
}
if match_length < 4 {
let step = ((ip as usize - anchor as usize) >> K_SEARCH_STRENGTH) + 1;
ip = ip.add(step);
s.lazy_skipping = step > LAZY_SKIPPING_STEP;
continue;
}
if DEPTH >= 1 {
while ip < ilimit {
ip = ip.add(1);
{
let rep_index = s.idx(ip).wrapping_sub(offset_1);
let ml_rep = rep_len(&s, ip, rep_index);
let gain2 = (ml_rep * 3) as i64;
let gain1 = (match_length * 3) as i64 - highbit32(off_base) as i64 + 1;
if ml_rep >= 4 && gain2 > gain1 {
match_length = ml_rep;
off_base = REPCODE1_TO_OFFBASE;
start = ip;
}
}
{
let mut ofb_candidate = OFFBASE_UNSET;
let ml2 = search_max::<MLS, METHOD, ROW_LOG, false, true, BMI2>(
&mut s,
ip,
iend,
&mut ofb_candidate,
);
let gain2 = (ml2 * 4) as i64 - highbit32(ofb_candidate) as i64;
let gain1 = (match_length * 4) as i64 - highbit32(off_base) as i64 + 4;
if ml2 >= 4 && gain2 > gain1 {
match_length = ml2;
off_base = ofb_candidate;
start = ip;
continue; }
}
if DEPTH == 2 && ip < ilimit {
ip = ip.add(1);
{
let rep_index = s.idx(ip).wrapping_sub(offset_1);
let ml_rep = rep_len(&s, ip, rep_index);
let gain2 = (ml_rep * 4) as i64;
let gain1 =
(match_length * 4) as i64 - highbit32(off_base) as i64 + 1;
if ml_rep >= 4 && gain2 > gain1 {
match_length = ml_rep;
off_base = REPCODE1_TO_OFFBASE;
start = ip;
}
}
{
let mut ofb_candidate = OFFBASE_UNSET;
let ml2 = search_max::<MLS, METHOD, ROW_LOG, false, true, BMI2>(
&mut s,
ip,
iend,
&mut ofb_candidate,
);
let gain2 = (ml2 * 4) as i64 - highbit32(ofb_candidate) as i64;
let gain1 =
(match_length * 4) as i64 - highbit32(off_base) as i64 + 7;
if ml2 >= 4 && gain2 > gain1 {
match_length = ml2;
off_base = ofb_candidate;
start = ip;
continue;
}
}
}
break; }
}
if off_base > REP_NUM {
let offset = off_base - REP_NUM;
let match_index = s.idx(start).wrapping_sub(offset);
let mut mtch = rep_pos(match_index);
let m_start = if match_index < prefix_lowest_index {
dict_lowest
} else {
prefix_lowest
};
while (start > anchor) && (mtch > m_start) && {
s.chk(mtch.wrapping_sub(1), 1);
*start.sub(1) == *mtch.sub(1)
} {
start = start.sub(1);
mtch = mtch.sub(1);
match_length += 1;
}
offset_2 = offset_1;
offset_1 = offset;
}
}
{
let lit_length = start as usize - anchor as usize;
ss.store_seq(
buf,
anchor as usize - bp as usize,
lit_length,
end,
off_base,
match_length,
);
ip = start.add(match_length);
anchor = ip;
}
if s.lazy_skipping {
if METHOD == SEARCH_ROW_HASH {
let ntu = s.next_to_update;
row::fill_hash_cache::<MLS, ROW_LOG>(&mut s, ntu, ilimit);
}
s.lazy_skipping = false;
}
while ip <= ilimit {
let rep_index = s.idx(ip).wrapping_sub(offset_2);
let ml = rep_len(&s, ip, rep_index);
if ml == 0 {
break;
}
match_length = ml;
core::mem::swap(&mut offset_1, &mut offset_2);
ss.store_seq(
buf,
anchor as usize - bp as usize,
0,
end,
REPCODE1_TO_OFFBASE,
match_length,
);
ip = ip.add(match_length);
anchor = ip;
}
}
}
ms.next_to_update = s.next_to_update;
ms.lazy_skipping = s.lazy_skipping;
ms.hash_cache = s.hash_cache;
ms.hash_salt_entropy = s.hash_salt_entropy;
rep[0] = offset_1;
rep[1] = offset_2;
iend as usize - anchor as usize
}
#[inline(never)]
pub(crate) fn compress_block_lazy_ext_dict(
ms: &mut MatchState,
ss: &mut SeqStore,
rep: &mut [u32; 3],
buf: &[u8],
start: usize,
end: usize,
) -> usize {
#[cfg(all(target_arch = "x86_64", not(feature = "portable")))]
if crate::cpu::Features::detect().bmi2 && bmi2_pays(ms) {
#[allow(unsafe_code)]
return unsafe { compress_block_lazy_ext_dict_bmi2(ms, ss, rep, buf, start, end) };
}
compress_block_lazy_ext_dict_impl::<false>(ms, ss, rep, buf, start, end)
}
#[cfg(all(target_arch = "x86_64", not(feature = "portable")))]
#[inline(never)]
#[target_feature(enable = "bmi1,bmi2,lzcnt")]
fn compress_block_lazy_ext_dict_bmi2(
ms: &mut MatchState,
ss: &mut SeqStore,
rep: &mut [u32; 3],
buf: &[u8],
start: usize,
end: usize,
) -> usize {
compress_block_lazy_ext_dict_impl::<true>(ms, ss, rep, buf, start, end)
}
#[inline(always)]
fn compress_block_lazy_ext_dict_impl<const BMI2: bool>(
ms: &mut MatchState,
ss: &mut SeqStore,
rep: &mut [u32; 3],
buf: &[u8],
start: usize,
end: usize,
) -> usize {
macro_rules! go {
($method:expr, $depth:literal) => {
match (
ms.cparams.min_match.clamp(4, 6),
ms.cparams.search_log.clamp(4, 6),
) {
(4, 4) => {
ext_generic::<4, { $method }, 4, $depth, BMI2>(ms, ss, rep, buf, start, end)
}
(4, 5) => {
ext_generic::<4, { $method }, 5, $depth, BMI2>(ms, ss, rep, buf, start, end)
}
(4, _) => {
ext_generic::<4, { $method }, 6, $depth, BMI2>(ms, ss, rep, buf, start, end)
}
(5, 4) => {
ext_generic::<5, { $method }, 4, $depth, BMI2>(ms, ss, rep, buf, start, end)
}
(5, 5) => {
ext_generic::<5, { $method }, 5, $depth, BMI2>(ms, ss, rep, buf, start, end)
}
(5, _) => {
ext_generic::<5, { $method }, 6, $depth, BMI2>(ms, ss, rep, buf, start, end)
}
(_, 4) => {
ext_generic::<6, { $method }, 4, $depth, BMI2>(ms, ss, rep, buf, start, end)
}
(_, 5) => {
ext_generic::<6, { $method }, 5, $depth, BMI2>(ms, ss, rep, buf, start, end)
}
(_, _) => {
ext_generic::<6, { $method }, 6, $depth, BMI2>(ms, ss, rep, buf, start, end)
}
}
};
}
macro_rules! go_mls {
($method:expr, $depth:literal) => {
match ms.cparams.min_match.clamp(4, 6) {
4 => ext_generic::<4, { $method }, 4, $depth, BMI2>(ms, ss, rep, buf, start, end),
5 => ext_generic::<5, { $method }, 4, $depth, BMI2>(ms, ss, rep, buf, start, end),
_ => ext_generic::<6, { $method }, 4, $depth, BMI2>(ms, ss, rep, buf, start, end),
}
};
}
let row = ms.use_row_match_finder;
match ms.cparams.strategy {
Strategy::Greedy if row => go!(SEARCH_ROW_HASH, 0),
Strategy::Greedy => go_mls!(SEARCH_HASH_CHAIN, 0),
Strategy::Lazy if row => go!(SEARCH_ROW_HASH, 1),
Strategy::Lazy => go_mls!(SEARCH_HASH_CHAIN, 1),
Strategy::Lazy2 if row => go!(SEARCH_ROW_HASH, 2),
Strategy::Lazy2 => go_mls!(SEARCH_HASH_CHAIN, 2),
Strategy::BtLazy2 => go_mls!(SEARCH_BINARY_TREE, 2),
s => unreachable!("not a lazy strategy: {s:?}"),
}
}
#[allow(unsafe_code)]
#[inline(always)]
fn ext_generic<
const MLS: u32,
const METHOD: u8,
const ROW_LOG: u32,
const DEPTH: u32,
const BMI2: bool,
>(
ms: &mut MatchState,
ss: &mut SeqStore,
rep: &mut [u32; 3],
buf: &[u8],
start: usize,
end: usize,
) -> usize {
let cparams = ms.cparams;
assert!(start <= end && end <= buf.len());
assert!(cparams.hash_log <= 32 && ms.hash_table.len() == 1usize << cparams.hash_log);
match METHOD {
SEARCH_ROW_HASH => {
assert!(ms.use_row_match_finder);
assert!(ms.tag_table.len() == ms.hash_table.len());
assert!(ms.row_hash_log + ROW_LOG == cparams.hash_log);
assert!(ms.row_hash_log + row::ROW_HASH_TAG_BITS <= 32);
assert!(cparams.search_log.clamp(4, 6) == ROW_LOG);
}
SEARCH_BINARY_TREE => {
assert!(cparams.chain_log >= 1 && cparams.chain_log <= 32);
assert!(ms.chain_table.len() == 1usize << cparams.chain_log);
}
_ => {
assert!(cparams.chain_log <= 32);
assert!(ms.chain_table.len() == 1usize << cparams.chain_log);
}
}
assert!(cparams.search_log < 32);
let dict_limit = ms.window.dict_limit;
let low_limit = ms.window.low_limit;
let w = ExtWindow::new(
&ms.window,
buf,
ms.ext_buf(buf),
start,
end,
low_limit,
dict_limit,
);
assert!(ms.next_to_update >= dict_limit);
let bp = buf.as_ptr();
let base = w.base;
let dict_base = w.dict_base;
let istart = w.istart;
let iend = w.iend;
let ilimit = if METHOD == SEARCH_ROW_HASH {
iend.wrapping_sub(8 + ROW_HASH_CACHE_SIZE)
} else {
iend.wrapping_sub(8)
};
let prefix_start = w.prefix_start;
let dict_end = w.dict_end;
let dict_start = w.dict_start;
let window_log = cparams.window_log;
let mut s = Searcher {
base,
hash_table: ms.hash_table.as_mut_ptr(),
chain_table: ms.chain_table.as_mut_ptr(),
tag_table: ms.tag_table.as_mut_ptr(),
hash_log: cparams.hash_log,
chain_log: cparams.chain_log,
row_hash_log: ms.row_hash_log,
search_log: cparams.search_log,
window_log,
low_limit,
loaded_dict_end: ms.loaded_dict_end,
hash_salt: ms.hash_salt,
next_to_update: ms.next_to_update,
lazy_skipping: false,
hash_cache: ms.hash_cache,
hash_salt_entropy: ms.hash_salt_entropy,
lowest: prefix_start,
iend,
dict_base,
dict_limit,
dict_lowest: dict_start,
dict_end,
prefix_start,
dms: DmsSearch::NONE,
};
let mut ip = istart;
let mut anchor = istart;
let mut offset_1 = rep[0];
let mut offset_2 = rep[1];
let rep_match = |s: &Searcher, ip: *const u8, curr: u32, offset: u32| -> usize {
let window_low = s.lowest_match_index(curr);
let rep_index = curr.wrapping_sub(offset);
let rep_match = s.ext_pos(rep_index);
if index_overlap_check(dict_limit, rep_index) & (offset <= curr.wrapping_sub(window_low))
&& {
s.chk(rep_match, 4);
unsafe { read32(ip) == read32(rep_match) }
}
{
let rep_end = if rep_index < dict_limit {
dict_end
} else {
iend
};
s.chk(rep_match, 0);
unsafe { count_2segments(ip.add(4), rep_match.add(4), iend, rep_end, prefix_start) + 4 }
} else {
0
}
};
ip = ip.wrapping_add((ip == prefix_start) as usize);
unsafe {
if METHOD == SEARCH_ROW_HASH {
let ntu = s.next_to_update;
row::fill_hash_cache::<MLS, ROW_LOG>(&mut s, ntu, ilimit);
}
while ip < ilimit {
let mut match_length = 0usize;
let mut off_base = REPCODE1_TO_OFFBASE;
let mut start = ip.add(1);
let mut curr = s.idx(ip);
let mut store_now = false;
{
let ml = rep_match(&s, ip.add(1), curr + 1, offset_1);
if ml != 0 {
match_length = ml;
store_now = DEPTH == 0;
}
}
if !store_now {
{
let mut ofb_candidate = OFFBASE_UNSET;
let ml2 = search_max::<MLS, METHOD, ROW_LOG, true, false, BMI2>(
&mut s,
ip,
iend,
&mut ofb_candidate,
);
if ml2 > match_length {
match_length = ml2;
start = ip;
off_base = ofb_candidate;
}
}
if match_length < 4 {
let step = (ip as usize - anchor as usize) >> K_SEARCH_STRENGTH;
ip = ip.add(step + 1);
s.lazy_skipping = step > LAZY_SKIPPING_STEP;
continue;
}
if DEPTH >= 1 {
while ip < ilimit {
ip = ip.add(1);
curr += 1;
if off_base != 0 {
let rep_length = rep_match(&s, ip, curr, offset_1);
let gain2 = (rep_length * 3) as i64;
let gain1 = (match_length * 3) as i64 - highbit32(off_base) as i64 + 1;
if rep_length >= 4 && gain2 > gain1 {
match_length = rep_length;
off_base = REPCODE1_TO_OFFBASE;
start = ip;
}
}
{
let mut ofb_candidate = OFFBASE_UNSET;
let ml2 = search_max::<MLS, METHOD, ROW_LOG, true, false, BMI2>(
&mut s,
ip,
iend,
&mut ofb_candidate,
);
let gain2 = (ml2 * 4) as i64 - highbit32(ofb_candidate) as i64;
let gain1 = (match_length * 4) as i64 - highbit32(off_base) as i64 + 4;
if ml2 >= 4 && gain2 > gain1 {
match_length = ml2;
off_base = ofb_candidate;
start = ip;
continue; }
}
if DEPTH == 2 && ip < ilimit {
ip = ip.add(1);
curr += 1;
if off_base != 0 {
let rep_length = rep_match(&s, ip, curr, offset_1);
let gain2 = (rep_length * 4) as i64;
let gain1 =
(match_length * 4) as i64 - highbit32(off_base) as i64 + 1;
if rep_length >= 4 && gain2 > gain1 {
match_length = rep_length;
off_base = REPCODE1_TO_OFFBASE;
start = ip;
}
}
{
let mut ofb_candidate = OFFBASE_UNSET;
let ml2 = search_max::<MLS, METHOD, ROW_LOG, true, false, BMI2>(
&mut s,
ip,
iend,
&mut ofb_candidate,
);
let gain2 = (ml2 * 4) as i64 - highbit32(ofb_candidate) as i64;
let gain1 =
(match_length * 4) as i64 - highbit32(off_base) as i64 + 7;
if ml2 >= 4 && gain2 > gain1 {
match_length = ml2;
off_base = ofb_candidate;
start = ip;
continue;
}
}
}
break; }
}
if off_base > REP_NUM {
let offset = off_base - REP_NUM;
let match_index = s.idx(start).wrapping_sub(offset);
let mut m = s.ext_pos(match_index);
let m_start = if match_index < dict_limit {
dict_start
} else {
prefix_start
};
while (start > anchor) && (m > m_start) && {
s.chk(m.wrapping_sub(1), 1);
*start.sub(1) == *m.sub(1)
} {
start = start.sub(1);
m = m.sub(1);
match_length += 1;
}
offset_2 = offset_1;
offset_1 = offset;
}
}
{
let lit_length = start as usize - anchor as usize;
ss.store_seq(
buf,
anchor as usize - bp as usize,
lit_length,
end,
off_base,
match_length,
);
ip = start.add(match_length);
anchor = ip;
}
if s.lazy_skipping {
if METHOD == SEARCH_ROW_HASH {
let ntu = s.next_to_update;
row::fill_hash_cache::<MLS, ROW_LOG>(&mut s, ntu, ilimit);
}
s.lazy_skipping = false;
}
while ip <= ilimit {
let rep_current = s.idx(ip);
let ml = rep_match(&s, ip, rep_current, offset_2);
if ml == 0 {
break;
}
match_length = ml;
core::mem::swap(&mut offset_1, &mut offset_2); ss.store_seq(
buf,
anchor as usize - bp as usize,
0,
end,
REPCODE1_TO_OFFBASE,
match_length,
);
ip = ip.add(match_length);
anchor = ip;
}
}
}
ms.next_to_update = s.next_to_update;
ms.lazy_skipping = s.lazy_skipping;
ms.hash_cache = s.hash_cache;
ms.hash_salt_entropy = s.hash_salt_entropy;
rep[0] = offset_1;
rep[1] = offset_2;
iend as usize - anchor as usize
}
#[cfg(test)]
mod tests {
use super::super::fast::tests::{
MIN_SEQUENCES, MIRI, miri_pick, random, ring_input, roundtrip, roundtrip_ring,
roundtrip_ring_with, texty, variants_input,
};
use super::super::fast::{MatchState, build_seq_store, reduce_table_btlazy2};
use super::super::params::{CParams, Strategy, block_size_max, one_shot_cparams};
use super::super::seq_store::{REP_START_VALUE, SeqDef, SeqStore};
use super::DUBT_UNSORTED_MARK;
use alloc::vec::Vec;
const LAZY: [Strategy; 4] = [
Strategy::Greedy,
Strategy::Lazy,
Strategy::Lazy2,
Strategy::BtLazy2,
];
fn inputs() -> [Vec<u8>; 6] {
let k = if MIRI { 50 } else { 1 }; [
texty(1000, 1),
texty(300_000 / k, 3),
random(50_000 / k, 4),
alloc::vec![0u8; 200_000 / k],
(0..(70_000 / k) as u32).map(|i| (i % 251) as u8).collect(),
{
let mut v = random(20_000 / k, 5);
v.extend_from_slice(&texty(30_000 / k, 6));
v.extend_from_within(..25_000 / k);
v
},
]
}
#[test]
fn roundtrip_levels() {
let inputs = inputs();
for level in 4..=15 {
for src in miri_pick(&inputs, level as usize) {
let cp = one_shot_cparams(level, src.len() as u64);
if !LAZY.contains(&cp.strategy) {
continue;
}
roundtrip(src, cp, block_size_max(&cp, src.len() as u64));
}
}
}
#[test]
fn roundtrip_all_variants() {
let src = variants_input();
let (mlss, search_logs, window_logs): (&[u32], &[u32], &[u32]) = if MIRI {
(&[4, 5, 6], &[4, 5, 6], &[14, 15])
} else {
(&[3, 4, 5, 6, 7], &[1, 4, 5, 6, 7], &[10, 14, 15, 18, 22])
};
for (s, strategy) in LAZY.into_iter().enumerate() {
for &mls in mlss {
for &search_log in miri_pick(search_logs, mls as usize + s) {
for &window_log in window_logs {
let cp = CParams {
window_log,
chain_log: 16,
hash_log: 16,
search_log,
min_match: mls,
target_length: 8,
strategy,
};
let ms = MatchState::new(cp);
let row = ms.use_row_match_finder;
assert_eq!(row, strategy != Strategy::BtLazy2 && window_log > 14);
let n = roundtrip(&src, cp, (1 << 17).min(1 << window_log));
assert!(
n > MIN_SEQUENCES,
"{strategy:?} mls {mls} slog {search_log} wlog {window_log}: {n} sequences"
);
}
}
}
}
}
#[test]
fn small_blocks() {
let src = texty(if MIRI { 600 } else { 5000 }, 9);
for level in [5, 7, 9, 10] {
let cp = one_shot_cparams(level, src.len() as u64);
for bs in [1, 6, 7, 8, 9, 16, 17, 20, 33, 100, 1000] {
roundtrip(&src, cp, bs);
}
}
for n in 0..60 {
let s = texty(n, 10);
for level in [5, 9, 10] {
roundtrip(&s, one_shot_cparams(level, n as u64), 1 << 17);
}
}
}
fn sequences(ms: &mut MatchState, src: &[u8]) -> Vec<(SeqDef, usize)> {
let cp = ms.cparams;
let bs = block_size_max(&cp, src.len() as u64);
let mut ss = SeqStore::new(bs, cp.min_match);
let mut rep = REP_START_VALUE;
let mut out = Vec::new();
ms.update_window(0, src.len());
let mut pos = 0;
while pos < src.len() {
let n = bs.min(src.len() - pos);
ms.prepare_block(pos, n);
if build_seq_store(ms, &mut ss, &mut rep, src, pos, pos + n) {
out.extend(ss.sequences().iter().map(|&s| (s, ss.lit_len)));
}
pos += n;
}
out
}
#[test]
fn stale_tags() {
let n = if MIRI { 1500 } else { 400_000 };
let src = texty(n, 11);
let other = random(n, 12);
for level in [5, 6, 8, 12] {
let mut cp = one_shot_cparams(level, 400_000);
if MIRI {
cp.window_log = 15;
cp.hash_log = cp.hash_log.min(16);
cp.chain_log = cp.chain_log.min(16);
}
let mut fresh = MatchState::new(cp);
assert!(fresh.use_row_match_finder);
let want = sequences(&mut fresh, &src);
let mut ms = MatchState::new(cp);
let mut s = 99u64;
for t in ms.tag_table.iter_mut() {
*t = super::super::fast::tests::xorshift(&mut s) as u8 % 7;
}
assert_eq!(sequences(&mut ms, &src), want, "L{level} garbage tags");
let mut ms = MatchState::new(cp);
sequences(&mut ms, &other);
let salt = ms.hash_salt;
ms.reset(cp);
ms.hash_salt = salt;
assert_eq!(sequences(&mut ms, &src), want, "L{level} stale tags");
}
}
#[test]
fn salt_advances() {
let src = texty(if MIRI { 4000 } else { 100_000 }, 13);
let cp = one_shot_cparams(6, 100_000);
let mut ms = MatchState::new(cp);
let first = ms.hash_salt;
assert_ne!(first, 0);
sequences(&mut ms, &src);
assert_ne!(ms.hash_salt_entropy, 0);
ms.reset(cp);
assert_ne!(ms.hash_salt, first);
ms.clear_hash_salt();
ms.reset(cp);
assert_eq!(ms.hash_salt, first);
let small = one_shot_cparams(6, 10_000);
let salt = ms.hash_salt;
ms.reset(small);
assert!(!ms.use_row_match_finder);
assert_eq!(ms.hash_salt, salt);
}
#[test]
fn reduce_keeps_unsorted_mark() {
let mut t = [0, 1, 2, 5, 1000, 1001, DUBT_UNSORTED_MARK, 1 << 30];
reduce_table_btlazy2(&mut t, 999);
assert_eq!(t, [0, 1, 0, 0, 0, 2, 1, (1 << 30) - 999]);
}
#[test]
fn roundtrip_ring_variants() {
if MIRI {
let src = ring_input(1500, 700, 700);
for strategy in LAZY {
for mls in [4, 5, 6] {
for &search_log in miri_pick(&[4, 5, 6], mls as usize) {
let cp = CParams {
window_log: 10,
chain_log: 16,
hash_log: 16,
search_log,
min_match: mls,
target_length: 8,
strategy,
};
let mut finders = Vec::new();
if search_log == 5 || strategy == Strategy::BtLazy2 {
let ms = MatchState::new(cp);
assert!(!ms.use_row_match_finder);
finders.push(ms);
}
if strategy != Strategy::BtLazy2 {
finders.push(MatchState::new_with(cp, true, false));
}
for ms in finders {
let row = ms.use_row_match_finder;
let (n, ext) = roundtrip_ring_with(&src, ms, 1 << 10);
assert!(
n > MIN_SEQUENCES && ext > 0,
"{strategy:?} mls {mls} slog {search_log} row {row}: {n} {ext}"
);
}
}
}
}
return;
}
let src = ring_input(150_000, 60_000, 60_000);
let search_logs: &[u32] = &[1, 5, 6];
let windows: &[(u32, usize)] = &[(12, 4096), (14, 3000), (15, 1 << 13), (17, 1 << 17)];
for strategy in LAZY {
for mls in [4, 5, 6] {
for &search_log in search_logs {
for &(window_log, bs) in windows {
let cp = CParams {
window_log,
chain_log: 16,
hash_log: 16,
search_log,
min_match: mls,
target_length: 8,
strategy,
};
let (n, ext) = roundtrip_ring(&src, cp, bs);
assert!(
n > MIN_SEQUENCES && ext > 0,
"{strategy:?} mls {mls} slog {search_log} wlog {window_log}: {n} {ext}"
);
}
}
}
}
}
}