Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Interpreter: optimize NaN checks #2586

Merged
merged 2 commits into from Jun 10, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
48 changes: 27 additions & 21 deletions Source/Core/Core/PowerPC/Interpreter/Interpreter_FPUtils.h
Expand Up @@ -4,6 +4,7 @@

#pragma once

#include <cmath>
#include <limits>

#include "Common/CPUDetect.h"
Expand Down Expand Up @@ -83,11 +84,11 @@ inline double Force25Bit(double d)

inline double NI_mul(double a, double b)
{
if (a != a) return a;
if (b != b) return b;
double t = a * b;
if (t != t)
if (std::isnan(t))
{
if (std::isnan(a)) return a;
if (std::isnan(b)) return b;
SetFPException(FPSCR_VXIMZ);
return PPC_NAN;
}
Expand All @@ -96,11 +97,11 @@ inline double NI_mul(double a, double b)

inline double NI_add(double a, double b)
{
if (a != a) return a;
if (b != b) return b;
double t = a + b;
if (t != t)
if (std::isnan(t))
{
if (std::isnan(a)) return a;
if (std::isnan(b)) return b;
SetFPException(FPSCR_VXISI);
return PPC_NAN;
}
Expand All @@ -109,31 +110,35 @@ inline double NI_add(double a, double b)

inline double NI_sub(double a, double b)
{
if (a != a) return a;
if (b != b) return b;
double t = a - b;
if (t != t)
if (std::isnan(t))
{
if (std::isnan(a)) return a;
if (std::isnan(b)) return b;
SetFPException(FPSCR_VXISI);
return PPC_NAN;
}
return t;
}

// FMA instructions on PowerPC are weird:
// They calculate (a * c) + b, but the order in which
// inputs are checked for NaN is still a, b, c.
inline double NI_madd(double a, double c, double b, bool negate = false)
{
if (a != a) return a;
if (b != b) return b;
if (c != c) return c;
double t = a * c;
if (t != t)
if (std::isnan(t))
{
if (std::isnan(a)) return a;
if (std::isnan(b)) return b; // !
if (std::isnan(c)) return c;
SetFPException(FPSCR_VXIMZ);
return PPC_NAN;
}
t = t + b;
if (t != t)
t += b;
if (std::isnan(t))
{
if (std::isnan(b)) return b;
SetFPException(FPSCR_VXISI);
return PPC_NAN;
}
Expand All @@ -142,19 +147,20 @@ inline double NI_madd(double a, double c, double b, bool negate = false)

inline double NI_msub(double a, double c, double b, bool negate = false)
{
if (a != a) return a;
if (b != b) return b;
if (c != c) return c;
double t = a * c;
if (t != t)
if (std::isnan(t))
{
if (std::isnan(a)) return a;
if (std::isnan(b)) return b; // !
if (std::isnan(c)) return c;
SetFPException(FPSCR_VXIMZ);
return PPC_NAN;
}

t = t - b;
if (t != t)
t -= b;
if (std::isnan(t))
{
if (std::isnan(b)) return b;
SetFPException(FPSCR_VXISI);
return PPC_NAN;
}
Expand Down