On Tue, 29 Apr 2025 at 07:38, Andrew Cooper <[email protected]> wrote:
>
> I tried that. (The thread started as a question around
> __builtin_constant_p() but did grow to cover __builtin_ffs().)
Maybe we could do something like
#define ffs(x) \
(statically_true((x) != 0) ? __ffs(x)+1 : __builtin_ffs(x))
which uses our "statically_true()" helper that is actually fairly good
at the whole "let the compiler tell us that it knows that value cannot
be zero"
I didn't check what code that generated, but I've seen gcc do well on
that statically_true() thing in the past.
Then we can just remove our current variable_ffs() thing entirely,
because we now depend on our (good) __ffs() and the builtin being
"good enough" for the bad case.
(And do the same thing for fls() too, of course)
Linus