Re: [PATCH] staging: rtl8723bs: Replace `& 0xfff` with `% 4096u`
From: Andy Shevchenko
Date: Thu Apr 10 2025 - 13:46:52 EST
On Wed, Apr 9, 2025 at 9:48 PM 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 `% 4096u` makes the wrap-around logic
> explicit and easier to understand. It clearly signals that the
> sequence number cycles through a range of 4096 values.
> It also makes the code robust against potential changes of the 4096
> upper limit, especially when it becomes a non power-of-2 value while
> the AND(&) works solely for power-of-2 values.
>
> The use of `% 4096u` also guarantees that the modulo operation is
> performed with unsigned arithmetic, preventing potential issues with
> the signed types.
>
> Found by Coccinelle.
This should be folded to the initial change since it's the same driver
and values most likely are related to each other.
> $ make drivers/staging/rtl8723bs/core/rtw_xmit.o
> $ cmp rtw_xmit_before.o rtw_xmit_after.o
cmp is good but not good enough in general. Here it shows the 1:1
binary, but in some cases code can be the same, while binaries are
different. To make sure the code is the same use the bloat-o-meter
tool instead.
--
With Best Regards,
Andy Shevchenko