2018-12-01 21:33:25 -05:00
|
|
|
#include "FpUtils.h"
|
|
|
|
#include "MipsJitter.h"
|
|
|
|
|
2020-03-06 13:26:13 -05:00
|
|
|
#ifdef _WIN32
|
2020-03-09 14:34:20 -04:00
|
|
|
#define DENORM_X86
|
2020-03-06 13:26:13 -05:00
|
|
|
#elif defined(__APPLE__)
|
2020-03-09 14:34:20 -04:00
|
|
|
#include <TargetConditionals.h>
|
|
|
|
#if TARGET_CPU_X86 || TARGET_CPU_X86_64
|
|
|
|
#define DENORM_X86
|
|
|
|
#elif TARGET_CPU_ARM64
|
|
|
|
#define DENORM_AARCH64
|
|
|
|
#endif
|
2020-03-06 13:26:13 -05:00
|
|
|
#elif defined(__ANDROID__) || defined(__linux__) || defined(__FreeBSD__)
|
2020-03-09 14:34:20 -04:00
|
|
|
#if defined(__i386__) || defined(__x86_64__)
|
|
|
|
#define DENORM_X86
|
|
|
|
#elif defined(__aarch64__)
|
|
|
|
#define DENORM_AARCH64
|
|
|
|
#endif
|
2020-03-06 13:26:13 -05:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef DENORM_X86
|
2020-03-06 13:07:01 -05:00
|
|
|
#include <xmmintrin.h>
|
|
|
|
#include <pmmintrin.h>
|
2020-03-06 13:26:13 -05:00
|
|
|
#endif
|
2020-03-06 13:07:01 -05:00
|
|
|
|
2018-12-01 21:33:25 -05:00
|
|
|
#define EXC_FP_MAX 0x7F7FFFFF
|
|
|
|
|
2020-03-06 13:07:01 -05:00
|
|
|
void FpUtils::SetDenormalHandlingMode()
|
|
|
|
{
|
2020-03-06 13:26:13 -05:00
|
|
|
#ifdef DENORM_X86
|
2020-03-06 13:07:01 -05:00
|
|
|
_MM_SET_DENORMALS_ZERO_MODE(_MM_DENORMALS_ZERO_ON);
|
|
|
|
_MM_SET_FLUSH_ZERO_MODE(_MM_FLUSH_ZERO_ON);
|
2020-03-06 13:26:13 -05:00
|
|
|
#endif
|
2020-03-09 13:53:42 -04:00
|
|
|
#ifdef DENORM_AARCH64
|
|
|
|
static const uint64 fpcrFZ = (1 << 24);
|
|
|
|
__asm__ __volatile__(
|
2020-03-09 14:34:20 -04:00
|
|
|
"mrs x0, fpcr\n"
|
|
|
|
"orr x0, x0, %0\n"
|
|
|
|
"msr fpcr, x0\n"
|
|
|
|
:
|
|
|
|
: "ri"(fpcrFZ)
|
|
|
|
: "x0");
|
2020-03-09 13:53:42 -04:00
|
|
|
#endif
|
2020-03-06 13:07:01 -05:00
|
|
|
}
|
|
|
|
|
2020-07-28 17:28:53 -04:00
|
|
|
void FpUtils::EnableFpExceptions()
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
unsigned int currentState = 0;
|
|
|
|
_controlfp_s(¤tState, _MCW_EM & ~(_EM_ZERODIVIDE | _EM_INVALID), _MCW_EM);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2018-12-01 21:33:25 -05:00
|
|
|
void FpUtils::IsZero(CMipsJitter* codeGen, size_t offset)
|
|
|
|
{
|
|
|
|
//Check wether an FP number is +/-0
|
2020-03-09 16:22:13 -04:00
|
|
|
//If the exponent is 0, then we have 0 (denormals count as 0)
|
2018-12-01 21:33:25 -05:00
|
|
|
//BeginIf(CONDITION_EQ) to verify result
|
|
|
|
codeGen->PushRel(offset);
|
2020-03-09 16:22:13 -04:00
|
|
|
codeGen->PushCst(0x7F800000);
|
2018-12-01 21:33:25 -05:00
|
|
|
codeGen->And();
|
|
|
|
codeGen->PushCst(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FpUtils::ComputeDivisionByZero(CMipsJitter* codeGen, size_t dividend, size_t divisor)
|
|
|
|
{
|
|
|
|
//Return either +/-FP_MAX
|
|
|
|
codeGen->PushCst(EXC_FP_MAX);
|
|
|
|
codeGen->PushRel(dividend);
|
|
|
|
codeGen->PushRel(divisor);
|
|
|
|
codeGen->Xor();
|
|
|
|
codeGen->PushCst(0x80000000);
|
|
|
|
codeGen->And();
|
|
|
|
codeGen->Or();
|
|
|
|
}
|