Skip to content

Commit 7b4547c

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 a370144 commit 7b4547c

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

rust/kernel/bits.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
//use crate::static_assert;
8+
9+
/// Generate a mask where all bits >= `h` and <= `l` are set
10+
///
11+
/// This is a re-implementation in rust of `GENMASK`
12+
pub const fn genmask(h: u32, l: u32) -> u32 {
13+
//static_assert!(h >= l);
14+
//static_assert!(h < 32);
15+
((!0u32) - (1 << l) + 1) & ((!0u32) >> (32 - 1 - h))
16+
}

rust/kernel/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ extern crate self as kernel;
3333
#[cfg(not(test))]
3434
#[cfg(not(testlib))]
3535
mod allocator;
36+
pub mod bits;
3637
mod build_assert;
3738
pub mod cred;
3839
pub mod device;

0 commit comments

Comments
 (0)