Skip to main content

msrtc_rans_core/
variant.rs

1// Copyright (c) Infinity Abundance.
2// Licensed under the MIT license.
3
4//! RANS variant definitions and macros.
5//!
6//! Defines the two supported rANS variants via a macro:
7//! - `RansByte`: 32-bit state, 8-bit units
8//! - `Rans64`: 64-bit state, 32-bit units
9
10/// RANS variant enum for runtime dispatch.
11#[derive(Debug, Clone, Copy, PartialEq, Eq)]
12pub enum RansVariant {
13    /// 32-bit state with 8-bit units
14    RansByte,
15    /// 64-bit state with 32-bit units
16    Rans64,
17}
18
19impl RansVariant {
20    /// Returns the size of the state type in bytes.
21    pub const fn state_size(&self) -> usize {
22        match self {
23            RansVariant::RansByte => 4,
24            RansVariant::Rans64 => 8,
25        }
26    }
27
28    /// Returns the size of the unit type in bytes.
29    pub const fn unit_size(&self) -> usize {
30        match self {
31            RansVariant::RansByte => 1,
32            RansVariant::Rans64 => 4,
33        }
34    }
35}
36
37/// Marker trait for rANS type-level parameters.
38pub trait RansParams {
39    /// The state type (u32 or u64).
40    type State: Copy + Clone + Default + core::fmt::Debug + PartialEq + Eq;
41    /// The unit type (u8 or u32).
42    type Unit: Copy + Clone + Default + core::fmt::Debug + PartialEq + Eq;
43    /// Human-readable name.
44    const NAME: &'static str;
45    /// STATE_BITS
46    const STATE_BITS: u32;
47    /// MAX_SCALE_BITS
48    const MAX_SCALE_BITS: u32;
49    /// LOWER_BOUND
50    const LOWER_BOUND: Self::State;
51    /// UNIT_BITS
52    const UNIT_BITS: u32;
53    /// UNITS_PER_STATE
54    const UNITS_PER_STATE: usize;
55}
56
57/// RansByte variant: u32 state, u8 unit.
58#[derive(Debug, Clone, Copy, PartialEq, Eq)]
59pub struct RansByte;
60
61impl RansParams for RansByte {
62    type State = u32;
63    type Unit = u8;
64    const NAME: &'static str = "RansByte";
65    const STATE_BITS: u32 = 31;
66    const MAX_SCALE_BITS: u32 = 30;
67    const LOWER_BOUND: u32 = 1u32 << 23;
68    const UNIT_BITS: u32 = 8;
69    const UNITS_PER_STATE: usize = 4;
70}
71
72/// Rans64 variant: u64 state, u32 unit.
73#[derive(Debug, Clone, Copy, PartialEq, Eq)]
74pub struct Rans64;
75
76impl RansParams for Rans64 {
77    type State = u64;
78    type Unit = u32;
79    const NAME: &'static str = "Rans64";
80    const STATE_BITS: u32 = 63;
81    const MAX_SCALE_BITS: u32 = 32;
82    const LOWER_BOUND: u64 = 1u64 << 31;
83    const UNIT_BITS: u32 = 32;
84    const UNITS_PER_STATE: usize = 2;
85}