From: Daniel Almeida <daniel.almeida@collabora.com>
To: wedsonaf@gmail.com, ojeda@kernel.org
Cc: Daniel Almeida <daniel.almeida@collabora.com>,
rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH] rust: kernel: add support for bits/genmask macros
Date: Thu, 22 Aug 2024 14:35:17 -0300 [thread overview]
Message-ID: <20240822173518.2717-1-daniel.almeida@collabora.com> (raw)
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
next reply other threads:[~2024-08-22 17:42 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-08-22 17:35 Daniel Almeida [this message]
2024-08-22 20:27 ` [PATCH] rust: kernel: add support for bits/genmask macros Benno Lossin
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240822173518.2717-1-daniel.almeida@collabora.com \
--to=daniel.almeida@collabora.com \
--cc=linux-kernel@vger.kernel.org \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=wedsonaf@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).