[Bug target/103973] x86: 4-way comparison of floats/doubles with spaceship operator possibly suboptimal

pinskia at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Tue Jan 11 04:55:53 GMT 2022


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103973

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
With -O2 -fno-trapping-math, we get:
        ucomisd %xmm1, %xmm0
        jp      .L9
        movl    $0, %eax
        jne     .L9
        ret
        .p2align 4,,10
        .p2align 3
.L9:
        ucomisd %xmm0, %xmm1
        ja      .L12
        ucomisd %xmm1, %xmm0
        setbe   %al
        addl    $1, %eax
        ret
        .p2align 4,,10
        .p2align 3
.L12:
        movl    $-1, %eax
        ret


With -O2 -fno-trapping-math -ffinite-math-only we get:
        comisd  %xmm1, %xmm0
        je      .L12
        jb      .L13
        movl    $1, %edx
        movl    $2, %eax
        cmova   %edx, %eax
        ret
        .p2align 4,,10
        .p2align 3
.L12:
        xorl    %eax, %eax
        ret
        .p2align 4,,10
        .p2align 3
.L13:
        movl    $-1, %eax
        ret

The main reason is NaNs are interesting and trapping comparisons are also
interesting. Note LLVM does not implement trapping math at all which is why
their code gen is different too.


More information about the Gcc-bugs mailing list