https://gcc.gnu.org/bugzilla/show_bug.cgi?id=122454
Bug ID: 122454
Summary: gcc suboptimal int128 compare
Product: gcc
Version: 16.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: rockeet at gmail dot com
Target Milestone: ---
bool bytewiseless(const void*, const void*);
bool less(unsigned __int128 x0, unsigned __int128 y0,
const void* x1, const void* y1) {
if (__builtin_expect(x0 != y0, 1))
return x0 < y0;
else
return bytewiseless(x1, y1);
}
-----------------
gcc generate suboptimal code:
"less(unsigned __int128, unsigned __int128, void const*, void const*)":
push rbx
mov rbx, rdi
mov rdi, rdx
mov rax, rbx
xor rax, rdx
mov rdx, rsi
xor rdx, rcx
or rax, rdx
je .L9
cmp rbx, rdi
pop rbx
sbb rsi, rcx
setc al
ret
.L9:
mov rsi, r9
mov rdi, r8
pop rbx
jmp "bytewiseless(void const*, void const*)"
-------------------------------------
clang generate better code:
less(unsigned __int128, unsigned __int128, void const*, void const*):
mov rax, rsi
xor rax, rcx
mov r10, rdi
xor r10, rdx
or r10, rax
je .LBB0_2
cmp rdi, rdx
sbb rsi, rcx
setb al
ret
.LBB0_2:
mov rdi, r8
mov rsi, r9
jmp bytewiseless(void const*, void const*)@PLT
see: https://godbolt.org/z/z8P7zMKb6