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#[must_use]
16#[inline(always)]
17#[allow(clippy::match_bool)] // reason="easier to visualize logic"
18pub const fn lower_n_mask(n: u64) -> u64 {
19 debug_assert!(n <= 64, "lower_n_mask() overflow in shl.");
20
21 match n == 64 {
22 true => u64::MAX,
23 false => (1 << n) - 1,
24 }
25}
26
27/// Calculate the halfway point for the lower `n` bits.
28///
29/// # Examples
30///
31/// ```rust
32/// # use lexical_parse_float::mask::lower_n_halfway;
33/// # pub fn main() {
34/// assert_eq!(lower_n_halfway(2), 0b10);
35/// # }
36/// ```
37#[must_use]
38#[inline(always)]
39#[allow(clippy::match_bool)] // reason="easier to visualize logic"
40pub const fn lower_n_halfway(n: u64) -> u64 {
41 debug_assert!(n <= 64, "lower_n_halfway() overflow in shl.");
42
43 match n == 0 {
44 true => 0,
45 false => nth_bit(n - 1),
46 }
47}
48
49/// Calculate a scalar factor of 2 above the halfway point.
50///
51/// # Examples
52///
53/// ```rust
54/// # use lexical_parse_float::mask::nth_bit;
55/// # pub fn main() {
56/// assert_eq!(nth_bit(2), 0b100);
57/// # }
58/// ```
59#[must_use]
60#[inline(always)]
61pub const fn nth_bit(n: u64) -> u64 {
62 debug_assert!(n < 64, "nth_bit() overflow in shl.");
63 1 << n
64}