1#![forbid(unsafe_code)]
2
3#[derive(Debug, Clone, Copy, PartialEq, Eq)]
5pub enum Strategy {
6 Fast,
8 DFast,
10}
11
12#[derive(Debug, Clone, Copy)]
14pub struct LdmParams {
15 pub hash_log: u32,
16 pub bucket_size_log: u32,
17 pub min_match_length: u32,
18 pub hash_rate_log: u32,
19}
20
21impl LdmParams {
22 pub fn default_for_window_log(window_log: u32) -> Self {
23 let hash_log = 20u32.min(window_log.saturating_sub(1));
24 let hash_rate_log = window_log.saturating_sub(hash_log).max(7);
25 Self {
26 hash_log,
27 bucket_size_log: 4,
28 min_match_length: 64,
29 hash_rate_log,
30 }
31 }
32}
33
34#[derive(Debug, Clone, Copy)]
39pub struct LevelParams {
40 pub strategy: Strategy,
41 pub window_log: u32,
42 pub hash_log: u32,
43 pub chain_log: u32,
45 pub search_log: u32,
46 pub min_match: u32,
47 pub target_length: u32,
48 pub search_strength: u32,
49 pub force_raw_literals: bool,
50 #[cfg(feature = "ldm")]
51 pub ldm_params: Option<LdmParams>,
52}
53
54impl LevelParams {
55 #[must_use]
56 pub fn with_window_log(mut self, window_log: u32) -> Self {
57 self.window_log = window_log;
58 self
59 }
60
61 #[cfg(feature = "ldm")]
62 #[must_use]
63 pub fn with_ldm(mut self, params: LdmParams) -> Self {
64 self.ldm_params = Some(params);
65 self
66 }
67}
68
69pub const DEFAULT_LEVEL: i32 = 1;
71
72pub fn level_params(level: i32) -> Option<LevelParams> {
77 level_params_for_size(level, usize::MAX)
78}
79
80pub fn level_params_for_size(level: i32, src_len: usize) -> Option<LevelParams> {
87 let mut params = level_params_inner(level)?;
88 if let Some(search_strength) = short_input_search_strength(level, src_len) {
89 params.search_strength = search_strength;
90 }
91 params.hash_log = params.hash_log.clamp(HASH_LOG_MIN, HASH_LOG_MAX);
92 params.chain_log = params.chain_log.clamp(HASH_LOG_MIN, HASH_LOG_MAX);
93 params.window_log = params.window_log.clamp(WINDOW_LOG_MIN, WINDOW_LOG_MAX);
94 if (2..usize::MAX).contains(&src_len) {
95 let src_log = src_log(src_len);
96 params.hash_log = params.hash_log.min(src_log).max(HASH_LOG_MIN);
97 params.chain_log = params.chain_log.min(src_log).max(HASH_LOG_MIN);
98 params.window_log = params.window_log.min(src_log);
99 }
100 if raw_literals_limit(level).is_some_and(|max| src_len <= max) {
101 params.force_raw_literals = true;
102 if let Some((target_length, min_match, search_strength)) = small_input_search(level) {
103 params.target_length = target_length;
104 params.min_match = min_match;
105 params.search_strength = search_strength;
106 }
107 }
108 Some(params)
109}
110
111pub(crate) fn src_log(src_len: usize) -> u32 {
113 usize::BITS - src_len.saturating_sub(1).leading_zeros()
114}
115
116pub(crate) fn level_params_with_prefix(
120 level: i32,
121 prefix_len: usize,
122 input_len: usize,
123) -> Option<LevelParams> {
124 let mut params = level_params_for_size(level, prefix_len.saturating_add(input_len))?;
125 if let Some(search_strength) = short_input_search_strength(level, input_len) {
126 params.search_strength = search_strength;
127 }
128 Some(params)
129}
130
131fn short_input_search_strength(level: i32, input_len: usize) -> Option<u32> {
134 if input_len > 256 * 1024 {
135 return None;
136 }
137 match level {
138 3 => Some(4),
139 4 => Some(6),
140 _ => None,
141 }
142}
143
144pub const HASH_LOG_MIN: u32 = 6;
145pub const HASH_LOG_MAX: u32 = 30;
146pub const WINDOW_LOG_MIN: u32 = 10;
147pub const WINDOW_LOG_MAX: u32 = 27;
148#[cfg(feature = "ldm")]
149const LDM_DEFAULT_WINDOW_LOG: u32 = 27;
150
151pub(crate) fn raw_literals_limit(level: i32) -> Option<usize> {
160 if !(-6..=-1).contains(&level) {
161 return None;
162 }
163 let rank = (-level - 1) as usize;
164 let base = 2048usize << (rank / 2);
165 Some(if rank % 2 == 1 { base + base / 2 } else { base })
166}
167
168fn small_input_search(level: i32) -> Option<(u32, u32, u32)> {
177 Some(match level {
178 -6 => (4, 5, 6),
179 -5 => (3, 5, 6),
180 -4 => (2, 5, 6),
181 -3 | -2 => (2, 5, 7),
182 -1 => (1, 5, 7),
183 _ => return None,
184 })
185}
186
187pub(crate) fn keeps_raw_literals(params: &LevelParams, _input_len: usize) -> bool {
190 params.force_raw_literals
191}
192
193#[derive(Clone, Copy, Debug)]
195pub(crate) struct BlockPolicy {
196 pub custom_tables: bool,
198 pub max_literal_entropy_fp8: u32,
201}
202
203pub(crate) fn block_policy(params: &LevelParams, input_len: usize) -> BlockPolicy {
210 let max_literal_entropy_fp8 = if params.strategy == Strategy::Fast && params.min_match >= 5 {
211 match params.target_length {
212 0 | 1 => u32::MAX,
213 _ => 6 * 256 + 64,
214 }
215 } else {
216 u32::MAX
217 };
218 BlockPolicy {
219 custom_tables: use_custom_sequence_tables(params, input_len),
220 max_literal_entropy_fp8,
221 }
222}
223
224fn use_custom_sequence_tables(params: &LevelParams, input_len: usize) -> bool {
225 !keeps_raw_literals(params, input_len)
227}
228
229pub fn max_hash_log(level: i32) -> Option<u32> {
232 let p = level_params_inner(level)?;
233 Some(p.hash_log.max(p.chain_log))
234}
235
236fn level_params_inner(level: i32) -> Option<LevelParams> {
237 Some(match level {
238 0 => return level_params_inner(DEFAULT_LEVEL),
239 -8 | -7 => LevelParams {
247 strategy: Strategy::Fast,
248 window_log: 19,
249 hash_log: 13,
250 chain_log: 13,
251 search_log: 0,
252 min_match: 8,
253 target_length: 2,
254 search_strength: 4,
255 force_raw_literals: true,
256 #[cfg(feature = "ldm")]
257 ldm_params: None,
258 },
259 -6 => LevelParams {
265 strategy: Strategy::Fast,
266 window_log: 19,
267 hash_log: 13,
268 chain_log: 13,
269 search_log: 0,
270 min_match: 8,
271 target_length: 5,
272 search_strength: 3,
273 force_raw_literals: false,
274 #[cfg(feature = "ldm")]
275 ldm_params: None,
276 },
277 -5 => LevelParams {
278 strategy: Strategy::Fast,
279 window_log: 19,
280 hash_log: 13,
281 chain_log: 13,
282 search_log: 0,
283 min_match: 8,
284 target_length: 4,
285 search_strength: 4,
286 force_raw_literals: false,
287 #[cfg(feature = "ldm")]
288 ldm_params: None,
289 },
290 -4 => LevelParams {
291 strategy: Strategy::Fast,
292 window_log: 19,
293 hash_log: 13,
294 chain_log: 13,
295 search_log: 0,
296 min_match: 8,
297 target_length: 2,
298 search_strength: 4,
299 force_raw_literals: false,
300 #[cfg(feature = "ldm")]
301 ldm_params: None,
302 },
303 -3 => LevelParams {
307 strategy: Strategy::Fast,
308 window_log: 19,
309 hash_log: 14,
310 chain_log: 14,
311 search_log: 0,
312 min_match: 7,
313 target_length: 3,
314 search_strength: 4,
315 force_raw_literals: false,
316 #[cfg(feature = "ldm")]
317 ldm_params: None,
318 },
319 -2 => LevelParams {
320 strategy: Strategy::Fast,
321 window_log: 19,
322 hash_log: 14,
323 chain_log: 14,
324 search_log: 0,
325 min_match: 7,
326 target_length: 2,
327 search_strength: 4,
328 force_raw_literals: false,
329 #[cfg(feature = "ldm")]
330 ldm_params: None,
331 },
332 -1 => LevelParams {
333 strategy: Strategy::Fast,
334 window_log: 19,
335 hash_log: 14,
336 chain_log: 14,
337 search_log: 0,
338 min_match: 7,
339 target_length: 2,
340 search_strength: 5,
341 force_raw_literals: false,
342 #[cfg(feature = "ldm")]
343 ldm_params: None,
344 },
345 1 => LevelParams {
351 strategy: Strategy::Fast,
352 window_log: 19,
353 hash_log: 14,
354 chain_log: 14,
355 search_log: 0,
356 min_match: 7,
357 target_length: 1,
358 search_strength: 4,
359 force_raw_literals: false,
360 #[cfg(feature = "ldm")]
361 ldm_params: None,
362 },
363 2 => LevelParams {
364 strategy: Strategy::Fast,
365 window_log: 20,
366 hash_log: 15,
367 chain_log: 15,
368 search_log: 0,
369 min_match: 7,
370 target_length: 1,
371 search_strength: 4,
372 force_raw_literals: false,
373 #[cfg(feature = "ldm")]
374 ldm_params: None,
375 },
376 3 => LevelParams {
379 strategy: Strategy::DFast,
380 window_log: 21,
381 hash_log: 17,
382 chain_log: 16,
383 search_log: 1,
384 min_match: 5,
385 target_length: 1,
386 search_strength: 0,
387 force_raw_literals: false,
388 #[cfg(feature = "ldm")]
389 ldm_params: None,
390 },
391 4 => LevelParams {
394 strategy: Strategy::DFast,
395 window_log: 24,
396 hash_log: 18,
397 chain_log: 18,
398 search_log: 0,
399 min_match: 5,
400 target_length: 1,
401 search_strength: 3,
402 force_raw_literals: false,
403 #[cfg(feature = "ldm")]
404 ldm_params: None,
405 },
406 _ => return None,
407 })
408}
409
410#[derive(Debug, Clone, Default)]
415pub struct Options {
416 pub(crate) window_log: Option<u32>,
417 #[cfg_attr(not(feature = "ldm"), allow(dead_code))]
418 pub(crate) ldm: bool,
419}
420
421impl Options {
422 #[must_use]
423 pub fn window_log(mut self, log: u32) -> Self {
424 self.window_log = Some(log);
425 self
426 }
427
428 #[cfg(feature = "ldm")]
431 #[must_use]
432 pub fn ldm(mut self, enable: bool) -> Self {
433 self.ldm = enable;
434 self
435 }
436}
437
438pub fn apply_options(params: &mut LevelParams, opts: &Options) {
439 if let Some(wl) = opts.window_log {
440 params.window_log = wl.clamp(WINDOW_LOG_MIN, WINDOW_LOG_MAX);
441 }
442 #[cfg(feature = "ldm")]
443 if opts.ldm {
444 if opts.window_log.is_none() {
445 params.window_log = LDM_DEFAULT_WINDOW_LOG;
446 }
447 params.ldm_params = Some(LdmParams::default_for_window_log(params.window_log));
448 }
449}