// Exact, independent-original comparisons: record values, overlapping memory, the complete CPU ABI and untouched VU state. #include "host_control.h" #include "tmb_port.h" #include "tmb_native_math.h" #include "bozo/libs/libraries.h" #include #include #include #include #include namespace tmb::math { void install(Registry &); } void testDirectionCallers(); namespace { uint32_t randomState = 0x1928d3a7u; uint32_t randomBits() { randomState ^= randomState << 13; randomState ^= randomState >> 17; randomState ^= randomState << 5; return randomState; } struct NativeHost : bozo::port::Host { bozo::libs::Libraries libs; bozo::libs::Libraries &libraries() override { return libs; } void frameEnd(Runtime *, R5900Context *) override { throw "unexpected frame operation"; } bool callLibrary(Runtime *, R5900Context *, uint32_t) override { throw "unexpected library operation"; } bool nativeGraphics() const override { return true; } }; struct Helper { uint32_t address; RecompiledFunction original; const char *name; bool direction = false; }; constexpr Helper helpers[] = { {0x1dcc08, vu0MulMatrix3x3__FPA3_fT0T0_0x1dcc08, "basis"}, {0x1dcc68, vu0MulMatrixTP3x3__FPA3_fT0T0_0x1dcc68, "transposed basis"}, {0x1dcd50, vu0MulMatrix4x4__FPA3_fT0T0_0x1dcd50, "full"}, {0x1da298, mathfMulTransVec__FPA3_A3_fP8_fvectorT1_0x1da298, "direction", true}, }; constexpr uint32_t special[] = {0, 0x80000000, 1, 0x80000001, 0x7fffff, 0x807fffff, 0x800000, 0x80800000, 0x800001, 0x3f7fffff, 0x3f800000, 0x3f800001, 0xbf800000, 0x7f7fffff, 0xff7fffff, 0x7f800000, 0xff800000, 0x7fffffff, 0xffffffff, 0x3f000000}; } int main() { auto rt = std::make_unique(); NativeHost host; rt->port = &host; tmb::Registry registry; tmb::math::install(registry); constexpr size_t arenaBytes = 512; constexpr uint32_t arenaBase = 0x10000; std::array input{}, expected{}; auto wholeRam = std::make_unique(bozo::kFlatBytes); uint64_t cases = 0, fullMemoryCases = 0; const unsigned savedControl = _mm_getcsr(); for (unsigned mode = 0; mode < 4; ++mode) for (const Helper &helper : helpers) for (uint32_t n = 0; n < 65536; ++n) { // Finite game-like values, special/underflow/overflow cases, and arbitrary 32-bit input records. for (size_t i = 0; i < input.size(); i += 4) { uint32_t value = randomBits(); if (n < 16384) value = (value & 0x807fffffu) | ((110u + (value % 30u)) << 23); else if (n < 32768) value = special[value % std::size(special)]; std::memcpy(input.data() + i, &value, 4); } const uint32_t physicalBase = n % 7u == 0 ? bozo::kScratchpadBase : arenaBase; const int64_t offset = bozo::GuestMemory::flatOffset(physicalBase, arenaBytes); uint8_t *arena = rt->memory.flat() + offset; std::memcpy(arena, input.data(), input.size()); R5900Context initial; for (unsigned i = 1; i < 32; ++i) for (unsigned lane = 0; lane < 4; ++lane) initial.r[i].uw[lane] = randomBits(); for (auto &v : initial.vu0_vf) for (auto &bits : v.u) bits = randomBits(); initial.vu0_vf[0] = Vu0Vector{}; initial.vu0_vf[0].u[3] = 0x3f800000u; initial.vu0_status = randomBits(); initial.vu0_mac = randomBits(); initial.vu0_acc.u[0] = randomBits(); initial.vi[5] = randomBits(); const uint32_t first = physicalBase + 128u; const uint32_t second = physicalBase + (n % 11u == 0 ? 144u : 256u); const uint32_t output = physicalBase + 64u + (n % 21u) * 16u; const uint32_t alias = physicalBase == arenaBase ? (n % 3u == 0 ? 0x80000000u : n % 3u == 1 ? 0x20000000u : 0u) : 0u; setRegU32(&initial, 4, (output | alias) + (n & 15u)); setRegU32(&initial, 5, (first | alias) + ((n >> 4u) & 15u)); setRegU32(&initial, 6, (second | alias) + ((n >> 8u) & 15u)); if (helper.direction) { setRegU32(&initial, 4, (second | alias) + ((n >> 8u) & 15u)); setRegU32(&initial, 6, (output | alias) + (n & 15u)); } setRegU32(&initial, 31, helper.direction ? tmb::math::directionReturns[n % tmb::math::directionReturns.size()] : 0x0bad0000u); initial.pc = helper.address; R5900Context original = initial, native = initial; // Deliberately vary caller rounding and denormal settings. Native code must restore the whole word. const unsigned control = ((savedControl | 0x1f80u) & ~0xe040u) | (mode << 13u) | (n & 1u ? 0x8040u : 0u); _mm_setcsr(tmb::test::referenceControl(control)); helper.original(rt->memory.flat(), &original, rt.get()); if (helper.direction) // direction outputs are XYZ; the native record contract retains destination W std::memcpy(arena + (output - physicalBase) + 12, input.data() + (output - physicalBase) + 12, 4); std::memcpy(expected.data(), arena, expected.size()); if (n < 32) { std::memcpy(wholeRam.get(), rt->memory.flat(), bozo::kFlatBytes); ++fullMemoryCases; } std::memcpy(arena, input.data(), input.size()); _mm_setcsr(control); registry.find(helper.address)(rt->memory.flat(), &native, rt.get()); const unsigned after = _mm_getcsr(); _mm_setcsr(savedControl); const size_t vuAt = offsetof(R5900Context, vu0_vf), vuBytes = offsetof(R5900Context, cycles) - vuAt; const bool untouched = std::memcmp(reinterpret_cast(&initial) + vuAt, reinterpret_cast(&native) + vuAt, vuBytes) == 0; std::memset(reinterpret_cast(&original) + vuAt, 0, vuBytes); std::memset(reinterpret_cast(&native) + vuAt, 0, vuBytes); if (std::memcmp(arena, expected.data(), expected.size()) || std::memcmp(&original, &native, sizeof(native)) || !untouched || !tmb::test::sameControl(after, control) || (n < 32 && std::memcmp(rt->memory.flat(), wholeRam.get(), bozo::kFlatBytes))) { std::fprintf(stderr, "FAIL %s case %u host mode %u: data %d, CPU %d, untouched VU %d, host control %d\n", helper.name, n, mode, std::memcmp(arena, expected.data(), expected.size()), std::memcmp(&original, &native, sizeof(native)), untouched, tmb::test::sameControl(after, control)); for (size_t i = 0; i < arenaBytes; i += 4) if (std::memcmp(arena + i, expected.data() + i, 4)) { uint32_t got, want; std::memcpy(&got, arena + i, 4); std::memcpy(&want, expected.data() + i, 4); std::fprintf(stderr, "first differing record word +%zu: %#x != %#x\n", i, got, want); break; } return 1; } ++cases; } // Other helper callers keep their original complete ABI. for (unsigned n = 0; n < 2048; ++n) { uint8_t *arena = rt->memory.ram() + arenaBase; for (size_t i = 0; i < input.size(); i += 4) { const uint32_t bits = randomBits(); std::memcpy(input.data() + i, &bits, 4); } std::memcpy(arena, input.data(), input.size()); R5900Context initial; for (auto &v : initial.vu0_vf) for (auto &bits : v.u) bits = randomBits(); initial.vu0_vf[0] = Vu0Vector{}; initial.vu0_vf[0].u[3] = 0x3f800000; setRegU32(&initial, 4, arenaBase + 128); setRegU32(&initial, 5, arenaBase + 256); setRegU32(&initial, 6, arenaBase + 64); setRegU32(&initial, 31, 0xbad0000); R5900Context original = initial, native = initial; mathfMulTransVec__FPA3_A3_fP8_fvectorT1_0x1da298(rt->memory.flat(), &original, rt.get()); std::memcpy(expected.data(), arena, expected.size()); std::memcpy(arena, input.data(), input.size()); registry.find(0x1da298)(rt->memory.flat(), &native, rt.get()); if (std::memcmp(arena, expected.data(), expected.size()) || std::memcmp(&original, &native, sizeof(native))) return 2; } _mm_setcsr(savedControl); testDirectionCallers(); std::printf("native math tests: %llu original/native cases exact (direction XYZ/preserved-W contract), %llu whole-memory cases; 4 host modes; VU state untouched; 2048 other-caller ABI cases exact\n", static_cast(cases), static_cast(fullMemoryCases)); return 0; }