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
#![feature(proc_macro_hygiene)]
extern crate alloc;
use alloc::{
format,
string::{String, ToString},
vec::Vec,
};
extern crate proc_macro;
use core::num::NonZeroUsize;
use awint_ext::ExtAwi;
use proc_macro::TokenStream;
const BITS: usize = usize::BITS as usize;
fn inlawi_digits(bw: usize) -> usize {
bw.wrapping_shr(BITS.trailing_zeros())
.wrapping_add(((bw & (BITS - 1)) != 0) as usize)
.wrapping_add(1)
}
#[proc_macro]
pub fn inlawi_ty(input: TokenStream) -> TokenStream {
let bw = input
.to_string()
.parse::<usize>()
.expect("Input should parse as a `usize`");
if bw == 0 {
panic!("Tried to make an `InlAwi` type with an invalid bitwidth of 0");
}
format!("InlAwi<{}, {}>", bw, inlawi_digits(bw))
.parse()
.unwrap()
}
#[proc_macro]
pub fn inlawi_zero(input: TokenStream) -> TokenStream {
let bw = input
.to_string()
.parse::<usize>()
.expect("Input should parse as a `usize`");
if bw == 0 {
panic!("Tried to construct an `InlAwi` with an invalid bitwidth of 0");
}
format!(
"InlAwi::<{}, {}>::unstable_zero({})",
bw,
inlawi_digits(bw),
bw
)
.parse()
.unwrap()
}
#[proc_macro]
pub fn inlawi_umax(input: TokenStream) -> TokenStream {
let bw = input
.to_string()
.parse::<usize>()
.expect("Input should parse as a `usize`");
if bw == 0 {
panic!("Tried to construct an `InlAwi` with an invalid bitwidth of 0");
}
format!(
"InlAwi::<{}, {}>::unstable_umax({})",
bw,
inlawi_digits(bw),
bw
)
.parse()
.unwrap()
}
fn general_inlawi(components: Vec<String>) -> String {
let mut awis: Vec<ExtAwi> = Vec::new();
for component in components {
match component.parse::<ExtAwi>() {
Ok(awi) => awis.push(awi),
Err(e) => panic!(
"could not parse component \"{}\": `<ExtAwi as FromStr>::from_str` returned \
SerdeError::{:?}",
component, e
),
}
}
if awis.is_empty() {
panic!("empty input");
}
let mut total_bw = 0;
for awi in awis.iter() {
total_bw += awi.bw();
}
let total_bw = NonZeroUsize::new(total_bw).unwrap();
let mut awi = ExtAwi::zero(total_bw);
let mut tmp = ExtAwi::zero(total_bw);
let mut shl = 0;
for component in awis {
tmp[..].zero_resize_assign(&component[..]);
tmp[..].shl_assign(shl).unwrap();
shl += component.bw();
awi[..].or_assign(&tmp[..]);
}
let mut raw: Vec<usize> = Vec::new();
raw.extend_from_slice(awi[..].as_slice());
raw.push(total_bw.get());
format!(
"InlAwi::<{}, {}>::unstable_from_slice(&{:?})",
total_bw,
awi.raw_len(),
&raw[..],
)
}
#[proc_macro]
pub fn inlawi(input: TokenStream) -> TokenStream {
let mut components: Vec<String> = Vec::new();
for part in input.to_string().split(',') {
let mut component = String::new();
for c in part.chars() {
if !c.is_whitespace() {
component.push(c);
}
}
components.push(component);
}
general_inlawi(components).parse().unwrap()
}
#[proc_macro]
pub fn inlawi_be(input: TokenStream) -> TokenStream {
let mut components: Vec<String> = Vec::new();
for part in input.to_string().split(',').rev() {
let mut component = String::new();
for c in part.chars() {
if !c.is_whitespace() {
component.push(c);
}
}
components.push(component);
}
general_inlawi(components).parse().unwrap()
}
#[proc_macro]
pub fn inlawi_le(input: TokenStream) -> TokenStream {
let mut components: Vec<String> = Vec::new();
for part in input.to_string().split(',') {
let mut component = String::new();
for c in part.chars().rev() {
if !c.is_whitespace() {
component.push(c);
}
}
components.push(component);
}
general_inlawi(components).parse().unwrap()
}