Skip to content

Commit 1cf952d

Browse files
committed
rust: add function to create masks
Create a function that is equivalent to Linux's GENMASK macro. Signed-off-by: Fabien Parent <[email protected]>
1 parent b087b60 commit 1cf952d

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

rust/kernel/bits.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
//! Bit manipulation helpers
4+
//!
5+
//! C header: [`include/linux/bits.h`](srctree/include/linux/bits.h)
6+
7+
/// Generate a mask where all bits >= `h` and <= `l` are set
8+
///
9+
/// This is a re-implementation in rust of `GENMASK`
10+
pub const fn genmask(h: u32, l: u32) -> u32 {
11+
((!0u32) - (1 << l) + 1) & ((!0u32) >> (32 - 1 - h))
12+
}

rust/kernel/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ compile_error!("Missing kernel configuration for conditional compilation");
3535
extern crate self as kernel;
3636

3737
pub mod alloc;
38+
pub mod bits;
3839
#[cfg(CONFIG_BLOCK)]
3940
pub mod block;
4041
mod build_assert;

0 commit comments

Comments
 (0)