mirror of
https://github.com/TheBozos/BozoEngine.git
synced 2026-12-01 12:53:50 -10:00
Merge pull request #131 from TheBozos/claude/ps2f-mul-min-normal
ps2f / VU0 SIMD: products that round up to the smallest normals take the exact path
This commit is contained in:
4 files changed
+55
-3
No files matched your search
@@ -212,8 +212,10 @@ namespace bozo::ps2f
|
||||
#ifndef BOZO_PS2F_EXACT_ONLY
|
||||
if (normal(a) && normal(b))
|
||||
{
|
||||
// Not the smallest normals (exponent 1): a product just below 2^-126 rounds up to 2^-126 on the host,
|
||||
// where the PS2 flushes it to zero and flags the underflow. A sum cannot: below 2^-126 it is exact.
|
||||
const uint32_t r = hostBits(hostFloat(a) * hostFloat(b));
|
||||
if (normal(r))
|
||||
if (expOf(r) - 2u < 253u)
|
||||
return r;
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -101,7 +101,7 @@ namespace bozo::vu0
|
||||
// ---- Four fields at once (x86-64) ----
|
||||
// The common cases of an FMAC instruction, all four fields through SSE together; the field-by-field path above gives
|
||||
// the same bits and flags for them:
|
||||
// - a product of two normal numbers that is normal, or with a zero operand (exponent 0: zero or denormal): the
|
||||
// - a product of two normal numbers that is normal above the smallest normals (exponent 2-254), or with a zero operand (exponent 0: zero or denormal): the
|
||||
// signed zero, no flag (ps2f::mul);
|
||||
// - a sum of two normal numbers that is normal, or with a zero operand: the other operand, or for two zeros -0 only
|
||||
// when both are -0 (ps2f::addZeros); a - b is a + (-b);
|
||||
@@ -128,6 +128,9 @@ namespace bozo::vu0
|
||||
inline __m128i zero(__m128i x) { return _mm_cmpeq_epi32(exponent(x), _mm_setzero_si128()); } // or denormal
|
||||
inline __m128i top(__m128i x) { return _mm_cmpeq_epi32(exponent(x), _mm_set1_epi32(0xFF)); }
|
||||
inline __m128i abnormal(__m128i x) { return _mm_or_si128(zero(x), top(x)); }
|
||||
// Exponent 1, the smallest normals: a product just below 2^-126 rounds up to them on the host, where the PS2
|
||||
// flushes it to zero with the underflow flags (ps2f::mul's shortcut leaves them to the exact path too).
|
||||
inline __m128i bottom(__m128i x) { return _mm_cmpeq_epi32(exponent(x), _mm_set1_epi32(1)); }
|
||||
inline __m128i select(__m128i mask, __m128i a, __m128i b) { return _mm_or_si128(_mm_and_si128(mask, a), _mm_andnot_si128(mask, b)); }
|
||||
inline __m128i signBits() { return _mm_set1_epi32(static_cast<int32_t>(0x80000000u)); }
|
||||
// Lane i on when dest enables field i (x = dest bit 3 = lane 0).
|
||||
@@ -141,7 +144,8 @@ namespace bozo::vu0
|
||||
{
|
||||
const __m128i z = _mm_or_si128(zero(s), zero(t));
|
||||
const __m128i p = _mm_castps_si128(_mm_mul_ps(_mm_castsi128_ps(s), _mm_castsi128_ps(t)));
|
||||
bad = _mm_or_si128(bad, _mm_andnot_si128(z, _mm_or_si128(_mm_or_si128(top(s), top(t)), abnormal(p))));
|
||||
bad = _mm_or_si128(bad, _mm_andnot_si128(z, _mm_or_si128(_mm_or_si128(top(s), top(t)),
|
||||
_mm_or_si128(abnormal(p), bottom(p)))));
|
||||
return select(z, _mm_and_si128(_mm_xor_si128(s, t), signBits()), p);
|
||||
}
|
||||
// a + b, likewise.
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
// bozo-runtime-tests: the runtime core (bozo/runtime.h): the guest addresses with memory behind them, and the
|
||||
// function table with its overrides looked up first.
|
||||
#include "bozo/runtime.h"
|
||||
#include "bozo/ops_fpu.h"
|
||||
|
||||
#include <cstdio>
|
||||
#include <memory>
|
||||
@@ -107,6 +108,22 @@ int main()
|
||||
bozo_set_interrupts(rt->memory.ram(), &ctx, rt.get(), false);
|
||||
eq("di", bozo_cop0_read(&ctx, rt.get(), 12), 0);
|
||||
|
||||
// ps2f's host shortcut (normal operands and result) at the bottom of the normals: a product just below 2^-126
|
||||
// rounds up to 2^-126 on the host; the PS2 flushes it to zero with the underflow flags.
|
||||
{
|
||||
const uint32_t cases[][4] = {{0x00800000u, 0x3f7fffffu, 0x00000000u, 0x4008u},
|
||||
{0x00800000u, 0xbf7fffffu, 0x80000000u, 0x4008u},
|
||||
{0x3effffffu, 0x01000000u, 0x00000000u, 0x4008u},
|
||||
{0x3f800000u, 0x00800000u, 0x00800000u, 0u},
|
||||
{0x3f000000u, 0x01000000u, 0x00800000u, 0u}};
|
||||
for (const auto &c : cases)
|
||||
{
|
||||
uint32_t flags = 0;
|
||||
eq("ps2f mul at the smallest normal", bozo::ps2f::mul(c[0], c[1], flags), c[2]);
|
||||
eq("ps2f mul's underflow flags", flags, c[3]);
|
||||
}
|
||||
}
|
||||
|
||||
std::printf("runtime tests: %s\n", g_failures ? "FAILED" : "passed");
|
||||
return g_failures ? 1 : 0;
|
||||
}
|
||||
@@ -116,6 +116,35 @@ int main()
|
||||
fast += v::simd::fmac<v::simd::Op::Madd>(&c, x, acc, s, t, dest) ? 1u : 0u;
|
||||
#endif
|
||||
}
|
||||
// Products at the bottom of the normals: (2 - 2^-23) * 2^(e-127) times 2^(127-e) or a little more, whose exact
|
||||
// value is (1 - 2^-24) * 2^-126 or just above: the PS2 gives zero and the underflow flags below 2^-126, the host
|
||||
// rounds it up to 2^-126. Swapped operands and either sign.
|
||||
for (int n = 0; n < 200000; ++n)
|
||||
{
|
||||
Vec s, t;
|
||||
const Vec acc = vec(rng), d0 = vec(rng);
|
||||
for (int i = 0; i < 4; ++i)
|
||||
{
|
||||
const uint32_t e = 1u + rng() % 126u;
|
||||
const uint32_t a = (rng() & 1u) << 31 | e << 23 | (0x7FFFFFu - rng() % 2u);
|
||||
const uint32_t b = (rng() & 1u) << 31 | (127u - e) << 23 | (rng() % 2u ? 0u : rng() % 4u);
|
||||
s.u[i] = rng() & 1u ? a : b;
|
||||
t.u[i] = s.u[i] == a ? b : a;
|
||||
}
|
||||
const uint32_t dest = 1u + rng() % 15u;
|
||||
auto run = [&](const char *what, auto fastOp, auto exactOp)
|
||||
{
|
||||
R5900Context a{}, b{};
|
||||
Vec got = d0, want = d0;
|
||||
fastOp(&a, got);
|
||||
exactOp(&b, want);
|
||||
compare(what, n, got, want, a, b);
|
||||
};
|
||||
run("mul at the bottom", [&](R5900Context *c, Vec &d) { v::mul(c, d, s, t, dest); },
|
||||
[&](R5900Context *c, Vec &d) { exactFmac(c, d, s, t, dest, v::mulf); });
|
||||
run("madd at the bottom", [&](R5900Context *c, Vec &d) { v::madd(c, d, acc, s, t, dest); },
|
||||
[&](R5900Context *c, Vec &d) { exactMadd(c, d, acc, s, t, dest, false); });
|
||||
}
|
||||
#ifdef BOZO_VU0_SIMD
|
||||
std::printf("vu0 simd: %llu of 2000000 madd cases on the fast path\n", static_cast<unsigned long long>(fast));
|
||||
#else
|
||||
|
||||
Reference in new issue
Block a user