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
use {Result, Tape, Value};

macro_rules! number(
    ($(#[$attribute:meta])* pub $name:ident($kind:ty | $fraction:expr)) => {
        $(#[$attribute])*
        #[derive(Clone, Copy, Debug, Eq, PartialEq)]
        pub struct $name(pub $kind);

        impl From<$name> for f32 {
            #[inline]
            fn from(number: $name) -> Self {
                const SCALE: f32 = 1f32 / (1 << $fraction) as f32;
                SCALE * (number.0 as f32)
            }
        }

        impl Value for $name {
            #[inline(always)]
            fn read<T: Tape>(tape: &mut T) -> Result<Self> {
                Ok($name(read_value!(tape)))
            }
        }
    }
);

number! {
    #[doc = "A fixed-point number in format Q2.14."]
    #[allow(non_camel_case_types)]
    pub q16(u16 | 14)
}

number! {
    #[doc = "A fixed-point number in format Q16.16."]
    #[allow(non_camel_case_types)]
    pub q32(u32 | 16)
}