Files
neoLegacy-mac/tests/test_reconstructed_rrmath.py

151 lines
6.0 KiB
Python

import ctypes
import math
from pathlib import Path
import struct
import subprocess
import tempfile
import unittest
ROOT = Path(__file__).resolve().parents[1]
SOURCE = ROOT / "iggy-decompiled/src/reconstructed/rrMath.cpp"
RECONSTRUCTED = ROOT / "iggy-decompiled/src/reconstructed"
INCLUDE = ROOT / "iggy-decompiled/include"
HARNESS = r"""
#include "rrMath.h"
extern "C" U32 recovered_rrF32AsInt(F32 value) {
return rrF32AsInt(value);
}
extern "C" U32 recovered_rrGetBitLevel_V(U32 value) {
return rrGetBitLevel_V(value);
}
"""
class ReconstructedRrMathTests(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.temporary_directory = tempfile.TemporaryDirectory()
temporary_path = Path(cls.temporary_directory.name)
harness_path = temporary_path / "harness.cpp"
harness_path.write_text(HARNESS)
library_path = temporary_path / "librrmath.dylib"
result = subprocess.run(
[
"clang++",
"-dynamiclib",
"-std=c++17",
"-Wall",
"-Wextra",
"-Werror",
"-I",
str(INCLUDE),
"-I",
str(RECONSTRUCTED),
str(SOURCE),
str(harness_path),
"-o",
str(library_path),
],
capture_output=True,
text=True,
)
if result.returncode != 0:
raise AssertionError(result.stdout + result.stderr)
cls.library = ctypes.CDLL(str(library_path))
cls.configure_signatures()
@classmethod
def tearDownClass(cls):
cls.temporary_directory.cleanup()
@classmethod
def configure_signatures(cls):
cls.library.recovered_rrF32AsInt.argtypes = [ctypes.c_float]
cls.library.recovered_rrF32AsInt.restype = ctypes.c_uint32
cls.library.recovered_rrGetBitLevel_V.argtypes = [ctypes.c_uint32]
cls.library.recovered_rrGetBitLevel_V.restype = ctypes.c_uint32
for name in ("rrIlog2ceil", "rrIlog2floor", "rrIlog2round"):
function = getattr(cls.library, name)
function.argtypes = [ctypes.c_uint32]
function.restype = ctypes.c_uint32
for name in ("rrIlog2ceilf", "rrIlog2floorf", "rrIlog2roundf"):
function = getattr(cls.library, name)
function.argtypes = [ctypes.c_float]
function.restype = ctypes.c_uint32
for name in ("rrNextPow2", "rrPrevPow2"):
function = getattr(cls.library, name)
function.argtypes = [ctypes.c_uint32]
function.restype = ctypes.c_uint32
cls.library.rrRecipSqrt.argtypes = [ctypes.c_float]
cls.library.rrRecipSqrt.restype = ctypes.c_float
cls.library.rrlog2.argtypes = [ctypes.c_double]
cls.library.rrlog2.restype = ctypes.c_double
cls.library.rrlog2f_approx.argtypes = [ctypes.c_float]
cls.library.rrlog2f_approx.restype = ctypes.c_float
for name, argument in (
("rr_isnan", ctypes.c_double),
("rr_isnan_f", ctypes.c_float),
("rr_isnaninf", ctypes.c_double),
("rr_isnaninf_f", ctypes.c_float),
):
function = getattr(cls.library, name)
function.argtypes = [argument]
function.restype = ctypes.c_int32
def test_float_bit_cast_matches_ieee754(self):
for value in (0.0, -0.0, 1.0, -2.5, float("inf")):
expected = struct.unpack("<I", struct.pack("<f", value))[0]
self.assertEqual(self.library.recovered_rrF32AsInt(value), expected)
def test_integer_bit_levels_and_powers(self):
cases = ((0, 0), (1, 1), (2, 2), (3, 2), (0x80000000, 32))
for value, expected in cases:
self.assertEqual(self.library.recovered_rrGetBitLevel_V(value), expected)
for value in (1, 2, 3, 4, 5, 255, 256, 257, 0x40000000):
self.assertEqual(self.library.rrIlog2ceil(value), (value - 1).bit_length())
self.assertEqual(self.library.rrNextPow2(value), 1 << (value - 1).bit_length())
self.assertEqual(self.library.rrPrevPow2(value), 1 << (value.bit_length() - 1))
self.assertEqual(self.library.rrPrevPow2(0), 0)
def test_float_exponent_helpers_match_recovered_bit_formulas(self):
for value in (0.125, 0.5, 1.0, 1.25, 2.0, 3.75, 16.0, 1024.0):
bits = struct.unpack("<I", struct.pack("<f", value))[0]
self.assertEqual(
self.library.rrIlog2floorf(value), ((bits >> 23) - 127) & 0xFFFFFFFF
)
self.assertEqual(
self.library.rrIlog2ceilf(value),
(((bits + 0x7FFFFF) >> 23) - 127) & 0xFFFFFFFF,
)
self.assertEqual(
self.library.rrIlog2roundf(value),
(((bits + 0x257D86) >> 23) - 127) & 0xFFFFFFFF,
)
def test_integer_log_helpers_match_float_conversion(self):
for value in (1, 2, 3, 4, 7, 255, 256, 1000000):
bits = struct.unpack("<I", struct.pack("<f", float(value)))[0]
self.assertEqual(self.library.rrIlog2floor(value), (bits >> 23) - 127)
self.assertEqual(self.library.rrIlog2round(value), ((bits + 0x257D86) >> 23) - 127)
def test_nan_infinity_and_log_helpers(self):
self.assertEqual(self.library.rr_isnan(float("nan")), 1)
self.assertEqual(self.library.rr_isnan_f(float("nan")), 1)
self.assertEqual(self.library.rr_isnan(float("inf")), 0)
self.assertEqual(self.library.rr_isnan_f(float("inf")), 0)
self.assertEqual(self.library.rr_isnaninf(float("inf")), 1)
self.assertEqual(self.library.rr_isnaninf_f(float("inf")), 1)
self.assertEqual(self.library.rr_isnaninf(1.0), 0)
self.assertEqual(self.library.rr_isnaninf_f(1.0), 0)
self.assertAlmostEqual(self.library.rrRecipSqrt(9.0), 1.0 / 3.0, places=6)
self.assertAlmostEqual(self.library.rrlog2(8.0), 3.0, places=12)
self.assertLess(abs(self.library.rrlog2f_approx(8.0) - 3.0), 0.02)
if __name__ == "__main__":
unittest.main()