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
CVTSI2SSis 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
ASMtopic c: ).Previously I asked why GCC inserts an "empty"
XOR. And the explanation there is that I had aUBthere andGCCsaved me from me. But here casting aninttofloatisn'tUB(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
intis not defined to be exactly 32bits unlikefloat. 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
CLANGdoesn't havePXORthere. - Finally the Godbolt link.