xref: /llvm-project/libc/AOR_v20.02/math/tools/plot.py (revision f98ee40f4b5d7474fc67e82824bf6abbaedb7b1c)
1#!/usr/bin/env python
2
3# ULP error plot tool.
4#
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
8
9import numpy as np
10import matplotlib.pyplot as plt
11import sys
12import re
13
14# example usage:
15# build/bin/ulp -e .0001 log 0.5 2.0 2345678 | math/tools/plot.py
16
17
18def fhex(s):
19    return float.fromhex(s)
20
21
22def parse(f):
23    xs = []
24    gs = []
25    ys = []
26    es = []
27    # Has to match the format used in ulp.c
28    r = re.compile(r"[^ (]+\(([^ )]*)\) got ([^ ]+) want ([^ ]+) [^ ]+ ulp err ([^ ]+)")
29    for line in f:
30        m = r.match(line)
31        if m:
32            x = fhex(m.group(1))
33            g = fhex(m.group(2))
34            y = fhex(m.group(3))
35            e = float(m.group(4))
36            xs.append(x)
37            gs.append(g)
38            ys.append(y)
39            es.append(e)
40        elif line.startswith("PASS") or line.startswith("FAIL"):
41            # Print the summary line
42            print(line)
43    return xs, gs, ys, es
44
45
46def plot(xs, gs, ys, es):
47    if len(xs) < 2:
48        print("not enough samples")
49        return
50    a = min(xs)
51    b = max(xs)
52    fig, (ax0, ax1) = plt.subplots(nrows=2)
53    es = np.abs(es)  # ignore the sign
54    emax = max(es)
55    ax0.text(a + (b - a) * 0.7, emax * 0.8, "%s\n%g" % (emax.hex(), emax))
56    ax0.plot(xs, es, "r.")
57    ax0.grid()
58    ax1.plot(xs, ys, "r.", label="want")
59    ax1.plot(xs, gs, "b.", label="got")
60    ax1.grid()
61    ax1.legend()
62    plt.show()
63
64
65xs, gs, ys, es = parse(sys.stdin)
66plot(xs, gs, ys, es)
67