68 lines
1.2 KiB
C++
68 lines
1.2 KiB
C++
|
|
#include "CommonUtil.h"
|
|
#include "float.h"
|
|
#include "math.h"
|
|
|
|
int fastf2i_round(float f)
|
|
{
|
|
#ifdef _WIN32
|
|
int i;
|
|
__asm
|
|
{
|
|
fld f
|
|
fistp i
|
|
}
|
|
return i;
|
|
#else
|
|
return (int)(f + 0.5f);
|
|
#endif
|
|
}
|
|
|
|
int fastf2i(float fValue)
|
|
{
|
|
#ifdef _WIN32
|
|
static const float round_to_nearest = -0.499999f;
|
|
int iValue;
|
|
__asm
|
|
{
|
|
fld fValue
|
|
fadd round_to_nearest
|
|
fistp iValue
|
|
}
|
|
return iValue;
|
|
#else
|
|
return (int)fValue;
|
|
#endif
|
|
}
|
|
|
|
int Float2Int( float a )
|
|
{
|
|
#ifdef _WIN32
|
|
int CtrlwdHolder;
|
|
int CtrlwdSetter;
|
|
int RetVal;
|
|
__asm
|
|
{
|
|
fld a // push 'a' onto the FP stack
|
|
fnstcw CtrlwdHolder // store FPU control word
|
|
movzx eax, CtrlwdHolder // move and zero extend word into eax
|
|
and eax, 0xFFFFF3FF // set all bits except rounding bits to 1
|
|
or eax, 0x00000C00 // set rounding mode bits to round down
|
|
mov CtrlwdSetter, eax // Prepare to set the rounding mode -- prepare to enter plaid!
|
|
fldcw CtrlwdSetter // Entering plaid!
|
|
fistp RetVal // Store and converted (to int) result
|
|
fldcw CtrlwdHolder // Restore control word
|
|
}
|
|
return RetVal;
|
|
#else
|
|
return (int)a;
|
|
#endif
|
|
}
|
|
|
|
/// 2011.04.26 - prodongi
|
|
bool isZero(float value)
|
|
{
|
|
if (fabs(value) < FLT_EPSILON)
|
|
return true;
|
|
return false;
|
|
} |