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
use crate::fixed_hash::HashConstructor;
use crate::utils;
use quote::quote;
impl HashConstructor {
pub fn defun_as_prim(&self) {
self.defun_as_prim_boundary();
self.defun_as_prim_bits();
self.defun_as_prim_checked();
self.defun_as_prim_overflowing();
}
fn defun_as_prim_boundary(&self) {
let unit_amount = &self.ts.unit_amount;
let part = quote!(
#[inline]
pub const fn min_value() -> Self {
Self::new([0; #unit_amount])
}
#[inline]
pub const fn max_value() -> Self {
Self::new([!0; #unit_amount])
}
);
self.defun(part);
}
fn defun_as_prim_bits(&self) {
let bits_size = &self.ts.bits_size;
let idx_unit_amount = &utils::pure_uint_list_to_ts(0..self.info.unit_amount);
let idx_unit_amount_rev = &utils::pure_uint_list_to_ts((0..self.info.unit_amount).rev());
let part = quote!(
#[inline]
pub fn count_ones(&self) -> u32 {
let mut ret = 0u32;
let inner = self.inner();
#(
ret += inner[#idx_unit_amount].count_ones();
)*
ret
}
#[inline]
pub fn count_zeros(&self) -> u32 {
let mut ret = 0u32;
let inner = self.inner();
#(
ret += inner[#idx_unit_amount].count_zeros();
)*
ret
}
#[inline]
pub fn leading_zeros(&self) -> u32 {
let mut ret = 0u32;
let inner = self.inner();
#({
let v = inner[#idx_unit_amount_rev];
if v != 0 {
return (8 * #idx_unit_amount) as u32 + v.leading_zeros();
}
})*
#bits_size
}
#[inline]
pub fn trailing_zeros(&self) -> u32 {
let mut ret = 0u32;
let inner = self.inner();
#({
let idx = #idx_unit_amount;
if inner[idx] != 0 {
return (8 * idx) as u32 + inner[idx].trailing_zeros();
}
})*
#bits_size
}
);
self.defun(part);
}
fn defun_as_prim_checked(&self) {
let bits_size = &self.ts.bits_size;
let part = quote!(
#[inline]
pub fn checked_shl(&self, rhs: u128) -> Option<Self> {
if rhs >= #bits_size {
None
} else {
Some(self._ushl(rhs))
}
}
#[inline]
pub fn checked_shr(&self, rhs: u128) -> Option<Self> {
if rhs >= #bits_size {
None
} else {
Some(self._ushr(rhs))
}
}
);
self.defun(part);
}
fn defun_as_prim_overflowing(&self) {
let bits_size = &self.ts.bits_size;
let part = quote!(
#[inline]
pub fn overflowing_shl(&self, rhs: u128) -> (Self, bool) {
if rhs >= #bits_size {
(self._ushl(rhs % #bits_size), true)
} else {
(self._ushl(rhs), false)
}
}
#[inline]
pub fn overflowing_shr(&self, rhs: u128) -> (Self, bool) {
if rhs >= #bits_size {
(self._ushr(rhs % #bits_size), true)
} else {
(self._ushr(rhs), false)
}
}
);
self.defun(part);
}
}