diff --git a/runtime/include/isa/ppc_isa_float.h b/runtime/include/isa/ppc_isa_float.h index c01377145..f83dd04ed 100644 --- a/runtime/include/isa/ppc_isa_float.h +++ b/runtime/include/isa/ppc_isa_float.h @@ -431,22 +431,74 @@ inline double PpcPackIntegerWordInline(int32_t value) static_cast(static_cast(value))); } +// Bit-identical std::fma(float) without an FMA instruction: a float product is exact in double. +// Halfway sums, results <= FLT_MIN and subnormal/non-finite operands fall back to std::fma. +inline float PpcFusedMultiplyAddFloatViaDoubleInline(float a, float c, float b) +{ + const auto isNormalOrZero = [](float value) { + const uint32_t absBits = PpcBitCastToU32Inline(value) & 0x7FFFFFFFu; + return absBits == 0u || (absBits - 0x00800000u) < 0x7F000000u; + }; + if (isNormalOrZero(a) && isNormalOrZero(c) && isNormalOrZero(b)) [[likely]] { + const double sum = + static_cast(a) * static_cast(c) + static_cast(b); + const uint64_t bits = PpcBitCastToU64Inline(sum); + const uint64_t magnitude = bits & 0x7FFFFFFFFFFFFFFFULL; + constexpr uint64_t kFloatMinNormal = 0x3810000000000000ULL; + // Double bits below a normal float's LSB, and the halfway pattern within them. + constexpr uint64_t kBelowFloatLsbMask = 0x1FFFFFFFULL; + constexpr uint64_t kFloatHalfway = 0x10000000ULL; + if ((magnitude == 0 || magnitude > kFloatMinNormal) && + (bits & kBelowFloatLsbMask) != kFloatHalfway) [[likely]] + return static_cast(sum); + } + return std::fma(a, c, b); +} + +// On v2, std::fma is a CRT call that is ~12x slower in software without hardware FMA (THP decode). +// Use the double path there; CPUs with FMA3/FMA4 keep the CRT's hardware path. +inline float PpcFusedMultiplyAddFloatInline(float a, float c, float b) +{ +#if defined(__x86_64__) && !defined(__FMA__) && !(defined(__GLIBC__) && defined(__FMA4__)) + bool crtHasHardwareFma = __builtin_cpu_supports("fma"); +#if defined(__GLIBC__) + // glibc's fmaf also dispatches to FMA4. + crtHasHardwareFma = crtHasHardwareFma || + (__builtin_cpu_supports("fma4") && __builtin_cpu_supports("avx")); +#endif + if (!crtHasHardwareFma) + return PpcFusedMultiplyAddFloatViaDoubleInline(a, c, b); +#endif + return std::fma(a, c, b); +} + +// The addend a lane FMA sees; a NaN subtractor stays unnegated so its sign matches Gekko/vfmsub. +template +inline float PpcPsMaddLaneAddendInline(float b) +{ + if constexpr (Subtract) { + if ((PpcBitCastToU32Inline(b) & 0x7FFFFFFFu) <= 0x7F800000u) + return -b; + } + return b; +} + template inline float PpcAccuratePsMaddLaneInline(float a, float c, float b) { - const float signedB = Subtract ? -b : b; + const float signedB = PpcPsMaddLaneAddendInline(b); // Paired values are stored as exact float32 lanes in this runtime. An // explicit float FMA therefore matches Gekko's single rounding point; // multiplying and adding separately can differ by an ULP on ordinary // vector and matrix workloads. - return PpcFlushSingleForNiInline(std::fma(a, c, signedB)); + return PpcFlushSingleForNiInline(PpcFusedMultiplyAddFloatInline(a, c, signedB)); } template inline float PpcAccuratePsMaddLaneNoNiInline(float a, float c, float b) { - const float signedB = Subtract ? -b : b; - return std::fma(a, c, signedB); + const float signedB = PpcPsMaddLaneAddendInline(b); + return PpcFusedMultiplyAddFloatInline(a, c, signedB); } template @@ -643,7 +695,14 @@ inline PpcPairVec PpcFmsubPairInline(PpcPairVec multiplicand, PpcPairVec multipl { #if defined(__x86_64__) #if defined(__FMA__) - return _mm_fmsub_ps(multiplicand, multiplier, subtractor); + // Asm, not _mm_fmsub_ps: Clang can fold that into vfmadd and flip a NaN subtractor's sign. + // Volatile + memory clobber keeps it ordered against MXCSR (NI mode) and fenv calls. + PpcPairVec result = multiplicand; + __asm__ __volatile__("vfmsub213ps {%2, %1, %0|%0, %1, %2}" + : "+x"(result) + : "x"(multiplier), "x"(subtractor) + : "memory"); + return result; #else const double a = PpcM128ToPsInline(multiplicand); const double c = PpcM128ToPsInline(multiplier); diff --git a/runtime/tests/ppc_pair_fma_tests.cpp b/runtime/tests/ppc_pair_fma_tests.cpp index 7d7720256..da624020b 100644 --- a/runtime/tests/ppc_pair_fma_tests.cpp +++ b/runtime/tests/ppc_pair_fma_tests.cpp @@ -1,8 +1,16 @@ #include "isa/ppc_isa_float.h" +#include +#include #include #include #include +#include +#include + +#if defined(__x86_64__) +#include +#endif namespace { @@ -11,48 +19,488 @@ bool SameBits(double lhs, double rhs) return PpcBitCastToU64Inline(lhs) == PpcBitCastToU64Inline(rhs); } +// The addend of a fused subtract: a NaN subtractor keeps its sign, as x86 vfmsub and Gekko leave it. +float SubtractAddend(float b, bool subtract) +{ + return subtract && !std::isnan(b) ? -b : b; +} + bool CheckPair(double actual, float a0, float a1, float c0, float c1, float b0, float b1, bool subtract, const char* operation) { - const float expected0 = subtract ? std::fma(a0, c0, -b0) : std::fma(a0, c0, b0); - const float expected1 = subtract ? std::fma(a1, c1, -b1) : std::fma(a1, c1, b1); + const float expected0 = std::fma(a0, c0, SubtractAddend(b0, subtract)); + const float expected1 = std::fma(a1, c1, SubtractAddend(b1, subtract)); const double expected = PpcPackPairedInline(expected0, expected1); if (SameBits(actual, expected)) { return true; } - std::fprintf(stderr, "%s produced the wrong paired lanes\n", operation); + std::fprintf(stderr, "%s produced the wrong paired lanes (0x%016llx, expected 0x%016llx)\n", + operation, static_cast(PpcBitCastToU64Inline(actual)), + static_cast(PpcBitCastToU64Inline(expected))); return false; } bool CheckNegatedPair(double actual, float a0, float a1, float c0, float c1, float b0, float b1, bool subtract, const char* operation) { - const float expected0 = -std::fma(a0, c0, subtract ? -b0 : b0); - const float expected1 = -std::fma(a1, c1, subtract ? -b1 : b1); + const float raw0 = std::fma(a0, c0, SubtractAddend(b0, subtract)); + const float raw1 = std::fma(a1, c1, SubtractAddend(b1, subtract)); + const float expected0 = std::isnan(raw0) ? raw0 : -raw0; + const float expected1 = std::isnan(raw1) ? raw1 : -raw1; const double expected = PpcPackPairedInline(expected0, expected1); if (SameBits(actual, expected)) { return true; } - std::fprintf(stderr, "%s produced the wrong paired lanes\n", operation); + std::fprintf(stderr, "%s produced the wrong paired lanes (0x%016llx, expected 0x%016llx)\n", + operation, static_cast(PpcBitCastToU64Inline(actual)), + static_cast(PpcBitCastToU64Inline(expected))); return false; } -} // namespace +bool CheckPairedFamily(float a0, float a1, float c0, float c1, float b0, float b1) +{ + const double a = PpcPackPairedInline(a0, a1); + const double c = PpcPackPairedInline(c0, c1); + const double b = PpcPackPairedInline(b0, b1); -int main() + bool ok = true; + ok &= CheckPair(PPC_PsMaddNoNiInline(a, c, b), a0, a1, c0, c1, b0, b1, false, "ps_madd"); + ok &= CheckPair(PPC_PsMsubNoNiInline(a, c, b), a0, a1, c0, c1, b0, b1, true, "ps_msub"); + ok &= CheckPair(PPC_PsMadds0Inline(a, c, b), a0, a1, c0, c0, b0, b1, false, "ps_madds0"); + ok &= CheckPair(PPC_PsMadds1Inline(a, c, b), a0, a1, c1, c1, b0, b1, false, "ps_madds1"); + ok &= CheckNegatedPair(PPC_PsNmaddInline(a, c, b), a0, a1, c0, c1, b0, b1, false, "ps_nmadd"); + ok &= CheckNegatedPair(PPC_PsNmsubNoNiInline(a, c, b), a0, a1, c0, c1, b0, b1, true, + "ps_nmsub"); + return ok; +} + +bool CheckPairedSpecialCases() +{ + const float posQNaN = PpcBitCastToFloatInline(0x7FC12345u); + const float negQNaN = PpcBitCastToFloatInline(0xFFC54321u); + const float sNaN = PpcBitCastToFloatInline(0x7F812345u); + + bool ok = true; + // Signed-zero propagation and exact cancellation across both lanes and broadcast ops. + ok &= CheckPairedFamily(0.0f, -0.0f, 1.0f, 1.0f, 0.0f, -0.0f); + ok &= CheckPairedFamily(-0.0f, 0.0f, 1.0f, -1.0f, -0.0f, -0.0f); + ok &= CheckPairedFamily(3.0f, -3.0f, 2.0f, 2.0f, -6.0f, 6.0f); + // Distinct broadcast multiplier lanes to catch ps_madds0 / ps_madds1 lane selection bugs. + ok &= CheckPairedFamily(2.0f, 5.0f, 7.0f, -11.0f, 13.0f, -17.0f); + // Quiet and signaling NaNs: verify negated ops preserve NaN sign bits. + ok &= CheckPairedFamily(posQNaN, 2.0f, 3.0f, -4.0f, 1.0f, negQNaN); + ok &= CheckPairedFamily(sNaN, 1.0f, 2.0f, 3.0f, 4.0f, sNaN); + return ok; +} + +#if defined(__GNUC__) || defined(__clang__) +#define PPC_FMA_TEST_NOINLINE __attribute__((noinline)) +#elif defined(_MSC_VER) +#define PPC_FMA_TEST_NOINLINE __declspec(noinline) +#else +#define PPC_FMA_TEST_NOINLINE +#endif + +PPC_FMA_TEST_NOINLINE float EvalStdFma(float a, float c, float b) +{ + return std::fma(a, c, b); +} + +PPC_FMA_TEST_NOINLINE float EvalViaDoubleFma(float a, float c, float b) +{ + return PpcFusedMultiplyAddFloatViaDoubleInline(a, c, b); +} + +PPC_FMA_TEST_NOINLINE float EvalDispatchedFma(float a, float c, float b) +{ + return PpcFusedMultiplyAddFloatInline(a, c, b); +} + +// Volatiles pin the call between feclearexcept and fetestexcept; Clang would otherwise move it. +float EvaluateTrackingInvalid(float (*eval)(float, float, float), float a, float c, float b, + bool& invalid) +{ + volatile float va = a; + volatile float vc = c; + volatile float vb = b; + volatile float result; + std::feclearexcept(FE_ALL_EXCEPT); + result = eval(va, vc, vb); + invalid = std::fetestexcept(FE_INVALID) != 0; + return result; +} + +PPC_FMA_TEST_NOINLINE double EvalPsMsub(double a, double c, double b) +{ + return PPC_PsMsubNoNiInline(a, c, b); +} + +PPC_FMA_TEST_NOINLINE double EvalPsNmsub(double a, double c, double b) +{ + return PPC_PsNmsubNoNiInline(a, c, b); +} + +// Same pinning as EvaluateTrackingInvalid, for a paired op on operands only known at run time. +double EvaluatePairTrackingInvalid(double (*eval)(double, double, double), double a, double c, + double b, bool& invalid) +{ + volatile double va = a; + volatile double vc = c; + volatile double vb = b; + volatile double result; + std::feclearexcept(FE_ALL_EXCEPT); + result = eval(va, vc, vb); + invalid = std::fetestexcept(FE_INVALID) != 0; + return result; +} + +// ps_msub/ps_nmsub keep a NaN subtractor's sign and raise FE_INVALID only for sNaN, on run-time +// operands. The CRT's software fmaf doesn't quiet sNaN, so that reference skips those cases. +bool CheckRuntimeFusedSubtract(const char* mode, bool crtSoftwareReference) +{ + struct Case { + float b0; + float b1; + uint32_t lane0; + uint32_t lane1; + bool invalid; + const char* label; + }; + const float negQNaN = PpcBitCastToFloatInline(0xFFC54321u); + const float posSNaN = PpcBitCastToFloatInline(0x7F812345u); + const float negSNaN = PpcBitCastToFloatInline(0xFF812345u); + // a = (2, 3), c = (5, 7): a*c - b = (10 - b0, 21 - b1). + const std::array cases{{ + {1.0f, 0.5f, PpcBitCastToU32Inline(9.0f), PpcBitCastToU32Inline(20.5f), false, + "finite"}, + {negQNaN, 1.0f, 0xFFC54321u, PpcBitCastToU32Inline(20.0f), false, "-qNaN subtractor"}, + {1.0f, posSNaN, PpcBitCastToU32Inline(9.0f), 0x7FC12345u, true, "+sNaN subtractor"}, + {negSNaN, 1.0f, 0xFFC12345u, PpcBitCastToU32Inline(20.0f), true, "-sNaN subtractor"}, + }}; + const double a = PpcPackPairedInline(2.0f, 3.0f); + const double c = PpcPackPairedInline(5.0f, 7.0f); + + bool ok = true; + for (const Case& tc : cases) { + if (crtSoftwareReference && tc.invalid) { + continue; + } + const double b = PpcPackPairedInline(tc.b0, tc.b1); + const auto lane = [](uint32_t bits, bool negate) { + const bool isNan = (bits & 0x7FFFFFFFu) > 0x7F800000u; + return negate && !isNan ? bits ^ 0x80000000u : bits; + }; + for (const bool negated : {false, true}) { + bool invalid = false; + const double actual = EvaluatePairTrackingInvalid(negated ? &EvalPsNmsub : &EvalPsMsub, + a, c, b, invalid); + const double expected = PpcPackPairedInline( + PpcBitCastToFloatInline(lane(tc.lane0, negated)), + PpcBitCastToFloatInline(lane(tc.lane1, negated))); + if (!SameBits(actual, expected) || invalid != tc.invalid) { + std::fprintf(stderr, + "%s %s (%s): got 0x%016llx (FE_INVALID=%d), expected 0x%016llx " + "(FE_INVALID=%d)\n", + mode, negated ? "ps_nmsub" : "ps_msub", tc.label, + static_cast(PpcBitCastToU64Inline(actual)), + invalid ? 1 : 0, + static_cast(PpcBitCastToU64Inline(expected)), + tc.invalid ? 1 : 0); + ok = false; + } + } + } + return ok; +} + +bool CheckInvalidExceptionParity(const char* mode) +{ + constexpr float kInf = std::numeric_limits::infinity(); + const float posQNaN = PpcBitCastToFloatInline(0x7FC01234u); + const float negQNaN = PpcBitCastToFloatInline(0xFFC05678u); + const float sNaN = PpcBitCastToFloatInline(0x7F801234u); + + struct Case { + float a; + float c; + float b; + const char* label; + }; + const std::array cases{{ + {0.0f, kInf, posQNaN, "0 * +Inf + qNaN"}, + {kInf, 0.0f, posQNaN, "+Inf * 0 + qNaN"}, + {-0.0f, -kInf, posQNaN, "-0 * -Inf + qNaN"}, + {0.0f, -kInf, negQNaN, "0 * -Inf + -qNaN"}, + {kInf, 1.0f, -kInf, "+Inf * 1 + -Inf"}, + {sNaN, 1.0f, 0.0f, "sNaN * 1 + 0"}, + {1.0f, 1.0f, posQNaN, "1 * 1 + qNaN"}, + }}; + + bool ok = true; + for (const Case& tc : cases) { + bool expectedInvalid = false; + const float expected = EvaluateTrackingInvalid(&EvalStdFma, tc.a, tc.c, tc.b, expectedInvalid); + + const auto verifyPath = [&](const char* path, float (*eval)(float, float, float)) { + bool actualInvalid = false; + const float actual = EvaluateTrackingInvalid(eval, tc.a, tc.c, tc.b, actualInvalid); + if (PpcBitCastToU32Inline(actual) != PpcBitCastToU32Inline(expected) || + actualInvalid != expectedInvalid) { + std::fprintf(stderr, + "%s %s (%s): got 0x%08x (FE_INVALID=%d), expected 0x%08x " + "(FE_INVALID=%d)\n", + mode, path, tc.label, PpcBitCastToU32Inline(actual), + actualInvalid ? 1 : 0, PpcBitCastToU32Inline(expected), + expectedInvalid ? 1 : 0); + return false; + } + return true; + }; + + ok &= verifyPath("via double", &EvalViaDoubleFma); + ok &= verifyPath("dispatched", &EvalDispatchedFma); + } + return ok; +} + +bool CheckFmaResult(float actual, float expected, float a, float c, float b, const char* path, + const char* context) +{ + if (PpcBitCastToU32Inline(actual) == PpcBitCastToU32Inline(expected)) { + return true; + } + std::fprintf(stderr, + "%s %s: fma(%a, %a, %a) = %a (0x%08x), expected %a (0x%08x)\n", context, path, + static_cast(a), static_cast(c), static_cast(b), + static_cast(actual), PpcBitCastToU32Inline(actual), + static_cast(expected), PpcBitCastToU32Inline(expected)); + return false; +} + +// Check the double path directly so hosts with FMA3 still cover it. +bool CheckScalarFma(float a, float c, float b, const char* context) +{ + const float expected = std::fma(a, c, b); + bool ok = CheckFmaResult(PpcFusedMultiplyAddFloatViaDoubleInline(a, c, b), expected, a, c, b, + "via double", context); + ok &= CheckFmaResult(PpcFusedMultiplyAddFloatInline(a, c, b), expected, a, c, b, "dispatched", + context); + return ok; +} + +// a*c sits just below a float halfway point, so fused gives b; rounding to double first lands on +// the halfway point and ties-to-even picks the next float. +bool CheckHalfwayDoubleRounding() { - const double a = PpcPackPairedInline(1.000000119f, -123.25f); - const double c = PpcPackPairedInline(33554431.0f, 0.0625f); - const double b = PpcPackPairedInline(-33554430.0f, 7.75f); + const float a = 1.0f + std::ldexp(1.0f, -23); + const float c = 64.0f * (1.0f - std::ldexp(1.0f, -23)); + const float b = std::ldexp(1.0f, 30) + std::ldexp(1.0f, 7); + + const float fused = std::fma(a, c, b); + const float viaDouble = + static_cast(static_cast(a) * static_cast(c) + static_cast(b)); + if (fused != b || viaDouble == fused) { + std::fprintf(stderr, "halfway case no longer exercises double rounding\n"); + return false; + } + + bool ok = CheckScalarFma(a, c, b, "halfway double rounding"); + ok &= CheckScalarFma(-a, c, -b, "halfway double rounding (negated)"); + ok &= CheckPairedFamily(a, -a, c, c, b, -b); + return ok; +} + +// Same double-rounding trap between two subnormals, where the normal-float halfway check can't see +// it; this is why results <= FLT_MIN fall back to std::fma. +bool CheckSubnormalHalfwayDoubleRounding() +{ + const float a = -std::ldexp(3.0f + std::ldexp(3.0f, -20), -75); + const float c = std::ldexp(1.0f - std::ldexp(1.0f, -20), -75); + const float b = std::numeric_limits::min(); + + const float fused = std::fma(a, c, b); + const float viaDouble = + static_cast(static_cast(a) * static_cast(c) + static_cast(b)); + if (PpcBitCastToU32Inline(fused) != 0x007FFFFFu || viaDouble == fused) { + std::fprintf(stderr, "subnormal halfway case no longer exercises double rounding\n"); + return false; + } + + bool ok = CheckScalarFma(a, c, b, "subnormal halfway double rounding"); + ok &= CheckScalarFma(-a, c, -b, "subnormal halfway double rounding (negated)"); + return ok; +} + +bool CheckEdgeValues(const char* mode) +{ + constexpr float kInf = std::numeric_limits::infinity(); + const std::array values{ + 0.0f, + -0.0f, + 1.0f, + -1.0f, + 0.5f, + 1.0f + std::numeric_limits::epsilon(), + std::numeric_limits::min(), + -std::numeric_limits::min(), + std::numeric_limits::denorm_min(), + -std::numeric_limits::min() * 0.75f, + std::numeric_limits::max(), + -std::numeric_limits::max(), + std::ldexp(1.0f, -63), + std::ldexp(1.0f, 64), + kInf, + -kInf, + }; bool ok = true; - ok &= CheckPair(PPC_PsMaddNoNiInline(a, c, b), 1.000000119f, -123.25f, - 33554431.0f, 0.0625f, -33554430.0f, 7.75f, false, "ps_madd"); - ok &= CheckPair(PPC_PsMsubNoNiInline(a, c, b), 1.000000119f, -123.25f, - 33554431.0f, 0.0625f, -33554430.0f, 7.75f, true, "ps_msub"); - ok &= CheckNegatedPair(PPC_PsNmaddInline(a, c, b), 1.000000119f, -123.25f, - 33554431.0f, 0.0625f, -33554430.0f, 7.75f, false, "ps_nmadd"); - ok &= CheckNegatedPair(PPC_PsNmsubNoNiInline(a, c, b), 1.000000119f, -123.25f, - 33554431.0f, 0.0625f, -33554430.0f, 7.75f, true, "ps_nmsub"); + for (const float a : values) { + for (const float c : values) { + for (const float b : values) { + ok &= CheckScalarFma(a, c, b, mode); + } + } + } + return ok; +} + +struct SplitMix64 { + uint64_t state; + + uint64_t Next() + { + uint64_t z = (state += 0x9E3779B97F4A7C15ULL); + z = (z ^ (z >> 30)) * 0xBF58476D1CE4E5B9ULL; + z = (z ^ (z >> 27)) * 0x94D049BB133111EBULL; + return z ^ (z >> 31); + } +}; + +float RandomFloat(SplitMix64& rng) +{ + const uint64_t r = rng.Next(); + if ((r & 3u) == 0) { + // Any bit pattern: NaNs, infinities, subnormals and extreme exponents. + return PpcBitCastToFloatInline(static_cast(r >> 32)); + } + // Ordinary magnitudes, where the fast path does its work. + const uint32_t sign = static_cast(r >> 63) << 31; + const uint32_t exponent = 127u - 40u + static_cast((r >> 40) % 81u); + const uint32_t mantissa = static_cast(r >> 8) & 0x007FFFFFu; + return PpcBitCastToFloatInline(sign | (exponent << 23) | mantissa); +} + +bool CheckRandomized(const char* mode) +{ + SplitMix64 rng{0x5EED0F7A11ULL}; + int failures = 0; + for (int i = 0; i < 2'000'000 && failures < 8; ++i) { + const float a = RandomFloat(rng); + const float c = RandomFloat(rng); + float b = RandomFloat(rng); + if ((rng.Next() & 7u) == 0) { + // Near-total cancellation against the exact product. + b = -static_cast(static_cast(a) * static_cast(c)); + } + if (!CheckScalarFma(a, c, b, mode)) { + ++failures; + } + } + return failures == 0; +} + +// The runtime models Gekko NI mode as MXCSR FTZ+DAZ (MkwApplyHostNiMode). +struct FlushToZeroScope { +#if defined(__x86_64__) + explicit FlushToZeroScope(bool enable) : saved(_mm_getcsr()) + { + constexpr unsigned kFtzDaz = 0x8040u; + _mm_setcsr(enable ? (saved | kFtzDaz) : (saved & ~kFtzDaz)); + } + ~FlushToZeroScope() { _mm_setcsr(saved); } + unsigned saved; +#else + explicit FlushToZeroScope(bool) {} +#endif +}; + +// Same ps_msub either side of an NI toggle must give FLT_MIN then 0; catches the v3 asm being +// merged or moved across the MXCSR write. +bool CheckFusedSubtractFollowsNiMode() +{ +#if defined(__x86_64__) && defined(__FMA__) + const float denormal = PpcBitCastToFloatInline(0x00000001u); + const double a = PpcPackPairedInline(denormal, 2.0f); + const double c = PpcPackPairedInline(std::ldexp(1.0f, 23), 3.0f); + const double b = PpcPackPairedInline(0.0f, 1.0f); + + MkwApplyHostNiMode(0); + const double ieee = PPC_PsMsubNoNiInline(a, c, b); + MkwApplyHostNiMode(0x4u); + const double flushed = PPC_PsMsubNoNiInline(a, c, b); + MkwApplyHostNiMode(0); + + const double expectedIeee = PpcPackPairedInline(PpcBitCastToFloatInline(0x00800000u), 5.0f); + const double expectedFlushed = PpcPackPairedInline(0.0f, 5.0f); + if (SameBits(ieee, expectedIeee) && SameBits(flushed, expectedFlushed)) { + return true; + } + std::fprintf(stderr, + "ps_msub across an NI toggle: got 0x%016llx then 0x%016llx, expected 0x%016llx " + "then 0x%016llx\n", + static_cast(PpcBitCastToU64Inline(ieee)), + static_cast(PpcBitCastToU64Inline(flushed)), + static_cast(PpcBitCastToU64Inline(expectedIeee)), + static_cast(PpcBitCastToU64Inline(expectedFlushed))); + return false; +#else + return true; +#endif +} + +bool CheckAgainstReference(const char* reference, bool crtSoftwareReference) +{ + bool ok = CheckPairedFamily(1.000000119f, -123.25f, 33554431.0f, 0.0625f, -33554430.0f, 7.75f); + ok &= CheckPairedSpecialCases(); + ok &= CheckHalfwayDoubleRounding(); + ok &= CheckSubnormalHalfwayDoubleRounding(); + + for (const bool flushToZero : {false, true}) { + FlushToZeroScope scope(flushToZero); + char mode[64]; + std::snprintf(mode, sizeof(mode), "%s, %s", reference, flushToZero ? "FTZ/DAZ" : "IEEE"); + ok &= CheckInvalidExceptionParity(mode); + ok &= CheckRuntimeFusedSubtract(mode, crtSoftwareReference); + ok &= CheckEdgeValues(mode); + ok &= CheckRandomized(mode); + } + return ok; +} + +} // namespace + +// Only a build without __FMA__ calls the CRT's fmaf; a v3 build compiles std::fma to vfmadd. +#if defined(_WIN32) && defined(__x86_64__) && !defined(__FMA__) +#define PPC_FMA_TEST_CRT_SOFTWARE_FMAF 1 +extern "C" __declspec(dllimport) int _get_FMA3_enable(void); +extern "C" __declspec(dllimport) int _set_FMA3_enable(int flag); +#endif + +int main() +{ + bool ok = CheckFusedSubtractFollowsNiMode(); +#if defined(PPC_FMA_TEST_CRT_SOFTWARE_FMAF) + // Also force the CRT's software fmaf for a second pass when this CPU has FMA3. + const int initialFma3 = _get_FMA3_enable(); + ok &= CheckAgainstReference(initialFma3 != 0 ? "CRT default (FMA3)" : "CRT default (software fmaf)", + initialFma3 == 0); + if (initialFma3 != 0) { + _set_FMA3_enable(0); + ok &= CheckAgainstReference("CRT software fmaf", true); + _set_FMA3_enable(initialFma3); + } +#else + ok &= CheckAgainstReference("CRT default", false); +#endif return ok ? 0 : 1; }