* [PATCH] rust: kernel: add support for bits/genmask macros
@ 2024-08-22 17:35 Daniel Almeida
2024-08-22 20:27 ` Benno Lossin
0 siblings, 1 reply; 2+ messages in thread
From: Daniel Almeida @ 2024-08-22 17:35 UTC (permalink / raw)
To: wedsonaf, ojeda; +Cc: Daniel Almeida, rust-for-linux, linux-kernel
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
diff --git a/rust/kernel/bits.rs b/rust/kernel/bits.rs
new file mode 100644
index 000000000000..8ac142392086
--- /dev/null
+++ b/rust/kernel/bits.rs
@@ -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)))
+ }};
+}
diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs
index 274bdc1b0a82..3aaa1c410d2c 100644
--- a/rust/kernel/lib.rs
+++ b/rust/kernel/lib.rs
@@ -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
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH] rust: kernel: add support for bits/genmask macros
2024-08-22 17:35 [PATCH] rust: kernel: add support for bits/genmask macros Daniel Almeida
@ 2024-08-22 20:27 ` Benno Lossin
0 siblings, 0 replies; 2+ messages in thread
From: Benno Lossin @ 2024-08-22 20:27 UTC (permalink / raw)
To: Daniel Almeida, wedsonaf, ojeda; +Cc: rust-for-linux, linux-kernel
On 22.08.24 19:35, Daniel Almeida wrote:
> 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
>
> diff --git a/rust/kernel/bits.rs b/rust/kernel/bits.rs
> new file mode 100644
> index 000000000000..8ac142392086
> --- /dev/null
> +++ b/rust/kernel/bits.rs
> @@ -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`.
> +///
> +#[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.
> + (1 << $n)
> + };
> +}
> +
> +/// Create a contiguous bitmask starting at bit position `l` and ending at
> +/// position `h`, where h <= l.
Ditto here for `h <= l`.
> +///
> +/// 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 :)
> +///
> +#[macro_export]
> +macro_rules! genmask {
> + ($h:expr, $l:expr) => {{
Same question about `expr` vs `literal` here.
> + const _: () = {
> + assert!($h >= $l);
Would be nice to mention this in the documentation.
---
Cheers,
Benno
> + };
> + ((!0u64 - (1u64 << $l) + 1) & (!0u64 >> (64 - 1 - $h)))
> + }};
> +}
> diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs
> index 274bdc1b0a82..3aaa1c410d2c 100644
> --- a/rust/kernel/lib.rs
> +++ b/rust/kernel/lib.rs
> @@ -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
>
>
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2024-08-22 20:27 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-08-22 17:35 [PATCH] rust: kernel: add support for bits/genmask macros Daniel Almeida
2024-08-22 20:27 ` Benno Lossin
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).