Alexandre Courbot
2025-May-21 06:44 UTC
[PATCH v4 04/20] rust: add new `num` module with useful integer operations
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 at nvidia.com> Signed-off-by: Joel Fernandes <joelagnelf at nvidia.com> Signed-off-by: Alexandre Courbot <acourbot at nvidia.com> --- rust/kernel/lib.rs | 1 + rust/kernel/num.rs | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+) diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs index ab0286857061d2de1be0279cbd2cd3490e5a48c3..be75b196aa7a29cf3eed7c902ed8fb98689bbb50 100644 --- a/rust/kernel/lib.rs +++ b/rust/kernel/lib.rs @@ -67,6 +67,7 @@ pub mod miscdevice; #[cfg(CONFIG_NET)] pub mod net; +pub mod num; pub mod of; pub mod page; #[cfg(CONFIG_PCI)] diff --git a/rust/kernel/num.rs b/rust/kernel/num.rs new file mode 100644 index 0000000000000000000000000000000000000000..05d45b59313d830876c1a7b452827689a6dd5400 --- /dev/null +++ b/rust/kernel/num.rs @@ -0,0 +1,82 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Numerical and binary utilities for primitive types. + +/// Extension trait providing useful methods for the kernel on integers. +pub trait NumExt { + /// Align `self` down to `alignment`. + /// + /// `alignment` must be a power of 2 for accurate results. + /// + /// # Examples + /// + /// ``` + /// use kernel::num::NumExt; + /// + /// assert_eq!(0x4fffu32.align_down(0x1000), 0x4000); + /// assert_eq!(0x4fffu32.align_down(0x0), 0x0); + /// ``` + fn align_down(self, alignment: Self) -> Self; + + /// 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; + + /// 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; +} + +macro_rules! numext_impl { + ($($t:ty),+) => { + $( + impl NumExt for $t { + #[inline] + fn align_down(self, alignment: Self) -> Self { + self & !alignment.wrapping_sub(1) + } + + #[inline] + fn align_up(self, alignment: Self) -> Self { + self.wrapping_add(alignment.wrapping_sub(1)).align_down(alignment) + } + + #[inline] + fn fls(self) -> u32 { + Self::BITS - self.leading_zeros() + } + } + )+ + }; +} + +numext_impl!(usize, u8, u16, u32, u64, u128); -- 2.49.0
Alexandre Courbot
2025-May-22 04:00 UTC
[PATCH v4 04/20] rust: add new `num` module with useful integer operations
On Wed May 21, 2025 at 3:44 PM JST, 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.A trait is nice, but prevents any use in const context... After looking at the genmask patch [1] I am now wondering (again) whether a set of const functions would not better serve the needs of the kernel. Either that, or we enable `#![feature(const_trait_impl)]`. I just tried and with it we could indeed define and implement `NumExt` as const, which looks like the cleanest way to do this to me. The functions of [1] could then also be implemented as methods of that trait, which would allow them to leverage the macro generating the impl blocks for all supporting types, while having their examples/doc-tests in the trait declaration. [1] https://lore.kernel.org/rust-for-linux/20250326-topic-panthor-rs-genmask-v5-1-bfa6140214da at collabora.com/
Alice Ryhl
2025-May-28 19:56 UTC
[PATCH v4 04/20] rust: add new `num` module with useful integer operations
On Wed, May 21, 2025 at 8:45?AM Alexandre Courbot <acourbot at nvidia.com> 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 at nvidia.com> > Signed-off-by: Joel Fernandes <joelagnelf at nvidia.com> > Signed-off-by: Alexandre Courbot <acourbot at nvidia.com> > --- > rust/kernel/lib.rs | 1 + > rust/kernel/num.rs | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ > 2 files changed, 83 insertions(+) > > diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs > index ab0286857061d2de1be0279cbd2cd3490e5a48c3..be75b196aa7a29cf3eed7c902ed8fb98689bbb50 100644 > --- a/rust/kernel/lib.rs > +++ b/rust/kernel/lib.rs > @@ -67,6 +67,7 @@ > pub mod miscdevice; > #[cfg(CONFIG_NET)] > pub mod net; > +pub mod num; > pub mod of; > pub mod page; > #[cfg(CONFIG_PCI)] > diff --git a/rust/kernel/num.rs b/rust/kernel/num.rs > new file mode 100644 > index 0000000000000000000000000000000000000000..05d45b59313d830876c1a7b452827689a6dd5400 > --- /dev/null > +++ b/rust/kernel/num.rs > @@ -0,0 +1,82 @@ > +// SPDX-License-Identifier: GPL-2.0 > + > +//! Numerical and binary utilities for primitive types. > + > +/// Extension trait providing useful methods for the kernel on integers. > +pub trait NumExt {I wonder if these should just be standalone methods instead of an extension trait?> + /// Align `self` down to `alignment`. > + /// > + /// `alignment` must be a power of 2 for accurate results. > + /// > + /// # Examples > + /// > + /// ``` > + /// use kernel::num::NumExt; > + /// > + /// assert_eq!(0x4fffu32.align_down(0x1000), 0x4000); > + /// assert_eq!(0x4fffu32.align_down(0x0), 0x0); > + /// ``` > + fn align_down(self, alignment: Self) -> Self; > + > + /// 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;I would probably make alignment into a const parameter. fn align_up<ALIGN: usize>(value: usize) -> usize { const { assert!(ALIGN.is_power_of_two()) }; self.wrapping_add(ALIGN.wrapping_sub(1)).align_down(ALIGN) } Here the check for power-of-two happens at compile time. Unless you have cases where the alignment is a dynamic parameter? Alice
Benno Lossin
2025-May-28 20:17 UTC
[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 at nvidia.com> > Signed-off-by: Joel Fernandes <joelagnelf at nvidia.com> > Signed-off-by: Alexandre Courbot <acourbot at nvidia.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