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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
use crate::eval::calc_index;
use crate::plugin::*;
use crate::{
def_package, ExclusiveRange, InclusiveRange, Position, RhaiResultOf, ERR, INT, INT_BITS,
UNSIGNED_INT,
};
use std::convert::TryFrom;
#[cfg(feature = "no_std")]
use std::prelude::v1::*;
def_package! {
/// Package of basic bit-field utilities.
pub BitFieldPackage(lib) {
lib.set_standard_lib(true);
combine_with_exported_module!(lib, "bit_field", bit_field_functions);
}
}
#[export_module]
mod bit_field_functions {
/// Return `true` if the specified `bit` in the number is set.
///
/// If `bit` < 0, position counts from the MSB (Most Significant Bit).
///
/// # Example
///
/// ```rhai
/// let x = 123456;
///
/// print(x.get_bit(5)); // prints false
///
/// print(x.get_bit(6)); // prints true
///
/// print(x.get_bit(-48)); // prints true on 64-bit
/// ```
#[rhai_fn(return_raw)]
pub fn get_bit(value: INT, bit: INT) -> RhaiResultOf<bool> {
let bit = calc_index(INT_BITS, bit, true, || {
ERR::ErrorBitFieldBounds(INT_BITS, bit, Position::NONE).into()
})?;
Ok((value & (1 << bit)) != 0)
}
/// Set the specified `bit` in the number if the new value is `true`.
/// Clear the `bit` if the new value is `false`.
///
/// If `bit` < 0, position counts from the MSB (Most Significant Bit).
///
/// # Example
///
/// ```rhai
/// let x = 123456;
///
/// x.set_bit(5, true);
///
/// print(x); // prints 123488
///
/// x.set_bit(6, false);
///
/// print(x); // prints 123424
///
/// x.set_bit(-48, false);
///
/// print(x); // prints 57888 on 64-bit
/// ```
#[rhai_fn(return_raw)]
pub fn set_bit(value: &mut INT, bit: INT, new_value: bool) -> RhaiResultOf<()> {
let bit = calc_index(INT_BITS, bit, true, || {
ERR::ErrorBitFieldBounds(INT_BITS, bit, Position::NONE).into()
})?;
let mask = 1 << bit;
if new_value {
*value |= mask;
} else {
*value &= !mask;
}
Ok(())
}
/// Return an exclusive range of bits in the number as a new number.
///
/// # Example
///
/// ```rhai
/// let x = 123456;
///
/// print(x.get_bits(5..10)); // print 18
/// ```
#[rhai_fn(name = "get_bits", return_raw)]
pub fn get_bits_range(value: INT, range: ExclusiveRange) -> RhaiResultOf<INT> {
let from = INT::max(range.start, 0);
let to = INT::max(range.end, from);
get_bits(value, from, to - from)
}
/// Return an inclusive range of bits in the number as a new number.
///
/// # Example
///
/// ```rhai
/// let x = 123456;
///
/// print(x.get_bits(5..=9)); // print 18
/// ```
#[rhai_fn(name = "get_bits", return_raw)]
pub fn get_bits_range_inclusive(value: INT, range: InclusiveRange) -> RhaiResultOf<INT> {
let from = INT::max(*range.start(), 0);
let to = INT::max(*range.end(), from - 1);
if to >= 0 && usize::try_from(to).map(|x| x >= INT_BITS).unwrap_or(true) {
return Err(ERR::ErrorBitFieldBounds(INT_BITS, to, Position::NONE).into());
}
get_bits(value, from, to - from + 1)
}
/// Return a portion of bits in the number as a new number.
///
/// * If `start` < 0, position counts from the MSB (Most Significant Bit).
/// * If `bits` ≤ 0, zero is returned.
/// * If `start` position + `bits` ≥ total number of bits, the bits after the `start` position are returned.
///
/// # Example
///
/// ```rhai
/// let x = 123456;
///
/// print(x.get_bits(5, 8)); // print 18
/// ```
#[rhai_fn(return_raw)]
pub fn get_bits(value: INT, start: INT, bits: INT) -> RhaiResultOf<INT> {
if bits <= 0 {
return Ok(0);
}
let bit = calc_index(INT_BITS, start, true, || {
ERR::ErrorBitFieldBounds(INT_BITS, start, Position::NONE).into()
})?;
let bits = usize::try_from(bits).map_or_else(
|_| INT_BITS - bit,
|bits| {
if bits.checked_add(bit).map_or(true, |x| x > INT_BITS) {
INT_BITS - bit
} else {
bits
}
},
);
if bit == 0 && bits == INT_BITS {
return Ok(value);
}
// 2^bits - 1
let mask =
INT::try_from((2 as UNSIGNED_INT).pow(u32::try_from(bits).unwrap()) - 1).unwrap();
Ok(((value & (mask << bit)) >> bit) & mask)
}
/// Replace an exclusive range of bits in the number with a new value.
///
/// # Example
///
/// ```rhai
/// let x = 123456;
///
/// x.set_bits(5..10, 42);
///
/// print(x); // print 123200
/// ```
#[rhai_fn(name = "set_bits", return_raw)]
pub fn set_bits_range(
value: &mut INT,
range: ExclusiveRange,
new_value: INT,
) -> RhaiResultOf<()> {
let from = INT::max(range.start, 0);
let to = INT::max(range.end, from);
set_bits(value, from, to - from, new_value)
}
/// Replace an inclusive range of bits in the number with a new value.
///
/// # Example
///
/// ```rhai
/// let x = 123456;
///
/// x.set_bits(5..=9, 42);
///
/// print(x); // print 123200
/// ```
#[rhai_fn(name = "set_bits", return_raw)]
pub fn set_bits_range_inclusive(
value: &mut INT,
range: InclusiveRange,
new_value: INT,
) -> RhaiResultOf<()> {
let from = INT::max(*range.start(), 0);
let to = INT::max(*range.end(), from - 1);
if to >= 0 && usize::try_from(to).map(|x| x >= INT_BITS).unwrap_or(true) {
return Err(ERR::ErrorBitFieldBounds(INT_BITS, to, Position::NONE).into());
}
set_bits(value, from, to - from + 1, new_value)
}
/// Replace a portion of bits in the number with a new value.
///
/// * If `start` < 0, position counts from the MSB (Most Significant Bit).
/// * If `bits` ≤ 0, the number is not modified.
/// * If `start` position + `bits` ≥ total number of bits, the bits after the `start` position are replaced.
///
/// # Example
///
/// ```rhai
/// let x = 123456;
///
/// x.set_bits(5, 8, 42);
///
/// print(x); // prints 124224
///
/// x.set_bits(-16, 10, 42);
///
/// print(x); // prints 11821949021971776 on 64-bit
/// ```
#[rhai_fn(return_raw)]
pub fn set_bits(value: &mut INT, bit: INT, bits: INT, new_value: INT) -> RhaiResultOf<()> {
if bits <= 0 {
return Ok(());
}
let bit = calc_index(INT_BITS, bit, true, || {
ERR::ErrorBitFieldBounds(INT_BITS, bit, Position::NONE).into()
})?;
let bits = usize::try_from(bits).map_or_else(
|_| INT_BITS - bit,
|bits| {
if bits.checked_add(bit).map_or(true, |x| x > INT_BITS) {
INT_BITS - bit
} else {
bits
}
},
);
if bit == 0 && bits == INT_BITS {
*value = new_value;
return Ok(());
}
// 2^bits - 1
let mask =
INT::try_from((2 as UNSIGNED_INT).pow(u32::try_from(bits).unwrap()) - 1).unwrap();
*value &= !(mask << bit);
*value |= (new_value & mask) << bit;
Ok(())
}
}