Change floating point rounding mode
What is the most efficient way to change the rounding mode* of IEEE 754 floating point numbers? A portable C function would be nice, but a solution that uses x86 assembly is ok too.
*I am referring to the standard rounding modes of towards nearest, towards zero, and towards positive/negative infinity
This is the standard C solution:
#include <fenv.h>
#pragma STDC FENV_ACCESS ON
// store the original rounding mode
const int originalRounding = fegetround( );
// establish the desired rounding mode
fesetround(FE_TOWARDZERO);
// do whatever you need to do ...
// ... and restore the original mode afterwards
fesetround(originalRounding);
On backwards platforms lacking C99 support, you may need to resort to assembly. In this case, you may want to set the rounding for both the x87 unit (via the fldcw
instruction) and SSE (via the ldmxcsr
instruction).
Edit You don't need to resort to assembly for MSVC. You can use the (totally non-standard) _control_fp( )
instead:
unsigned int originalRounding = _control_fp(0, 0);
_control_fp(_RC_CHOP, _MCW_RC);
// do something ...
_control_fp(originalRounding, _MCW_RC);
You can read more about _control_fp( ) on MSDN.
And, just for completeness, a decoder ring for the macro names for rounding modes:
rounding mode C name MSVC name
-----------------------------------------
to nearest FE_TONEAREST _RC_NEAR
toward zero FE_TOWARDZERO _RC_CHOP
to +infinity FE_UPWARD _RC_UP
to -infinity FE_DOWNWARD _RC_DOWN
this might help.
Edit: I would say you would need your own function. You can use assembly inside C.
But if you register size is 64bits, round it to 32bit would make your calculations faster. It will actually make it slower. Remember 64bit calculations is easy for a 64 microprocessor rather than 2-32bit. I don't know what exactly you want to achieve. I know performance is on your criteria.
链接地址: http://www.djcxy.com/p/85628.html上一篇: 如何使用浮点数执行循环
下一篇: 更改浮点舍入模式