upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/tests/test_f64_diff.py
diff options
context:
space:
mode:
Diffstat (limited to 'tests/test_f64_diff.py')
-rw-r--r--tests/test_f64_diff.py159
1 files changed, 159 insertions, 0 deletions
diff --git a/tests/test_f64_diff.py b/tests/test_f64_diff.py
new file mode 100644
index 000000000..f4dc3f5cd
--- /dev/null
+++ b/tests/test_f64_diff.py
@@ -0,0 +1,159 @@
1"""CLI and reference checks for tools/f64_diff.py; standard library only."""
2import contextlib
3import io
4import os
5import random
6import subprocess
7import sys
8import tempfile
9import unittest
10from unittest import mock
11from fractions import Fraction
12
13ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
14TOOL = os.path.join(ROOT, "tools", "f64_diff.py")
15sys.path.insert(0, os.path.join(ROOT, "tools"))
16import f64_diff as fd
17
18
19class F64DiffTests(unittest.TestCase):
20 def command(self, *args):
21 return subprocess.run([sys.executable, "-I", TOOL, *args], cwd=ROOT,
22 capture_output=True, text=True, timeout=60)
23
24 def test_cli_self_test(self):
25 self.assertEqual(self.command("--self-test").returncode, 0)
26
27 def test_lists_47_supported_forms_and_exclusions(self):
28 result = self.command("--list-ops")
29 listed = [line for line in result.stdout.splitlines() if line.startswith("v_")]
30 self.assertEqual(result.returncode, 0)
31 self.assertEqual(len(listed), 47)
32 self.assertIn("v_div_scale_f64", result.stdout)
33
34 def test_exit_2_for_invalid_cli(self):
35 self.assertEqual(self.command("--ops", "v_not_real_f64").returncode, 2)
36 self.assertEqual(self.command("--programs", "-1").returncode, 2)
37
38 def test_reference_rounds_all_four_binary64_modes(self):
39 half_ulp = Fraction(1, 1 << 53)
40 self.assertEqual(fd.round_fraction(Fraction(1) + half_ulp, 0), 0x3FF0000000000000)
41 self.assertEqual(fd.round_fraction(Fraction(1) + half_ulp, 1), 0x3FF0000000000001)
42 self.assertEqual(fd.round_fraction(Fraction(1) + half_ulp, 2), 0x3FF0000000000000)
43 self.assertEqual(fd.round_fraction(Fraction(1) + half_ulp, 3), 0x3FF0000000000000)
44 self.assertEqual(fd.round_fraction(-(Fraction(1) + half_ulp), 1), 0xBFF0000000000000)
45 self.assertEqual(fd.round_fraction(-(Fraction(1) + half_ulp), 2), 0xBFF0000000000001)
46
47 def test_nan_tolerance_preserves_integer_low_word_after_partial_write(self):
48 expected = [0, 0x7FF80000, 0, 0]
49 actual = [0, 0xFFFFFFFF, 0, 0]
50 self.assertTrue(fd.equivalent(expected, actual, ("int", "f64")))
51 self.assertFalse(fd.equivalent([1, *expected[1:]], actual, ("int", "f64")))
52 self.assertFalse(fd.equivalent(expected, actual, ("int", "int")))
53
54 def test_exit_77_when_requested_oracle_is_unavailable(self):
55 self.assertEqual(self.command("--oracle", "hw").returncode, 77)
56 self.assertEqual(self.command("--oracle", "gpu", "--exe", "missing.exe").returncode, 77)
57 self.assertEqual(self.command("--oracle", "gpu", "--exe", "missing.exe", "--ops", "not_f64").returncode, 2)
58
59 def test_skips_are_counted_with_reason(self):
60 result = self.command("--ops", "v_cvt_f32_f64", "--programs", "0")
61 self.assertEqual(result.returncode, 0)
62 self.assertIn("1 total", result.stdout)
63 self.assertIn("OpFConvert", result.stdout)
64
65 def test_sqrt_preserves_negative_zero(self):
66 self.assertEqual(fd.ref_program(["v_sqrt_f64 v[8:9], -0.0"]), [0, 0x80000000, 0, 0])
67
68 def test_rsq_subnormal_is_an_explicit_skip(self):
69 lines = ["v_mov_b32 v12, 0x00000001", "v_mov_b32 v13, 0x00000000",
70 "v_rsq_f64 v[8:9], v[12:13]"]
71 with tempfile.TemporaryDirectory() as tmp:
72 status, reason, _, _ = fd.run_one(lines, "ps_f64_subnormal", tmp)
73 self.assertEqual(status, "skip")
74 self.assertIn("subnormal", reason)
75 self.assertIn("FTZ", reason)
76
77 def test_gpu_rsq_precision_is_counted_as_unjudged(self):
78 with tempfile.TemporaryDirectory() as tmp:
79 status, reason, _, actual = fd.run_one(["v_rsq_f64 v[8:9], 3.25"],
80 "ps_f64_gpu_rsq_precision", tmp, "gpu", {"exe": "unused"})
81 self.assertEqual(status, "skip")
82 self.assertIn("implementation-defined", reason)
83 self.assertIsNone(actual)
84
85 def test_div_fixup_nan_s0_is_skipped_by_default_and_runs_with_known(self):
86 setup, body, _, _ = fd.make_case(random.Random(1), "v_div_fixup_f64", known_case="div-fixup-nan-s0")
87 lines = setup + body
88 with tempfile.TemporaryDirectory() as tmp:
89 skipped = fd.run_one(lines, "ps_f64_fixup_default", tmp)
90 self.assertEqual(skipped[0], "skip")
91 self.assertIn("div-fixup-nan-s0", skipped[1])
92 known = fd.run_one(lines, "ps_f64_fixup_known", tmp, include_known=True)
93 self.assertEqual(known[0], "mismatch")
94 self.assertEqual(fd.words_to_bits(*known[2][:2]), fd.EXP)
95
96 def test_compare_reference_does_not_write_v8_without_cndmask(self):
97 for op in ("v_cmp_eq_f64_e64 s20, v[12:13], 0.0",
98 "v_cmp_class_f64_e64 s20, v[12:13], 0x3ff"):
99 result = fd.ref_program(["v_mov_b32 v8, 0x12345678", "v_mov_b32 v12, 0x00000000",
100 "v_mov_b32 v13, 0x3ff00000", op])
101 self.assertEqual(result[0], 0x12345678)
102
103 def test_shrink_reuses_prepared_gpu_context(self):
104 context = {"exe": "fake"}
105 observed = []
106 def fake_run_one(lines, name, tmp, oracle, gpu_ctx, include_known=False):
107 observed.append(gpu_ctx)
108 return ("mismatch" if len(lines) > 1 else "match", "", [], [])
109 with tempfile.TemporaryDirectory() as tmp, \
110 mock.patch.object(fd, "prepare_gpu", side_effect=AssertionError("GPU setup repeated")), \
111 mock.patch.object(fd, "run_one", side_effect=fake_run_one):
112 reduced = fd.shrink(["first", "second"], [], "gpu", "fake.exe", context, tmp)
113 self.assertEqual(reduced, ["first", "second"])
114 self.assertTrue(observed)
115 self.assertTrue(all(item is context for item in observed))
116
117 def test_known_divergences_only_reinserted_with_known(self):
118 normal = self.command("--ops", "v_add_f64", "--programs", "0")
119 known = self.command("--ops", "v_add_f64", "--programs", "0", "--no-sweep", "--known", "--all-failures")
120 self.assertNotIn("KNOWN_DIVERGENCE", normal.stdout)
121 self.assertEqual(known.returncode, 1)
122 self.assertIn("MISMATCH:", known.stdout)
123 self.assertIn("v_div_fixup_f64", known.stdout) # the known NaN(S0) case is actually executed
124
125 def test_sabotaged_translator_mismatch_and_shrinking(self):
126 lines = ["v_mov_b32 v12, 0x00000000", "v_mov_b32 v13, 0x3ff80000",
127 "v_trunc_f64 v[8:9], v[12:13]"]
128 original = fd.translate
129 def planted_bug(listing, name, temp):
130 out = original(listing, name, temp)
131 if any(line.startswith("v_trunc_f64 ") for line in listing): out[0] ^= 1
132 return out
133 try:
134 fd.translate = planted_bug
135 with tempfile.TemporaryDirectory() as tmp:
136 status, _, expected, actual = fd.run_one(lines, "ps_f64_negative_control", tmp)
137 self.assertEqual(status, "mismatch")
138 self.assertNotEqual(expected, actual)
139 reduced = fd.shrink(lines, expected)
140 self.assertEqual(len(reduced), 1)
141 self.assertTrue(reduced[0].startswith("v_trunc_f64 "))
142 finally:
143 fd.translate = original
144
145 def test_cli_exit_1_on_plantable_mismatch(self):
146 original = fd.translate
147 stdout = io.StringIO()
148 try:
149 fd.translate = lambda listing, name, temp: [0xDEADBEEF, 0, 0, 0]
150 with contextlib.redirect_stdout(stdout):
151 code = fd.main(["--ops", "v_trunc_f64", "--programs", "0"])
152 finally:
153 fd.translate = original
154 self.assertEqual(code, 1)
155 self.assertIn("minimal program:", stdout.getvalue())
156
157
158if __name__ == "__main__":
159 unittest.main()