So I've searched about this topic and found nothing really relevant about it.
I've tried to look at the assembly behind this simple code :
int main(int argc, char *argv[])
{
double d = 1.0;
float f = static_cast<float>(d);
system("PAUSE");
return 0;
}
which is (with Visual Studio 2012) :
15: double d = 1.0;
000000013FD7C16D movsd xmm0,mmword ptr [__real@3ff0000000000000 (013FD91AB0h)]
000000013FD7C175 movsd mmword ptr [d],xmm0
16: float f = static_cast<float>(d);
000000013FD7C17B cvtsd2ss xmm0,mmword ptr [d]
000000013FD7C181 movss dword ptr [f],xmm0
I'm not that comfortable with assembly but tried to analyze that anyway.
So the first two lines seems to move the double-precision value 3ff0000000000000
into a register, and then move the content of the register to the memory adress of d.
Then, I just don't know exactly what does the next lines. The cvtsd2ss
operation is apparently an instruction that convert double precision floating point value to single precision floating point value but I couldn't find what this instruction actually does.
(Then the converted value is moved to the memory space of f).
So my question is, how is this conversion actually done by this instruction ? I know that the C++ cast will yield the closest value in the other type but apart from that, I have no idea about the actual operations performed...
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…