refactor(decompress): move sequence mode policy to Rust
Keep the C build-time mutual-exclusion check and private decoder-context assembly in the C shim, but pass the selected force-short/force-long policy through Rust. Rust now owns normalization to the runtime, short, or long sequence decoder mode, with ABI assertions and focused null/build-policy tests. Test Plan: Not run in this atomic commit; the capped serial Rust, native, smoke, and original test-suite verification follows.
This commit is contained in:
@@ -33,7 +33,7 @@ use crate::mem::{MEM_32bits, MEM_64bits, MEM_readLE16, MEM_readLE24, U32};
|
||||
use crate::zstd_decompress::ZSTD_DCtx;
|
||||
use std::cmp::min;
|
||||
use std::ffi::c_void;
|
||||
use std::mem::MaybeUninit;
|
||||
use std::mem::{offset_of, size_of, MaybeUninit};
|
||||
use std::os::raw::{c_int, c_short, c_uint};
|
||||
use std::ptr;
|
||||
use std::sync::OnceLock;
|
||||
@@ -68,6 +68,47 @@ const LONG_OFFSET_HISTORY_THRESHOLD: usize = 1 << 24;
|
||||
const LONG_OFFSET_MIN_SHARE_32: u32 = 20;
|
||||
const LONG_OFFSET_MIN_SHARE_64: u32 = 7;
|
||||
|
||||
/// Build-system sequence-decoder choices passed without exposing `ZSTD_DCtx`.
|
||||
#[repr(C)]
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
|
||||
pub struct ZSTD_rustSequenceDecoderPolicy {
|
||||
force_short: c_int,
|
||||
force_long: c_int,
|
||||
}
|
||||
|
||||
const _: () = {
|
||||
assert!(offset_of!(ZSTD_rustSequenceDecoderPolicy, force_short) == 0);
|
||||
assert!(
|
||||
offset_of!(ZSTD_rustSequenceDecoderPolicy, force_long) == size_of::<c_int>()
|
||||
);
|
||||
assert!(size_of::<ZSTD_rustSequenceDecoderPolicy>() == 2 * size_of::<c_int>());
|
||||
};
|
||||
|
||||
#[inline]
|
||||
fn sequence_decoder_mode(policy: &ZSTD_rustSequenceDecoderPolicy) -> c_int {
|
||||
if policy.force_short != 0 {
|
||||
SEQUENCE_DECODER_FORCE_SHORT
|
||||
} else if policy.force_long != 0 {
|
||||
SEQUENCE_DECODER_FORCE_LONG
|
||||
} else {
|
||||
SEQUENCE_DECODER_RUNTIME
|
||||
}
|
||||
}
|
||||
|
||||
/// Normalize the build's sequence-decoder choice before block dispatch.
|
||||
///
|
||||
/// C retains the compile-time mutual-exclusion check and private context
|
||||
/// projection; Rust owns the scalar mode selected by that public build policy.
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn ZSTD_rust_sequence_decoder_mode(
|
||||
policy: *const ZSTD_rustSequenceDecoderPolicy,
|
||||
) -> c_int {
|
||||
let Some(policy) = (unsafe { policy.as_ref() }) else {
|
||||
return SEQUENCE_DECODER_RUNTIME;
|
||||
};
|
||||
sequence_decoder_mode(policy)
|
||||
}
|
||||
|
||||
const LL_BASE: [u32; MAX_LL + 1] = [
|
||||
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 18, 20, 22, 24, 28, 32, 40, 48, 64,
|
||||
0x80, 0x100, 0x200, 0x400, 0x800, 0x1000, 0x2000, 0x4000, 0x8000, 0x10000,
|
||||
@@ -1892,6 +1933,34 @@ pub unsafe extern "C" fn ZSTD_decompressBlock(
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn sequence_decoder_mode_preserves_build_forcing_policy() {
|
||||
let runtime = ZSTD_rustSequenceDecoderPolicy {
|
||||
force_short: 0,
|
||||
force_long: 0,
|
||||
};
|
||||
let short = ZSTD_rustSequenceDecoderPolicy {
|
||||
force_short: 1,
|
||||
force_long: 0,
|
||||
};
|
||||
let long = ZSTD_rustSequenceDecoderPolicy {
|
||||
force_short: 0,
|
||||
force_long: 1,
|
||||
};
|
||||
|
||||
assert_eq!(sequence_decoder_mode(&runtime), SEQUENCE_DECODER_RUNTIME);
|
||||
assert_eq!(sequence_decoder_mode(&short), SEQUENCE_DECODER_FORCE_SHORT);
|
||||
assert_eq!(sequence_decoder_mode(&long), SEQUENCE_DECODER_FORCE_LONG);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sequence_decoder_mode_null_policy_defaults_to_runtime() {
|
||||
assert_eq!(
|
||||
unsafe { ZSTD_rust_sequence_decoder_mode(ptr::null()) },
|
||||
SEQUENCE_DECODER_RUNTIME
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn offset_code_22_and_23_keep_their_22_and_23_bit_bases() {
|
||||
assert_eq!(OF_BASE[22], 0x3F_FFFD);
|
||||
|
||||
Reference in New Issue
Block a user