The easiest is probably:
double input;
long long output;
__asm fld input
__asm fisttp output
This does a 'normal' double to long long conversion, truncating towards zero, just like a C cast. Very old (pre Pentium4) CPUs don't support fistpp
, however, so on such machines you need to use fistp
instead, which uses the current rounding mode (usually round to nearest). So if you want to eg round towards -infinity, you need to save the current rounding mode, set it to what you want, do the fistp
and restore the rounding mode:
double input;
long long output;
unsigned short oldcw, cw;
__asm fld input
__asm fstcw oldcw
cw = (oldcw & ~0xc00) | 0x400; // round towards -infinity
__asm fldcw cw
__asm fistp output
__asm fldcw oldcw