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
use core::{
fmt,
hash::{Hash, Hasher},
mem,
num::NonZeroUsize,
};
use awint_internals::*;
use crate::Bits;
const _ASSERT_INLAWI_ASSUMPTIONS: () = {
let _ = ["Assertion that `InlAwi` size is what we expect"]
[(mem::size_of::<InlAwi<7, 2>>() != (mem::size_of::<usize>() * 2)) as usize];
let x = InlAwi::<7, 2>::unstable_zero(7);
let _ = ["Assertion that layouts are working"]
[((x.const_as_ref().raw_len() != 2) || (x.bw() != 7)) as usize];
};
#[derive(Clone, Copy)]
pub struct InlAwi<const BW: usize, const LEN: usize> {
raw: [usize; LEN],
}
const fn assert_inlawi_invariants_0<const BW: usize, const LEN: usize>() {
if BW == 0 {
panic!("Tried to create an `InlAwi<BW, LEN>` with `BW == 0`")
}
if LEN < 2 {
panic!("Tried to create an `InlAwi<BW, LEN>` with `LEN < 2`")
}
}
const fn assert_inlawi_invariants_1<const BW: usize, const LEN: usize>(raw: &[usize]) {
if raw.len() != LEN {
panic!("`assert_inlawi_invariants` expects `LEN == raw.len()`")
}
let bw = raw[raw.len() - 1];
if bw == 0 {
panic!("Tried to create an InlAwi with zero bitwidth")
}
if bw <= ((LEN - 2) * BITS) {
panic!("Tried to create an `InlAwi<BW, LEN>` with `BW <= BITS*(LEN - 2)`")
}
if bw > ((LEN - 1) * BITS) {
panic!("Tried to create an `InlAwi<BW, LEN>` with `BW > BITS*(LEN - 1)`")
}
}
unsafe impl<const BW: usize, const LEN: usize> Send for InlAwi<BW, LEN> {}
unsafe impl<const BW: usize, const LEN: usize> Sync for InlAwi<BW, LEN> {}
impl<'a, const BW: usize, const LEN: usize> InlAwi<BW, LEN> {
#[inline]
pub const fn const_as_ref(&'a self) -> &'a Bits {
unsafe { Bits::from_raw_parts(self.raw.as_ptr(), self.raw.len()) }
}
#[inline]
pub const fn const_as_mut(&'a mut self) -> &'a mut Bits {
unsafe { Bits::from_raw_parts_mut(self.raw.as_mut_ptr(), self.raw.len()) }
}
#[inline]
pub const fn nzbw(&self) -> NonZeroUsize {
self.const_as_ref().nzbw()
}
#[inline]
pub const fn bw(&self) -> usize {
self.const_as_ref().bw()
}
#[inline]
pub const fn len(&self) -> usize {
self.const_as_ref().len()
}
#[doc(hidden)]
pub const fn unstable_from_slice(raw: &[usize]) -> Self {
assert_inlawi_invariants_0::<BW, LEN>();
assert_inlawi_invariants_1::<BW, LEN>(&raw);
let mut copy = [0; LEN];
const_for!(i in {0..raw.len()} {
copy[i] = raw[i];
});
InlAwi { raw: copy }
}
#[doc(hidden)]
pub const fn unstable_zero(bw: usize) -> Self {
assert_inlawi_invariants_0::<BW, LEN>();
let mut raw = [0; LEN];
raw[raw.len() - 1] = bw;
assert_inlawi_invariants_1::<BW, LEN>(&raw);
InlAwi { raw }
}
#[doc(hidden)]
pub const fn unstable_umax(bw: usize) -> Self {
assert_inlawi_invariants_0::<BW, LEN>();
let mut raw = [MAX; LEN];
raw[raw.len() - 1] = bw;
assert_inlawi_invariants_1::<BW, LEN>(&raw);
let mut awi = InlAwi { raw };
awi.const_as_mut().clear_unused_bits();
awi
}
}
impl<const BW: usize, const LEN: usize> PartialEq for InlAwi<BW, LEN> {
fn eq(&self, rhs: &Self) -> bool {
self.const_as_ref() == rhs.const_as_ref()
}
}
impl<const BW: usize, const LEN: usize> Eq for InlAwi<BW, LEN> {}
macro_rules! impl_fmt {
($($ty:ident)*) => {
$(
impl<const BW: usize, const LEN: usize> fmt::$ty for InlAwi<BW, LEN> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::$ty::fmt(self.const_as_ref(), f)
}
}
)*
};
}
impl_fmt!(Debug LowerHex UpperHex Octal Binary);
impl<const BW: usize, const LEN: usize> Hash for InlAwi<BW, LEN> {
fn hash<H: Hasher>(&self, state: &mut H) {
self.const_as_ref().hash(state);
}
}