[clang] Avoid linking libdl unless needed
[llvm-project.git] / libc / AOR_v20.02 / math / tools / plot.py
blob611c99a9e69f2ab6390863702b4004f79c1497d7
1 #!/usr/bin/python
3 # ULP error plot tool.
5 # Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
6 # See https://llvm.org/LICENSE.txt for license information.
7 # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
9 import numpy as np
10 import matplotlib.pyplot as plt
11 import sys
12 import re
14 # example usage:
15 # build/bin/ulp -e .0001 log 0.5 2.0 2345678 | math/tools/plot.py
17 def fhex(s):
18 return float.fromhex(s)
20 def parse(f):
21 xs = []
22 gs = []
23 ys = []
24 es = []
25 # Has to match the format used in ulp.c
26 r = re.compile(r'[^ (]+\(([^ )]*)\) got ([^ ]+) want ([^ ]+) [^ ]+ ulp err ([^ ]+)')
27 for line in f:
28 m = r.match(line)
29 if m:
30 x = fhex(m.group(1))
31 g = fhex(m.group(2))
32 y = fhex(m.group(3))
33 e = float(m.group(4))
34 xs.append(x)
35 gs.append(g)
36 ys.append(y)
37 es.append(e)
38 elif line.startswith('PASS') or line.startswith('FAIL'):
39 # Print the summary line
40 print(line)
41 return xs, gs, ys, es
43 def plot(xs, gs, ys, es):
44 if len(xs) < 2:
45 print('not enough samples')
46 return
47 a = min(xs)
48 b = max(xs)
49 fig, (ax0,ax1) = plt.subplots(nrows=2)
50 es = np.abs(es) # ignore the sign
51 emax = max(es)
52 ax0.text(a+(b-a)*0.7, emax*0.8, '%s\n%g'%(emax.hex(),emax))
53 ax0.plot(xs,es,'r.')
54 ax0.grid()
55 ax1.plot(xs,ys,'r.',label='want')
56 ax1.plot(xs,gs,'b.',label='got')
57 ax1.grid()
58 ax1.legend()
59 plt.show()
61 xs, gs, ys, es = parse(sys.stdin)
62 plot(xs, gs, ys, es)