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 {
245 strategy: Strategy::Fast,
246 window_log: 19,
247 hash_log: 13,
248 chain_log: 13,
249 search_log: 0,
250 min_match: 8,
251 target_length: 2,
252 search_strength: 4,
253 force_raw_literals: true,
254 #[cfg(feature = "ldm")]
255 ldm_params: None,
256 },
257 -6 => LevelParams {
261 strategy: Strategy::Fast,
262 window_log: 19,
263 hash_log: 13,
264 chain_log: 13,
265 search_log: 0,
266 min_match: 8,
267 target_length: 9,
268 search_strength: 5,
269 force_raw_literals: false,
270 #[cfg(feature = "ldm")]
271 ldm_params: None,
272 },
273 -5 => LevelParams {
274 strategy: Strategy::Fast,
275 window_log: 19,
276 hash_log: 13,
277 chain_log: 13,
278 search_log: 0,
279 min_match: 8,
280 target_length: 5,
281 search_strength: 6,
282 force_raw_literals: false,
283 #[cfg(feature = "ldm")]
284 ldm_params: None,
285 },
286 -4 => LevelParams {
287 strategy: Strategy::Fast,
288 window_log: 19,
289 hash_log: 13,
290 chain_log: 13,
291 search_log: 0,
292 min_match: 7,
293 target_length: 4,
294 search_strength: 5,
295 force_raw_literals: false,
296 #[cfg(feature = "ldm")]
297 ldm_params: None,
298 },
299 -3 => LevelParams {
303 strategy: Strategy::Fast,
304 window_log: 19,
305 hash_log: 14,
306 chain_log: 14,
307 search_log: 0,
308 min_match: 6,
309 target_length: 4,
310 search_strength: 5,
311 force_raw_literals: false,
312 #[cfg(feature = "ldm")]
313 ldm_params: None,
314 },
315 -2 => LevelParams {
316 strategy: Strategy::Fast,
317 window_log: 19,
318 hash_log: 14,
319 chain_log: 14,
320 search_log: 0,
321 min_match: 6,
322 target_length: 3,
323 search_strength: 5,
324 force_raw_literals: false,
325 #[cfg(feature = "ldm")]
326 ldm_params: None,
327 },
328 -1 => LevelParams {
329 strategy: Strategy::Fast,
330 window_log: 19,
331 hash_log: 14,
332 chain_log: 14,
333 search_log: 0,
334 min_match: 6,
335 target_length: 2,
336 search_strength: 5,
337 force_raw_literals: false,
338 #[cfg(feature = "ldm")]
339 ldm_params: None,
340 },
341 1 => LevelParams {
345 strategy: Strategy::Fast,
346 window_log: 19,
347 hash_log: 15,
348 chain_log: 15,
349 search_log: 0,
350 min_match: 7,
351 target_length: 1,
352 search_strength: 4,
353 force_raw_literals: false,
354 #[cfg(feature = "ldm")]
355 ldm_params: None,
356 },
357 2 => LevelParams {
358 strategy: Strategy::Fast,
359 window_log: 20,
360 hash_log: 16,
361 chain_log: 16,
362 search_log: 0,
363 min_match: 7,
364 target_length: 1,
365 search_strength: 4,
366 force_raw_literals: false,
367 #[cfg(feature = "ldm")]
368 ldm_params: None,
369 },
370 3 => LevelParams {
373 strategy: Strategy::DFast,
374 window_log: 21,
375 hash_log: 17,
376 chain_log: 16,
377 search_log: 1,
378 min_match: 5,
379 target_length: 1,
380 search_strength: 0,
381 force_raw_literals: false,
382 #[cfg(feature = "ldm")]
383 ldm_params: None,
384 },
385 4 => LevelParams {
388 strategy: Strategy::DFast,
389 window_log: 24,
390 hash_log: 18,
391 chain_log: 18,
392 search_log: 0,
393 min_match: 5,
394 target_length: 1,
395 search_strength: 3,
396 force_raw_literals: false,
397 #[cfg(feature = "ldm")]
398 ldm_params: None,
399 },
400 _ => return None,
401 })
402}
403
404#[derive(Debug, Clone, Default)]
409pub struct Options {
410 pub(crate) window_log: Option<u32>,
411 #[cfg_attr(not(feature = "ldm"), allow(dead_code))]
412 pub(crate) ldm: bool,
413}
414
415impl Options {
416 #[must_use]
417 pub fn window_log(mut self, log: u32) -> Self {
418 self.window_log = Some(log);
419 self
420 }
421
422 #[cfg(feature = "ldm")]
425 #[must_use]
426 pub fn ldm(mut self, enable: bool) -> Self {
427 self.ldm = enable;
428 self
429 }
430}
431
432pub fn apply_options(params: &mut LevelParams, opts: &Options) {
433 if let Some(wl) = opts.window_log {
434 params.window_log = wl.clamp(WINDOW_LOG_MIN, WINDOW_LOG_MAX);
435 }
436 #[cfg(feature = "ldm")]
437 if opts.ldm {
438 if opts.window_log.is_none() {
439 params.window_log = LDM_DEFAULT_WINDOW_LOG;
440 }
441 params.ldm_params = Some(LdmParams::default_for_window_log(params.window_log));
442 }
443}