Re: [PATCH v3] staging: rtl8723bs: Use % 4096u instead of & 0xfff
From: Andy Shevchenko
Date: Mon Apr 07 2025 - 00:42:51 EST
On Mon, Apr 7, 2025 at 3:27 AM Abraham Samuel Adekunle
<abrahamadekunle50@xxxxxxxxx> 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 <andy@xxxxxxxxxx>
> David Laight <david.laight.linux@xxxxxxxxx>
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 <abrahamadekunle50@xxxxxxxxx>
--
With Best Regards,
Andy Shevchenko