Skip to contentSkip to conversation

rust: kernel: add support for bits/genmask macros

PATCH Superseded Daniel Almeida 2 participants 2 messages Aug 22, 2024 Rust

v1 of 9 Superseded Latest is v9 →

lore
Share & tools
lore

Daniel Almeida

author reviewer
#0 lore raw

PATCH 1/1 rust: kernel: add support for bits/genmask macros

These macros were converted from their C equivalent.
---

Hey all, I did not see any patch for this floating in the mailing list.

Please let me know your thoughts. This one should be rather trivial.

 rust/kernel/bits.rs | 32 ++++++++++++++++++++++++++++++++
 rust/kernel/lib.rs  |  1 +
 2 files changed, 33 insertions(+)
 create mode 100644 rust/kernel/bits.rs
rust/kernel/bits.rs new file +32 -0
new file mode 100644
index 000000000000..8ac142392086
@@ -0,0 +1,32 @@
+// SPDX-License-Identifier: GPL-2.0
+
+//! Bit manipulation macros.
+//!
+//! C header: [`include/linux/bits.h`](srctree/include/linux/bits.h)
+
+/// Produces a literal where bit `n` is set.
+///
+/// Equivalent to the kernel's BIT macro.
+///
+#[macro_export]
+macro_rules! bit {
+    ($n:expr) => {
+        (1 << $n)
+    };
+}
+
+/// Create a contiguous bitmask starting at bit position `l` and ending at
+/// position `h`, where h <= l.
+///
+/// For example genmask(39, 21) gives us the 64bit vector
+/// 0x000000ffffe00000.
+///
+#[macro_export]
+macro_rules! genmask {
+    ($h:expr, $l:expr) => {{
+        const _: () = {
+            assert!($h >= $l);
+        };
+        ((!0u64 - (1u64 << $l) + 1) & (!0u64 >> (64 - 1 - $h)))
+    }};
+}
rust/kernel/lib.rs +1 -0
index 274bdc1b0a82..3aaa1c410d2c 100644
@@ -27,6 +27,7 @@
 extern crate self as kernel;
 
 pub mod alloc;
+pub mod bits;
 #[cfg(CONFIG_BLOCK)]
 pub mod block;
 mod build_assert;
Signature · 1 line
2.45.2
#1 lore raw
Quoted 19 lines · Daniel Almeida

These macros were converted from their C equivalent.
---

Hey all, I did not see any patch for this floating in the mailing list.

Please let me know your thoughts. This one should be rather trivial.

 rust/kernel/bits.rs | 32 ++++++++++++++++++++++++++++++++
 rust/kernel/lib.rs  |  1 +
 2 files changed, 33 insertions(+)
 create mode 100644 rust/kernel/bits.rs
rust/kernel/bits.rs new file +0 -0
new file mode 100644
index 000000000000..8ac142392086

rust/kernel/bits.rs line 9

Quoted 9 lines
rust/kernel/bits.rs new file +9 -0
@@ -0,0 +1,32 @@
+// SPDX-License-Identifier: GPL-2.0
+
+//! Bit manipulation macros.
+//!
+//! C header: [`include/linux/bits.h`](srctree/include/linux/bits.h)
+
+/// Produces a literal where bit `n` is set.
+///
+/// Equivalent to the kernel's BIT macro.

For better markdown rendering, you should use backtics on BIT.

rust/kernel/bits.rs line 13

Quoted 4 lines
rust/kernel/bits.rs new file +4 -0
+///
+#[macro_export]
+macro_rules! bit {
+    ($n:expr) => {

Using expr here will allow things like bit!(3 + 8). I am not sure if we want that, is that the same behavior as in C?
If we don't want that, we can use literal instead.
I am not suggesting changing it, only if it makes sense.

rust/kernel/bits.rs line 19

Quoted 6 lines
rust/kernel/bits.rs new file +6 -0
+        (1 << $n)
+    };
+}
+
+/// Create a contiguous bitmask starting at bit position `l` and ending at
+/// position `h`, where h <= l.

Ditto here for h <= l.

rust/kernel/bits.rs line 22

Quoted 3 lines
rust/kernel/bits.rs new file +3 -0
+///
+/// For example genmask(39, 21) gives us the 64bit vector
+/// 0x000000ffffe00000.

This can probably be an example (with ``` and assert!), because then we also test the macro already :)

rust/kernel/bits.rs line 26

Quoted 4 lines
rust/kernel/bits.rs new file +4 -0
+///
+#[macro_export]
+macro_rules! genmask {
+    ($h:expr, $l:expr) => {{

Same question about expr vs literal here.

rust/kernel/bits.rs line 28

Quoted 2 lines
rust/kernel/bits.rs new file +2 -0
+        const _: () = {
+            assert!($h >= $l);

Would be nice to mention this in the documentation.

---
Cheers,
Benno

Quoted 20 lines
rust/kernel/bits.rs new file +4 -0
+        };
+        ((!0u64 - (1u64 << $l) + 1) & (!0u64 >> (64 - 1 - $h)))
+    }};
+}
rust/kernel/lib.rs +1 -1
index 274bdc1b0a82..3aaa1c410d2c 100644
@@ -27,6 +27,7 @@
 extern crate self as kernel;
 
 pub mod alloc;
+pub mod bits;
 #[cfg(CONFIG_BLOCK)]
 pub mod block;
 mod build_assert;
--

2.45.2