https://gcc.gnu.org/bugzilla/show_bug.cgi?id=78103
Bug ID: 78103
Summary: Failure to optimize with __builtin_clzl
Product: gcc
Version: unknown
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: tree-optimization
Assignee: unassigned at gcc dot gnu.org
Reporter: ben.maurer at gmail dot com
Target Milestone: ---
constexpr
unsigned long findLastSet(unsigned long x) {
return x ? 8 * sizeof(unsigned long) - __builtin_clzl(x) : 0;
}
constexpr
unsigned long findLastSet2(unsigned long x) {
return x ? ((8 * sizeof(unsigned long) - 1) ^ __builtin_clzl(x)) + 1 : 0;
}
These two functions are the same, but GCC does a better job at compiling the
second vs the more idiomatic first
https://godbolt.org/g/B2x5iG
findLastSet(unsigned long):
xor eax, eax
test rdi, rdi
je .L1
bsr rdi, rdi
mov eax, 64
xor rdi, 63
movsx rdi, edi
sub rax, rdi
.L1:
rep ret
findLastSet2(unsigned long):
xor eax, eax
test rdi, rdi
je .L6
bsr rdi, rdi
movsx rax, edi
add rax, 1