/rust/registry/src/index.crates.io-1949cf8c6b5b557f/lexical-parse-float-1.0.6/src/mask.rs
Line | Count | Source |
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 | 711 | pub const fn lower_n_mask(n: u64) -> u64 { |
17 | 711 | debug_assert!(n <= 64, "lower_n_mask() overflow in shl."); |
18 | | |
19 | 711 | match n == 64 { |
20 | 0 | true => u64::MAX, |
21 | 711 | false => (1 << n) - 1, |
22 | | } |
23 | 711 | } |
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 | 711 | pub const fn lower_n_halfway(n: u64) -> u64 { |
37 | 711 | debug_assert!(n <= 64, "lower_n_halfway() overflow in shl."); |
38 | | |
39 | 711 | match n == 0 { |
40 | 0 | true => 0, |
41 | 711 | false => nth_bit(n - 1), |
42 | | } |
43 | 711 | } |
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 | 711 | pub const fn nth_bit(n: u64) -> u64 { |
56 | 711 | debug_assert!(n < 64, "nth_bit() overflow in shl."); |
57 | 711 | 1 << n |
58 | 711 | } |