diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
commit | 36d22d82aa202bb199967e9512281e9a53db42c9 (patch) | |
tree | 105e8c98ddea1c1e4784a60a5a6410fa416be2de /third_party/rust/minimal-lexical/src/mask.rs | |
parent | Initial commit. (diff) | |
download | firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.tar.xz firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.zip |
Adding upstream version 115.7.0esr.upstream/115.7.0esr
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/minimal-lexical/src/mask.rs')
-rw-r--r-- | third_party/rust/minimal-lexical/src/mask.rs | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/third_party/rust/minimal-lexical/src/mask.rs b/third_party/rust/minimal-lexical/src/mask.rs new file mode 100644 index 0000000000..1957c8be03 --- /dev/null +++ b/third_party/rust/minimal-lexical/src/mask.rs @@ -0,0 +1,60 @@ +//! Utilities to generate bitmasks. + +#![doc(hidden)] + +/// Generate a bitwise mask for the lower `n` bits. +/// +/// # Examples +/// +/// ```rust +/// # use minimal_lexical::mask::lower_n_mask; +/// # pub fn main() { +/// assert_eq!(lower_n_mask(2), 0b11); +/// # } +/// ``` +#[inline] +pub fn lower_n_mask(n: u64) -> u64 { + debug_assert!(n <= 64, "lower_n_mask() overflow in shl."); + + match n == 64 { + // u64::MAX for older Rustc versions. + true => 0xffff_ffff_ffff_ffff, + false => (1 << n) - 1, + } +} + +/// Calculate the halfway point for the lower `n` bits. +/// +/// # Examples +/// +/// ```rust +/// # use minimal_lexical::mask::lower_n_halfway; +/// # pub fn main() { +/// assert_eq!(lower_n_halfway(2), 0b10); +/// # } +/// ``` +#[inline] +pub fn lower_n_halfway(n: u64) -> u64 { + debug_assert!(n <= 64, "lower_n_halfway() overflow in shl."); + + match n == 0 { + true => 0, + false => nth_bit(n - 1), + } +} + +/// Calculate a scalar factor of 2 above the halfway point. +/// +/// # Examples +/// +/// ```rust +/// # use minimal_lexical::mask::nth_bit; +/// # pub fn main() { +/// assert_eq!(nth_bit(2), 0b100); +/// # } +/// ``` +#[inline] +pub fn nth_bit(n: u64) -> u64 { + debug_assert!(n < 64, "nth_bit() overflow in shl."); + 1 << n +} |