Alexandre Courbot
2025-Jun-15 10:58 UTC
[PATCH v5 05/23] rust: num: add the `fls` operation
On Sun Jun 15, 2025 at 7:51 PM JST, Alexandre Courbot wrote:> On Sun Jun 15, 2025 at 6:37 PM JST, Miguel Ojeda wrote: >> On Thu, Jun 12, 2025 at 4:02?PM Alexandre Courbot <acourbot at nvidia.com> wrote: >>> >>> + /// ``` >>> + /// use kernel::num::fls_u32; >>> + /// >>> + /// assert_eq!(fls_u32(0x0), 0); >>> + /// assert_eq!(fls_u32(0x1), 1); >>> + /// assert_eq!(fls_u32(0x10), 5); >>> + /// assert_eq!(fls_u32(0xffff), 16); >>> + /// assert_eq!(fls_u32(0x8000_0000), 32); >>> + /// ``` >> >> For a future patch series: this could provide examples per type >> (passing them in the `impl_fls!` call). >> >> I can create a good first issue if this lands and it is not somewhere already. > > I was worried that the examples would be mostly duplicated, although > it is true that seeing how the function behaves at the limits of each > type is valuable. I'll prepare a patch to either squash for v6 or submit > as a follow-up.Also, although this will work nicely for `impl_fls!` which is a single function, I'm afraid this won't scale well for `power_of_two_impl!`, which defines 6 functions per type... Any suggestions for this case?
On Sun, Jun 15, 2025 at 12:58?PM Alexandre Courbot <acourbot at nvidia.com> wrote:> > Also, although this will work nicely for `impl_fls!` which is a single > function, I'm afraid this won't scale well for `power_of_two_impl!`, > which defines 6 functions per type... Any suggestions for this case?We can always generate the same "cases", i.e. sharing as much as possible the lines, and just passing the values (numbers) that actually differ, which you then plug into the example line concatenating. The standard library does that for their integer macros, e.g. https://doc.rust-lang.org/src/core/num/int_macros.rs.html#3639-3644 If that happened to be too onerous for some reason, then we could ignore it for the time being (i.e. we don't need to delay things just for that), or we could put them as `#[test]`s to at least have them as tests. Cheers, Miguel
Alexandre Courbot
2025-Jun-16 06:36 UTC
[PATCH v5 05/23] rust: num: add the `fls` operation
On Sun Jun 15, 2025 at 10:25 PM JST, Miguel Ojeda wrote:> On Sun, Jun 15, 2025 at 12:58?PM Alexandre Courbot <acourbot at nvidia.com> wrote: >> >> Also, although this will work nicely for `impl_fls!` which is a single >> function, I'm afraid this won't scale well for `power_of_two_impl!`, >> which defines 6 functions per type... Any suggestions for this case? > > We can always generate the same "cases", i.e. sharing as much as > possible the lines, and just passing the values (numbers) that > actually differ, which you then plug into the example line > concatenating. > > The standard library does that for their integer macros, e.g. > > https://doc.rust-lang.org/src/core/num/int_macros.rs.html#3639-3644 > > If that happened to be too onerous for some reason, then we could > ignore it for the time being (i.e. we don't need to delay things just > for that), or we could put them as `#[test]`s to at least have them as > tests.Thanks, this appears to work quite nicely (if a bit verbose), and I can adjust the tests to avoid the need to take extra arguments.