2013-04-18 03:09:55 +00:00
|
|
|
// Copyright 2013 Dolphin Emulator Project
|
|
|
|
// Licensed under GPLv2
|
|
|
|
// Refer to the license.txt file included.
|
2013-02-26 19:49:00 +00:00
|
|
|
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "Common/Common.h"
|
|
|
|
#include "Common/CPUDetect.h"
|
2014-02-23 13:57:31 +00:00
|
|
|
#include "Common/FPURoundMode.h"
|
2013-02-26 19:49:00 +00:00
|
|
|
|
2014-02-23 13:57:31 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
# include <mmintrin.h>
|
|
|
|
#else
|
|
|
|
# include <xmmintrin.h>
|
2013-02-26 19:49:00 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace FPURoundMode
|
|
|
|
{
|
|
|
|
// Get the default SSE states here.
|
|
|
|
static u32 saved_sse_state = _mm_getcsr();
|
|
|
|
static const u32 default_sse_state = _mm_getcsr();
|
|
|
|
|
2014-02-23 22:03:39 +00:00
|
|
|
void SetRoundMode(RoundModes mode)
|
2013-02-26 19:49:00 +00:00
|
|
|
{
|
|
|
|
// Set FPU rounding mode to mimic the PowerPC's
|
2014-03-02 11:21:50 +00:00
|
|
|
#ifdef _M_X86_32
|
2013-02-26 19:49:00 +00:00
|
|
|
// This shouldn't really be needed anymore since we use SSE
|
|
|
|
#ifdef _WIN32
|
2013-10-29 05:23:17 +00:00
|
|
|
const int table[4] =
|
2013-02-26 19:49:00 +00:00
|
|
|
{
|
|
|
|
_RC_NEAR,
|
|
|
|
_RC_CHOP,
|
|
|
|
_RC_UP,
|
|
|
|
_RC_DOWN
|
|
|
|
};
|
|
|
|
_set_controlfp(_MCW_RC, table[mode]);
|
|
|
|
#else
|
2014-02-23 13:57:31 +00:00
|
|
|
const unsigned short X87_ROUND_MASK = 3 << 10;
|
|
|
|
const unsigned short x87_rounding_table[] =
|
2013-02-26 19:49:00 +00:00
|
|
|
{
|
2014-02-23 13:57:31 +00:00
|
|
|
0 << 10, // nearest
|
|
|
|
3 << 10, // zero
|
|
|
|
2 << 10, // +inf
|
|
|
|
1 << 10, // -inf
|
2013-02-26 19:49:00 +00:00
|
|
|
};
|
|
|
|
unsigned short _mode;
|
2014-02-23 13:57:31 +00:00
|
|
|
asm ("fstcw %0" : "=m" (_mode));
|
|
|
|
_mode = (_mode & ~X87_ROUND_MASK) | x87_rounding_table[mode];
|
2013-02-26 19:49:00 +00:00
|
|
|
asm ("fldcw %0" : : "m" (_mode));
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-02-23 22:03:39 +00:00
|
|
|
void SetPrecisionMode(PrecisionModes mode)
|
2013-03-20 01:51:12 +00:00
|
|
|
{
|
2014-03-02 11:21:50 +00:00
|
|
|
#ifdef _M_X86_32
|
2013-02-26 19:49:00 +00:00
|
|
|
// sets the floating-point lib to 53-bit
|
|
|
|
// PowerPC has a 53bit floating pipeline only
|
|
|
|
// eg: sscanf is very sensitive
|
|
|
|
#ifdef _WIN32
|
|
|
|
_control87(_PC_53, MCW_PC);
|
|
|
|
#else
|
2014-02-23 13:57:31 +00:00
|
|
|
const unsigned short PRECISION_MASK = 3 << 8;
|
|
|
|
const unsigned short precision_table[] = {
|
|
|
|
0 << 8, // 24 bits
|
|
|
|
2 << 8, // 53 bits
|
|
|
|
3 << 8, // 64 bits
|
2013-02-26 19:49:00 +00:00
|
|
|
};
|
|
|
|
unsigned short _mode;
|
2013-10-06 02:55:01 +00:00
|
|
|
asm ("fstcw %0" : "=m" (_mode));
|
2014-02-23 13:57:31 +00:00
|
|
|
_mode = (_mode & ~PRECISION_MASK) | precision_table[mode];
|
2013-02-26 19:49:00 +00:00
|
|
|
asm ("fldcw %0" : : "m" (_mode));
|
|
|
|
#endif
|
|
|
|
#else
|
|
|
|
//x64 doesn't need this - fpu is done with SSE
|
|
|
|
//but still - set any useful sse options here
|
|
|
|
#endif
|
|
|
|
}
|
2013-10-24 11:52:22 +00:00
|
|
|
|
2014-02-23 22:03:39 +00:00
|
|
|
void SetSIMDMode(RoundModes rounding_mode, bool non_ieee_mode)
|
2013-02-26 19:49:00 +00:00
|
|
|
{
|
2014-02-23 13:57:31 +00:00
|
|
|
// OR-mask for disabling FPU exceptions (bits 7-12 in the MXCSR register)
|
|
|
|
const u32 EXCEPTION_MASK = 0x1F80;
|
|
|
|
// Denormals-Are-Zero (non-IEEE mode: denormal inputs are set to +/- 0)
|
|
|
|
const u32 DAZ = 0x40;
|
|
|
|
// Flush-To-Zero (non-IEEE mode: denormal outputs are set to +/- 0)
|
|
|
|
const u32 FTZ = 0x8000;
|
2013-10-24 11:52:22 +00:00
|
|
|
// lookup table for FPSCR.RN-to-MXCSR.RC translation
|
2014-02-23 13:57:31 +00:00
|
|
|
static const u32 simd_rounding_table[] =
|
2013-02-26 19:49:00 +00:00
|
|
|
{
|
2013-10-24 11:52:22 +00:00
|
|
|
(0 << 13) | EXCEPTION_MASK, // nearest
|
|
|
|
(3 << 13) | EXCEPTION_MASK, // -inf
|
|
|
|
(2 << 13) | EXCEPTION_MASK, // +inf
|
|
|
|
(1 << 13) | EXCEPTION_MASK, // zero
|
2013-02-26 19:49:00 +00:00
|
|
|
};
|
2014-02-23 13:57:31 +00:00
|
|
|
u32 csr = simd_rounding_table[rounding_mode];
|
2013-10-24 11:52:22 +00:00
|
|
|
|
2014-02-23 13:57:31 +00:00
|
|
|
// Some initial steppings of Pentium 4 CPUs support FTZ but not DAZ.
|
|
|
|
// They will not flush input operands but flushing outputs only is better than nothing.
|
2013-10-24 11:52:22 +00:00
|
|
|
static const u32 denormalLUT[2] =
|
|
|
|
{
|
|
|
|
FTZ, // flush-to-zero only
|
|
|
|
FTZ | DAZ, // flush-to-zero and denormals-are-zero (may not be supported)
|
|
|
|
};
|
2014-02-23 13:57:31 +00:00
|
|
|
if (non_ieee_mode)
|
2013-10-24 11:52:22 +00:00
|
|
|
{
|
2013-10-24 20:05:53 +00:00
|
|
|
csr |= denormalLUT[cpu_info.bFlushToZero];
|
2013-10-24 11:52:22 +00:00
|
|
|
}
|
2013-02-26 19:49:00 +00:00
|
|
|
_mm_setcsr(csr);
|
|
|
|
}
|
2013-03-20 01:51:12 +00:00
|
|
|
|
2013-02-26 19:49:00 +00:00
|
|
|
void SaveSIMDState()
|
|
|
|
{
|
|
|
|
saved_sse_state = _mm_getcsr();
|
|
|
|
}
|
|
|
|
void LoadSIMDState()
|
|
|
|
{
|
|
|
|
_mm_setcsr(saved_sse_state);
|
|
|
|
}
|
|
|
|
void LoadDefaultSIMDState()
|
|
|
|
{
|
|
|
|
_mm_setcsr(default_sse_state);
|
|
|
|
}
|
|
|
|
}
|