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
#![doc = include_str!("../README.md")]
#![no_std]
#![deny(warnings, missing_docs)]

#[macro_use]
mod macros;
pub mod types;

/// A layout of a digit data type in memory.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[repr(transparent)]
pub struct DigitLayout(u32);

/// The content of a digit layout.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum LayoutContent {
    /// An unsigned integer type.
    Unsigned {
        /// The width of the integer in bits.
        width: u32,
    },
    /// A real number type.
    Real {
        /// The width of the exponent in bits.
        exponent: u32,
        /// The width of the mantissa in bits.
        mantissa: u32,
    },
    /// A named type.
    Named {
        /// The name of the type.
        name: [u8; 8],
    },
}

#[repr(u32)]
enum DigitLayoutType {
    Unsigned = 0xe0_00_00_00, // 0b111...
    Real = 0xc0_00_00_00,     // 0b110...
    Named = 0,                // 0b...
}
const HEAD: u32 = DigitLayoutType::Unsigned as _;

impl DigitLayout {
    /// Create a new digit layout for an unsigned integer type.
    #[inline]
    pub const fn unsigned(width: u32) -> Self {
        assert!(width & HEAD == 0);
        Self::new(DigitLayoutType::Unsigned, width)
    }

    /// Create a new digit layout for a real number type.
    #[inline]
    pub const fn real(exponent: u32, mantissa: u32) -> Self {
        let body = (exponent << 16) | mantissa;
        assert!(body & HEAD == 0);
        Self::new(DigitLayoutType::Real, body)
    }

    /// Create a new digit layout for a named type.
    pub const fn named(name: &str) -> Self {
        let mut exp = 1;
        let mut bytes = name.as_bytes();
        let mut body = 0;
        while let [b, tail @ ..] = bytes {
            bytes = tail;

            let b = match b {
                b'0'..=b'9' => *b - b'0',
                b'a'..=b'z' => *b - b'a' + 10,
                b'A'..=b'Z' => *b - b'A' + 10,
                b'_' | b'.' => continue,
                _ => panic!("Invalid character in digit name"),
            };
            body += (b as u32 + 1) * exp;
            const GUARD: u32 = 0xc0_00_00_00; // 0b110...
            assert!(body & GUARD != GUARD);
            assert!(exp & GUARD != GUARD);
            exp *= 37; // 37 = 10 + 26 + 1
        }
        Self::new(DigitLayoutType::Named, body)
    }

    #[inline(always)]
    const fn new(ty: DigitLayoutType, body: u32) -> Self {
        Self((ty as u32) | body)
    }

    /// Raw transmutation to `u32`.
    #[inline]
    pub const fn to_u32(self) -> u32 {
        self.0
    }

    /// Decode the content of the digit layout.
    #[inline]
    pub const fn decode(self) -> LayoutContent {
        const UNSIGNED: u32 = DigitLayoutType::Unsigned as _;
        const SIGNED: u32 = DigitLayoutType::Real as _;

        let head = self.0 & HEAD;
        match head {
            UNSIGNED => LayoutContent::Unsigned {
                width: self.0 & !HEAD,
            },
            SIGNED => LayoutContent::Real {
                exponent: ((self.0 & !HEAD) >> 16) & 0xff,
                mantissa: self.0 & 0xffff,
            },
            _ => {
                let mut name = [0; 8];
                let mut body = self.0;
                let mut i = 0;
                while body > 0 {
                    let b = (body % 37) as u8 - 1;
                    name[i] = b + if b < 10 { b'0' } else { b'a' - 10 };
                    body /= 37;
                    i += 1;
                }
                LayoutContent::Named { name }
            }
        }
    }
}

use core::fmt;

impl fmt::Display for DigitLayout {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        use LayoutContent::*;
        match self.decode() {
            Unsigned { width } => write!(f, "u{width}"),
            Real { exponent, mantissa } => {
                write!(f, "f{}(e{exponent}m{mantissa})", 1 + exponent + mantissa)
            }
            Named { name } => {
                for c in name {
                    if c == 0 {
                        break;
                    }
                    write!(f, "{}", c as char)?;
                }
                Ok(())
            }
        }
    }
}

#[test]
fn test_unsigned() {
    assert!(matches!(
        types::U8.decode(),
        LayoutContent::Unsigned { width: 8 }
    ));

    assert!(matches!(
        types::U16.decode(),
        LayoutContent::Unsigned { width: 16 }
    ));

    assert!(matches!(
        types::U32.decode(),
        LayoutContent::Unsigned { width: 32 }
    ));

    assert!(matches!(
        types::U64.decode(),
        LayoutContent::Unsigned { width: 64 }
    ));
}

#[test]
fn test_real() {
    assert!(matches!(
        types::I8.decode(),
        LayoutContent::Real {
            exponent: 0,
            mantissa: 8,
        }
    ));

    assert!(matches!(
        types::I16.decode(),
        LayoutContent::Real {
            exponent: 0,
            mantissa: 16,
        }
    ));

    assert!(matches!(
        types::I32.decode(),
        LayoutContent::Real {
            exponent: 0,
            mantissa: 32,
        }
    ));

    assert!(matches!(
        types::I64.decode(),
        LayoutContent::Real {
            exponent: 0,
            mantissa: 64,
        }
    ));

    assert!(matches!(
        types::F16.decode(),
        LayoutContent::Real {
            exponent: 5,
            mantissa: 10,
        }
    ));

    assert!(matches!(
        types::BF16.decode(),
        LayoutContent::Real {
            exponent: 8,
            mantissa: 7,
        }
    ));

    assert!(matches!(
        types::F32.decode(),
        LayoutContent::Real {
            exponent: 8,
            mantissa: 23,
        }
    ));

    assert!(matches!(
        types::F64.decode(),
        LayoutContent::Real {
            exponent: 11,
            mantissa: 52,
        }
    ));
}

#[test]
fn test_named() {
    assert!(matches!(
        types::Bool.decode(),
        LayoutContent::Named {
            name: [b'b', b'o', b'o', b'l', 0, 0, 0, 0]
        }
    ));

    let q8_0 = DigitLayout::named("Q8_0");
    assert!(matches!(
        q8_0.decode(),
        LayoutContent::Named {
            name: [b'q', b'8', b'0', 0, 0, 0, 0, 0]
        }
    ));

    let iq2xxs = DigitLayout::named("IQ2XXS");
    assert!(matches!(
        iq2xxs.decode(),
        LayoutContent::Named {
            name: [b'i', b'q', b'2', b'x', b'x', b's', 0, 0]
        }
    ));

    let zzzzzz = DigitLayout::named("zzzzzz");
    assert!(matches!(
        zzzzzz.decode(),
        LayoutContent::Named {
            name: [b'z', b'z', b'z', b'z', b'z', b'z', 0, 0]
        }
    ));
}