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

--- Comment #6 from Antony Polukhin <antoshkka at gmail dot com> ---
> And I don't think you can reassociate here validly unless -ffast-math.

But you can. In the isolated test case, instead of getting r*r at first, just
move the constant into the xmm1 first and after that multiply it twice with r.
Clang does that

.LCPI0_0:
        .quad   4616189618054758400     # double 4
        .quad   4614256447914709615     # double 3.1415000000000002
test_switch_native_slow(int, double):          # @test_switch_native_slow(int,
double)
        xor     eax, eax
        cmp     edi, 123
        sete    al
        movsd   xmm1, qword ptr [8*rax + .LCPI0_0] # xmm1 = mem[0],zero
        mulsd   xmm1, xmm0
        mulsd   xmm0, xmm1
        ret

Moreover, the current approach "multiply r twice and then multiply it on the
constant" changes the observable behavior, such optimization could be enabled
only with -ffast-math

Reply via email to