[PATCH] powerpc/bitops: Fix possible undefined behaviour with fls() and fls64()
Segher Boessenkool
segher at kernel.crashing.org
Fri Oct 23 03:37:39 AEDT 2020
On Thu, Oct 22, 2020 at 02:05:46PM +0000, Christophe Leroy wrote:
> fls() and fls64() are using __builtin_ctz() and _builtin_ctzll().
> On powerpc, those builtins trivially use ctlzw and ctlzd power
> instructions.
>
> Allthough those instructions provide the expected result with
> input argument 0, __builtin_ctz() and __builtin_ctzll() are
> documented as undefined for value 0.
> When the input of fls(x) is a constant, just check x for nullity and
> return either 0 or __builtin_clz(x). Otherwise, use cntlzw instruction
> directly.
That looks good :-)
Acked-by: Segher Boessenkool <segher at kernel.crashing.org>
Segher
More information about the Linuxppc-dev
mailing list