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
use crate::fixed_uint::UintConstructor;
use crate::utils;
use quote::quote;
impl UintConstructor {
pub fn defun_priv_conv(&self) {
self.defun_priv_from_primitive_uints();
}
fn defun_priv_from_primitive_uints(&self) {
let unit_suffix = &self.ts.unit_suffix;
for prim_bits_size in &[8u64, 16, 32, 64, 128] {
let prim_type = utils::uint_suffix_to_ts(*prim_bits_size);
let func_name = utils::ident_to_ts(format!("_from_u{}", prim_bits_size).as_ref());
let part = match *prim_bits_size {
x if x <= self.info.unit_bits_size => quote!(
#[inline]
fn #func_name(input: #prim_type) -> Self {
let mut ret = Self::zero();
ret.mut_inner()[0] = input as #unit_suffix;
ret
}
),
_ => {
if prim_bits_size % self.info.unit_bits_size != 0 {
unreachable!();
}
let times = prim_bits_size / self.info.unit_bits_size;
let loop_times = &utils::pure_uint_list_to_ts(0..times);
let loop_shift = &utils::pure_uint_list_to_ts(
(0..times).map(|x| x * self.info.unit_bits_size),
);
let loop_unit_suffix = &vec![unit_suffix; times as usize];
quote!(
#[inline]
fn #func_name(input: #prim_type) -> Self {
let mut ret = Self::zero();
#({
ret.mut_inner()[#loop_times] = (input >> #loop_shift) as #loop_unit_suffix;
})*
ret
}
)
}
};
self.defun(part);
}
}
}