Page MenuHomeFreeBSD

powerof2: replace loops with fls or ilog2
ClosedPublic

Authored by dougm on Jun 5 2024, 5:27 AM.
Tags
None
Referenced Files
Unknown Object (File)
Sun, Sep 22, 10:54 PM
Unknown Object (File)
Sat, Sep 7, 4:49 PM
Unknown Object (File)
Wed, Sep 4, 10:59 PM
Unknown Object (File)
Sat, Aug 31, 2:47 PM
Unknown Object (File)
Sat, Aug 31, 2:47 PM
Unknown Object (File)
Sat, Aug 31, 2:47 PM
Unknown Object (File)
Sat, Aug 31, 2:47 PM
Unknown Object (File)
Sat, Aug 31, 2:37 PM

Details

Summary

In several places, a loop tests for powers of two, or iterates through powers of two. In those places, replace the loop with an invocation of fls or ilog2 without changing the meaning of the code.

Diff Detail

Lint
Lint Skipped
Unit
Tests Skipped

Event Timeline

dougm requested review of this revision.Jun 5 2024, 5:27 AM

Looks good to me.

Potentially of interest are a couple of macros that I spotted in the code: roundup2p, roundup_pow_of_two.
Oh, and there's also ROUNDUP_POW_OF_TWO.

This revision is now accepted and ready to land.Jun 5 2024, 7:23 AM

Get rid of one more loop (in src/sys/netpfil/ipfw/ip_fw_table.c).

This revision now requires review to proceed.Jun 5 2024, 7:44 AM

Oops. Left the shift out of that last one. Put it in.

markj added subscribers: erj, np, kib.

The diff looks right to me. @np , @kib , and @erj might want to comment on the cxgbe, mlx5 and irdma diffs respectively.

This revision is now accepted and ready to land.Jun 7 2024, 1:45 PM
alc added inline comments.
sys/dev/aic7xxx/aic79xx.c
8596

This comment explains what the intent of 1 << ilog2() is. However, elsewhere, there isn't this explanatory comment. For most people, I believe that what this is really doing requires some thought. For that reason, I would add a new #define/inline with a self-documenting name. If Linux already has such a #define/inline, I would borrow its name.

dougm removed a subscriber: erj.
dougm added inline comments.
sys/dev/aic7xxx/aic79xx.c
8596

The linuxkpi log2.h file has inlines roundup_pow_of_2 adn rounddown_pow_of_2. I could remove those from the linuxkpi file, put them in libkern.h, and use them all over the place.

That file also has defined
order_base_2(x) ilog2(roundup_pow_of_two(x)
which you'd think is the same as just (flsl(x-1))
and
is_power_of_two(n) (n == roundup_power_of_two(n))
which you'd think is the same as the powerof2() macro that we use everywhere.

And that's all that's in the include file.

Do you wish to see this file entirely absorbed into libkern.h?

erj added a subscriber: bartosz.sobczak_intel.com.

It would be nice if someone added a comment about what the operations in irdma_ctrl.c are doing, but I think I understand them and the replacements look ok to me.

I'll add @bartosz.sobczak_intel.com for his input.