lexical_parse_float/mask.rs
1//! Utilities to generate bitmasks.
2
3#![doc(hidden)]
4
5/// Generate a bitwise mask for the lower `n` bits.
6///
7/// # Examples
8///
9/// ```rust
10/// # use lexical_parse_float::mask::lower_n_mask;
11/// # pub fn main() {
12/// assert_eq!(lower_n_mask(2), 0b11);
13/// # }
14/// ```
15#[inline]
16pub fn lower_n_mask(n: u64) -> u64 {
17 debug_assert!(n <= 64, "lower_n_mask() overflow in shl.");
18
19 match n == 64 {
20 true => u64::MAX,
21 false => (1 << n) - 1,
22 }
23}
24
25/// Calculate the halfway point for the lower `n` bits.
26///
27/// # Examples
28///
29/// ```rust
30/// # use lexical_parse_float::mask::lower_n_halfway;
31/// # pub fn main() {
32/// assert_eq!(lower_n_halfway(2), 0b10);
33/// # }
34/// ```
35#[inline]
36pub fn lower_n_halfway(n: u64) -> u64 {
37 debug_assert!(n <= 64, "lower_n_halfway() overflow in shl.");
38
39 match n == 0 {
40 true => 0,
41 false => nth_bit(n - 1),
42 }
43}
44
45/// Calculate a scalar factor of 2 above the halfway point.
46///
47/// # Examples
48///
49/// ```rust
50/// # use lexical_parse_float::mask::nth_bit;
51/// # pub fn main() {
52/// assert_eq!(nth_bit(2), 0b100);
53/// # }
54/// ```
55#[inline]
56pub fn nth_bit(n: u64) -> u64 {
57 debug_assert!(n < 64, "nth_bit() overflow in shl.");
58 1 << n
59}