/src/rust-lexical/lexical-parse-float/src/mask.rs
Line | Count | Source (jump to first uncovered line) |
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 | | /// assert_eq!(lower_n_mask(2), 0b11); |
12 | | /// ``` |
13 | | #[must_use] |
14 | | #[inline(always)] |
15 | | #[allow(clippy::match_bool)] // reason="easier to visualize logic" |
16 | 2.52k | pub const fn lower_n_mask(n: u64) -> u64 { |
17 | 2.52k | debug_assert!(n <= 64, "lower_n_mask() overflow in shl."); |
18 | | |
19 | 2.52k | match n == 64 { |
20 | 1 | true => u64::MAX, |
21 | 2.52k | false => (1 << n) - 1, |
22 | | } |
23 | 2.52k | } |
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 | | /// assert_eq!(lower_n_halfway(2), 0b10); |
32 | | /// ``` |
33 | | #[must_use] |
34 | | #[inline(always)] |
35 | | #[allow(clippy::match_bool)] // reason="easier to visualize logic" |
36 | 2.52k | pub const fn lower_n_halfway(n: u64) -> u64 { |
37 | 2.52k | debug_assert!(n <= 64, "lower_n_halfway() overflow in shl."); |
38 | | |
39 | 2.52k | match n == 0 { |
40 | 0 | true => 0, |
41 | 2.52k | false => nth_bit(n - 1), |
42 | | } |
43 | 2.52k | } |
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 | | /// assert_eq!(nth_bit(2), 0b100); |
52 | | /// ``` |
53 | | #[must_use] |
54 | | #[inline(always)] |
55 | 2.52k | pub const fn nth_bit(n: u64) -> u64 { |
56 | 2.52k | debug_assert!(n < 64, "nth_bit() overflow in shl."); |
57 | 2.52k | 1 << n |
58 | 2.52k | } |