float_ieee_host.py (2061B)
1 #!/usr/bin/env python3 2 """Check a separately compiled C fenv reference against the exact policy oracle.""" 3 import argparse 4 import hashlib 5 import json 6 from pathlib import Path 7 import subprocess 8 from float_ieee import OPS, cases, oracle, nan, QUIET 9 10 11 def main(): 12 parser=argparse.ArgumentParser(description=__doc__) 13 parser.add_argument('reference',type=Path) 14 parser.add_argument('--directory',required=True,type=Path) 15 args=parser.parse_args();root=args.directory.resolve();root.mkdir(parents=True,exist_ok=False) 16 rows=[(i,a,b) for i,op in enumerate(OPS) for a,b in cases(op)] 17 raw=''.join(f'{i} {a:016x} {b:016x}\n' for i,a,b in rows).encode() 18 (root/'input.txt').write_bytes(raw) 19 result=subprocess.run([str(args.reference.resolve())],input=raw,capture_output=True,check=True,timeout=60) 20 (root/'output.txt').write_bytes(result.stdout) 21 lines=result.stdout.splitlines();assert len(lines)==len(rows) 22 nan_count=0 23 for (i,a,b),line in zip(rows,lines): 24 word,flags=line.split();word=int(word,16);flags=int(flags) 25 ok,(expected,bits)=oracle(a,b,OPS[i]);assert ok 26 wanted=sum(int(bit)<<j for j,bit in enumerate(bits)) 27 assert flags==wanted,(OPS[i],hex(a),hex(b),'flags',flags,wanted) 28 if nan(expected): 29 assert nan(word) and word&QUIET 30 nan_count+=1 31 else:assert word==expected,(OPS[i],hex(a),hex(b),hex(word),hex(expected)) 32 sha=lambda p:hashlib.sha256(Path(p).read_bytes()).hexdigest() 33 report=dict(complete=True,checked=len(rows),nan_bits_compared_by_class_only=nan_count, 34 zero_divisor_cases_included=True,reference_sha256=sha(args.reference), 35 source_sha256=sha(Path(__file__).with_suffix('.c')),harness_sha256=sha(__file__), 36 oracle_sha256=sha(Path(__file__).with_name('float_ieee.py')), 37 input_sha256=sha(root/'input.txt'),output_sha256=sha(root/'output.txt')) 38 (root/'report.json').write_text(json.dumps(report,indent=2)+'\n');print(json.dumps(report)) 39 40 41 if __name__=='__main__':main()