Skip to main content

zrip_core/fse/
mod.rs

1pub mod decode;
2pub mod encode;
3pub mod table_builder;
4
5#[derive(Clone, Copy)]
6pub struct FseDecodeEntry {
7    pub base_line: u16,
8    pub num_bits: u8,
9    pub symbol: u8,
10}
11
12#[derive(Clone, Copy)]
13#[repr(C)]
14pub struct FseSeqDecodeEntry {
15    pub base_line: u16,
16    pub num_bits: u8,
17    pub extra_bits: u8,
18    pub baseline_value: u32,
19}
20
21#[derive(Clone)]
22pub struct FseEncodeEntry {
23    pub delta_nb_bits: u32,
24    pub delta_find_state: i16,
25    pub num_bits_out: u8,
26}
27
28#[derive(Clone)]
29pub struct FseTable {
30    pub entries: &'static [FseDecodeEntry],
31    pub accuracy_log: u8,
32}
33
34pub const MAX_SYMBOL: usize = 255;
35pub const MAX_TABLE_LOG: u8 = 12;
36pub const MIN_TABLE_LOG: u8 = 5;
37
38pub const LL_MAX_ACCURACY_LOG: u8 = 9;
39pub const ML_MAX_ACCURACY_LOG: u8 = 9;
40pub const OF_MAX_ACCURACY_LOG: u8 = 8;
41
42pub const LL_MAX_SYMBOL: u8 = 35;
43pub const ML_MAX_SYMBOL: u8 = 52;
44pub const OF_MAX_SYMBOL: u8 = 31;
45
46pub const LL_DEFAULT_ACCURACY: u8 = 6;
47pub const ML_DEFAULT_ACCURACY: u8 = 6;
48pub const OF_DEFAULT_ACCURACY: u8 = 5;
49
50pub static LL_DEFAULT_DIST: [i16; 36] = [
51    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,
52    -1, -1, -1, -1,
53];
54
55pub static ML_DEFAULT_DIST: [i16; 53] = [
56    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,
57    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1,
58];
59
60pub static OF_DEFAULT_DIST: [i16; 29] = [
61    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,
62];
63
64pub static LL_BITS_TABLE: [u8; 36] = [
65    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,
66    12, 13, 14, 15, 16,
67];
68
69pub static LL_BASELINE_TABLE: [u32; 36] = [
70    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,
71    128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536,
72];
73
74pub static ML_BITS_TABLE: [u8; 53] = [
75    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,
76    1, 1, 1, 1, 2, 2, 3, 3, 4, 4, 5, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
77];
78
79pub static ML_BASELINE_TABLE: [u32; 53] = [
80    3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27,
81    28, 29, 30, 31, 32, 33, 34, 35, 37, 39, 41, 43, 47, 51, 59, 67, 83, 99, 131, 259, 515, 1027,
82    2051, 4099, 8195, 16387, 32771, 65539,
83];
84
85const fn max_seq_accuracy() -> u8 {
86    let mut m = LL_MAX_ACCURACY_LOG;
87    if ML_MAX_ACCURACY_LOG > m {
88        m = ML_MAX_ACCURACY_LOG;
89    }
90    if OF_MAX_ACCURACY_LOG > m {
91        m = OF_MAX_ACCURACY_LOG;
92    }
93    m
94}
95
96pub const FSE_SEQ_TABLE_CAPACITY: usize = 1 << max_seq_accuracy();
97pub const FSE_SEQ_TABLE_MASK: u32 = FSE_SEQ_TABLE_CAPACITY as u32 - 1;
98
99pub const FSE_SEQ_DECODE_ENTRY_ZERO: FseSeqDecodeEntry = FseSeqDecodeEntry {
100    base_line: 0,
101    num_bits: 0,
102    extra_bits: 0,
103    baseline_value: 0,
104};