[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-Id: <DBL1ZGZCSJF3.29HNS9BSN89C6@kernel.org>
Date: Fri, 25 Jul 2025 12:10:52 +0200
From: "Benno Lossin" <lossin@...nel.org>
To: "Alexandre Courbot" <acourbot@...dia.com>, "Miguel Ojeda"
<ojeda@...nel.org>, "Alex Gaynor" <alex.gaynor@...il.com>, "Boqun Feng"
<boqun.feng@...il.com>, "Gary Guo" <gary@...yguo.net>,
Björn Roy Baron <bjorn3_gh@...tonmail.com>, "Andreas
Hindborg" <a.hindborg@...nel.org>, "Alice Ryhl" <aliceryhl@...gle.com>,
"Trevor Gross" <tmgross@...ch.edu>, "Danilo Krummrich" <dakr@...nel.org>
Cc: <linux-kernel@...r.kernel.org>, <rust-for-linux@...r.kernel.org>,
<nouveau@...ts.freedesktop.org>
Subject: Re: [PATCH 1/3] rust: add `num` module with `PowerOfTwo` type
On Fri Jul 25, 2025 at 5:38 AM CEST, Alexandre Courbot wrote:
> Hi Benno,
>
> Sorry, took some time to come back to this!
No worries!
> On Sun Jun 22, 2025 at 5:11 PM JST, Benno Lossin wrote:
>> On Fri Jun 20, 2025 at 3:14 PM CEST, Alexandre Courbot wrote:
>>> + /// Returns the value of this instance.
>>> + ///
>>> + /// It is guaranteed to be a power of two.
>>> + ///
>>> + /// # Examples
>>> + ///
>>> + /// ```
>>> + /// use kernel::num::PowerOfTwo;
>>> + ///
>>> + #[doc = concat!("let v = PowerOfTwo::<", stringify!($t), ">::new(16);")]
>>> + /// assert_eq!(v.value(), 16);
>>> + /// ```
>>> + #[inline(always)]
>>> + pub const fn value(self) -> $t {
>>> + self.0
>>
>> Let's add:
>>
>> if !self.0.is_power_of_two() {
>> core::hint::unreachable_unchecked()
>> }
>> self.0
>
> Sure. Is it to enable compiler optimizations by making assumptions about
> the returned value?
Exactly, it will for example turn dividing by it into a right shift and
prevent a branch for the zero check.
---
Cheers,
Benno
Powered by blists - more mailing lists