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
use crate::num::basic::signeds::PrimitiveSigned;
use crate::num::basic::unsigneds::PrimitiveUnsigned;
use crate::num::logic::traits::LowMask;
fn low_mask_unsigned<T: PrimitiveUnsigned>(bits: u64) -> T {
assert!(bits <= T::WIDTH);
if bits == T::WIDTH {
T::MAX
} else {
T::power_of_2(bits) - T::ONE
}
}
macro_rules! impl_low_mask_unsigned {
($t:ident) => {
impl LowMask for $t {
/// Returns a number whose least significant $b$ bits are `true` and whose other bits
/// are `false`.
///
/// $f(b) = 2^b - 1$.
///
/// # Worst-case complexity
/// Constant time and additional memory.
///
/// # Panics
/// Panics if `bits` is greater than the width of of the type.
///
/// # Examples
/// See [here](super::low_mask#low_mask).
#[inline]
fn low_mask(bits: u64) -> $t {
low_mask_unsigned(bits)
}
}
};
}
apply_to_unsigneds!(impl_low_mask_unsigned);
fn low_mask_signed<T: PrimitiveSigned>(bits: u64) -> T {
assert!(bits <= T::WIDTH);
if bits == T::WIDTH {
T::NEGATIVE_ONE
} else if bits == T::WIDTH - 1 {
T::MAX
} else {
T::power_of_2(bits) - T::ONE
}
}
macro_rules! impl_low_mask_signed {
($t:ident) => {
impl LowMask for $t {
/// Returns a number whose least significant $b$ bits are `true` and whose other bits
/// are `false`.
///
/// $$
/// f(b) = \\begin{cases}
/// 2^b - 1 & \text{if} \\quad 0 \leq n < W, \\\\
/// -1 & \text{if} \\quad n = W,
/// \\end{cases}
/// $$
/// where $W$ is the width of the type.
///
/// # Worst-case complexity
/// Constant time and additional memory.
///
/// # Panics
/// Panics if `bits` is greater than the width of the type.
///
/// # Examples
/// See [here](super::low_mask#low_mask).
#[inline]
fn low_mask(bits: u64) -> $t {
low_mask_signed(bits)
}
}
};
}
apply_to_signeds!(impl_low_mask_signed);