2013-04-18 03:09:55 +00:00
|
|
|
// Copyright 2013 Dolphin Emulator Project
|
2015-05-17 23:08:10 +00:00
|
|
|
// Licensed under GPLv2+
|
2013-04-18 03:09:55 +00:00
|
|
|
// Refer to the license.txt file included.
|
2013-02-26 19:49:00 +00:00
|
|
|
|
2014-03-28 18:06:12 +00:00
|
|
|
#include <cfenv>
|
|
|
|
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "Common/CPUDetect.h"
|
2014-09-08 01:06:58 +00:00
|
|
|
#include "Common/CommonTypes.h"
|
2014-02-23 13:57:31 +00:00
|
|
|
#include "Common/FPURoundMode.h"
|
2015-02-23 19:40:05 +00:00
|
|
|
#include "Common/Intrinsics.h"
|
2013-02-26 19:49:00 +00:00
|
|
|
|
|
|
|
namespace FPURoundMode
|
|
|
|
{
|
|
|
|
// Get the default SSE states here.
|
|
|
|
static u32 saved_sse_state = _mm_getcsr();
|
|
|
|
static const u32 default_sse_state = _mm_getcsr();
|
|
|
|
|
2014-03-09 14:21:50 +00:00
|
|
|
void SetRoundMode(int mode)
|
2013-02-26 19:49:00 +00:00
|
|
|
{
|
2014-03-28 18:06:12 +00:00
|
|
|
// Convert PowerPC to native rounding mode.
|
2015-03-19 13:07:33 +00:00
|
|
|
static const int rounding_mode_lut[] = {FE_TONEAREST, FE_TOWARDZERO, FE_UPWARD, FE_DOWNWARD};
|
2014-03-28 18:06:12 +00:00
|
|
|
fesetround(rounding_mode_lut[mode]);
|
2013-02-26 19:49:00 +00:00
|
|
|
}
|
|
|
|
|
2014-03-09 14:21:50 +00:00
|
|
|
void SetSIMDMode(int 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;
|
|
|
|
// 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-10-24 11:52:22 +00:00
|
|
|
(0 << 13) | EXCEPTION_MASK, // nearest
|
2015-06-23 00:17:36 +00:00
|
|
|
(3 << 13) | EXCEPTION_MASK, // zero
|
2013-10-24 11:52:22 +00:00
|
|
|
(2 << 13) | EXCEPTION_MASK, // +inf
|
2015-06-23 00:17:36 +00:00
|
|
|
(1 << 13) | EXCEPTION_MASK, // -inf
|
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
|
|
|
if (non_ieee_mode)
|
2013-10-24 11:52:22 +00:00
|
|
|
{
|
2014-06-15 04:22:34 +00:00
|
|
|
csr |= FTZ;
|
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);
|
|
|
|
}
|
2019-05-05 23:48:12 +00:00
|
|
|
} // namespace FPURoundMode
|