summaryrefslogtreecommitdiff
path: root/rust/kernel/bits.rs
blob: 553d5026588329e169e5354e17b17b41b4c9f3e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// SPDX-License-Identifier: GPL-2.0

//! Bit manipulation macros.
//!
//! C header: [`include/linux/bits.h`](srctree/include/linux/bits.h)

use crate::prelude::*;
use core::ops::RangeInclusive;
use macros::paste;

macro_rules! impl_bit_fn {
    (
        $ty:ty
    ) => {
        paste! {
            /// Computes `1 << n` if `n` is in bounds, i.e.: if `n` is smaller than
            /// the maximum number of bits supported by the type.
            ///
            /// Returns [`None`] otherwise.
            #[inline]
            pub fn [<checked_bit_ $ty>](n: u32) -> Option<$ty> {
                (1 as $ty).checked_shl(n)
            }

            /// Computes `1 << n` by performing a compile-time assertion that `n` is
            /// in bounds.
            ///
            /// This version is the default and should be used if `n` is known at
            /// compile time.
            #[inline]
            pub const fn [<bit_ $ty>](n: u32) -> $ty {
                build_assert!(n < <$ty>::BITS);
                (1 as $ty) << n
            }
        }
    };
}

impl_bit_fn!(u64);
impl_bit_fn!(u32);
impl_bit_fn!(u16);
impl_bit_fn!(u8);

macro_rules! impl_genmask_fn {
    (
        $ty:ty,
        $(#[$genmask_checked_ex:meta])*,
        $(#[$genmask_ex:meta])*
    ) => {
        paste! {
            /// Creates a contiguous bitmask for the given range by validating
            /// the range at runtime.
            ///
            /// Returns [`None`] if the range is invalid, i.e.: if the start is
            /// greater than the end or if the range is outside of the
            /// representable range for the type.
            $(#[$genmask_checked_ex])*
            #[inline]
            pub fn [<genmask_checked_ $ty>](range: RangeInclusive<u32>) -> Option<$ty> {
                let start = *range.start();
                let end = *range.end();

                if start > end {
                    return None;
                }

                let high = [<checked_bit_ $ty>](end)?;
                let low = [<checked_bit_ $ty>](start)?;
                Some((high | (high - 1)) & !(low - 1))
            }

            /// Creates a compile-time contiguous bitmask for the given range by
            /// performing a compile-time assertion that the range is valid.
            ///
            /// This version is the default and should be used if the range is known
            /// at compile time.
            $(#[$genmask_ex])*
            #[inline]
            pub const fn [<genmask_ $ty>](range: RangeInclusive<u32>) -> $ty {
                let start = *range.start();
                let end = *range.end();

                build_assert!(start <= end);

                let high = [<bit_ $ty>](end);
                let low = [<bit_ $ty>](start);
                (high | (high - 1)) & !(low - 1)
            }
        }
    };
}

impl_genmask_fn!(
    u64,
    /// # Examples
    ///
    /// ```
    /// # #![expect(clippy::reversed_empty_ranges)]
    /// # use kernel::bits::genmask_checked_u64;
    /// assert_eq!(genmask_checked_u64(0..=0), Some(0b1));
    /// assert_eq!(genmask_checked_u64(0..=63), Some(u64::MAX));
    /// assert_eq!(genmask_checked_u64(21..=39), Some(0x0000_00ff_ffe0_0000));
    ///
    /// // `80` is out of the supported bit range.
    /// assert_eq!(genmask_checked_u64(21..=80), None);
    ///
    /// // Invalid range where the start is bigger than the end.
    /// assert_eq!(genmask_checked_u64(15..=8), None);
    /// ```
    ,
    /// # Examples
    ///
    /// ```
    /// # use kernel::bits::genmask_u64;
    /// assert_eq!(genmask_u64(21..=39), 0x0000_00ff_ffe0_0000);
    /// assert_eq!(genmask_u64(0..=0), 0b1);
    /// assert_eq!(genmask_u64(0..=63), u64::MAX);
    /// ```
);

impl_genmask_fn!(
    u32,
    /// # Examples
    ///
    /// ```
    /// # #![expect(clippy::reversed_empty_ranges)]
    /// # use kernel::bits::genmask_checked_u32;
    /// assert_eq!(genmask_checked_u32(0..=0), Some(0b1));
    /// assert_eq!(genmask_checked_u32(0..=31), Some(u32::MAX));
    /// assert_eq!(genmask_checked_u32(21..=31), Some(0xffe0_0000));
    ///
    /// // `40` is out of the supported bit range.
    /// assert_eq!(genmask_checked_u32(21..=40), None);
    ///
    /// // Invalid range where the start is bigger than the end.
    /// assert_eq!(genmask_checked_u32(15..=8), None);
    /// ```
    ,
    /// # Examples
    ///
    /// ```
    /// # use kernel::bits::genmask_u32;
    /// assert_eq!(genmask_u32(21..=31), 0xffe0_0000);
    /// assert_eq!(genmask_u32(0..=0), 0b1);
    /// assert_eq!(genmask_u32(0..=31), u32::MAX);
    /// ```
);

impl_genmask_fn!(
    u16,
    /// # Examples
    ///
    /// ```
    /// # #![expect(clippy::reversed_empty_ranges)]
    /// # use kernel::bits::genmask_checked_u16;
    /// assert_eq!(genmask_checked_u16(0..=0), Some(0b1));
    /// assert_eq!(genmask_checked_u16(0..=15), Some(u16::MAX));
    /// assert_eq!(genmask_checked_u16(6..=15), Some(0xffc0));
    ///
    /// // `20` is out of the supported bit range.
    /// assert_eq!(genmask_checked_u16(6..=20), None);
    ///
    /// // Invalid range where the start is bigger than the end.
    /// assert_eq!(genmask_checked_u16(10..=5), None);
    /// ```
    ,
    /// # Examples
    ///
    /// ```
    /// # use kernel::bits::genmask_u16;
    /// assert_eq!(genmask_u16(6..=15), 0xffc0);
    /// assert_eq!(genmask_u16(0..=0), 0b1);
    /// assert_eq!(genmask_u16(0..=15), u16::MAX);
    /// ```
);

impl_genmask_fn!(
    u8,
    /// # Examples
    ///
    /// ```
    /// # #![expect(clippy::reversed_empty_ranges)]
    /// # use kernel::bits::genmask_checked_u8;
    /// assert_eq!(genmask_checked_u8(0..=0), Some(0b1));
    /// assert_eq!(genmask_checked_u8(0..=7), Some(u8::MAX));
    /// assert_eq!(genmask_checked_u8(6..=7), Some(0xc0));
    ///
    /// // `10` is out of the supported bit range.
    /// assert_eq!(genmask_checked_u8(6..=10), None);
    ///
    /// // Invalid range where the start is bigger than the end.
    /// assert_eq!(genmask_checked_u8(5..=2), None);
    /// ```
    ,
    /// # Examples
    ///
    /// ```
    /// # use kernel::bits::genmask_u8;
    /// assert_eq!(genmask_u8(6..=7), 0xc0);
    /// assert_eq!(genmask_u8(0..=0), 0b1);
    /// assert_eq!(genmask_u8(0..=7), u8::MAX);
    /// ```
);