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
#![allow(clippy::reversed_empty_ranges)]
use awint_internals::*;
use crate::Bits;
macro_rules! bits_assign {
($($unsigned_name:ident, $uX:ident, $signed_name:ident, $iX:ident);*;) => {
$(
pub const fn $unsigned_name(&mut self, x: $uX) {
const BW: usize = $uX::BITS as usize;
const LEN: usize = BW / BITS;
let mut x = x;
if LEN < 2 {
*self.first_mut() = x as usize;
if self.len() > 1 {
unsafe {
self.digit_set(false, 1..self.len(), false);}
}
} else if self.bw() > BW {
unsafe {
const_for!(i in {0..LEN} {
*self.get_unchecked_mut(i) = x as usize;
x = x.wrapping_shr(usize::BITS);
});
self.digit_set(false, LEN..self.len(), false);
}
} else {
unsafe {
const_for!(i in {0..self.len()} {
*self.get_unchecked_mut(i) = x as usize;
x = x.wrapping_shr(usize::BITS);
});
}
}
self.clear_unused_bits();
}
pub const fn $signed_name(&mut self, x: $iX) {
const BW: usize = $iX::BITS as usize;
const LEN: usize = BW / BITS;
let mut x = x;
if LEN < 2 {
*self.first_mut() = x as isize as usize;
if self.len() >= 1 {
unsafe {
self.digit_set(x < 0, 1..self.len(), true);
}
}
} else if self.bw() >= BW {
unsafe {
let sign = x < 0;
const_for!(i in {0..LEN} {
*self.get_unchecked_mut(i) = x as isize as usize;
x = x.wrapping_shr(usize::BITS);
});
self.digit_set(sign, LEN..self.len(), true);
}
} else {
unsafe {
const_for!(i in {0..self.len()} {
*self.get_unchecked_mut(i) = x as isize as usize;
x = x.wrapping_shr(usize::BITS);
});
}
self.clear_unused_bits();
}
}
)*
};
}
impl Bits {
bits_assign!(
usize_assign, usize, isize_assign, isize;
u8_assign, u8, i8_assign, i8;
u16_assign, u16, i16_assign, i16;
u32_assign, u32, i32_assign, i32;
u64_assign, u64, i64_assign, i64;
u128_assign, u128, i128_assign, i128;
);
pub const fn bool_assign(&mut self, x: bool) {
self.zero_assign();
*self.first_mut() = x as usize;
}
}
macro_rules! bits_convert {
($($unsigned_name:ident, $uX:ident, $signed_name:ident, $iX:ident);*;) => {
$(
pub const fn $unsigned_name(&self) -> $uX {
const BW: usize = $uX::BITS as usize;
const LEN: usize = BW / BITS;
if LEN < 2 {
self.first() as $uX
} else if self.bw() >= BW {
let mut tmp = 0;
unsafe {
const_for!(i in {0..LEN} {
tmp |= (self.get_unchecked(i) as $uX) << (i * BITS);
});
}
tmp
} else {
let mut tmp = 0;
unsafe {
const_for!(i in {0..self.len()} {
tmp |= (self.get_unchecked(i) as $uX) << (i * BITS);
});
}
tmp
}
}
pub const fn $signed_name(&self) -> $iX {
const BW: usize = $uX::BITS as usize;
const LEN: usize = BW / BITS;
if LEN < 2 && self.len() == 1 {
let sign_bit = 1usize << (self.bw() - 1);
let extension = $uX::MIN.wrapping_sub((self.first() & sign_bit) as $uX);
((self.first() as $uX) | extension) as $iX
} else {
let mut tmp = self.$unsigned_name() as $iX;
if self.bw() < BW {
if self.msb() {
let extension = $uX::MIN.wrapping_sub(1 << (self.bw() - 1)) as $iX;
tmp |= extension;
}
}
tmp
}
}
)*
};
}
impl Bits {
bits_convert!(
to_usize, usize, to_isize, isize;
to_u8, u8, to_i8, i8;
to_u16, u16, to_i16, i16;
to_u32, u32, to_i32, i32;
to_u64, u64, to_i64, i64;
to_u128, u128, to_i128, i128;
);
pub const fn to_bool(&self) -> bool {
(self.first() & 1) != 0
}
}
impl From<&Bits> for bool {
fn from(x: &Bits) -> bool {
x.to_bool()
}
}
macro_rules! from_bits {
($($uX:ident, $to_u:ident, $iX:ident, $to_i:ident);*;) => {
$(
#[allow(clippy::reversed_empty_ranges)]
impl From<&Bits> for $uX {
fn from(x: &Bits) -> Self {
x.$to_u()
}
}
#[allow(clippy::reversed_empty_ranges)]
impl From<&Bits> for $iX {
fn from(x: &Bits) -> Self {
x.$to_i()
}
}
)*
};
}
from_bits!(
usize, to_usize, isize, to_isize;
u8, to_u8, i8, to_i8;
u16, to_u16, i16, to_i16;
u32, to_u32, i32, to_i32;
u64, to_u64, i64, to_i64;
u128, to_u128, i128, to_i128;
);