Files
zstd-rs/rust/src/common.rs
T
ddidderr 089f8e5b4d feat(rust): add common primitive compatibility layer
Introduce the Rust static library and move the shared byte, bitstream, CPU,
error, xxHash, debug, and public-common implementations into it. Thin C shims
preserve the existing header-driven C build while original tests link the Rust
archive.

This establishes the ABI-safe foundation for later codec and CLI ports;
entropy coding, runtime support, codecs, dictionaries, and the CLI remain C.
The top-down migration map documents that boundary and its validation path.

Test Plan:
- cargo fmt --check
- cargo test --all-targets
- cargo clippy --all-targets -- -D warnings
- cargo build --release

Refs: rust/README.md
2026-07-10 20:02:17 +02:00

143 lines
4.1 KiB
Rust

#![allow(non_snake_case)]
use crate::mem::*;
pub const ZSTD_OPT_NUM: usize = 1 << 12;
pub const ZSTD_REP_NUM: usize = 3;
pub const REP_START_VALUE: [U32; ZSTD_REP_NUM] = [1, 4, 8];
pub const ZSTD_WINDOWLOG_ABSOLUTEMIN: usize = 10;
pub const ZSTD_FCS_FIELD_SIZE: [usize; 4] = [0, 2, 4, 8];
pub const ZSTD_DID_FIELD_SIZE: [usize; 4] = [0, 1, 2, 4];
pub const ZSTD_FRAMEIDSIZE: usize = 4;
pub const ZSTD_BLOCKHEADERSIZE: usize = 3;
pub const ZSTD_FRAMECHECKSUMSIZE: usize = 4;
pub const MIN_SEQUENCES_SIZE: usize = 1;
pub const MIN_CBLOCK_SIZE: usize = 2;
pub const MIN_LITERALS_FOR_4_STREAMS: usize = 6;
pub const LONGNBSEQ: U32 = 0x7F00;
pub const MINMATCH: usize = 3;
pub const LITBITS: usize = 8;
pub const LIT_HUF_LOG: usize = 11;
pub const MAX_LIT: usize = (1 << LITBITS) - 1;
pub const MAX_ML: usize = 52;
pub const MAX_LL: usize = 35;
pub const DEFAULT_MAX_OFF: usize = 28;
pub const MAX_OFF: usize = 31;
pub const MAX_SEQ: usize = 52;
pub const ML_FSE_LOG: usize = 9;
pub const LL_FSE_LOG: usize = 9;
pub const OFF_FSE_LOG: usize = 8;
pub const MAX_FSE_LOG: usize = 9;
pub const MAX_ML_BITS: usize = 16;
pub const MAX_LL_BITS: usize = 16;
pub const ZSTD_MAX_HUF_HEADER_SIZE: usize = 128;
pub const ZSTD_MAX_FSE_HEADERS_SIZE: usize = 133;
pub const LL_BITS: [U8; MAX_LL + 1] = [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 3, 4, 6, 7, 8, 9, 10, 11,
12, 13, 14, 15, 16,
];
pub const LL_DEFAULT_NORM: [S16; MAX_LL + 1] = [
4, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 2, 1, 1, 1, 1, 1,
-1, -1, -1, -1,
];
pub const LL_DEFAULT_NORM_LOG: U32 = 6;
pub const ML_BITS: [U8; MAX_ML + 1] = [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1, 1, 1, 1, 2, 2, 3, 3, 4, 4, 5, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
];
pub const ML_DEFAULT_NORM: [S16; MAX_ML + 1] = [
1, 4, 3, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1,
];
pub const ML_DEFAULT_NORM_LOG: U32 = 6;
pub const OF_DEFAULT_NORM: [S16; DEFAULT_MAX_OFF + 1] = [
1, 1, 1, 1, 1, 1, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1,
];
pub const OF_DEFAULT_NORM_LOG: U32 = 5;
#[repr(C)]
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum BlockType {
Raw,
Rle,
Compressed,
Reserved,
}
#[repr(C)]
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum SymbolEncodingType {
Basic,
Rle,
Compressed,
Repeat,
}
#[repr(C)]
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum BufferMode {
Buffered = 0,
Stable = 1,
}
#[repr(C)]
pub struct FrameSizeInfo {
pub nb_blocks: usize,
pub compressed_size: usize,
pub decompressed_bound: u64,
}
#[repr(C)]
pub struct BlockProperties {
pub block_type: BlockType,
pub last_block: U32,
pub orig_size: U32,
}
#[inline]
pub fn ZSTD_cpuSupportsBmi2() -> bool {
crate::cpu::ZSTD_cpuSupportsBmi2()
}
#[cfg(test)]
mod tests {
use super::*;
use std::mem::{align_of, size_of};
#[test]
fn normalized_distributions_have_the_declared_table_sizes() {
let ll_sum: i32 = LL_DEFAULT_NORM.iter().map(|&v| i32::from(v.abs())).sum();
let ml_sum: i32 = ML_DEFAULT_NORM.iter().map(|&v| i32::from(v.abs())).sum();
let of_sum: i32 = OF_DEFAULT_NORM.iter().map(|&v| i32::from(v.abs())).sum();
assert_eq!(ll_sum, 1 << LL_DEFAULT_NORM_LOG);
assert_eq!(ml_sum, 1 << ML_DEFAULT_NORM_LOG);
assert_eq!(of_sum, 1 << OF_DEFAULT_NORM_LOG);
}
#[test]
fn c_layout_types_match_the_header() {
assert_eq!(size_of::<BlockType>(), size_of::<i32>());
assert_eq!(size_of::<SymbolEncodingType>(), size_of::<i32>());
assert_eq!(size_of::<BufferMode>(), size_of::<i32>());
assert_eq!(size_of::<FrameSizeInfo>(), 2 * size_of::<usize>() + 8);
assert_eq!(align_of::<FrameSizeInfo>(), align_of::<usize>());
assert_eq!(size_of::<BlockProperties>(), 3 * size_of::<u32>());
assert_eq!(align_of::<BlockProperties>(), align_of::<u32>());
}
}