[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <DA82KFLNAOG7.R7YT4BHCLNZQ@kernel.org>
Date: Wed, 28 May 2025 22:17:37 +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>, "Benno Lossin"
<benno.lossin@...ton.me>, "Andreas Hindborg" <a.hindborg@...nel.org>,
"Alice Ryhl" <aliceryhl@...gle.com>, "Trevor Gross" <tmgross@...ch.edu>,
"Danilo Krummrich" <dakr@...nel.org>, "David Airlie" <airlied@...il.com>,
"Simona Vetter" <simona@...ll.ch>, "Maarten Lankhorst"
<maarten.lankhorst@...ux.intel.com>, "Maxime Ripard" <mripard@...nel.org>,
"Thomas Zimmermann" <tzimmermann@...e.de>
Cc: "John Hubbard" <jhubbard@...dia.com>, "Ben Skeggs" <bskeggs@...dia.com>,
"Joel Fernandes" <joelagnelf@...dia.com>, "Timur Tabi" <ttabi@...dia.com>,
"Alistair Popple" <apopple@...dia.com>, <linux-kernel@...r.kernel.org>,
<rust-for-linux@...r.kernel.org>, <nouveau@...ts.freedesktop.org>,
<dri-devel@...ts.freedesktop.org>
Subject: Re: [PATCH v4 04/20] rust: add new `num` module with useful integer
operations
On Wed May 21, 2025 at 8:44 AM CEST, Alexandre Courbot wrote:
> Introduce the `num` module, featuring the `NumExt` extension trait
> that expands unsigned integers with useful operations for the kernel.
>
> These are to be used by the nova-core driver, but they are so ubiquitous
> that other drivers should be able to take advantage of them as well.
>
> The currently implemented operations are:
>
> - align_down()
> - align_up()
> - fls()
>
> But this trait is expected to be expanded further.
>
> `NumExt` is on unsigned types using a macro. An approach using another
> trait constrained by the operator traits that we need (`Add`, `Sub`,
> etc) was also considered, but had to be dropped as we need to use
> wrapping operations, which are not provided by any trait.
>
> Co-developed-by: Joel Fernandes <joelagnelf@...dia.com>
> Signed-off-by: Joel Fernandes <joelagnelf@...dia.com>
> Signed-off-by: Alexandre Courbot <acourbot@...dia.com>
> ---
> rust/kernel/lib.rs | 1 +
> rust/kernel/num.rs | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
> 2 files changed, 83 insertions(+)
Have you proposed `align_down` to upstream rust? Not saying that we
shouldn't do it here, but if we haven't tried yet, it might be a good
idea to just get them upstreamed. (if you do, it should probably be
named `prev_multiple_of`)
> + /// Align `self` up to `alignment`.
> + ///
> + /// `alignment` must be a power of 2 for accurate results.
> + ///
> + /// Wraps around to `0` if the requested alignment pushes the result above the type's limits.
> + ///
> + /// # Examples
> + ///
> + /// ```
> + /// use kernel::num::NumExt;
> + ///
> + /// assert_eq!(0x4fffu32.align_up(0x1000), 0x5000);
> + /// assert_eq!(0x4000u32.align_up(0x1000), 0x4000);
> + /// assert_eq!(0x0u32.align_up(0x1000), 0x0);
> + /// assert_eq!(0xffffu16.align_up(0x100), 0x0);
> + /// assert_eq!(0x4fffu32.align_up(0x0), 0x0);
> + /// ```
> + fn align_up(self, alignment: Self) -> Self;
Isn't this `next_multiple_of` [1] (it also allows non power of 2
inputs).
[1]: https://doc.rust-lang.org/std/primitive.u32.html#method.next_multiple_of
> +
> + /// Find Last Set Bit: return the 1-based index of the last (i.e. most significant) set bit in
> + /// `self`.
> + ///
> + /// Equivalent to the C `fls` function.
> + ///
> + /// # Examples
> + ///
> + /// ```
> + /// use kernel::num::NumExt;
> + ///
> + /// assert_eq!(0x0u32.fls(), 0);
> + /// assert_eq!(0x1u32.fls(), 1);
> + /// assert_eq!(0x10u32.fls(), 5);
> + /// assert_eq!(0xffffu32.fls(), 16);
> + /// assert_eq!(0x8000_0000u32.fls(), 32);
> + /// ```
> + fn fls(self) -> u32;
Isn't this just `trailing_zeros` [2]?
[2]: https://doc.rust-lang.org/std/primitive.u32.html#method.trailing_zeros
---
Cheers,
Benno
Powered by blists - more mailing lists