Messages in this thread | | | From | Vincent MAILHOL <> | Date | Wed, 24 Aug 2022 05:31:20 +0900 | Subject | Re: [PATCH v5 2/2] x86/asm/bitops: __ffs,ffz: use __builtin_ctzl to evaluate constant expressions |
| |
On Wed. 24 Aug. 2022 at 02:43, Borislav Petkov <bp@alien8.de> wrote: > On Tue, Aug 23, 2022 at 10:12:17AM -0700, Nick Desaulniers wrote: > > Callers of these need to guard against zero input, as the pre-existing > > comment notes: > > > > >> Undefined if no bit exists, so code should check against 0 first.
If the fact that __ffs(0) is undefined is a concern, I can add a safety net:
#define __ffs(word) \ (__builtin_constant_p(word) ? \ (unsigned long)__builtin_ctzl(word) + BUILD_BUG_ON_ZERO(word): \ variable___ffs(word))
It will only catch the constant expression but still better than nothing (this comment also applies to the other functions undefined when the argument is zero).
Also, if this aspect was unclear, I can add a comment in the commit log to explain.
> This is just silly. > > And then there's > > * ffs(value) returns 0 if value is 0 or the position of the first > * set bit if value is nonzero. The first (least significant) bit > * is at position 1. > */ > static __always_inline int ffs(int x) > > Can we unify the two and move the guard against 0 inside the function > or, like ffs() does, have it return 0 if value is 0?
There is an index issue. __ffs() starts at 0 but ffs() starts at one. i.e.: __ffs(0x01) is 0 but ffs(0x01) is 1. Aside from the zero edge case, ffs(x) equals __ffs(x) + 1. This explains why __fss(0) is undefined.
Yours sincerely, Vincent Mailhol
| |