[PATCH 1/3] rust: add `num` module with `PowerOfTwo` type
Benno Lossin
lossin at kernel.org
Fri Jul 25 10:10:52 UTC 2025
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
More information about the Nouveau
mailing list