interplane.Get Omega
Read in your language

CUDApp

CUDA++: AIEN’s GPU machine-code toolchain, runtime and driver client for NVIDIA GPUs without the CUDA toolkit. Hardware coverage and driver replacement limits are explicit.

Brought by drakestapleton

branch main Branches Tags History

CUDApp / om / q38-head / envelope-check.py

62 lines · 1996 bytes Raw Blame History
1# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2# Independent external numerical verification; not a runtime dependency.
3import math
4import random
5import struct
6import subprocess
7import sys
8from fractions import Fraction
9
10
11def fbits(f):
12 return struct.unpack('<I', struct.pack('<f', f))[0]
13
14
15def value(b):
16 return struct.unpack('<f', struct.pack('<I', b))[0]
17
18
19def run(bits):
20 assert len(bits) == 5120
21 p = subprocess.run([sys.argv[1]], input=struct.pack('<5120I', *bits), capture_output=True)
22 assert p.returncode == 0, (p.returncode, p.stderr)
23 assert len(p.stdout) == 8
24 return struct.unpack('<Q', p.stdout)[0]
25
26
27rng = random.Random(710)
28cases = [[0] * 5120, [fbits(1.0)] * 5120, [fbits(2.7109375)] * 5120]
29cases += [[fbits(-1.0)] * 5120, [0] * 5119 + [fbits(-2.7109375)]]
30for _ in range(25):
31 cases.append([rng.randrange(0, 0x7d000000) | (rng.randrange(2) << 31) for _ in range(5120)])
32for b in [1, 0x007fffff, 0x00800000, 0x7e000000, 0x7f7fffff]:
33 a = [0] * 5120
34 a[-1] = b
35 cases.append(a)
36for exponent in range(1, 255):
37 for mantissa in [0, 1, 0x7ffffe, 0x7fffff, rng.randrange(0x800000)]:
38 a = [0] * 5120
39 a[rng.randrange(5120)] = (exponent << 23) | mantissa
40 cases.append(a)
41checks = 0
42for a in cases:
43 w = max(abs(value(b)) for b in a)
44 r = run(a)
45 if w < value(0x00800000):
46 assert r == fbits(1.0), (w, r)
47 else:
48 exact = Fraction(w) * 72 * Fraction(1025, 1024)
49 if exact > Fraction(value(0x7f7fffff)):
50 assert r == 0
51 else:
52 assert 0 < r < 0x7f800000
53 assert Fraction(value(r)) >= exact
54 assert Fraction(value(r - 1)) < exact
55 assert value(r) >= math.sqrt(5120) * w
56 checks += 1
57for b in [0x7f800000, 0xff800000, 0x7fc00000, 0xffc00001]:
58 a = [fbits(1.0)] * 5120
59 a[-1] = b
60 assert run(a) == 0
61 checks += 1
62print(f'PASS: {checks} envelope cases, exact rational upward rounding, tail invalid-input refusal')