arithmetic_coding_core/
bitstore.rs

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
use std::ops::{Add, AddAssign, Div, Mul, Shl, ShlAssign, Sub};

/// A trait for a type that can be used for the internal integer representation
/// of an encoder or decoder
pub trait BitStore:
    Shl<u32, Output = Self>
    + ShlAssign<u32>
    + Sub<Output = Self>
    + Add<Output = Self>
    + Mul<Output = Self>
    + Div<Output = Self>
    + AddAssign
    + PartialOrd
    + Copy
    + std::fmt::Debug
{
    /// the number of bits needed to represent this type
    const BITS: u32;

    /// the additive identity
    const ZERO: Self;

    /// the multiplicative identity
    const ONE: Self;

    /// integer natural logarithm, rounded down
    fn log2(self) -> u32;
}

macro_rules! impl_bitstore {
    ($t:ty) => {
        impl BitStore for $t {
            const BITS: u32 = Self::BITS;
            const ONE: Self = 1;
            const ZERO: Self = 0;

            fn log2(self) -> u32 {
                Self::ilog2(self)
            }
        }
    };
}

impl_bitstore! {u32}
impl_bitstore! {u64}
impl_bitstore! {u128}
impl_bitstore! {usize}