From: "Danilo Krummrich" <dakr@kernel.org>
To: "Daniel Almeida" <daniel.almeida@collabora.com>
Cc: "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" <lossin@kernel.org>,
"Andreas Hindborg" <a.hindborg@kernel.org>,
"Alice Ryhl" <aliceryhl@google.com>,
"Trevor Gross" <tmgross@umich.edu>,
linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org,
"Alexandre Courbot" <acourbot@nvidia.com>
Subject: Re: [PATCH v9] rust: kernel: add support for bits/genmask macros
Date: Wed, 16 Jul 2025 20:38:22 +0200 [thread overview]
Message-ID: <DBDP54SLN4EZ.2EQ004NXWCX2L@kernel.org> (raw)
In-Reply-To: <20250714-topics-tyr-genmask2-v9-1-9e6422cbadb6@collabora.com>
On Tue Jul 15, 2025 at 1:29 AM CEST, Daniel Almeida wrote:
> +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 the end or if the range is outside of 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)
> + }
> + }
> + };
> +}
Just for reference, I asked some questions regarding this code in [1].
Additional to "Why does genmask_u64(0..=100) compile?" I would expect the
corresponding genmask_checked_u64() call to return None.
[1] https://lore.kernel.org/lkml/DBDP0BJW9VAZ.5KRU4V4288R8@kernel.org/
next prev parent reply other threads:[~2025-07-16 18:38 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-07-14 23:29 [PATCH v9] rust: kernel: add support for bits/genmask macros Daniel Almeida
2025-07-16 18:38 ` Danilo Krummrich [this message]
2025-07-16 19:11 ` Daniel Almeida
2025-07-16 19:18 ` Daniel Almeida
2025-07-16 19:32 ` Danilo Krummrich
2025-07-16 19:44 ` Daniel Almeida
2025-07-16 19:49 ` Danilo Krummrich
2025-07-16 20:06 ` Danilo Krummrich
2025-07-16 20:07 ` Danilo Krummrich
2025-07-19 22:17 ` Miguel Ojeda
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=DBDP54SLN4EZ.2EQ004NXWCX2L@kernel.org \
--to=dakr@kernel.org \
--cc=a.hindborg@kernel.org \
--cc=acourbot@nvidia.com \
--cc=alex.gaynor@gmail.com \
--cc=aliceryhl@google.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun.feng@gmail.com \
--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