The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: "Alexandre Courbot" <acourbot@nvidia.com>
To: "Alexandre Courbot" <acourbot@nvidia.com>,
	"Daniel Almeida" <daniel.almeida@collabora.com>,
	"Miguel Ojeda" <ojeda@kernel.org>,
	"Alex Gaynor" <alex.gaynor@gmail.com>,
	"Boqun Feng" <boqun.feng@gmail.com>,
	"Gary Guo" <gary@garyguo.net>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	"Benno Lossin" <benno.lossin@proton.me>,
	"Andreas Hindborg" <a.hindborg@kernel.org>,
	"Alice Ryhl" <aliceryhl@google.com>,
	"Trevor Gross" <tmgross@umich.edu>,
	"Danilo Krummrich" <dakr@kernel.org>
Cc: <linux-kernel@vger.kernel.org>, <rust-for-linux@vger.kernel.org>,
	"Fiona Behrens" <me@kloenk.dev>
Subject: Re: [PATCH v4] rust: kernel: add support for bits/genmask macros
Date: Thu, 20 Mar 2025 13:13:57 +0900	[thread overview]
Message-ID: <D8KSV0ADDYQW.30CIS1DFZOPN9@nvidia.com> (raw)
In-Reply-To: <D8KB8CPBOIY1.10ATK35TV2X79@nvidia.com>

On Wed Mar 19, 2025 at 11:25 PM JST, Alexandre Courbot wrote:
> Hi Daniel,
>
> On Wed Mar 19, 2025 at 12:34 AM JST, Daniel Almeida wrote:
>> In light of bindgen being unable to generate bindings for macros,
>> manually define the bit and genmask C macros in Rust.
>>
>> Bit and genmask are frequently used in drivers, and are simple enough to
>> just be redefined. Their implementation is also unlikely to ever change.
>>
>> These macros are converted from their kernel equivalent. Since
>> genmask_u32 and genmask_u64 are thought to suffice, these two versions
>> are implemented as const fns instead of declarative macros.
>>
>> Reviewed-by: Fiona Behrens <me@kloenk.dev>
>> Reviewed-by: Alice Ryhl <aliceryhl@google.com>
>> Signed-off-by: Daniel Almeida <daniel.almeida@collabora.com>
>
> Cool, I'll probably depend on this for the next version of my register
> layout patch [1].
>
> [1]
> https://lore.kernel.org/rust-for-linux/20250313-registers-v1-1-8d498537e8b2@nvidia.com/
>
>> ---
>> Changes in v4:
>> - Split bits into bits_u32 and bits_u64
>> - Added r-b's
>> - Rebased on top of rust-next
>> - Link to v3: https://lore.kernel.org/r/20250121-topic-panthor-rs-genmask-v3-1-5c3bdf21ce05@collabora.com
>>
>> Changes in v3:
>> - Changed from declarative macro to const fn
>> - Added separate versions for u32 and u64
>> - Link to v2: https://lore.kernel.org/r/20241024-topic-panthor-rs-genmask-v2-1-85237c1f0cea@collabora.com
>>
>> Changes in v2:
>>
>> - Added ticks around `BIT`, and `h >=l` (Thanks, Benno).
>> - Decided to keep the arguments as `expr`, as I see no issues with that
>> - Added a proper example, with an assert_eq!() (Thanks, Benno)
>> - Fixed the condition h <= l, which should be h >= l.
>> - Checked that the assert for the condition above is described in the
>>   docs.
>> ---
>>  rust/kernel/bits.rs | 49 +++++++++++++++++++++++++++++++++++++++++++++++++
>>  rust/kernel/lib.rs  |  1 +
>>  2 files changed, 50 insertions(+)
>>
>> diff --git a/rust/kernel/bits.rs b/rust/kernel/bits.rs
>> new file mode 100644
>> index 0000000000000000000000000000000000000000..ec13bb480082de9584b7d23c78df0e76d0fbf132
>> --- /dev/null
>> +++ b/rust/kernel/bits.rs
>> @@ -0,0 +1,49 @@
>> +// 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.
>> +pub const fn bit_u32(n: u32) -> u32 {
>> +    1 << n
>> +}
>> +
>> +/// Produces a literal where bit `n` is set.
>> +///
>> +/// Equivalent to the kernel's `BIT` macro.
>> +pub const fn bit_u64(n: u32) -> u64 {
>> +    1u64 << n as u64
>> +}
>> +
>> +/// Create a contiguous bitmask starting at bit position `l` and ending at
>> +/// position `h`, where `h >= l`.
>> +///
>> +/// # Examples
>> +/// ```
>> +///     use kernel::bits::genmask_u64;
>> +///     let mask = genmask_u64(39, 21);
>> +///     assert_eq!(mask, 0x000000ffffe00000);
>> +/// ```
>> +///
>> +pub const fn genmask_u64(h: u32, l: u32) -> u64 {
>> +    assert!(h >= l);
>> +    (!0u64 - (1u64 << l) + 1) & (!0u64 >> (64 - 1 - h))
>> +}
>> +
>> +/// Create a contiguous bitmask starting at bit position `l` and ending at
>> +/// position `h`, where `h >= l`.
>> +///
>> +/// # Examples
>> +/// ```
>> +///     use kernel::bits::genmask_u32;
>> +///     let mask = genmask_u32(9, 0);
>> +///     assert_eq!(mask, 0x000003ff);
>> +/// ```
>> +///
>> +pub const fn genmask_u32(h: u32, l: u32) -> u32 {
>> +    assert!(h >= l);
>> +    (!0u32 - (1u32 << l) + 1) & (!0u32 >> (32 - 1 - h))
>> +}
>
> Would it make sense to also have u16 and u8 variants? I guess one can
> always use genmask_u32 and then cast to the desired type though.

Forgot to mention:

Reviewed-by: Alexandre Courbot <acourbot@nvidia.com>
Tested-by: Alexandre Courbot <acourbot@nvidia.com>

  reply	other threads:[~2025-03-20  4:14 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-03-18 15:34 [PATCH v4] rust: kernel: add support for bits/genmask macros Daniel Almeida
2025-03-19 14:25 ` Alexandre Courbot
2025-03-20  4:13   ` Alexandre Courbot [this message]
2025-03-20 13:50   ` Daniel Almeida
2025-03-20 14:02     ` Alexandre Courbot

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=D8KSV0ADDYQW.30CIS1DFZOPN9@nvidia.com \
    --to=acourbot@nvidia.com \
    --cc=a.hindborg@kernel.org \
    --cc=alex.gaynor@gmail.com \
    --cc=aliceryhl@google.com \
    --cc=benno.lossin@proton.me \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun.feng@gmail.com \
    --cc=dakr@kernel.org \
    --cc=daniel.almeida@collabora.com \
    --cc=gary@garyguo.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=me@kloenk.dev \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=tmgross@umich.edu \
    /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