1
2
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
use crate::bit_io::{BitReader, BitReaderReversed};
use crate::decoding::errors::{FSEDecoderError, FSETableError};
use alloc::vec::Vec;
use core::ptr;
pub struct FSEDecoder<'table> {
/// An FSE state value represents an index in the FSE table.
pub state: Entry,
/// A reference to the table used for decoding.
table: &'table FSETable,
}
impl<'t> FSEDecoder<'t> {
/// Initialize a new Finite State Entropy decoder.
pub fn new(table: &'t FSETable) -> FSEDecoder<'t> {
FSEDecoder {
state: table.decode.first().copied().unwrap_or(Entry {
new_state: 0,
symbol: 0,
num_bits: 0,
}),
table,
}
}
/// Returns the byte associated with the symbol the internal cursor is pointing at.
pub fn decode_symbol(&self) -> u8 {
self.state.symbol
}
/// Initialize internal state and prepare for decoding. After this, `decode_symbol` can be called
/// to read the first symbol and `update_state` can be called to prepare to read the next symbol.
pub fn init_state(&mut self, bits: &mut BitReaderReversed<'_>) -> Result<(), FSEDecoderError> {
if self.table.accuracy_log == 0 {
return Err(FSEDecoderError::TableIsUninitialized);
}
// Externally-constructible-table guard: when
// `feature = "fuzz_exports"` is on, `FSETable.decode` /
// `FSETable.accuracy_log` are settable from outside the crate,
// so a fuzz harness can hand the decoder a mis-shaped table
// that skips `build_decoding_table`'s invariants. Validate the
// table-shape invariant `decode.len() == 1 << accuracy_log`
// up-front and surface as a typed `InvalidTableShape` error
// (distinct from `TableIsUninitialized` to keep fuzz triage
// unambiguous) — without this, `read_entry`'s bounds-checked
// indexing under the same cfg would panic on a malformed
// table, which fuzz harnesses cannot distinguish from a
// legitimate decoder failure. `checked_shl` covers the
// pathological case where `accuracy_log >= usize::BITS`.
#[cfg(feature = "fuzz_exports")]
{
let accuracy_log = self.table.accuracy_log;
let decode_len = self.table.decode.len();
let expected = 1usize.checked_shl(accuracy_log.into()).ok_or(
FSEDecoderError::InvalidTableShape {
decode_len,
accuracy_log,
},
)?;
if decode_len != expected {
return Err(FSEDecoderError::InvalidTableShape {
decode_len,
accuracy_log,
});
}
}
let new_state = bits.get_bits(self.table.accuracy_log);
// SAFETY: `accuracy_log` bits read from the bitstream produce
// `new_state < (1 << accuracy_log) = table_size = decode.len()`.
// `build_decoding_table` ensures the table is sized exactly
// `1 << accuracy_log` entries. The bounds check that the
// checked indexing would emit is provably redundant. Under
// `feature = "fuzz_exports"` `read_entry` falls back to the
// bounds-checked path — see comment on `read_entry`.
self.state = self.read_entry(new_state as usize);
Ok(())
}
/// Advance the internal state to decode the next symbol in the bitstream.
///
/// # Panics
///
/// Panics if called on an `FSEDecoder` whose backing `FSETable` has
/// not been built yet (empty `decode` vec). `FSEDecoder::new`
/// produces such a decoder with a zero-default `state`; the
/// well-behaved pipeline is `new` → `init_state` → `update_state*`,
/// and `init_state` returns `Err` on an uninitialized table. This
/// assertion converts what would otherwise be UB (from the
/// unchecked indexing in `read_entry`) into a clear fail-fast
/// panic that surfaces the API misuse immediately instead of
/// leaving the bitstream and decode state silently desynchronised.
pub fn update_state(&mut self, bits: &mut BitReaderReversed<'_>) {
// Public-API safety guard: `FSEDecoder::new` builds a decoder
// with a zero-default `state` (Entry { new_state: 0, num_bits:
// 0, symbol: 0 }) regardless of whether the table was actually
// populated. A caller that constructs the decoder and then
// calls `update_state` BEFORE a successful `init_state` would
// hit `read_entry(0)` → `get_unchecked(0)` on an empty
// `decode` vec — UB in release mode, since `debug_assert!` is
// stripped. Fail-fast with `assert!` instead of silently
// returning so that misuse surfaces immediately rather than
// leaving the bitstream advanced by some bits but the decode
// state stuck at the zero-default Entry — a corruption mode
// that the caller has no way to diagnose. The well-behaved
// decode pipeline always pairs `new` → `init_state` →
// `update_state*`, so this branch is strongly biased "not
// taken" and the predictor amortises it to zero cost on the
// hot path. The corresponding `update_state_fast` is
// `pub(crate)` with controlled callers, so it relies on the
// documented precondition instead of paying for a per-call
// check.
assert!(
!self.table.decode.is_empty(),
concat!(
"FSEDecoder::update_state called on an uninitialized table; ",
"call init_state successfully before any update_state* call",
),
);
let num_bits = self.state.num_bits;
let add = bits.get_bits(num_bits);
let next_state = usize::from(self.state.new_state) + add as usize;
// SAFETY: same invariant as `update_state_fast` below —
// `new_state` and `num_bits` were paired by
// `calc_baseline_and_numbits` during table construction such
// that `new_state + (1 << num_bits) - 1 < table_size =
// decode.len()`. `add < 1 << num_bits` by definition of the
// `num_bits`-wide read, so `next_state < decode.len()`.
self.state = self.read_entry(next_state);
}
/// Read `decode[idx]` — bounds-checked under `fuzz_exports`, unchecked
/// otherwise. The call sites all hold the FSE invariant `idx <
/// decode.len()` by construction (`init_state` reads
/// `accuracy_log` bits, `update_state*` derive `next_state` from
/// `Entry.new_state + add` where `calc_baseline_and_numbits`
/// guarantees `new_state + (1 << num_bits) - 1 < table_size`).
/// Under `fuzz_exports` external code can construct a mis-shaped
/// table that violates the invariant — fall back to checked
/// indexing so a fuzz harness sees a panic rather than UB, even
/// when the fuzz binary is built in release mode (which makes
/// `debug_assert!` a no-op and is the default for `cargo fuzz`).
#[inline(always)]
fn read_entry(&self, idx: usize) -> Entry {
#[cfg(feature = "fuzz_exports")]
{
self.table.decode[idx]
}
#[cfg(not(feature = "fuzz_exports"))]
// SAFETY: see comments at the individual call sites — `idx` is
// invariant-bounded by the FSE table-build / state-transition
// contract. LLVM cannot prove this on its own because the
// invariant spans `build_decoding_table` and decode call sites.
unsafe {
*self.table.decode.get_unchecked(idx)
}
}
/// Advance the internal state **without** an individual refill check.
///
/// # Preconditions (caller-enforced)
///
/// 1. **Bit budget:** enough bits MUST be available in the bit
/// reader (e.g. via [`BitReaderReversed::ensure_bits`] with a
/// budget that covers this and any other unchecked reads in the
/// same batch).
/// 2. **State initialisation:** [`init_state`] MUST have returned
/// `Ok` on this decoder before any `update_state_fast` call.
/// Calling `update_state_fast` on a fresh `FSEDecoder::new`
/// output (which holds a zero-default `state` and may reference
/// an empty `decode` vec) would resolve to
/// `read_entry(0).get_unchecked(0)` on an empty slice — UB.
/// The empty-table guard in [`update_state`] is intentionally
/// omitted here to keep the per-sequence fast path branch-free;
/// the only call site (`decode_and_execute_sequences`) always
/// succeeds `init_state` before entering the per-sequence loop,
/// so the precondition holds by construction.
///
/// This is the "fast path" used in the interleaved sequence decode loop
/// where a single refill check covers all three FSE state updates.
///
/// [`init_state`]: Self::init_state
/// [`update_state`]: Self::update_state
#[inline(always)]
pub(crate) fn update_state_fast(&mut self, bits: &mut BitReaderReversed<'_>) {
let num_bits = self.state.num_bits;
let add = bits.get_bits_unchecked(num_bits);
let next_state = usize::from(self.state.new_state) + add as usize;
// SAFETY: `new_state` and `num_bits` were paired by
// `calc_baseline_and_numbits` during table construction such that
// `new_state + (2.pow(num_bits) - 1) < table_size = self.table.decode.len()`.
// `add` is the value of `num_bits` bits read from the bitstream, so
// `add < 2.pow(num_bits)` by construction of `BitReaderReversed::get_bits_unchecked`.
// Therefore `next_state < self.table.decode.len()` and the indexed read
// is in bounds; LLVM cannot prove this invariant on its own because it
// spans the table-build and decode call sites. Under
// `feature = "fuzz_exports"` `read_entry` falls back to bounds-checked
// indexing — see comment on `read_entry`.
self.state = self.read_entry(next_state);
}
}
/// FSE decoding involves a decoding table that describes the probabilities of
/// all literals from 0 to the highest present one
///
/// <https://github.com/facebook/zstd/blob/dev/doc/zstd_compression_format.md#fse-table-description>
#[derive(Debug, Clone)]
pub struct FSETable {
/// The maximum symbol in the table (inclusive). Limits the probabilities length to max_symbol + 1.
max_symbol: u8,
/// The actual table containing the decoded symbol and the compression data
/// connected to that symbol.
pub decode: Vec<Entry>, //used to decode symbols, and calculate the next state
/// Reused scratch buffer for symbol spreading to avoid per-build allocations.
symbol_spread_buffer: Vec<u8>,
/// The size of the table is stored in logarithm base 2 format,
/// with the **size of the table** being equal to `(1 << accuracy_log)`.
/// This value is used so that the decoder knows how many bits to read from the bitstream.
pub accuracy_log: u8,
/// In this context, probability refers to the likelihood that a symbol occurs in the given data.
/// Given this info, the encoder can assign shorter codes to symbols that appear more often,
/// and longer codes that appear less often, then the decoder can use the probability
/// to determine what code was assigned to what symbol.
///
/// The probability of a single symbol is a value representing the proportion of times the symbol
/// would fall within the data.
///
/// If a symbol probability is set to `-1`, it means that the probability of a symbol
/// occurring in the data is less than one.
pub symbol_probabilities: Vec<i32>, //used while building the decode Vector
/// The number of times each symbol occurs (The first entry being 0x0, the second being 0x1) and so on
/// up until the highest possible symbol (255).
symbol_counter: Vec<u32>,
}
impl FSETable {
/// Initialize a new empty Finite State Entropy decoding table.
pub fn new(max_symbol: u8) -> FSETable {
FSETable {
max_symbol,
symbol_probabilities: Vec::with_capacity(256), //will never be more than 256 symbols because u8
symbol_counter: Vec::with_capacity(256), //will never be more than 256 symbols because u8
symbol_spread_buffer: Vec::new(),
decode: Vec::new(), //depending on acc_log.
accuracy_log: 0,
}
}
/// Reset `self` and update `self`'s state to mirror the provided table.
pub fn reinit_from(&mut self, other: &Self) {
self.reset();
self.symbol_counter.extend_from_slice(&other.symbol_counter);
self.symbol_probabilities
.extend_from_slice(&other.symbol_probabilities);
self.symbol_spread_buffer
.reserve(other.symbol_spread_buffer.len());
self.decode.extend_from_slice(&other.decode);
self.accuracy_log = other.accuracy_log;
}
/// Empty the table and clear all internal state.
pub fn reset(&mut self) {
self.symbol_counter.clear();
self.symbol_probabilities.clear();
self.symbol_spread_buffer.clear();
self.decode.clear();
self.accuracy_log = 0;
}
/// Build the equivalent encoder-side table from a parsed decoder table.
pub(crate) fn to_encoder_table(&self) -> Option<crate::fse::fse_encoder::FSETable> {
if self.accuracy_log == 0 || self.symbol_probabilities.is_empty() {
return None;
}
Some(crate::fse::fse_encoder::build_table_from_probabilities(
&self.symbol_probabilities,
self.accuracy_log,
))
}
/// returns how many BYTEs (not bits) were read while building the decoder
pub fn build_decoder(&mut self, source: &[u8], max_log: u8) -> Result<usize, FSETableError> {
let max_log = max_log.min(ENTRY_MAX_ACCURACY_LOG);
self.accuracy_log = 0;
let bytes_read = self.read_probabilities(source, max_log)?;
self.build_decoding_table()?;
Ok(bytes_read)
}
/// Given the provided accuracy log, build a decoding table from that log.
pub fn build_from_probabilities(
&mut self,
acc_log: u8,
probs: &[i32],
) -> Result<(), FSETableError> {
if acc_log == 0 {
return Err(FSETableError::AccLogIsZero);
}
if acc_log > ENTRY_MAX_ACCURACY_LOG {
return Err(FSETableError::AccLogTooBig {
got: acc_log,
max: ENTRY_MAX_ACCURACY_LOG,
});
}
self.symbol_probabilities = probs.to_vec();
self.accuracy_log = acc_log;
self.build_decoding_table()
}
/// Build the actual decoding table after probabilities have been read into the table.
/// After this function is called, the decoding process can begin.
fn build_decoding_table(&mut self) -> Result<(), FSETableError> {
if self.symbol_probabilities.len() > self.max_symbol as usize + 1 {
return Err(FSETableError::TooManySymbols {
got: self.symbol_probabilities.len(),
});
}
self.decode.clear();
let table_size = 1 << self.accuracy_log;
// After clear(), len == 0, so reserve(table_size) guarantees capacity
// ≥ table_size. The matching `set_len` runs later, right before
// `copy_symbols_into_decode`, so that any panic in the intervening
// table_symbols / symbol_spread_buffer allocations unwinds with
// `self.decode.len() == 0` instead of leaving uninitialized Entry
// slots reachable through `&mut self.decode[..]` on the next call.
self.decode.reserve(table_size);
let mut table_symbols = core::mem::take(&mut self.symbol_spread_buffer);
table_symbols.clear();
table_symbols.resize(table_size, 0);
let negative_idx = {
let table_symbols = &mut table_symbols;
let mut negative_idx = table_size; //will point to the highest index with is already occupied by a negative-probability-symbol
//first scan for all -1 probabilities and place them at the top of the table
for symbol in 0..self.symbol_probabilities.len() {
if self.symbol_probabilities[symbol] == -1 {
negative_idx -= 1;
table_symbols[negative_idx] = symbol as u8;
}
}
//then place in a semi-random order all of the other symbols
let mut position = 0;
for idx in 0..self.symbol_probabilities.len() {
let symbol = idx as u8;
if self.symbol_probabilities[idx] <= 0 {
continue;
}
//for each probability point the symbol gets on slot
let prob = self.symbol_probabilities[idx];
for _ in 0..prob {
table_symbols[position] = symbol;
position = next_position(position, table_size);
while position >= negative_idx {
position = next_position(position, table_size);
//everything above negative_idx is already taken
}
}
}
negative_idx
};
// `copy_symbols_into_decode` is responsible for sizing
// `self.decode` AND writing every slot in 0..table_size before
// it returns. Keeping the set_len call adjacent to the init
// loop (rather than pre-extending here) is what guarantees no
// panic-able code can run between "tell Vec the length is
// table_size" and "every slot has a fully-initialized Entry".
// capacity is reserved earlier in this function.
self.copy_symbols_into_decode(&table_symbols, table_size);
self.symbol_spread_buffer = table_symbols;
for idx in negative_idx..table_size {
self.decode[idx].num_bits = self.accuracy_log;
}
// baselines and num_bits can only be calculated when all symbols have been spread
self.symbol_counter.clear();
self.symbol_counter
.resize(self.symbol_probabilities.len(), 0);
for idx in 0..negative_idx {
let entry = &mut self.decode[idx];
let symbol = entry.symbol;
let prob = self.symbol_probabilities[symbol as usize];
let symbol_count = self.symbol_counter[symbol as usize];
let (bl, nb) = calc_baseline_and_numbits(table_size as u32, prob as u32, symbol_count);
//println!("symbol: {:2}, table: {}, prob: {:3}, count: {:3}, bl: {:3}, nb: {:2}", symbol, table_size, prob, symbol_count, bl, nb);
assert!(nb <= self.accuracy_log);
self.symbol_counter[symbol as usize] += 1;
entry.new_state = u16::try_from(bl).map_err(|_| FSETableError::AccLogTooBig {
got: self.accuracy_log,
max: ENTRY_MAX_ACCURACY_LOG,
})?;
entry.num_bits = nb;
}
Ok(())
}
fn copy_symbols_into_decode(&mut self, table_symbols: &[u8], table_size: usize) {
debug_assert_eq!(table_symbols.len(), table_size);
debug_assert!(table_size <= self.decode.capacity());
#[cfg(target_endian = "little")]
{
debug_assert_eq!(core::mem::size_of::<Entry>(), 4);
debug_assert_eq!(core::mem::offset_of!(Entry, new_state), 0);
debug_assert_eq!(core::mem::offset_of!(Entry, symbol), 2);
debug_assert_eq!(core::mem::offset_of!(Entry, num_bits), 3);
// SAFETY: capacity is guaranteed by the caller; the init
// loop immediately below writes a full Entry to every slot
// in 0..table_size via raw `ptr::write_unaligned`. The
// set_len + writes form a single panic-free window —
// nothing in between this call and the loop body can unwind
// (no allocations, no user-visible &mut self.decode[..],
// no borrows that could observe uninitialized slots).
unsafe {
self.decode.set_len(table_size);
}
// Write two packed entries (8 bytes) at once:
// Entry bytes are [new_state_lo, new_state_hi, symbol, num_bits].
let mut idx = 0usize;
while idx + 1 < table_size {
let packed =
((table_symbols[idx] as u64) << 16) | ((table_symbols[idx + 1] as u64) << 48);
// SAFETY: idx + 1 < table_size == self.decode.len()
// (just set above) and capacity covers two u32 slots.
// Unaligned writes are intentional because `Entry`
// alignment may be < 8.
unsafe {
ptr::write_unaligned(self.decode.as_mut_ptr().add(idx).cast::<u64>(), packed);
}
idx += 2;
}
if idx < table_size {
// Trailing odd entry: write a full 4-byte Entry { new_state: 0,
// symbol, num_bits: 0 } via a single u32 store. Field assignment
// (`self.decode[idx].symbol = …`) would have left new_state /
// num_bits uninitialized after the set_len above — leaking UB
// until the per-entry baseline pass overwrote them.
let packed = (table_symbols[idx] as u32) << 16;
// SAFETY: idx < table_size == self.decode.len(), capacity
// covers a u32 slot. Unaligned write is intentional
// because `Entry`'s alignment may be < 4 on some targets.
unsafe {
ptr::write_unaligned(self.decode.as_mut_ptr().add(idx).cast::<u32>(), packed);
}
}
}
#[cfg(not(target_endian = "little"))]
{
// BE path uses iter_mut(), which constructs `&mut Entry` and
// therefore needs initialized storage — resize-with-default
// is the natural shape. No production target ships BE so
// the extra zero-fill is irrelevant.
self.decode.resize(
table_size,
Entry {
new_state: 0,
symbol: 0,
num_bits: 0,
},
);
for (entry, symbol) in self.decode.iter_mut().zip(table_symbols.iter().copied()) {
entry.symbol = symbol;
}
}
}
/// Read the accuracy log and the probability table from the source and return the number of bytes
/// read. If the size of the table is larger than the provided `max_log`, return an error.
fn read_probabilities(&mut self, source: &[u8], max_log: u8) -> Result<usize, FSETableError> {
self.symbol_probabilities.clear(); //just clear, we will fill a probability for each entry anyways. No need to force new allocs here
let mut br = BitReader::new(source);
self.accuracy_log = ACC_LOG_OFFSET + (br.get_bits(4)? as u8);
if self.accuracy_log > ENTRY_MAX_ACCURACY_LOG {
return Err(FSETableError::AccLogTooBig {
got: self.accuracy_log,
max: ENTRY_MAX_ACCURACY_LOG,
});
}
if self.accuracy_log > max_log {
return Err(FSETableError::AccLogTooBig {
got: self.accuracy_log,
max: max_log,
});
}
if self.accuracy_log == 0 {
return Err(FSETableError::AccLogIsZero);
}
let probability_sum = 1 << self.accuracy_log;
let mut probability_counter = 0;
while probability_counter < probability_sum {
let max_remaining_value = probability_sum - probability_counter + 1;
let bits_to_read = highest_bit_set(max_remaining_value);
let unchecked_value = br.get_bits(bits_to_read as usize)? as u32;
let low_threshold = ((1 << bits_to_read) - 1) - (max_remaining_value);
let mask = (1 << (bits_to_read - 1)) - 1;
let small_value = unchecked_value & mask;
let value = if small_value < low_threshold {
br.return_bits(1);
small_value
} else if unchecked_value > mask {
unchecked_value - low_threshold
} else {
unchecked_value
};
//println!("{}, {}, {}", self.symbol_probablilities.len(), unchecked_value, value);
let prob = (value as i32) - 1;
self.symbol_probabilities.push(prob);
if prob != 0 {
if prob > 0 {
probability_counter += prob as u32;
} else {
// probability -1 counts as 1
assert!(prob == -1);
probability_counter += 1;
}
} else {
//fast skip further zero probabilities
loop {
let skip_amount = br.get_bits(2)? as usize;
self.symbol_probabilities
.resize(self.symbol_probabilities.len() + skip_amount, 0);
if skip_amount != 3 {
break;
}
}
}
}
if probability_counter != probability_sum {
return Err(FSETableError::ProbabilityCounterMismatch {
got: probability_counter,
expected_sum: probability_sum,
symbol_probabilities: self.symbol_probabilities.clone(),
});
}
if self.symbol_probabilities.len() > self.max_symbol as usize + 1 {
return Err(FSETableError::TooManySymbols {
got: self.symbol_probabilities.len(),
});
}
let bytes_read = if br.bits_read().is_multiple_of(8) {
br.bits_read() / 8
} else {
(br.bits_read() / 8) + 1
};
Ok(bytes_read)
}
}
/// A single entry in an FSE table.
#[repr(C)]
#[derive(Copy, Clone, Debug)]
pub struct Entry {
/// Base index for the next state. The low bits read from the bitstream are
/// added to this value to produce the final state index.
pub new_state: u16,
/// The byte that should be put in the decode output when encountering this state.
pub symbol: u8,
/// How many bits should be read from the stream when decoding this entry.
pub num_bits: u8,
}
#[cfg(target_endian = "little")]
const _: [(); 0] = [(); core::mem::offset_of!(Entry, new_state)];
#[cfg(target_endian = "little")]
const _: [(); 2] = [(); core::mem::offset_of!(Entry, symbol)];
#[cfg(target_endian = "little")]
const _: [(); 3] = [(); core::mem::offset_of!(Entry, num_bits)];
#[cfg(target_endian = "little")]
const _: [(); 4] = [(); core::mem::size_of::<Entry>()];
/// This value is added to the first 4 bits of the stream to determine the
/// `Accuracy_Log`
const ACC_LOG_OFFSET: u8 = 5;
const ENTRY_MAX_ACCURACY_LOG: u8 = 16;
fn highest_bit_set(x: u32) -> u32 {
assert!(x > 0);
u32::BITS - x.leading_zeros()
}
//utility functions for building the decoding table from probabilities
/// Calculate the position of the next entry of the table given the current
/// position and size of the table.
fn next_position(mut p: usize, table_size: usize) -> usize {
p += (table_size >> 1) + (table_size >> 3) + 3;
p &= table_size - 1;
p
}
fn calc_baseline_and_numbits(
num_states_total: u32,
num_states_symbol: u32,
state_number: u32,
) -> (u32, u8) {
if num_states_symbol == 0 {
return (0, 0);
}
let num_state_slices = if 1 << (highest_bit_set(num_states_symbol) - 1) == num_states_symbol {
num_states_symbol
} else {
1 << (highest_bit_set(num_states_symbol))
}; //always power of two
let num_double_width_state_slices = num_state_slices - num_states_symbol; //leftovers to the power of two need to be distributed
let num_single_width_state_slices = num_states_symbol - num_double_width_state_slices; //these will not receive a double width slice of states
let slice_width = num_states_total / num_state_slices; //size of a single width slice of states
let num_bits = highest_bit_set(slice_width) - 1; //number of bits needed to read for one slice
if state_number < num_double_width_state_slices {
let baseline = num_single_width_state_slices * slice_width + state_number * slice_width * 2;
(baseline, num_bits as u8 + 1)
} else {
let index_shifted = state_number - num_double_width_state_slices;
((index_shifted * slice_width), num_bits as u8)
}
}