[Bug tree-optimization/108341] argument to `__builtin_ctz` should be assumed non-zero when CTZ_DEFINED_VALUE_AT_ZERO says it is undefined
lh_mouse at 126 dot com
gcc-bugzilla@gcc.gnu.org
Tue Jan 10 08:05:07 GMT 2023
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108341
--- Comment #7 from LIU Hao <lh_mouse at 126 dot com> ---
(In reply to Jakub Jelinek from comment #4)
> I think 0 argument for __builtin_c[lt]z{,l,ll,imax} is always undefined, 0
> argument
> to .C[LT]Z (internal calls) is undefined if C[LT]Z_DEFINED_VALUE_AT_ZERO is
> not 2 and
> 0 argument to C[LT]Z RTL is undefined if C[LT]Z_DEFINED_VALUE_AT_ZERO is not
> non-zero.
I agree with this.
#94801 mentioned the `if(value == 0) __builtin_unreachable();` trick, but it
isn't an option if the argument is really possibly a zero:
(https://gcc.godbolt.org/z/dePvcMhTr)
```
#include <stdint.h>
uint32_t
my_tzcnt(uint32_t value)
{
return (value == 0) ? 32 : __builtin_ctz(value);
}
```
This can be TZCNT if the CPU supports it.
More information about the Gcc-bugs
mailing list