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
use core::{ops::Range, ptr};
use awint_internals::*;
use const_fn::const_fn;
use crate::Bits;
impl Bits {
#[const_fn(cfg(feature = "const_support"))]
pub const fn zero_(&mut self) {
unsafe { self.digit_set(false, 0..self.len(), false) }
}
#[const_fn(cfg(feature = "const_support"))]
pub const fn umax_(&mut self) {
unsafe { self.digit_set(true, 0..self.len(), true) }
}
#[const_fn(cfg(feature = "const_support"))]
pub const fn imax_(&mut self) {
unsafe { self.digit_set(true, 0..self.len(), false) }
*self.last_mut() = (MAX >> 1) >> self.unused();
}
#[const_fn(cfg(feature = "const_support"))]
pub const fn imin_(&mut self) {
unsafe { self.digit_set(false, 0..self.len(), false) }
*self.last_mut() = (IDigit::MIN as Digit) >> self.unused();
}
#[const_fn(cfg(feature = "const_support"))]
pub const fn uone_(&mut self) {
unsafe { self.digit_set(false, 0..self.len(), false) }
*self.first_mut() = 1;
}
#[const_fn(cfg(feature = "const_support"))]
pub const fn not_(&mut self) {
unsafe_for_each_mut!(self, x, { *x = !*x }, true);
}
#[const_fn(cfg(feature = "const_support"))]
#[must_use]
pub const fn copy_(&mut self, rhs: &Self) -> Option<()> {
if self.bw() != rhs.bw() {
return None
}
unsafe {
ptr::copy_nonoverlapping(rhs.as_ptr(), self.as_mut_ptr(), self.len());
}
Some(())
}
#[const_fn(cfg(feature = "const_support"))]
#[must_use]
pub const fn or_(&mut self, rhs: &Self) -> Option<()> {
unsafe_binop_for_each_mut!(self, rhs, x, y, { *x |= y }, false)
}
#[const_fn(cfg(feature = "const_support"))]
#[must_use]
pub const fn and_(&mut self, rhs: &Self) -> Option<()> {
unsafe_binop_for_each_mut!(self, rhs, x, y, { *x &= y }, false)
}
#[const_fn(cfg(feature = "const_support"))]
#[must_use]
pub const fn xor_(&mut self, rhs: &Self) -> Option<()> {
unsafe_binop_for_each_mut!(self, rhs, x, y, { *x ^= y }, false)
}
#[const_fn(cfg(feature = "const_support"))]
#[must_use]
pub const fn range_and_(&mut self, range: Range<usize>) -> Option<()> {
if range.start > self.bw() || range.end > self.bw() {
return None
}
if range.start >= range.end {
self.zero_();
return Some(())
}
let start = digits_u(range.start);
let end = digits_u(range.end);
let start_bits = extra_u(range.start);
let end_bits = extra_u(range.end);
unsafe {
self.digit_set(false, 0..start, false);
match (end_bits == 0, start == end) {
(false, false) => {
*self.get_unchecked_mut(start) &= MAX << start_bits;
*self.get_unchecked_mut(end) &= MAX >> (BITS - end_bits);
}
(false, true) => {
*self.get_unchecked_mut(start) &=
(MAX << start_bits) & (MAX >> (BITS - end_bits));
}
(true, _) => {
*self.get_unchecked_mut(start) &= MAX << start_bits;
if end < self.len() {
*self.get_unchecked_mut(end) = 0;
}
}
}
if (end + 1) < self.len() {
self.digit_set(false, (end + 1)..self.len(), false);
}
}
Some(())
}
#[const_fn(cfg(feature = "const_support"))]
pub const fn digit_or_(&mut self, rhs: Digit, shl: usize) {
if shl >= self.bw() {
return
}
let bits = extra_u(shl);
let digits = digits_u(shl);
unsafe {
if bits == 0 {
*self.get_unchecked_mut(digits) |= rhs;
} else {
*self.get_unchecked_mut(digits) |= rhs << bits;
if (digits + 1) < self.len() {
*self.get_unchecked_mut(digits + 1) |= rhs >> (BITS - bits);
}
}
}
}
}