https://gcc.gnu.org/bugzilla/show_bug.cgi?id=118879
Bug ID: 118879
Summary: Missed optimisation: std::in_range simpler size check
Product: gcc
Version: unknown
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: target
Assignee: unassigned at gcc dot gnu.org
Reporter: thiago at kde dot org
Target Milestone: ---
In:
#include <utility>
int f1(long long v)
{
return std::in_range<int>(v) ? v : 0;
}
int f2(long long v)
{
return v == int(v) ? v : 0;
}
GCC generates for the std::in_range version:
movl $2147483648, %edx
movl $4294967295, %ecx
movq %rdi, %rax
addq %rdi, %rdx
cmpq %rdx, %rcx
movl $0, %edx
cmovb %rdx, %rax
Whereas for the second function, which operates identically, it generates a
sign-extension from the 32-bit component:
movslq %edi, %rax
cmpq %rdi, %rax
movl $0, %eax
cmove %edi, %eax
Clang generates that second for both functions (using the libstdc++ content).
https://gcc.godbolt.org/z/nqfMKejoe