From 16885d0f74521c904d76dc1e0cdac2d2b690cdc2 Mon Sep 17 00:00:00 2001 From: Tillmann Karras Date: Sun, 9 Mar 2014 19:31:50 +0100 Subject: [PATCH] Interpreter: less duplicate code in float compares --- Source/Core/Core/PowerPC/Gekko.h | 3 +- .../Core/PowerPC/Interpreter/Interpreter.h | 3 + .../PowerPC/Interpreter/Interpreter_FPUtils.h | 8 ++ .../Interpreter/Interpreter_FloatingPoint.cpp | 62 +++++++++---- .../Interpreter/Interpreter_Paired.cpp | 86 +------------------ 5 files changed, 60 insertions(+), 102 deletions(-) diff --git a/Source/Core/Core/PowerPC/Gekko.h b/Source/Core/Core/PowerPC/Gekko.h index 4141d074c7..a100c97c97 100644 --- a/Source/Core/Core/PowerPC/Gekko.h +++ b/Source/Core/Core/PowerPC/Gekko.h @@ -410,7 +410,8 @@ union UReg_FPSCR u32 VXSOFT : 1; // reserved u32 : 1; - // Floating point result flags (not sticky) + // Floating point result flags (includes FPCC) (not sticky) + // from more to less significand: class, <, >, =, ? u32 FPRF : 5; // Fraction inexact (not sticky) u32 FI : 1; diff --git a/Source/Core/Core/PowerPC/Interpreter/Interpreter.h b/Source/Core/Core/PowerPC/Interpreter/Interpreter.h index 0e58f98776..b1321df18d 100644 --- a/Source/Core/Core/PowerPC/Interpreter/Interpreter.h +++ b/Source/Core/Core/PowerPC/Interpreter/Interpreter.h @@ -326,6 +326,9 @@ private: Interpreter(const Interpreter &); Interpreter & operator=(const Interpreter &); + static void Helper_FloatCompareOrdered(UGeckoInstruction _inst, double a, double b); + static void Helper_FloatCompareUnordered(UGeckoInstruction _inst, double a, double b); + // TODO: These should really be in the save state, although it's unlikely to matter much. // They are for lwarx and its friend stwcxd. static bool g_bReserve; diff --git a/Source/Core/Core/PowerPC/Interpreter/Interpreter_FPUtils.h b/Source/Core/Core/PowerPC/Interpreter/Interpreter_FPUtils.h index 72136d71d3..5131e526b3 100644 --- a/Source/Core/Core/PowerPC/Interpreter/Interpreter_FPUtils.h +++ b/Source/Core/Core/PowerPC/Interpreter/Interpreter_FPUtils.h @@ -40,6 +40,14 @@ const u32 FPSCR_ANY_X = FPSCR_OX | FPSCR_UX | FPSCR_ZX | FPSCR_XX | FPSCR_V const u64 PPC_NAN_U64 = 0x7ff8000000000000ull; const double PPC_NAN = *(double* const)&PPC_NAN_U64; +// the 4 less-significand bits in FPSCR[FPRF] +enum FPCC { + FL = 8, // < + FG = 4, // > + FE = 2, // = + FU = 1, // ? +}; + inline void SetFPException(u32 mask) { if ((FPSCR.Hex & mask) != mask) diff --git a/Source/Core/Core/PowerPC/Interpreter/Interpreter_FloatingPoint.cpp b/Source/Core/Core/PowerPC/Interpreter/Interpreter_FloatingPoint.cpp index 3b51496663..4a375ff06b 100644 --- a/Source/Core/Core/PowerPC/Interpreter/Interpreter_FloatingPoint.cpp +++ b/Source/Core/Core/PowerPC/Interpreter/Interpreter_FloatingPoint.cpp @@ -33,29 +33,36 @@ void Interpreter::Helper_UpdateCR1(double _fValue) SetCRField(1, (FPSCR.FX << 4) | (FPSCR.FEX << 3) | (FPSCR.VX << 2) | FPSCR.OX); } -void Interpreter::fcmpo(UGeckoInstruction _inst) +void Interpreter::Helper_FloatCompareOrdered(UGeckoInstruction _inst, double fa, double fb) { - double fa = rPS0(_inst.FA); - double fb = rPS0(_inst.FB); - int compareResult; - if (fa < fb) compareResult = 8; - else if (fa > fb) compareResult = 4; - else if (fa == fb) compareResult = 2; - else + if (fa < fb) + { + compareResult = FPCC::FL; + } + else if (fa > fb) + { + compareResult = FPCC::FG; + } + else if (fa == fb) + { + compareResult = FPCC::FE; + } + else // NaN { FPSCR.FX = 1; - compareResult = 1; + compareResult = FPCC::FU; if (IsSNAN(fa) || IsSNAN(fb)) { SetFPException(FPSCR_VXSNAN); if (FPSCR.VE == 0) + { SetFPException(FPSCR_VXVC); + } } - else + else // QNaN { - //if (IsQNAN(fa) || IsQNAN(fb)) // this is always true SetFPException(FPSCR_VXVC); } } @@ -64,21 +71,28 @@ void Interpreter::fcmpo(UGeckoInstruction _inst) SetCRField(_inst.CRFD, compareResult); } -void Interpreter::fcmpu(UGeckoInstruction _inst) +void Interpreter::Helper_FloatCompareUnordered(UGeckoInstruction _inst, double fa, double fb) { - double fa = rPS0(_inst.FA); - double fb = rPS0(_inst.FB); - int compareResult; - if (fa < fb) compareResult = 8; - else if (fa > fb) compareResult = 4; - else if (fa == fb) compareResult = 2; + if (fa < fb) + { + compareResult = FPCC::FL; + } + else if (fa > fb) + { + compareResult = FPCC::FG; + } + else if (fa == fb) + { + compareResult = FPCC::FE; + } else { - compareResult = 1; + compareResult = FPCC::FU; if (IsSNAN(fa) || IsSNAN(fb)) { + FPSCR.FX = 1; SetFPException(FPSCR_VXSNAN); } } @@ -86,6 +100,16 @@ void Interpreter::fcmpu(UGeckoInstruction _inst) SetCRField(_inst.CRFD, compareResult); } +void Interpreter::fcmpo(UGeckoInstruction _inst) +{ + Helper_FloatCompareOrdered(_inst, rPS0(_inst.FA), rPS0(_inst.FB)); +} + +void Interpreter::fcmpu(UGeckoInstruction _inst) +{ + Helper_FloatCompareUnordered(_inst, rPS0(_inst.FA), rPS0(_inst.FB)); +} + // Apply current rounding mode void Interpreter::fctiwx(UGeckoInstruction _inst) { diff --git a/Source/Core/Core/PowerPC/Interpreter/Interpreter_Paired.cpp b/Source/Core/Core/PowerPC/Interpreter/Interpreter_Paired.cpp index ce99a5cfe9..c50798b308 100644 --- a/Source/Core/Core/PowerPC/Interpreter/Interpreter_Paired.cpp +++ b/Source/Core/Core/PowerPC/Interpreter/Interpreter_Paired.cpp @@ -354,100 +354,22 @@ void Interpreter::ps_madds1(UGeckoInstruction _inst) void Interpreter::ps_cmpu0(UGeckoInstruction _inst) { - double fa = rPS0(_inst.FA); - double fb = rPS0(_inst.FB); - int compareResult; - - if (fa < fb) compareResult = 8; - else if (fa > fb) compareResult = 4; - else if (fa == fb) compareResult = 2; - else - { - compareResult = 1; - if (IsSNAN(fa) || IsSNAN(fb)) - { - SetFPException(FPSCR_VXSNAN); - } - } - FPSCR.FPRF = compareResult; - SetCRField(_inst.CRFD, compareResult); + Helper_FloatCompareUnordered(_inst, rPS0(_inst.FA), rPS0(_inst.FB)); } void Interpreter::ps_cmpo0(UGeckoInstruction _inst) { - double fa = rPS0(_inst.FA); - double fb = rPS0(_inst.FB); - int compareResult; - - if (fa < fb) compareResult = 8; - else if (fa > fb) compareResult = 4; - else if (fa == fb) compareResult = 2; - else - { - compareResult = 1; - if (IsSNAN(fa) || IsSNAN(fb)) - { - SetFPException(FPSCR_VXSNAN); - if (!FPSCR.VE) - SetFPException(FPSCR_VXVC); - } - else - { - //if (IsQNAN(fa) || IsQNAN(fb)) // this is always true - SetFPException(FPSCR_VXVC); - } - } - FPSCR.FPRF = compareResult; - SetCRField(_inst.CRFD, compareResult); + Helper_FloatCompareOrdered(_inst, rPS0(_inst.FA), rPS0(_inst.FB)); } void Interpreter::ps_cmpu1(UGeckoInstruction _inst) { - double fa = rPS1(_inst.FA); - double fb = rPS1(_inst.FB); - int compareResult; - - if (fa < fb) compareResult = 8; - else if (fa > fb) compareResult = 4; - else if (fa == fb) compareResult = 2; - else - { - compareResult = 1; - if (IsSNAN(fa) || IsSNAN(fb)) - { - SetFPException(FPSCR_VXSNAN); - } - } - FPSCR.FPRF = compareResult; - SetCRField(_inst.CRFD, compareResult); + Helper_FloatCompareUnordered(_inst, rPS1(_inst.FA), rPS1(_inst.FB)); } void Interpreter::ps_cmpo1(UGeckoInstruction _inst) { - double fa = rPS1(_inst.FA); - double fb = rPS1(_inst.FB); - int compareResult; - - if (fa < fb) compareResult = 8; - else if (fa > fb) compareResult = 4; - else if (fa == fb) compareResult = 2; - else - { - compareResult = 1; - if (IsSNAN(fa) || IsSNAN(fb)) - { - SetFPException(FPSCR_VXSNAN); - if (!FPSCR.VE) - SetFPException(FPSCR_VXVC); - } - else - { - //if (IsQNAN(fa) || IsQNAN(fb)) // this is always true - SetFPException(FPSCR_VXVC); - } - } - FPSCR.FPRF = compareResult; - SetCRField(_inst.CRFD, compareResult); + Helper_FloatCompareOrdered(_inst, rPS1(_inst.FA), rPS1(_inst.FB)); } // __________________________________________________________________________________________________