public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: "Alexandre Courbot" <acourbot@nvidia.com>
To: "Alice Ryhl" <aliceryhl@google.com>,
	"Alexandre Courbot" <acourbot@nvidia.com>
Cc: "Danilo Krummrich" <dakr@kernel.org>,
	"Miguel Ojeda" <ojeda@kernel.org>,
	"Joel Fernandes" <joelagnelf@nvidia.com>,
	"Yury Norov" <yury.norov@gmail.com>,
	"Jesung Yang" <y.j3ms.n@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>,
	"Trevor Gross" <tmgross@umich.edu>,
	linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org
Subject: Re: [PATCH v3 2/4] rust: num: add Bounded integer wrapping type
Date: Thu, 06 Nov 2025 20:45:07 +0900	[thread overview]
Message-ID: <DE1L4ADMFPXX.3UAY6MJAFNNKO@nvidia.com> (raw)
In-Reply-To: <aQxwBn6wOarZ5ApN@google.com>

On Thu Nov 6, 2025 at 6:53 PM JST, Alice Ryhl wrote:
> On Thu, Nov 06, 2025 at 04:07:14PM +0900, Alexandre Courbot wrote:
>> Add the `Bounded` integer wrapper type, which restricts the number of
>> bits allowed to represent of value.
>> 
>> This is useful to e.g. enforce guarantees when working with bitfields
>> that have an arbitrary number of bits.
>> 
>> Alongside this type, provide many `From` and `TryFrom` implementations
>> are to reduce friction when using with regular integer types. Proxy
>> implementations of common integer operations are also provided.
>> 
>> Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
>> ---
>>  rust/kernel/num.rs         |    3 +
>>  rust/kernel/num/bounded.rs | 1045 ++++++++++++++++++++++++++++++++++++++++++++
>>  2 files changed, 1048 insertions(+)
>> 
>> diff --git a/rust/kernel/num.rs b/rust/kernel/num.rs
>> index 3f85e50b8632..bc9abcc3a317 100644
>> --- a/rust/kernel/num.rs
>> +++ b/rust/kernel/num.rs
>> @@ -4,6 +4,9 @@
>>  
>>  use core::ops;
>>  
>> +pub mod bounded;
>> +pub use bounded::*;
>> +
>>  /// Designates unsigned primitive types.
>>  pub struct Unsigned(());
>>  
>> diff --git a/rust/kernel/num/bounded.rs b/rust/kernel/num/bounded.rs
>> new file mode 100644
>> index 000000000000..2e4bc4ce9af5
>> --- /dev/null
>> +++ b/rust/kernel/num/bounded.rs
>> @@ -0,0 +1,1045 @@
>> +// SPDX-License-Identifier: GPL-2.0
>> +
>> +//! Implementation of [`Bounded`], a wrapper around integer types limiting the number of bits
>> +//! usable for value representation.
>> +
>> +use core::{
>> +    cmp,
>> +    fmt,
>> +    ops::{
>> +        self,
>> +        Deref, //
>> +    }, //,
>> +};
>> +
>> +use kernel::{
>> +    num::Integer,
>> +    prelude::*, //
>> +};
>> +
>> +/// Evaluates to `true` if `$value` can be represented using at most `$n` bits in a `$type`.
>> +///
>> +/// Can be used in const context.
>> +macro_rules! fits_within {
>> +    ($value:expr, $type:ty, $n:expr) => {{
>> +        let shift: u32 = <$type>::BITS - $n;
>> +
>> +        // `value` fits within `$n` bits if shifting it left by the number of unused bits, then
>> +        // right by the same number, doesn't change it.
>> +        //
>> +        // This method has the benefit of working for both unsigned and signed values.
>> +        ($value << shift) >> shift == $value
>
> I'm still confused about whether this works or not for signed values.
>
> I guess for a signed 4-bit int, the range of values is -8 to 7, so those
> are the values that this shift should preserve the values of. Is that
> what it does?

Let's roll these examples, using a 4 bit integer backed by a i8.

-8i8 in binary is 1111_1000. Shift it left by 4 (`i8::BITS - 4`), and
you get 1000_0000. Shift it back right by 4, you get 1111_1000, which is
the original value. The smallest possible representation of -8 is
`1000`, which indeed fits in 4 bits.

Now -9i8. In binary it is 1111_0111. Shift it left by 4, you get
0111_0000. Shift back right, you get 0000_0111. The value is different,
it doesn't fit - and indeed, its smallest representation is 1_0111,
which requires 5 bits.

And if you go with smaller negative numbers, some `0` will eventually
end up in the 4 MSBs and lost in the shift, so any value < -9 is
properly detected as non-fitting.

Now for the positive limit. 7i8 is 0000_0111. Shift left by 4,
0111_0000. Shift back right, 0000_0111, original value. Smallest
possible representation of 7 as a signed integer (thus including the bit
sign) is 0111, so that works.

8i8 now. In binary, it's 0000_1000. Shift left by 4, 1000_0000. Shift
back right, 1111_1000. Doesn't fit, because its smallest possible
representation is 0_1000, 5 bits.

I have confirmed the above with a kunit test as well. Actually I will
probably add these to the doctest for `try_new` - since all that
constructor does is call `fits_within`, that will cover these edge
cases.

  reply	other threads:[~2025-11-06 11:45 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-11-06  7:07 [PATCH v3 0/4] rust: add Bounded integer type Alexandre Courbot
2025-11-06  7:07 ` [PATCH v3 1/4] rust: add num module and Integer trait Alexandre Courbot
2025-11-06  9:46   ` Alice Ryhl
2025-11-06 11:05     ` Alexandre Courbot
2025-11-06  7:07 ` [PATCH v3 2/4] rust: num: add Bounded integer wrapping type Alexandre Courbot
2025-11-06  9:53   ` Alice Ryhl
2025-11-06 11:45     ` Alexandre Courbot [this message]
2025-11-06  7:07 ` [PATCH v3 3/4] MAINTAINERS: add entry for the Rust `num` module Alexandre Courbot
2025-11-06  7:07 ` [PATCH FOR REFERENCE v3 4/4] gpu: nova-core: use BitInt for bitfields 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=DE1L4ADMFPXX.3UAY6MJAFNNKO@nvidia.com \
    --to=acourbot@nvidia.com \
    --cc=a.hindborg@kernel.org \
    --cc=aliceryhl@google.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun.feng@gmail.com \
    --cc=dakr@kernel.org \
    --cc=gary@garyguo.net \
    --cc=joelagnelf@nvidia.com \
    --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 \
    --cc=y.j3ms.n@gmail.com \
    --cc=yury.norov@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