Skip to main content

ruvector_dither/
pi.rs

1//! π-digit dither: cyclic table of the first 256 digits of π scaled to [-0.5, 0.5].
2//!
3//! Period = 256.  Each entry is an independent offset making the sequence
4//! suitable for small buffers where you want exact reproducibility from a
5//! named tensor / layer rather than a stateful RNG.
6
7use crate::DitherSource;
8
9/// First 256 bytes of π (hex digits 3.243F6A8885A308D3…).
10///
11/// Each byte spans [0, 255]; we map to [-0.5, 0.5] by `(b as f32 / 255.0) - 0.5`.
12#[rustfmt::skip]
13const PI_BYTES: [u8; 256] = [
14    0x32, 0x43, 0xF6, 0xA8, 0x88, 0x5A, 0x30, 0x8D, 0x31, 0x31, 0x98, 0xA2,
15    0xE0, 0x37, 0x07, 0x34, 0x4A, 0x40, 0x93, 0x82, 0x22, 0x99, 0xF3, 0x1D,
16    0x00, 0x82, 0xEF, 0xA9, 0x8E, 0xC4, 0xE6, 0xC8, 0x94, 0x52, 0x21, 0xE6,
17    0x38, 0xD0, 0x13, 0x77, 0xBE, 0x54, 0x66, 0xCF, 0x34, 0xE9, 0x0C, 0x6C,
18    0xC0, 0xAC, 0x29, 0xB7, 0xC9, 0x7C, 0x50, 0xDD, 0x3F, 0x84, 0xD5, 0xB5,
19    0xB5, 0x47, 0x09, 0x17, 0x92, 0x16, 0xD5, 0xD9, 0x89, 0x79, 0xFB, 0x1B,
20    0xD1, 0x31, 0x0B, 0xA6, 0x98, 0xDF, 0xB5, 0xAC, 0x2F, 0xFD, 0x72, 0xDB,
21    0xD0, 0x1A, 0xDF, 0xB7, 0xB8, 0xE1, 0xAF, 0xED, 0x6A, 0x26, 0x7E, 0x96,
22    0xBA, 0x7C, 0x90, 0x45, 0xF1, 0x2C, 0x7F, 0x99, 0x24, 0xA1, 0x99, 0x47,
23    0xB3, 0x91, 0x6C, 0xF7, 0x08, 0x01, 0xF2, 0xE2, 0x85, 0x8E, 0xFC, 0x16,
24    0x63, 0x69, 0x20, 0xD8, 0x71, 0x57, 0x4E, 0x69, 0xA4, 0x58, 0xFE, 0xA3,
25    0xF4, 0x93, 0x3D, 0x7E, 0x0D, 0x95, 0x74, 0x8F, 0x72, 0x8E, 0xB6, 0x58,
26    0x71, 0x8B, 0xCD, 0x58, 0x82, 0x15, 0x4A, 0xEE, 0x7B, 0x54, 0xA4, 0x1D,
27    0xC2, 0x5A, 0x59, 0xB5, 0x9C, 0x30, 0xD5, 0x39, 0x2A, 0xF2, 0x60, 0x13,
28    0xC5, 0xD1, 0xB0, 0x23, 0x28, 0x60, 0x85, 0xF0, 0xCA, 0x41, 0x79, 0x18,
29    0xB8, 0xDB, 0x38, 0xEF, 0x8E, 0x79, 0xDC, 0xB0, 0x60, 0x3A, 0x18, 0x0E,
30    0x6C, 0x9E, 0xD0, 0xE8, 0x9D, 0x44, 0x8F, 0x39, 0xF9, 0x93, 0xDB, 0x07,
31    0x3A, 0xA3, 0x45, 0x22, 0x7E, 0xD8, 0xAC, 0x87, 0x2F, 0x85, 0x5D, 0x28,
32    0x55, 0xB0, 0x89, 0x73, 0x36, 0xF3, 0xEB, 0xCD, 0xF6, 0x00, 0x4A, 0xDB,
33    0x36, 0x47, 0xDB, 0xF7, 0x82, 0x48, 0xDB, 0xF3, 0xD3, 0x7C, 0x45, 0x10,
34    0xC6, 0x7A, 0x70, 0xAA, 0x56, 0x78, 0x5A, 0xC6, 0x37, 0x10, 0xA2, 0x44,
35    0x32, 0x34, 0xFE, 0x08,
36];
37
38/// Cyclic π-digit dither.  Period = 256; index wraps with bitwise AND.
39#[derive(Clone, Debug)]
40pub struct PiDither {
41    idx: u8,
42}
43
44impl PiDither {
45    /// Create a new instance starting at `offset` (0–255).
46    #[inline]
47    pub fn new(offset: u8) -> Self {
48        Self { idx: offset }
49    }
50
51    /// Construct from a tensor/layer identifier for structural reproducibility.
52    #[inline]
53    pub fn from_tensor_id(tensor_id: u32) -> Self {
54        // Mix bits so different tensor IDs get distinct offsets
55        let mixed = tensor_id.wrapping_mul(0x9E37_79B9).wrapping_add(tensor_id >> 16);
56        Self { idx: (mixed & 0xFF) as u8 }
57    }
58}
59
60impl DitherSource for PiDither {
61    /// Advance and return next value in `[-0.5, 0.5]`.
62    #[inline]
63    fn next_unit(&mut self) -> f32 {
64        let b = PI_BYTES[self.idx as usize];
65        self.idx = self.idx.wrapping_add(1);
66        (b as f32 / 255.0) - 0.5
67    }
68}
69
70#[cfg(test)]
71mod tests {
72    use super::*;
73    use crate::DitherSource;
74
75    #[test]
76    fn output_is_in_range() {
77        let mut d = PiDither::new(0);
78        for _ in 0..256 * 4 {
79            let v = d.next_unit();
80            assert!(v >= -0.5 && v <= 0.5, "out of range: {v}");
81        }
82    }
83
84    #[test]
85    fn period_is_256() {
86        let mut d = PiDither::new(0);
87        let first: Vec<f32> = (0..256).map(|_| d.next_unit()).collect();
88        let second: Vec<f32> = (0..256).map(|_| d.next_unit()).collect();
89        assert_eq!(first, second);
90    }
91
92    #[test]
93    fn mean_is_near_zero() {
94        let mut d = PiDither::new(0);
95        let sum: f32 = (0..256).map(|_| d.next_unit()).sum();
96        let mean = sum / 256.0;
97        assert!(mean.abs() < 0.05, "π-digit mean too large: {mean}");
98    }
99
100    #[test]
101    fn from_tensor_id_gives_distinct_offsets() {
102        let d0 = PiDither::from_tensor_id(0);
103        let d1 = PiDither::from_tensor_id(1);
104        assert_ne!(d0.idx, d1.idx);
105    }
106}