You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
To prevent ubsan warnings, we mustn't call the ctz/clz builtins with a zero-value. So we typically insert an all-bits value to make it shutup
Unfortunately this conflicts with the post-combine that recognise cases where the zero-value input should be correctly handled (e.g tzcnt/lzcnt x86 instructions).
Funnily enough, gcc has better code from the ubsan-safe cases.....
The text was updated successfully, but these errors were encountered:
Extended Description
https://simd.godbolt.org/z/KrahKsdhe
clang -g0 -O3 -march=haswell
#include
#include
auto lz(unsigned x) {
int c = __builtin_clz( x );
return x ? c : sizeof(x) * CHAR_BIT;
}
auto tz(unsigned x) {
int c = __builtin_ctz( x );
return x ? c : sizeof(x) * CHAR_BIT;
}
lz:
lzcntl %edi, %eax
retq
tz:
tzcntl %edi, %eax
retq
auto lz_ubsan(unsigned x) {
int c = __builtin_clz( x ? x : ~0U );
return x ? c : sizeof(x) * CHAR_BIT;
}
auto tz_ubsan(unsigned x) {
int c = __builtin_ctz( x ? x : ~0U );
return x ? c : sizeof(x) * CHAR_BIT;
}
lz_ubsan:
xorl %eax, %eax
cmpl $1, %edi
sbbl %eax, %eax
orl %edi, %eax
lzcntl %eax, %ecx
testl %edi, %edi
movl $32, %eax
cmovnel %ecx, %eax
retq
tz_ubsan:
xorl %eax, %eax
cmpl $1, %edi
sbbl %eax, %eax
orl %edi, %eax
tzcntl %eax, %ecx
testl %edi, %edi
movl $32, %eax
cmovnel %ecx, %eax
retq
To prevent ubsan warnings, we mustn't call the ctz/clz builtins with a zero-value. So we typically insert an all-bits value to make it shutup
Unfortunately this conflicts with the post-combine that recognise cases where the zero-value input should be correctly handled (e.g tzcnt/lzcnt x86 instructions).
Funnily enough, gcc has better code from the ubsan-safe cases.....
The text was updated successfully, but these errors were encountered: