Path: blob/main/contrib/arm-optimized-routines/math/tools/plot.py
48249 views
#!/usr/bin/python12# ULP error plot tool.3#4# Copyright (c) 2019, Arm Limited.5# SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception67import numpy as np8import matplotlib.pyplot as plt9import sys10import re1112# example usage:13# build/bin/ulp -e .0001 log 0.5 2.0 2345678 | math/tools/plot.py1415def fhex(s):16return float.fromhex(s)1718def parse(f):19xs = []20gs = []21ys = []22es = []23# Has to match the format used in ulp.c24r = re.compile(r'[^ (]+\(([^ )]*)\) got ([^ ]+) want ([^ ]+) [^ ]+ ulp err ([^ ]+)')25for line in f:26m = r.match(line)27if m:28x = fhex(m.group(1))29g = fhex(m.group(2))30y = fhex(m.group(3))31e = float(m.group(4))32xs.append(x)33gs.append(g)34ys.append(y)35es.append(e)36elif line.startswith('PASS') or line.startswith('FAIL'):37# Print the summary line38print(line)39return xs, gs, ys, es4041def plot(xs, gs, ys, es):42if len(xs) < 2:43print('not enough samples')44return45a = min(xs)46b = max(xs)47fig, (ax0,ax1) = plt.subplots(nrows=2)48es = np.abs(es) # ignore the sign49emax = max(es)50ax0.text(a+(b-a)*0.7, emax*0.8, '%s\n%g'%(emax.hex(),emax))51ax0.plot(xs,es,'r.')52ax0.grid()53ax1.plot(xs,ys,'r.',label='want')54ax1.plot(xs,gs,'b.',label='got')55ax1.grid()56ax1.legend()57plt.show()5859xs, gs, ys, es = parse(sys.stdin)60plot(xs, gs, ys, es)616263