maph/
bit.rs

1pub struct Byte(u8);
2
3pub fn sesp(f: f32) -> (bool, u32, u32) {
4    eprintln!("{:b}", f.to_bits());
5    let raw_bytes = f.to_be_bytes();
6    let mut bytes = [
7        Byte(raw_bytes[0]), Byte(raw_bytes[1]),
8        Byte(raw_bytes[2]), Byte(raw_bytes[3])
9    ];
10    let sign = !bytes[0].get_bit(7);
11    let mut exp = Byte(bytes[0].0 << 1);
12    if bytes[1].get_bit(7) { exp.0 += 1; }
13
14    bytes[1].set_bit(7, false);
15    let sp = u32::from_be_bytes([0, bytes[1].0, bytes[2].0, bytes[3].0]);
16
17    (sign, exp.0 as u32, sp)
18}
19
20impl Byte {
21    pub fn new(byte: u8) -> Self { Self(byte) }
22    pub fn flags(flags: Vec<usize>) -> Self {
23        flags.iter().fold(Self(0), |acc, flag| acc + Self::bitmask(*flag))
24    }
25    pub fn bitmask(index: usize) -> Self {
26        Self(1 << index)
27    }
28    pub fn get_bit(&self, index: usize) -> bool {
29        (self.0 & Self::bitmask(index).0).count_ones() > 0
30    }
31    pub fn set_bit(&mut self, index: usize, val: bool) {
32        match val {
33            true => self.0 = self.0 | Self::bitmask(index).0,
34            false => self.0 = self.0 & !Self::bitmask(index).0
35        }
36    }
37    pub fn toggle_bit(&mut self, index: usize) {
38        self.0 = self.0 ^ Self::bitmask(index).0
39    }
40    pub fn clear(&mut self) {
41        self.0 = self.0 & 0;
42    }
43}
44
45impl Byte {
46    pub fn from_u16_ne(from: u16) -> [Self; 2] {
47        let bytes = from.to_ne_bytes();
48        [Byte(bytes[0]), Byte(bytes[1])]
49    }
50    pub fn from_u32_ne(from: u32) -> [Self; 4] {
51        let bytes = from.to_ne_bytes();
52        [Byte(bytes[0]), Byte(bytes[1]),
53        Byte(bytes[2]), Byte(bytes[3])]
54    }
55    pub fn from_u64_ne(from: u64) -> [Self; 8] {
56        let bytes = from.to_ne_bytes();
57        [Byte(bytes[0]), Byte(bytes[1]),
58        Byte(bytes[2]), Byte(bytes[3]),
59        Byte(bytes[4]), Byte(bytes[5]),
60        Byte(bytes[6]), Byte(bytes[7])]
61    }
62    pub fn from_u128_ne(from: u128) -> [Self; 16] {
63        let bytes = from.to_ne_bytes();
64        [Byte(bytes[0]), Byte(bytes[1]),
65        Byte(bytes[2]), Byte(bytes[3]),
66        Byte(bytes[4]), Byte(bytes[5]),
67        Byte(bytes[6]), Byte(bytes[7]),
68        Byte(bytes[8]), Byte(bytes[9]),
69        Byte(bytes[10]), Byte(bytes[11]),
70        Byte(bytes[12]), Byte(bytes[13]),
71        Byte(bytes[14]), Byte(bytes[15])]
72    }
73    pub fn from_u16_le(from: u16) -> [Self; 2] {
74        let bytes = from.to_le_bytes();
75        [Byte(bytes[0]), Byte(bytes[1])]
76    }
77    pub fn from_u32_le(from: u32) -> [Self; 4] {
78        let bytes = from.to_le_bytes();
79        [Byte(bytes[0]), Byte(bytes[1]),
80        Byte(bytes[2]), Byte(bytes[3])]
81    }
82    pub fn from_u64_le(from: u64) -> [Self; 8] {
83        let bytes = from.to_le_bytes();
84        [Byte(bytes[0]), Byte(bytes[1]),
85        Byte(bytes[2]), Byte(bytes[3]),
86        Byte(bytes[4]), Byte(bytes[5]),
87        Byte(bytes[6]), Byte(bytes[7])]
88    }
89    pub fn from_u128_le(from: u128) -> [Self; 16] {
90        let bytes = from.to_le_bytes();
91        [Byte(bytes[0]), Byte(bytes[1]),
92        Byte(bytes[2]), Byte(bytes[3]),
93        Byte(bytes[4]), Byte(bytes[5]),
94        Byte(bytes[6]), Byte(bytes[7]),
95        Byte(bytes[8]), Byte(bytes[9]),
96        Byte(bytes[10]), Byte(bytes[11]),
97        Byte(bytes[12]), Byte(bytes[13]),
98        Byte(bytes[14]), Byte(bytes[15])]
99    }
100    pub fn from_u16_be(from: u16) -> [Self; 2] {
101        let bytes = from.to_be_bytes();
102        [Byte(bytes[0]), Byte(bytes[1])]
103    }
104    pub fn from_u32_be(from: u32) -> [Self; 4] {
105        let bytes = from.to_be_bytes();
106        [Byte(bytes[0]), Byte(bytes[1]),
107        Byte(bytes[2]), Byte(bytes[3])]
108    }
109    pub fn from_u64_be(from: u64) -> [Self; 8] {
110        let bytes = from.to_be_bytes();
111        [Byte(bytes[0]), Byte(bytes[1]),
112        Byte(bytes[2]), Byte(bytes[3]),
113        Byte(bytes[4]), Byte(bytes[5]),
114        Byte(bytes[6]), Byte(bytes[7])]
115    }
116    pub fn from_u128_be(from: u128) -> [Self; 16] {
117        let bytes = from.to_be_bytes();
118        [Byte(bytes[0]), Byte(bytes[1]),
119        Byte(bytes[2]), Byte(bytes[3]),
120        Byte(bytes[4]), Byte(bytes[5]),
121        Byte(bytes[6]), Byte(bytes[7]),
122        Byte(bytes[8]), Byte(bytes[9]),
123        Byte(bytes[10]), Byte(bytes[11]),
124        Byte(bytes[12]), Byte(bytes[13]),
125        Byte(bytes[14]), Byte(bytes[15])]
126    }
127}
128
129impl std::ops::BitOr for Byte {
130    type Output = Byte;
131    fn bitor(self, other: Byte) -> Byte {
132        Byte(self.0 | other.0)
133    }
134}
135impl std::ops::BitAnd for Byte {
136    type Output = Byte;
137    fn bitand(self, other: Byte) -> Byte {
138        Byte(self.0 & other.0)
139    }
140}
141impl std::ops::Not for Byte {
142    type Output = Byte;
143    fn not(self) -> Byte {
144        Byte(!self.0)
145    }
146}
147impl std::ops::Add for Byte {
148    type Output = Byte;
149    fn add(self, other: Byte) -> Byte { self | other }
150}