3 # SPDX-License-Identifier: BSD-2-Clause
5 # Copyright (c) 2018-2020 Gavin D. Howard and contributors.
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions are met:
10 # * Redistributions of source code must retain the above copyright notice, this
11 # list of conditions and the following disclaimer.
13 # * Redistributions in binary form must reproduce the above copyright notice,
14 # this list of conditions and the following disclaimer in the documentation
15 # and/or other materials provided with the distribution.
17 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 # POSSIBILITY OF SUCH DAMAGE.
36 print("usage: {} [num_iterations test_num exe]".format(script))
37 print("\n num_iterations is the number of times to run each karatsuba number; default is 4")
38 print("\n test_num is the last Karatsuba number to run through tests")
41 def run(cmd, env=None):
42 return subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env)
45 testdir = os.path.dirname(script)
50 print("\nWARNING: This script is for distro and package maintainers.")
51 print("It is for finding the optimal Karatsuba number.")
52 print("Though it only needs to be run once per release/platform,")
53 print("it takes forever to run.")
54 print("You have been warned.\n")
55 print("Note: If you send an interrupt, it will report the current best number.\n")
57 if __name__ != "__main__":
68 if len(sys.argv) >= 2:
69 num_iterations = int(sys.argv[1])
73 if len(sys.argv) >= 3:
74 test_num = int(sys.argv[2])
78 if len(sys.argv) >= args_idx:
81 exe = testdir + "/bin/bc"
83 exedir = os.path.dirname(exe)
85 indata = "for (i = 0; i < 100; ++i) {} * {}\n"
86 indata += "1.23456789^100000\n1.23456789^100000\nhalt"
87 indata = indata.format(num, num).encode()
92 nruns = num_iterations + 1
94 for i in range(0, nruns):
97 tests = [ "multiply", "modulus", "power", "sqrt" ]
98 scripts = [ "multiply" ]
100 print("Testing CFLAGS=\"-flto\"...")
102 flags = dict(os.environ)
104 flags["CFLAGS"] = flags["CFLAGS"] + " " + "-flto"
106 flags["CFLAGS"] = "-flto"
108 p = run([ "./configure.sh", "-O3" ], flags)
109 if p.returncode != 0:
110 print("configure.sh returned an error ({}); exiting...".format(p.returncode))
111 sys.exit(p.returncode)
115 if p.returncode == 0:
117 print("Using CFLAGS=\"-flto\"")
119 config_env = os.environ
120 print("Not using CFLAGS=\"-flto\"")
122 p = run([ "make", "clean" ])
124 print("Testing \"make -j4\"")
126 if p.returncode != 0:
127 print("make returned an error ({}); exiting...".format(p.returncode))
128 sys.exit(p.returncode)
130 p = run([ "make", "-j4" ])
132 if p.returncode == 0:
133 makecmd = [ "make", "-j4" ]
134 print("Using \"make -j4\"")
137 print("Not using \"make -j4\"")
144 for i in range(mn, mx2 + 1):
146 print("\nCompiling...\n")
148 p = run([ "./configure.sh", "-O3", "-k{}".format(i) ], config_env)
150 if p.returncode != 0:
151 print("configure.sh returned an error ({}); exiting...".format(p.returncode))
152 sys.exit(p.returncode)
156 if p.returncode != 0:
157 print("make returned an error ({}); exiting...".format(p.returncode))
158 sys.exit(p.returncode)
162 print("Running tests for Karatsuba Num: {}\n".format(i))
166 cmd = [ "{}/tests/test.sh".format(testdir), "bc", test, "0", "0", exe ]
168 p = subprocess.run(cmd + sys.argv[args_idx:], stderr=subprocess.PIPE)
170 if p.returncode != 0:
171 print("{} test failed:\n".format(test, p.returncode))
172 print(p.stderr.decode())
173 print("\nexiting...")
174 sys.exit(p.returncode)
178 for script in scripts:
180 cmd = [ "{}/tests/script.sh".format(testdir), "bc", script + ".bc",
181 "0", "1", "1", "0", exe ]
183 p = subprocess.run(cmd + sys.argv[args_idx:], stderr=subprocess.PIPE)
185 if p.returncode != 0:
186 print("{} test failed:\n".format(test, p.returncode))
187 print(p.stderr.decode())
188 print("\nexiting...")
189 sys.exit(p.returncode)
195 print("Timing Karatsuba Num: {}".format(i), end='', flush=True)
197 for j in range(0, nruns):
199 cmd = [ exe, "{}/tests/bc/power.txt".format(testdir) ]
201 start = time.perf_counter()
202 p = subprocess.run(cmd, input=indata, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
203 end = time.perf_counter()
205 if p.returncode != 0:
206 print("bc returned an error; exiting...")
207 sys.exit(p.returncode)
209 runs[j] = end - start
212 avg = sum(run_times) / len(run_times)
216 print(", Time: {}".format(times[i - mn]))
218 except KeyboardInterrupt:
224 opt = nums[times.index(min(times))]
226 print("\n\nOptimal Karatsuba Num (for this machine): {}".format(opt))
227 print("Run the following:\n")
228 if "-flto" in config_env["CFLAGS"]:
229 print("CFLAGS=\"-flto\" ./configure.sh -O3 -k {}".format(opt))
231 print("./configure.sh -O3 -k {}".format(opt))