From: "Alexandre Courbot" <acourbot@nvidia.com>
To: "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>,
"Andreas Hindborg" <a.hindborg@kernel.org>,
"Alice Ryhl" <aliceryhl@google.com>,
"Trevor Gross" <tmgross@umich.edu>,
"Danilo Krummrich" <dakr@kernel.org>,
"Benno Lossin" <lossin@kernel.org>
Cc: <linux-kernel@vger.kernel.org>, <rust-for-linux@vger.kernel.org>
Subject: Re: [PATCH v8] rust: kernel: add support for bits/genmask macros
Date: Wed, 09 Jul 2025 17:08:56 +0900 [thread overview]
Message-ID: <DB7DDEDNP1HF.T0MDDNPF26P3@nvidia.com> (raw)
In-Reply-To: <20250708-topic-panthor-rs-genmask-v8-1-fb4256744c9b@collabora.com>
On Wed Jul 9, 2025 at 1:50 AM JST, Daniel Almeida wrote:
<snip>
> +macro_rules! impl_genmask_fn {
> + (
> + $ty:ty,
> + $(#[$genmask_checked_ex:meta])*,
> + $(#[$genmask_ex:meta])*
> + ) => {
> + paste! {
> + /// Creates a contiguous bitmask for the given range by validating
> + /// the range at runtime.
> + ///
> + /// Returns [`None`] if the range is invalid, i.e.: if the start is
> + /// greater than or equal to the end or if the range is outside of
Should be: "if the start is greater than the end", as the two being
equal is valid.
> + /// the representable range for the type.
> + $(#[$genmask_checked_ex])*
> + #[inline]
> + pub fn [<genmask_checked_ $ty>](range: RangeInclusive<u32>) -> Option<$ty> {
> + let start = *range.start();
> + let end = *range.end();
> +
> + if start > end {
> + return None;
> + }
> +
> + let high = [<checked_bit_ $ty>](end)?;
> + let low = [<checked_bit_ $ty>](start)?;
> + Some((high | (high - 1)) & !(low - 1))
> + }
> +
> + /// Creates a compile-time contiguous bitmask for the given range by
> + /// performing a compile-time assertion that the range is valid.
> + ///
> + /// This version is the default and should be used if the range is known
> + /// at compile time.
> + $(#[$genmask_ex])*
> + #[inline]
> + pub const fn [<genmask_ $ty>](range: RangeInclusive<u32>) -> $ty {
> + let start = *range.start();
> + let end = *range.end();
> +
> + build_assert!(start <= end);
> +
> + let high = [<bit_ $ty>](end);
> + let low = [<bit_ $ty>](start);
> + (high | (high - 1)) & !(low - 1)
> + }
> + }
> + };
> +}
> +
> +impl_genmask_fn!(
> + u64,
> + /// # Examples
> + ///
> + /// The example below highlights the default use case, i.e., when the range
> + /// is being built from non-constant values, which are represented here as
> + /// the function arguments `a` and `b`.
> + ///
> + /// ```
> + /// fn build_mask(a: u32, b: u32) -> Option<u64> {
> + /// # use kernel::bits::genmask_checked_u64;
> + /// // Ensures that a valid mask can be constructed for the range
> + /// // `a..=b` by performing runtime checks.
> + /// genmask_checked_u64(a..=b)
> + /// }
> + /// ```
This example just wraps `genmask_checked` into another function and
doesn't test it at all. I think it would be more useful if it mirrored
the non-checked equivalent, and also showed cases where `None` is
returned:
# use kernel::bits::genmask_u64;
assert_eq!(genmask_checked_u64(21..=39), Some(0x0000_00ff_ffe0_0000));
// `80` is out of the supported bit range.
assert_eq!(genmask_checked_u64(21..=80), None);
// Invalid range where the start is bigger than the end.
assert_eq!(genmask_checked_u64(15..=8), None);
Sure, here one should just use `genmask_u64`, but the function's
description already says that - here we just want to show how it works,
and test that it behaves as expected.
> + ///
> + /// This example tests an edge case where only the first bit is
> + /// supposed to be set.
I don't think these comments are necessary as the examples are rather
obvious. You can put all these examples into the same code block,
separated by a blank line, without the `mask` temporary variable. Just
add an inline comment if some explanation is required for some of them.
With these last things:
Reviewed-by: Alexandre Courbot <acourbot@nvidia.com>
prev parent reply other threads:[~2025-07-09 8:09 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-07-08 16:50 [PATCH v8] rust: kernel: add support for bits/genmask macros Daniel Almeida
2025-07-09 8:08 ` Alexandre Courbot [this message]
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=DB7DDEDNP1HF.T0MDDNPF26P3@nvidia.com \
--to=acourbot@nvidia.com \
--cc=a.hindborg@kernel.org \
--cc=alex.gaynor@gmail.com \
--cc=aliceryhl@google.com \
--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=lossin@kernel.org \
--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;
as well as URLs for NNTP newsgroup(s).