GCC inserts a PXOR when casting int to float

Viewed 58

I noticed when I try to cast an int to float then GCC (with -O3) inserts a PXOR instruction.

float
f(int n) {
    return n;
}

this would generate:

f:
        pxor    xmm0, xmm0             ; this line. 
        cvtsi2ss        xmm0, edi
        ret

Question

  • I can't understand why is it needed here. This link says that CVTSI2SS is used for:

Converting Doubleword Integer to Scalar Single-Precision Floating-Point Value.

Based on this document I don't understand why we need to insert a PXOR there.

Notes

  • I couldn't find anything while searching on internet (I'm really having hard time for searching answers in ASM topic c: ).

  • Previously I asked why GCC inserts an "empty" XOR. And the explanation there is that I had a UB there and GCC saved me from me. But here casting an int to float isn't UB (If I'm not wrong).

  • I thought that maybe explicitly saying that I want a float cast would help but no.

float
g(int n) {
    return (float)n;
}

returns same asm:

g:
        pxor    xmm0, xmm0
        cvtsi2ss        xmm0, edi
        ret
  • If I'm not wrong int is not defined to be exactly 32bits unlike float. So I thought maybe that's the reason. But following code didn't gave me the output I was looking for:
float
h(int32_t n) {
    return (float)n;
}

this outputs:

h:
        pxor    xmm0, xmm0
        cvtsi2ss        xmm0, edi
        ret
  • Also interesting that CLANG doesn't have PXOR there.
  • Finally the Godbolt link.
0 Answers
Related