upleb.uk

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

summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTreXito <81475944+TreXito@users.noreply.github.com>2026-11-22 21:03:17 +0000
committerTreXito <81475944+TreXito@users.noreply.github.com>2026-11-22 21:12:48 +0000
commitbbc3d9105d16c756ca2bec194e19d4e090af6e8d (patch)
treef18444fe64234306c87247e3aa9de9196ac6a81f
parentf847748cc76bd3777f9c070c4034eb2fc1c04e64 (diff)
d16_format_probe: round in integers, the same bits about 4x faster
round_frac built Fractions for 2^e (twice), for the ulp and for the quotient on every call, and models16/models32 call it five times per value. The tool and tests/test_d16_format_probe.py sweep all 65,536 UNORM16 and SNORM16 values per instruction, so that file was among the slowest in the check job. The same exact rounding now runs on the numerator and denominator: floor(log2) from the bit lengths and one comparison, then divmod and a half-to-even check. The result is the same Fraction. - models16 and models32 give identical results to origin/main for every value of all five non-f16 formats (133,376 values). - New test: round_frac against the Fraction form it replaced, for f16 and f32 in both modes. It covers 16-bit quotients, exact values, ties and just-under-ties at the ends of a binade (including the carry into the next one) and in the denormal range, powers of two down past the smallest denormal, random fractions, and their negations. It fails for ties away from zero, ties up, a missing exponent correction and a missing denormal clamp. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
-rw-r--r--tests/test_d16_format_probe.py40
-rw-r--r--tools/d16_format_probe.py26
2 files changed, 54 insertions, 12 deletions
diff --git a/tests/test_d16_format_probe.py b/tests/test_d16_format_probe.py
index 79107df65..6019fa850 100644
--- a/tests/test_d16_format_probe.py
+++ b/tests/test_d16_format_probe.py
@@ -6,7 +6,9 @@ import io
6import json 6import json
7import os 7import os
8from contextlib import redirect_stderr, redirect_stdout 8from contextlib import redirect_stderr, redirect_stdout
9from fractions import Fraction
9from pathlib import Path 10from pathlib import Path
11import random
10import re 12import re
11import subprocess 13import subprocess
12import sys 14import sys
@@ -49,6 +51,44 @@ class Rules(unittest.TestCase):
49 self.assertEqual(pr.models32("unorm", 16, 0xFFFF)["f32_rte"], 0x3F800000) 51 self.assertEqual(pr.models32("unorm", 16, 0xFFFF)["f32_rte"], 0x3F800000)
50 self.assertEqual(pr.models32("unorm", 8, 0x80)["f32_rte"], 0x3F008081) # 128 / 255 52 self.assertEqual(pr.models32("unorm", 8, 0x80)["f32_rte"], 0x3F008081) # 128 / 255
51 53
54 def test_round_frac_matches_the_fraction_form(self):
55 """round_frac works in integers; the Fraction form it replaced is the reference here, for f16 and f32 and
56 both modes: 16-bit format quotients, the first and last significands of a binade exact, at a tie and just
57 under one (a tie at the last one carries into the next binade), the same for denormals, powers of two down
58 past the smallest denormal, random fractions in [-1, 1], and all of them negated."""
59 def reference(x, prec, emin, rtz=False):
60 if x == 0:
61 return Fraction(0)
62 sign, a = (-1 if x < 0 else 1), abs(x)
63 e = a.numerator.bit_length() - a.denominator.bit_length()
64 if Fraction(2) ** e > a:
65 e -= 1
66 if Fraction(2) ** (e + 1) <= a:
67 e += 1
68 ulp = Fraction(2) ** (max(e, emin) - prec + 1)
69 q = a / ulp
70 return sign * (q.numerator // q.denominator if rtz else round(q)) * ulp
71
72 rng = random.Random(161)
73 for prec, emin in ((11, -14), (24, -126)):
74 cases = {Fraction(v, 65535) for v in range(0, 65536, 97)}
75 cases |= {Fraction(v, 32767) for v in range(0, 32768, 89)}
76 for e in (-1, -2, -7, emin): # the binade [2^e, 2^(e+1)), ulp 2^(e - prec + 1)
77 ulp = Fraction(2) ** (e - prec + 1)
78 for m in (2 ** (prec - 1), 2 ** (prec - 1) + 1, 2 ** prec - 2, 2 ** prec - 1): # first and last
79 cases |= {m * ulp, (m + Fraction(1, 2)) * ulp, (m + Fraction(1, 3)) * ulp} # exact, tie, below
80 ulp = Fraction(2) ** (emin - prec + 1) # denormals: 0 < x < 2^emin, the same ulp
81 for m in (0, 1, 2, 3, 2 ** (prec - 1) - 1):
82 cases |= {m * ulp, (m + Fraction(1, 2)) * ulp, (m + Fraction(1, 4)) * ulp}
83 cases |= {Fraction(1, 2 ** j) for j in range(prec - emin + 3)} # down to under the smallest denormal
84 cases |= {Fraction(rng.randrange(-(1 << 40), 1 << 40), rng.randrange(1 << 40, 1 << 41))
85 for _ in range(400)}
86 cases |= {-c for c in cases}
87 for x in sorted(cases):
88 for rtz in (False, True):
89 self.assertEqual(pr.round_frac(x, prec, emin, rtz), reference(x, prec, emin, rtz),
90 (x, prec, emin, rtz))
91
52 92
53class Kernels(unittest.TestCase): 93class Kernels(unittest.TestCase):
54 def test_descriptor_word3(self): 94 def test_descriptor_word3(self):
diff --git a/tools/d16_format_probe.py b/tools/d16_format_probe.py
index b8ed5e838..20467644c 100644
--- a/tools/d16_format_probe.py
+++ b/tools/d16_format_probe.py
@@ -71,20 +71,22 @@ RULES32 = ("f32_rte", "f32_rtz", "mul_f32")
71 71
72def round_frac(x, prec, emin, rtz=False): 72def round_frac(x, prec, emin, rtz=False):
73 """x rounded to a binary float with prec significand bits (implicit bit included) and minimum normal exponent 73 """x rounded to a binary float with prec significand bits (implicit bit included) and minimum normal exponent
74 emin, denormals kept; nearest even, or toward zero. No overflow: every value here is in [-1, 1].""" 74 emin, denormals kept; nearest even, or toward zero. No overflow: every value here is in [-1, 1].
75 if x == 0: 75 Exact, in integers: x = p/q, 2^e <= p/q < 2^(e+1), ulp = 2^k; the significand is p/q / 2^k rounded. Building
76 Fractions for 2^e and the quotient instead gave the same bits about 4x slower (minutes for a 16-bit sweep)."""
77 p, q = x.numerator, x.denominator
78 if p == 0:
76 return Fraction(0) 79 return Fraction(0)
77 sign = -1 if x < 0 else 1 80 sign, p = (-1, -p) if p < 0 else (1, p)
78 a = abs(x) 81 e = p.bit_length() - q.bit_length() # floor(log2(p/q)) is e or e - 1
79 e = a.numerator.bit_length() - a.denominator.bit_length() 82 if (p << -e if e < 0 else p) < (q << e if e > 0 else q):
80 if Fraction(2) ** e > a:
81 e -= 1 83 e -= 1
82 if Fraction(2) ** (e + 1) <= a: 84 k = max(e, emin) - prec + 1
83 e += 1 85 num, den = (p, q << k) if k >= 0 else (p << -k, q)
84 ulp = Fraction(2) ** (max(e, emin) - prec + 1) 86 n, r = divmod(num, den)
85 q = a / ulp 87 if not rtz and (2 * r > den or (2 * r == den and n & 1)): # half to even
86 n = q.numerator // q.denominator if rtz else round(q) # round() of a Fraction is half to even 88 n += 1
87 return sign * n * ulp 89 return Fraction(sign * n << k) if k >= 0 else Fraction(sign * n, 1 << -k)
88 90
89 91
90def f16_bits(v): 92def f16_bits(v):