Files
AnyPS5/core/libs/tests/GuestSceV2FloatHelpers.cpp
T

328 lines
16 KiB
C++

#include "prx/libc/libc.hpp"
#include <bit>
#include <cstdint>
#include <cstdlib>
static void Require(bool value) { if (!value) std::abort(); }
struct TestCase { std::uint64_t input; short code; short extendedCode; };
struct FloatTestCase { std::uint32_t input; short code; };
struct IntCase { std::uint64_t input; short scale; short code; std::uint64_t result; short extendedCode; std::uint64_t extendedResult; };
struct FloatIntCase { std::uint32_t input; short scale; short code; std::uint32_t result; };
struct NormCase { std::uint64_t input; short code; std::uint64_t result; };
struct FloatNormCase { std::uint32_t input; short code; std::uint32_t result; };
static const TestCase kTests[] = {
{0x0000000000000000ull, 0, 0},
{0x0000000000000001ull, -2, -2},
{0x3fefffffffffffffull, -1, -1},
{0x3fe0000000000000ull, -1, -1},
{0x3fe0000000000001ull, -1, -1},
{0xc000000000000000ull, -1, -1},
{0x4008000000000000ull, -1, -1},
{0x4007ffffffffffffull, -1, -1},
{0xc024000000000000ull, -1, -1},
{0x4024000000000001ull, -1, -1},
{0x3fb9999999999999ull, -1, -1},
{0x81a56e1fc2f8f359ull, -1, -1},
{0x01a56e1fc2f8f35aull, -1, -1},
{0x7e37e43c8800759bull, -1, -1},
{0x4086280000000000ull, -1, -1},
{0x4086280000000001ull, -1, -1},
{0xc0862e3d70a3d70aull, -1, -1},
{0x4086300000000000ull, -1, -1},
{0x40862fffffffffffull, -1, -1},
{0xc08633c28f5c28f6ull, -1, -1},
{0x408633d70a3d70a4ull, -1, -1},
{0x408633d70a3d70a3ull, -1, -1},
{0xc086380000000000ull, -1, -1},
{0x4086380000000001ull, -1, -1},
{0x40873fffffffffffull, -1, -1},
{0x4087480000000000ull, -1, -1},
{0x4087480000000001ull, -1, -1},
{0xc087500000000000ull, -1, -1},
{0x3e45798ee2308c3aull, -1, -1},
{0x3e45798ee2308c3bull, -1, -1},
{0xbc9cd2b297d889bcull, -1, -1},
{0x4036000000000000ull, -1, -1},
{0x4035ffffffffffffull, -1, -1},
{0xc044000000000000ull, -1, -1},
{0x4044000000000001ull, -1, -1},
{0x4055ffffffffffffull, -1, -1},
{0x4056400000000000ull, -1, -1},
{0x4056400000000001ull, -1, -1},
{0x408ff7ffffffffffull, -1, -1},
{0x4090000000000000ull, -1, -1},
{0x4090000000000001ull, -1, -1},
{0xc090cc0000000000ull, -1, -1},
{0x4202a05f20000000ull, -1, -1},
{0x4202a05f1fffffffull, -1, -1},
{0xd4b249ad2594c37dull, -1, -1},
{0x54b249ad2594c37eull, -1, -1},
{0x0010000000000000ull, -1, -1},
{0x0010000000000001ull, -1, -1},
{0xffefffffffffffffull, -1, -1},
{0x3cb0000000000000ull, -1, -1},
{0x3cafffffffffffffull, -1, -1},
{0x7ff8000000000000ull, 2, 2},
{0x7ff0000000000001ull, 2, 2},
{0x4006000000000000ull, -1, -1},
{0x405edd2f1a9fbe77ull, -1, -1},
{0x800abcdef0123456ull, -2, -2},
};
static const FloatTestCase kFloatTests[] = {
{0x00000000u, 0},
{0x80000000u, 0},
{0xbf800000u, -1},
{0xbf000000u, -1},
{0xc0000000u, -1},
{0xc0400000u, -1},
{0xc1200000u, -1},
{0x3dcccccdu, -1},
{0x7f800000u, 1},
{0x44314000u, -1},
{0x443171ecu, -1},
{0x44318000u, -1},
{0x44319e14u, -1},
{0x44319eb8u, -1},
{0xc4319eb8u, -1},
{0xc431c000u, -1},
{0xc43a0000u, -1},
{0xc43a4000u, -1},
{0xc43a8000u, -1},
{0xb22bcc77u, -1},
{0xa4e69595u, -1},
{0x41b00000u, -1},
{0x42200000u, -1},
{0x42b00000u, -1},
{0x42b20000u, -1},
{0x447fc000u, -1},
{0x44800000u, -1},
{0x44866000u, -1},
{0xc4866000u, -1},
{0xd01502f9u, -1},
{0xa5800000u, -1},
{0xffc00000u, 2},
{0x7fffffffu, 2},
{0xc2b170a4u, -1},
{0xc2b175c3u, -1},
{0x42b2d1ecu, -1},
{0x42b2d70au, -1},
{0x42ce0000u, -1},
{0x42d00000u, -1},
{0x006ce3eeu, -2},
{0x00000001u, -2},
{0x7f7fc99eu, -1},
{0xff7fc99eu, -1},
{0x7fa00000u, 2},
{0x40300000u, -1},
{0x42f6e979u, -1},
{0x00400000u, -2},
{0x4b7fffffu, -1},
};
static const IntCase kInts[] = {
{0x0000000000000000ull, 0, 0, 0x0000000000000000ull, 0, 0x0000000000000000ull},
{0x8000000000000000ull, 1100, 0, 0x8000000000000000ull, 0, 0x8000000000000000ull},
{0x3ff0000000000000ull, 3, 0, 0x3ff0000000000000ull, 0, 0x3ff0000000000000ull},
{0x3fefffffffffffffull, 0, -1, 0x0000000000000000ull, -1, 0x0000000000000000ull},
{0x3ff0000000000001ull, -32000, -1, 0x0000000000000000ull, -1, 0x0000000000000000ull},
{0xbfe0000000000000ull, 52, 0, 0xbfe0000000000000ull, 0, 0xbfe0000000000000ull},
{0x3fe0000000000001ull, 1, -1, 0x3fe0000000000000ull, -1, 0x3fe0000000000000ull},
{0x4000000000000000ull, 32000, 0, 0x4000000000000000ull, 0, 0x4000000000000000ull},
{0x3fffffffffffffffull, 53, 0, 0x3fffffffffffffffull, 0, 0x3fffffffffffffffull},
{0x4008000000000000ull, -1, -1, 0x4000000000000000ull, -1, 0x4000000000000000ull},
{0x4007ffffffffffffull, 0, -1, 0x4000000000000000ull, -1, 0x4000000000000000ull},
{0x4008000000000001ull, 1100, 0, 0x4008000000000001ull, 0, 0x4008000000000001ull},
{0xc024000000000000ull, 3, 0, 0xc024000000000000ull, 0, 0xc024000000000000ull},
{0x4024000000000001ull, 1, -1, 0x4024000000000000ull, -1, 0x4024000000000000ull},
{0x3fb999999999999aull, -32000, -1, 0x0000000000000000ull, -1, 0x0000000000000000ull},
{0x3fb9999999999999ull, 52, -1, 0x3fb9999999999990ull, -1, 0x3fb9999999999990ull},
{0x01a56e1fc2f8f359ull, -1, -1, 0x0000000000000000ull, -1, 0x0000000000000000ull},
{0x81a56e1fc2f8f359ull, 32000, 0, 0x81a56e1fc2f8f359ull, 0, 0x81a56e1fc2f8f359ull},
{0x01a56e1fc2f8f35aull, 53, -1, 0x0000000000000000ull, -1, 0x0000000000000000ull},
{0xfe37e43c8800759cull, 3, 0, 0xfe37e43c8800759cull, 0, 0xfe37e43c8800759cull},
{0x7e37e43c8800759dull, 0, 0, 0x7e37e43c8800759dull, 0, 0x7e37e43c8800759dull},
{0x4086280000000000ull, 1100, 0, 0x4086280000000000ull, 0, 0x4086280000000000ull},
{0x408627ffffffffffull, 52, 0, 0x408627ffffffffffull, 0, 0x408627ffffffffffull},
{0x40862e3d70a3d70aull, 1, -1, 0x40862c0000000000ull, -1, 0x40862c0000000000ull},
{0xc0862e3d70a3d70aull, -32000, -1, 0x8000000000000000ull, -1, 0x8000000000000000ull},
{0x40862e3d70a3d70bull, 53, 0, 0x40862e3d70a3d70bull, 0, 0x40862e3d70a3d70bull},
{0xc086300000000000ull, -1, 0, 0xc086300000000000ull, 0, 0xc086300000000000ull},
{0x40862fffffffffffull, 32000, 0, 0x40862fffffffffffull, 0, 0x40862fffffffffffull},
{0x408633c28f5c28f6ull, 1100, 0, 0x408633c28f5c28f6ull, 0, 0x408633c28f5c28f6ull},
{0x408633c28f5c28f5ull, 3, -1, 0x4086330000000000ull, -1, 0x4086330000000000ull},
{0x408633d70a3d70a4ull, 0, -1, 0x4086300000000000ull, -1, 0x4086300000000000ull},
{0xc08633d70a3d70a4ull, 1100, 0, 0xc08633d70a3d70a4ull, 0, 0xc08633d70a3d70a4ull},
{0x408633d70a3d70a5ull, 52, 0, 0x408633d70a3d70a5ull, 0, 0x408633d70a3d70a5ull},
{0xc086380000000000ull, 1, 0, 0xc086380000000000ull, 0, 0xc086380000000000ull},
{0x408637ffffffffffull, -32000, -1, 0x0000000000000000ull, -1, 0x0000000000000000ull},
{0x4087400000000000ull, 53, 0, 0x4087400000000000ull, 0, 0x4087400000000000ull},
{0x40873fffffffffffull, -1, -1, 0x4087300000000000ull, -1, 0x4087300000000000ull},
{0x4087400000000001ull, 32000, 0, 0x4087400000000001ull, 0, 0x4087400000000001ull},
{0xc087480000000000ull, 1100, 0, 0xc087480000000000ull, 0, 0xc087480000000000ull},
{0x4087480000000001ull, 3, -1, 0x4087480000000000ull, -1, 0x4087480000000000ull},
{0xc087500000000000ull, 0, 0, 0xc087500000000000ull, 0, 0xc087500000000000ull},
{0x40874fffffffffffull, -32000, -1, 0x0000000000000000ull, -1, 0x0000000000000000ull},
{0x3e45798ee2308c3aull, 52, -1, 0x3e45798ee0000000ull, -1, 0x3e45798ee0000000ull},
{0x3e45798ee2308c39ull, 1, -1, 0x0000000000000000ull, -1, 0x0000000000000000ull},
{0x3e45798ee2308c3bull, 32000, 0, 0x3e45798ee2308c3bull, 0, 0x3e45798ee2308c3bull},
{0xbc9cd2b297d889bcull, 53, -1, 0x8000000000000000ull, -1, 0x8000000000000000ull},
{0x3c9cd2b297d889bdull, -1, -1, 0x0000000000000000ull, -1, 0x0000000000000000ull},
{0xc036000000000000ull, 0, 0, 0xc036000000000000ull, 0, 0xc036000000000000ull},
{0x4035ffffffffffffull, 1100, 0, 0x4035ffffffffffffull, 0, 0x4035ffffffffffffull},
{0x4044000000000000ull, 3, 0, 0x4044000000000000ull, 0, 0x4044000000000000ull},
{0x4043ffffffffffffull, 1, -1, 0x4043c00000000000ull, -1, 0x4043c00000000000ull},
{0x4044000000000001ull, -32000, -1, 0x0000000000000000ull, -1, 0x0000000000000000ull},
{0xc056000000000000ull, 52, 0, 0xc056000000000000ull, 0, 0xc056000000000000ull},
{0x4056000000000001ull, -1, -1, 0x4056000000000000ull, -1, 0x4056000000000000ull},
{0x4056400000000000ull, 32000, 0, 0x4056400000000000ull, 0, 0x4056400000000000ull},
{0x40563fffffffffffull, 53, 0, 0x40563fffffffffffull, 0, 0x40563fffffffffffull},
{0x408ff80000000000ull, 3, 0, 0x408ff80000000000ull, 0, 0x408ff80000000000ull},
{0x408ff7ffffffffffull, 0, -1, 0x408ff00000000000ull, -1, 0x408ff00000000000ull},
{0x408ff80000000001ull, 1100, 0, 0x408ff80000000001ull, 0, 0x408ff80000000001ull},
{0xc090000000000000ull, 3, 0, 0xc090000000000000ull, 0, 0xc090000000000000ull},
{0x4090000000000001ull, 1, -1, 0x4090000000000000ull, -1, 0x4090000000000000ull},
{0x4090cc0000000000ull, -32000, -1, 0x0000000000000000ull, -1, 0x0000000000000000ull},
{0x4090cbffffffffffull, 52, 0, 0x4090cbffffffffffull, 0, 0x4090cbffffffffffull},
{0x4202a05f20000000ull, -1, 0, 0x4202a05f20000000ull, 0, 0x4202a05f20000000ull},
{0xc202a05f20000000ull, 32000, 0, 0xc202a05f20000000ull, 0, 0xc202a05f20000000ull},
{0x4202a05f20000001ull, 53, 0, 0x4202a05f20000001ull, 0, 0x4202a05f20000001ull},
{0xd4b249ad2594c37dull, 3, 0, 0xd4b249ad2594c37dull, 0, 0xd4b249ad2594c37dull},
{0x54b249ad2594c37eull, 0, 0, 0x54b249ad2594c37eull, 0, 0x54b249ad2594c37eull},
{0x8000000000000001ull, 1100, 0, 0x8000000000000001ull, 0, 0x8000000000000001ull},
{0x0010000000000000ull, 52, -1, 0x0000000000000000ull, -1, 0x0000000000000000ull},
{0x000fffffffffffffull, 1, -1, 0x0000000000000000ull, -1, 0x0000000000000000ull},
{0x0010000000000001ull, -32000, 0, 0x0010000000000001ull, 0, 0x0010000000000001ull},
{0xffefffffffffffffull, 53, 0, 0xffefffffffffffffull, 0, 0xffefffffffffffffull},
{0x7ff0000000000000ull, -1, 1, 0x7ff0000000000000ull, 1, 0x7ff0000000000000ull},
{0x3cb0000000000000ull, 32000, 0, 0x3cb0000000000000ull, 0, 0x3cb0000000000000ull},
{0x3cafffffffffffffull, 1100, 0, 0x3cafffffffffffffull, 0, 0x3cafffffffffffffull},
{0xfff0000000000000ull, 3, 1, 0xfff0000000000000ull, 1, 0xfff0000000000000ull},
{0xfff8000000000000ull, 0, 2, 0xfff8000000000000ull, 2, 0xfff8000000000000ull},
{0x7ff0000000000001ull, -32000, 2, 0x7ff0000000000001ull, 2, 0x7ff0000000000001ull},
{0x7fffffffffffffffull, 52, 2, 0x7fffffffffffffffull, 2, 0x7fffffffffffffffull},
{0xc006000000000000ull, 1, -1, 0xc004000000000000ull, -1, 0xc004000000000000ull},
{0x405edd2f1a9fbe77ull, 32000, 0, 0x405edd2f1a9fbe77ull, 0, 0x405edd2f1a9fbe77ull},
{0x0008000000000000ull, 53, -1, 0x0000000000000000ull, -1, 0x0000000000000000ull},
{0x433fffffffffffffull, -1, -1, 0x433ffffffffffffeull, -1, 0x433ffffffffffffeull},
};
static const FloatIntCase kFloatInts[] = {
{0x00000000u, 0, 0, 0x00000000u},
{0x80000000u, 23, 0, 0x80000000u},
{0x3f800000u, 32700, 0, 0x3f800000u},
{0x3f000000u, 23, 0, 0x3f000000u},
{0xbf000000u, 32700, 0, 0xbf000000u},
{0xc0000000u, 3, 0, 0xc0000000u},
{0x40400000u, 32700, 0, 0x40400000u},
{0x41200000u, 3, 0, 0x41200000u},
{0xc1200000u, -32700, -1, 0x80000000u},
{0xbdcccccdu, 3, -1, 0x80000000u},
{0x7f800000u, -32700, 1, 0x7f800000u},
{0x44314000u, -1, -1, 0x44310000u},
{0xc4314000u, -32700, -1, 0x80000000u},
{0xc43171ecu, -1, -1, 0xc4310000u},
{0x44318000u, 200, 0, 0x44318000u},
{0x44319e14u, -1, -1, 0x44318000u},
{0xc4319e14u, 200, 0, 0xc4319e14u},
{0xc4319eb8u, 1, -1, 0xc4318000u},
{0x4431c000u, 200, 0, 0x4431c000u},
{0x443a0000u, 1, 0, 0x443a0000u},
{0xc43a0000u, 200, 0, 0xc43a0000u},
{0xc43a4000u, 1, 0, 0xc43a4000u},
{0x443a8000u, 24, 0, 0x443a8000u},
{0x322bcc77u, 1, -1, 0x00000000u},
{0xb22bcc77u, 24, -1, 0x80000000u},
{0xa4e69595u, 0, -1, 0x80000000u},
{0x41b00000u, 24, 0, 0x41b00000u},
{0x42200000u, 0, 0, 0x42200000u},
{0xc2200000u, 23, 0, 0xc2200000u},
{0xc2b00000u, 0, 0, 0xc2b00000u},
{0x42b20000u, 23, 0, 0x42b20000u},
{0xc2b20000u, 32700, 0, 0xc2b20000u},
{0xc47fc000u, 23, 0, 0xc47fc000u},
{0x44800000u, 32700, 0, 0x44800000u},
{0x44866000u, 3, 0, 0x44866000u},
{0xc4866000u, 32700, 0, 0xc4866000u},
{0xd01502f9u, 3, 0, 0xd01502f9u},
{0x25800000u, -32700, 0, 0x25800000u},
{0x7fc00000u, 3, 2, 0x7fc00000u},
{0xffc00000u, -32700, 2, 0xffc00000u},
{0x7fffffffu, 3, 2, 0x7fffffffu},
{0x42b170a4u, -32700, -1, 0x00000000u},
{0x42b175c3u, -1, -1, 0x42b00000u},
{0xc2b175c3u, -32700, -1, 0x80000000u},
{0xc2b2d1ecu, -1, -1, 0xc2b00000u},
{0x42b2d70au, 200, 0, 0x42b2d70au},
{0x42ce0000u, -1, -1, 0x42cc0000u},
{0xc2ce0000u, 200, 0, 0xc2ce0000u},
{0xc2d00000u, 1, 0, 0xc2d00000u},
{0x006ce3eeu, 200, 0, 0x006ce3eeu},
{0x00000001u, 1, -1, 0x00000000u},
{0x80000001u, 24, -1, 0x80000000u},
{0xff7fc99eu, 1, 0, 0xff7fc99eu},
{0x7f800001u, 24, 2, 0x7f800001u},
{0x007fffffu, 0, -1, 0x00000000u},
{0x40300000u, 24, 0, 0x40300000u},
{0x42f6e979u, 0, -1, 0x42f60000u},
{0x3e800000u, 23, 0, 0x3e800000u},
{0x803456ffu, 0, -1, 0x80000000u},
{0x4b7fffffu, 23, 0, 0x4b7fffffu},
};
static const NormCase kNorms[] = {
{0x0000000000000001ull, -51, 0x0000000000000000ull},
{0x0000000000000003ull, -50, 0x0008000000000000ull},
{0x0008000000000000ull, 0, 0x0000000000000000ull},
{0x000fffffffffffffull, 0, 0x000ffffffffffffeull},
{0x8000000000000001ull, -51, 0x8000000000000000ull},
{0x800abcdef0123456ull, 0, 0x800579bde02468acull},
{0x0000123456789abcull, -7, 0x00023456789abc00ull},
{0x0010000000000000ull, 1, 0x0000000000000000ull},
{0x3ff0000000000000ull, 1, 0x0000000000000000ull},
};
static const FloatNormCase kFloatNorms[] = {
{0x00000001u, -22, 0x00000000u},
{0x00000003u, -21, 0x00400000u},
{0x00400000u, 0, 0x00000000u},
{0x007fffffu, 0, 0x007ffffeu},
{0x80000001u, -22, 0x80000000u},
{0x803456ffu, -1, 0x80515bfcu},
{0x00012345u, -6, 0x0011a280u},
{0x00800000u, 1, 0x00000000u},
{0x3f800000u, 1, 0x00000000u},
};
int main() {
for (const auto& c : kTests) {
double value = std::bit_cast<double>(c.input);
Require(_ZNSt8__sce_v26_DtestEPd_nid_postfix(&value) == c.code && std::bit_cast<std::uint64_t>(value) == c.input);
Require(_ZNSt8__sce_v27_LDtestEPe_nid_postfix(&value) == c.extendedCode && std::bit_cast<std::uint64_t>(value) == c.input);
}
for (const auto& c : kFloatTests) {
float value = std::bit_cast<float>(c.input);
Require(_ZNSt8__sce_v27_FDtestEPf_nid_postfix(&value) == c.code && std::bit_cast<std::uint32_t>(value) == c.input);
}
for (const auto& c : kInts) {
double value = std::bit_cast<double>(c.input);
Require(_ZNSt8__sce_v25_DintEPds_nid_postfix(&value, c.scale) == c.code && std::bit_cast<std::uint64_t>(value) == c.result);
value = std::bit_cast<double>(c.input);
Require(_ZNSt8__sce_v26_LDintEPes_nid_postfix(&value, c.scale) == c.extendedCode && std::bit_cast<std::uint64_t>(value) == c.extendedResult);
}
for (const auto& c : kFloatInts) {
float value = std::bit_cast<float>(c.input);
Require(_ZNSt8__sce_v26_FDintEPfs_nid_postfix(&value, c.scale) == c.code && std::bit_cast<std::uint32_t>(value) == c.result);
}
for (const auto& c : kNorms) {
std::uint64_t words = c.input;
Require(_ZNSt8__sce_v26_DnormEPNS_5_DvalE_nid_postfix(reinterpret_cast<std::uint16_t*>(&words)) == c.code && words == c.result);
}
for (const auto& c : kFloatNorms) {
std::uint32_t words = c.input;
Require(_ZNSt8__sce_v27_FDnormEPNS_5_FvalE_nid_postfix(reinterpret_cast<std::uint16_t*>(&words)) == c.code && words == c.result);
}
}