https://gcc.gnu.org/bugzilla/show_bug.cgi?id=117612
Bug ID: 117612
Summary: spaceship codegen is inferior to hand written operator
when comparing doubles
Product: gcc
Version: 15.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: tree-optimization
Assignee: unassigned at gcc dot gnu.org
Reporter: cuzdav at gmail dot com
Target Milestone: ---
Codegen for using spaceship operator is not as good as normal operators with
obvious implementations with -std=c++20 and -O3
This code was run on Linux X86_64. This is the same code output from gcc 11.2
through the trunk. This code demonstrates:
#include <compare>
struct CustomOperators {
double value;
friend bool operator<(const CustomOperators& a,
const CustomOperators& b) {
return a.value < b.value;
}
};
struct SpaceOperators {
double value;
friend auto operator<=>(SpaceOperators, SpaceOperators) = default;
};
bool lt(CustomOperators l, CustomOperators r) { return l < r; }
bool lt(SpaceOperators l, SpaceOperators r) { return l < r; }
As seen on Compiler Explorer
https://godbolt.org/z/zrhM5bE94
OUTPUT:
lt(CustomOperators, CustomOperators):
comisd xmm1, xmm0
seta al
ret
lt(SpaceOperators, SpaceOperators):
ucomisd xmm0, xmm1
jp .L7
mov eax, 0
jne .L7
ret
.L7:
comisd xmm1, xmm0
seta al
ret
It's worth noting that clang generates identical (good) code for both hand
written and for the spaceship operator, also visible in above godbolt link.