Re: [PATCH v3] staging: rtl8723bs: Use % 4096u instead of & 0xfff
Andy Shevchenko <[email protected]> Mon, 7 Apr 2025 07:42:05 +0300
| Newsgroups | dev.linux.lists.outreachy,org.kernel.vger.linux-kernel |
|---|---|
| Message-ID | <CAHp75Vfy0KMs1STmS4_5MqdCysKiZs_GaG1ahRNDr8ARhjRAUg@mail.gmail.com> |
On Mon, Apr 7, 2025 at 3:27 AM Abraham Samuel Adekunle <[email protected]> wrote: > > The sequence number is constrained to a range of [0, 4095], which > is a total of 4096 values. The bitmask operation using `0xfff` is > used to perform this wrap-around. While this is functionally correct, > it obscures the intended semantic of a 4096-based wrap. > > Using a modulo operation with `4096u` makes the wrap-around logic > explicit and easier to understand. It clearly signals that the sequence > number cycles though a range of 4096 values. > > The use of `4096u` also guarantees that the modulo operation is performed > with unsigned arithmetic, preventing potential issues with signed types. Also there are a couple of important things about modulo vs. AND: The modulo use for wrapping around is making code robust against potential changes of the upper range, especially when it becomes non-power-of-2 value, the AND works solely for power-of-2 values. The modulo in case of power-of-2 is optimised by the compiler to the same or equivalent code. > Suggested-by: Andy Shevchenko <[email protected]> > David Laight <[email protected]> Not sure what tag David's name is meant for. > This blank line shouldn't be here. It's a tag block where we don't put blank lines. > Signed-off-by: Abraham Samuel Adekunle <[email protected]> -- With Best Regards, Andy Shevchenko