aboutsummaryrefslogtreecommitdiff
path: root/tests/perf_bench
diff options
context:
space:
mode:
authorDavid Lechner2020-03-22 21:26:08 -0500
committerDamien George2020-03-30 13:21:58 +1100
commit3dc324d3f1312e40d3a8ed87e7244966bb756f26 (patch)
tree94ff44f8eabba0039582c245b901173597edd11e /tests/perf_bench
parent488613bca6c460340ed2995ae5cafafe22d0bfff (diff)
tests: Format all Python code with black, except tests in basics subdir.
This adds the Python files in the tests/ directory to be formatted with ./tools/codeformat.py. The basics/ subdirectory is excluded for now so we aren't changing too much at once. In a few places `# fmt: off`/`# fmt: on` was used where the code had special formatting for readability or where the test was actually testing the specific formatting.
Diffstat (limited to 'tests/perf_bench')
-rw-r--r--tests/perf_bench/benchrun.py3
-rw-r--r--tests/perf_bench/bm_chaos.py91
-rw-r--r--tests/perf_bench/bm_fannkuch.py7
-rw-r--r--tests/perf_bench/bm_fft.py3
-rw-r--r--tests/perf_bench/bm_float.py8
-rw-r--r--tests/perf_bench/bm_hexiom.py111
-rw-r--r--tests/perf_bench/bm_nqueens.py11
-rw-r--r--tests/perf_bench/bm_pidigits.py10
-rw-r--r--tests/perf_bench/misc_aes.py28
-rw-r--r--tests/perf_bench/misc_mandel.py9
-rw-r--r--tests/perf_bench/misc_pystone.py66
-rw-r--r--tests/perf_bench/misc_raytrace.py24
-rw-r--r--tests/perf_bench/viper_call0.py3
-rw-r--r--tests/perf_bench/viper_call1a.py3
-rw-r--r--tests/perf_bench/viper_call1b.py3
-rw-r--r--tests/perf_bench/viper_call1c.py5
-rw-r--r--tests/perf_bench/viper_call2a.py3
-rw-r--r--tests/perf_bench/viper_call2b.py5
18 files changed, 247 insertions, 146 deletions
diff --git a/tests/perf_bench/benchrun.py b/tests/perf_bench/benchrun.py
index 9cbc9695a..90c303dd2 100644
--- a/tests/perf_bench/benchrun.py
+++ b/tests/perf_bench/benchrun.py
@@ -3,6 +3,7 @@ def bm_run(N, M):
from utime import ticks_us, ticks_diff
except ImportError:
import time
+
ticks_us = lambda: int(time.perf_counter() * 1000000)
ticks_diff = lambda a, b: a - b
@@ -14,7 +15,7 @@ def bm_run(N, M):
cur_nm = nm
param = p
if param is None:
- print(-1, -1, 'no matching params')
+ print(-1, -1, "no matching params")
return
# Run and time benchmark
diff --git a/tests/perf_bench/bm_chaos.py b/tests/perf_bench/bm_chaos.py
index 04e04531c..55d282561 100644
--- a/tests/perf_bench/bm_chaos.py
+++ b/tests/perf_bench/bm_chaos.py
@@ -9,7 +9,6 @@ import random
class GVector(object):
-
def __init__(self, x=0, y=0, z=0):
self.x = x
self.y = y
@@ -19,9 +18,9 @@ class GVector(object):
return math.sqrt(self.x ** 2 + self.y ** 2 + self.z ** 2)
def dist(self, other):
- return math.sqrt((self.x - other.x) ** 2
- + (self.y - other.y) ** 2
- + (self.z - other.z) ** 2)
+ return math.sqrt(
+ (self.x - other.x) ** 2 + (self.y - other.y) ** 2 + (self.z - other.z) ** 2
+ )
def __add__(self, other):
if not isinstance(other, GVector):
@@ -35,14 +34,15 @@ class GVector(object):
def __mul__(self, other):
v = GVector(self.x * other, self.y * other, self.z * other)
return v
+
__rmul__ = __mul__
def linear_combination(self, other, l1, l2=None):
if l2 is None:
l2 = 1 - l1
- v = GVector(self.x * l1 + other.x * l2,
- self.y * l1 + other.y * l2,
- self.z * l1 + other.z * l2)
+ v = GVector(
+ self.x * l1 + other.x * l2, self.y * l1 + other.y * l2, self.z * l1 + other.z * l2
+ )
return v
def __str__(self):
@@ -75,8 +75,7 @@ class Spline(object):
def GetDomain(self):
"""Returns the domain of the B-Spline"""
- return (self.knots[self.degree - 1],
- self.knots[len(self.knots) - self.degree])
+ return (self.knots[self.degree - 1], self.knots[len(self.knots) - self.degree])
def __call__(self, u):
"""Calculates a point of the B-Spline using de Boors Algorithm"""
@@ -88,8 +87,7 @@ class Spline(object):
if u == dom[1]:
return self.points[-1]
I = self.GetIndex(u)
- d = [self.points[I - self.degree + 1 + ii]
- for ii in range(self.degree + 1)]
+ d = [self.points[I - self.degree + 1 + ii] for ii in range(self.degree + 1)]
U = self.knots
for ik in range(1, self.degree + 1):
for ii in range(I - self.degree + ik + 1, I + 2):
@@ -120,16 +118,15 @@ class Spline(object):
def write_ppm(im, w, h, filename):
with open(filename, "wb") as f:
- f.write(b'P6\n%i %i\n255\n' % (w, h))
+ f.write(b"P6\n%i %i\n255\n" % (w, h))
for j in range(h):
for i in range(w):
val = im[j * w + i]
c = val * 255
- f.write(b'%c%c%c' % (c, c, c))
+ f.write(b"%c%c%c" % (c, c, c))
class Chaosgame(object):
-
def __init__(self, splines, thickness, subdivs):
self.splines = splines
self.thickness = thickness
@@ -178,10 +175,8 @@ class Chaosgame(object):
neighbour = self.splines[trafo[0]](t + 1 / 50000)
derivative = basepoint - neighbour
if derivative.Mag() != 0:
- basepoint.x += derivative.y / derivative.Mag() * (y - 0.5) * \
- self.thickness
- basepoint.y += -derivative.x / derivative.Mag() * (y - 0.5) * \
- self.thickness
+ basepoint.x += derivative.y / derivative.Mag() * (y - 0.5) * self.thickness
+ basepoint.y += -derivative.x / derivative.Mag() * (y - 0.5) * self.thickness
else:
# can happen, especially with single precision float
pass
@@ -204,8 +199,7 @@ class Chaosgame(object):
random.seed(rng_seed)
im = bytearray(w * h)
- point = GVector((self.maxx + self.minx) / 2,
- (self.maxy + self.miny) / 2, 0)
+ point = GVector((self.maxx + self.minx) / 2, (self.maxy + self.miny) / 2, 0)
for _ in range(iterations):
point = self.transform_point(point)
x = (point.x - self.minx) / self.width * w
@@ -230,29 +224,42 @@ bm_params = {
(5000, 1000): (0.25, 400, 500, 500, 7000, 1234),
}
+
def bm_setup(params):
splines = [
- Spline([
- GVector(1.597, 3.304, 0.0),
- GVector(1.576, 4.123, 0.0),
- GVector(1.313, 5.288, 0.0),
- GVector(1.619, 5.330, 0.0),
- GVector(2.890, 5.503, 0.0),
- GVector(2.373, 4.382, 0.0),
- GVector(1.662, 4.360, 0.0)],
- 3, [0, 0, 0, 1, 1, 1, 2, 2, 2]),
- Spline([
- GVector(2.805, 4.017, 0.0),
- GVector(2.551, 3.525, 0.0),
- GVector(1.979, 2.620, 0.0),
- GVector(1.979, 2.620, 0.0)],
- 3, [0, 0, 0, 1, 1, 1]),
- Spline([
- GVector(2.002, 4.011, 0.0),
- GVector(2.335, 3.313, 0.0),
- GVector(2.367, 3.233, 0.0),
- GVector(2.367, 3.233, 0.0)],
- 3, [0, 0, 0, 1, 1, 1])
+ Spline(
+ [
+ GVector(1.597, 3.304, 0.0),
+ GVector(1.576, 4.123, 0.0),
+ GVector(1.313, 5.288, 0.0),
+ GVector(1.619, 5.330, 0.0),
+ GVector(2.890, 5.503, 0.0),
+ GVector(2.373, 4.382, 0.0),
+ GVector(1.662, 4.360, 0.0),
+ ],
+ 3,
+ [0, 0, 0, 1, 1, 1, 2, 2, 2],
+ ),
+ Spline(
+ [
+ GVector(2.805, 4.017, 0.0),
+ GVector(2.551, 3.525, 0.0),
+ GVector(1.979, 2.620, 0.0),
+ GVector(1.979, 2.620, 0.0),
+ ],
+ 3,
+ [0, 0, 0, 1, 1, 1],
+ ),
+ Spline(
+ [
+ GVector(2.002, 4.011, 0.0),
+ GVector(2.335, 3.313, 0.0),
+ GVector(2.367, 3.233, 0.0),
+ GVector(2.367, 3.233, 0.0),
+ ],
+ 3,
+ [0, 0, 0, 1, 1, 1],
+ ),
]
chaos = Chaosgame(splines, params[0], params[1])
@@ -267,7 +274,7 @@ def bm_setup(params):
norm = params[4]
# Images are not the same when floating point behaviour is different,
# so return percentage of pixels that are set (rounded to int).
- #write_ppm(image, params[2], params[3], 'out-.ppm')
+ # write_ppm(image, params[2], params[3], 'out-.ppm')
pix = int(100 * sum(image) / len(image))
return norm, pix
diff --git a/tests/perf_bench/bm_fannkuch.py b/tests/perf_bench/bm_fannkuch.py
index c9782e3e9..9f7ae797f 100644
--- a/tests/perf_bench/bm_fannkuch.py
+++ b/tests/perf_bench/bm_fannkuch.py
@@ -5,6 +5,7 @@
# http://benchmarksgame.alioth.debian.org/
# Contributed by Sokolov Yura, modified by Tupteq.
+
def fannkuch(n):
count = list(range(1, n + 1))
max_flips = 0
@@ -29,7 +30,7 @@ def fannkuch(n):
flips_count = 0
k = perm[0]
while k:
- perm[:k + 1] = perm[k::-1]
+ perm[: k + 1] = perm[k::-1]
flips_count += 1
k = perm[0]
@@ -57,11 +58,15 @@ bm_params = {
(5000, 10): (9,),
}
+
def bm_setup(params):
state = None
+
def run():
nonlocal state
state = fannkuch(params[0])
+
def result():
return params[0], state
+
return run, result
diff --git a/tests/perf_bench/bm_fft.py b/tests/perf_bench/bm_fft.py
index 9ea8b08f4..fb79a9fd2 100644
--- a/tests/perf_bench/bm_fft.py
+++ b/tests/perf_bench/bm_fft.py
@@ -3,6 +3,7 @@
import math, cmath
+
def transform_radix2(vector, inverse):
# Returns the integer whose value is the reverse of the lowest 'bits' bits of the integer 'x'.
def reverse(x, bits):
@@ -34,6 +35,7 @@ def transform_radix2(vector, inverse):
size *= 2
return vector
+
###########################################################################
# Benchmark interface
@@ -44,6 +46,7 @@ bm_params = {
(5000, 1000): (100, 512),
}
+
def bm_setup(params):
state = None
signal = [math.cos(2 * math.pi * i / params[1]) + 0j for i in range(params[1])]
diff --git a/tests/perf_bench/bm_float.py b/tests/perf_bench/bm_float.py
index 5a66b9bb3..9e55deaee 100644
--- a/tests/perf_bench/bm_float.py
+++ b/tests/perf_bench/bm_float.py
@@ -7,7 +7,7 @@ from math import sin, cos, sqrt
class Point(object):
- __slots__ = ('x', 'y', 'z')
+ __slots__ = ("x", "y", "z")
def __init__(self, i):
self.x = x = sin(i)
@@ -59,12 +59,16 @@ bm_params = {
(5000, 1000): (20, 3000),
}
+
def bm_setup(params):
state = None
+
def run():
nonlocal state
for _ in range(params[0]):
state = benchmark(params[1])
+
def result():
- return params[0] * params[1], 'Point(%.4f, %.4f, %.4f)' % (state.x, state.y, state.z)
+ return params[0] * params[1], "Point(%.4f, %.4f, %.4f)" % (state.x, state.y, state.z)
+
return run, result
diff --git a/tests/perf_bench/bm_hexiom.py b/tests/perf_bench/bm_hexiom.py
index 3a6f1f6c4..84eda9a90 100644
--- a/tests/perf_bench/bm_hexiom.py
+++ b/tests/perf_bench/bm_hexiom.py
@@ -9,18 +9,12 @@
##################################
class Dir(object):
-
def __init__(self, x, y):
self.x = x
self.y = y
-DIRS = [Dir(1, 0),
- Dir(-1, 0),
- Dir(0, 1),
- Dir(0, -1),
- Dir(1, 1),
- Dir(-1, -1)]
+DIRS = [Dir(1, 0), Dir(-1, 0), Dir(0, 1), Dir(0, -1), Dir(1, 1), Dir(-1, -1)]
EMPTY = 7
@@ -37,8 +31,7 @@ class Done(object):
def __init__(self, count, empty=False):
self.count = count
- self.cells = None if empty else [
- [0, 1, 2, 3, 4, 5, 6, EMPTY] for i in range(count)]
+ self.cells = None if empty else [[0, 1, 2, 3, 4, 5, 6, EMPTY] for i in range(count)]
def clone(self):
ret = Done(self.count, True)
@@ -100,7 +93,7 @@ class Done(object):
maxval = -1
maxi = -1
for i in range(self.count):
- if (not self.already_done(i)):
+ if not self.already_done(i):
maxvali = max(k for k in self.cells[i] if k != EMPTY)
if maxval < maxvali:
maxval = maxvali
@@ -109,7 +102,7 @@ class Done(object):
def next_cell_first(self):
for i in range(self.count):
- if (not self.already_done(i)):
+ if not self.already_done(i):
return i
return -1
@@ -119,8 +112,10 @@ class Done(object):
for i in range(self.count):
if not self.already_done(i):
cells_around = pos.hex.get_by_id(i).links
- n = sum(1 if (self.already_done(nid) and (self[nid][0] != EMPTY)) else 0
- for nid in cells_around)
+ n = sum(
+ 1 if (self.already_done(nid) and (self[nid][0] != EMPTY)) else 0
+ for nid in cells_around
+ )
if n > maxn:
maxn = n
maxi = i
@@ -132,8 +127,10 @@ class Done(object):
for i in range(self.count):
if not self.already_done(i):
cells_around = pos.hex.get_by_id(i).links
- n = sum(1 if (self.already_done(nid) and (self[nid][0] != EMPTY)) else 0
- for nid in cells_around)
+ n = sum(
+ 1 if (self.already_done(nid) and (self[nid][0] != EMPTY)) else 0
+ for nid in cells_around
+ )
if n < minn:
minn = n
mini = i
@@ -155,21 +152,21 @@ class Done(object):
else:
raise Exception("Wrong strategy: %d" % strategy)
+
##################################
class Node(object):
-
def __init__(self, pos, id, links):
self.pos = pos
self.id = id
self.links = links
+
##################################
class Hex(object):
-
def __init__(self, size):
self.size = size
self.count = 3 * size * (size - 1) + 1
@@ -213,7 +210,6 @@ class Hex(object):
##################################
class Pos(object):
-
def __init__(self, hex, tiles, done=None):
self.hex = hex
self.tiles = tiles
@@ -222,6 +218,7 @@ class Pos(object):
def clone(self):
return Pos(self.hex, self.tiles, self.done.clone())
+
##################################
@@ -231,8 +228,7 @@ def constraint_pass(pos, last_move=None):
done = pos.done
# Remove impossible values from free cells
- free_cells = (range(done.count) if last_move is None
- else pos.hex.get_by_id(last_move).links)
+ free_cells = range(done.count) if last_move is None else pos.hex.get_by_id(last_move).links
for i in free_cells:
if not done.already_done(i):
vmax = 0
@@ -273,8 +269,7 @@ def constraint_pass(pos, last_move=None):
changed = True
# Force empty or non-empty around filled cells
- filled_cells = (range(done.count) if last_move is None
- else [last_move])
+ filled_cells = range(done.count) if last_move is None else [last_move]
for i in filled_cells:
if done.already_done(i):
num = done[i][0]
@@ -320,8 +315,7 @@ def find_moves(pos, strategy, order):
return [(cell_id, v) for v in done[cell_id]]
else:
# Try higher values first and EMPTY last
- moves = list(reversed([(cell_id, v)
- for v in done[cell_id] if v != EMPTY]))
+ moves = list(reversed([(cell_id, v) for v in done[cell_id] if v != EMPTY]))
if EMPTY in done[cell_id]:
moves.append((cell_id, EMPTY))
return moves
@@ -378,7 +372,7 @@ def solved(pos, output, verbose=False):
elif done.already_done(i):
num = done[i][0]
tiles[num] -= 1
- if (tiles[num] < 0):
+ if tiles[num] < 0:
return IMPOSSIBLE
vmax = 0
vmin = 0
@@ -448,8 +442,7 @@ def check_valid(pos):
tiles[i] = 0
# check total
if tot != hex.count:
- raise Exception(
- "Invalid input. Expected %d tiles, got %d." % (hex.count, tot))
+ raise Exception("Invalid input. Expected %d tiles, got %d." % (hex.count, tot))
def solve(pos, strategy, order, output):
@@ -459,6 +452,7 @@ def solve(pos, strategy, order, output):
# TODO Write an 'iterator' to go over all x,y positions
+
def read_file(file):
lines = [line.strip("\r\n") for line in file.splitlines()]
size = int(lines[0])
@@ -467,10 +461,10 @@ def read_file(file):
tiles = 8 * [0]
done = Done(hex.count)
for y in range(size):
- line = lines[linei][size - y - 1:]
+ line = lines[linei][size - y - 1 :]
p = 0
for x in range(size + y):
- tile = line[p:p + 2]
+ tile = line[p : p + 2]
p += 2
if tile[1] == ".":
inctile = EMPTY
@@ -489,7 +483,7 @@ def read_file(file):
line = lines[linei][y:]
p = 0
for x in range(y, size * 2 - 1):
- tile = line[p:p + 2]
+ tile = line[p : p + 2]
p += 2
if tile[1] == ".":
inctile = EMPTY
@@ -514,63 +508,76 @@ def solve_file(file, strategy, order, output):
LEVELS = {}
-LEVELS[2] = ("""
+LEVELS[2] = (
+ """
2
. 1
. 1 1
1 .
-""", """\
+""",
+ """\
1 1
. . .
1 1
-""")
+""",
+)
-LEVELS[10] = ("""
+LEVELS[10] = (
+ """
3
+.+. .
+. 0 . 2
. 1+2 1 .
2 . 0+.
.+.+.
-""", """\
+""",
+ """\
. . 1
. 1 . 2
0 . 2 2 .
. . . .
0 . .
-""")
+""",
+)
-LEVELS[20] = ("""
+LEVELS[20] = (
+ """
3
. 5 4
. 2+.+1
. 3+2 3 .
+2+. 5 .
. 3 .
-""", """\
+""",
+ """\
3 3 2
4 5 . 1
3 5 2 . .
2 . . .
. . .
-""")
+""",
+)
-LEVELS[25] = ("""
+LEVELS[25] = (
+ """
3
4 . .
. . 2 .
4 3 2 . 4
2 2 3 .
4 2 4
-""", """\
+""",
+ """\
3 4 2
2 4 4 .
. . . 4 2
. 2 4 3
. 2 .
-""")
+""",
+)
-LEVELS[30] = ("""
+LEVELS[30] = (
+ """
4
5 5 . .
3 . 2+2 6
@@ -579,7 +586,8 @@ LEVELS[30] = ("""
4 5 4 . 5 4
5+2 . . 3
4 . . .
-""", """\
+""",
+ """\
3 4 3 .
4 6 5 2 .
2 5 5 . . 2
@@ -587,9 +595,11 @@ LEVELS[30] = ("""
. 3 5 4 5 4
. 2 . 3 3
. . . .
-""")
+""",
+)
-LEVELS[36] = ("""
+LEVELS[36] = (
+ """
4
2 1 1 2
3 3 3 . .
@@ -598,7 +608,8 @@ LEVELS[36] = ("""
2 2 . . . 2
4 3 4 . .
3 2 3 3
-""", """\
+""",
+ """\
3 4 3 2
3 4 4 . 3
2 . . 3 4 3
@@ -606,7 +617,8 @@ LEVELS[36] = ("""
3 3 . 2 . 2
3 . 2 . 2
2 2 . 1
-""")
+""",
+)
###########################################################################
@@ -618,6 +630,7 @@ bm_params = {
(5000, 1000): (10, 25, DESCENDING, Done.FIRST_STRATEGY),
}
+
def bm_setup(params):
try:
import uio as io
@@ -641,7 +654,7 @@ def bm_setup(params):
def result():
norm = params[0] * params[1]
- out = '\n'.join(line.rstrip() for line in output.splitlines())
+ out = "\n".join(line.rstrip() for line in output.splitlines())
return norm, ((out == expected), out)
return run, result
diff --git a/tests/perf_bench/bm_nqueens.py b/tests/perf_bench/bm_nqueens.py
index 87e7245c0..773dd3f7e 100644
--- a/tests/perf_bench/bm_nqueens.py
+++ b/tests/perf_bench/bm_nqueens.py
@@ -19,7 +19,7 @@ def permutations(iterable, r=None):
for i in reversed(range(r)):
cycles[i] -= 1
if cycles[i] == 0:
- indices[i:] = indices[i + 1:] + indices[i:i + 1]
+ indices[i:] = indices[i + 1 :] + indices[i : i + 1]
cycles[i] = n - i
else:
j = cycles[i]
@@ -29,6 +29,7 @@ def permutations(iterable, r=None):
else:
return
+
# From http://code.activestate.com/recipes/576647/
def n_queens(queen_count):
"""N-Queens solver.
@@ -37,10 +38,10 @@ def n_queens(queen_count):
"""
cols = range(queen_count)
for vec in permutations(cols):
- if (queen_count == len(set(vec[i] + i for i in cols))
- == len(set(vec[i] - i for i in cols))):
+ if queen_count == len(set(vec[i] + i for i in cols)) == len(set(vec[i] - i for i in cols)):
yield vec
+
###########################################################################
# Benchmark interface
@@ -51,12 +52,16 @@ bm_params = {
(5000, 100): (1, 8),
}
+
def bm_setup(params):
res = None
+
def run():
nonlocal res
for _ in range(params[0]):
res = len(list(n_queens(params[1])))
+
def result():
return params[0] * 10 ** (params[1] - 3), res
+
return run, result
diff --git a/tests/perf_bench/bm_pidigits.py b/tests/perf_bench/bm_pidigits.py
index ca2e5297d..5949b9306 100644
--- a/tests/perf_bench/bm_pidigits.py
+++ b/tests/perf_bench/bm_pidigits.py
@@ -9,10 +9,7 @@
def compose(a, b):
aq, ar, as_, at = a
bq, br, bs, bt = b
- return (aq * bq,
- aq * br + ar * bt,
- as_ * bq + at * bs,
- as_ * br + at * bt)
+ return (aq * bq, aq * br + ar * bt, as_ * bq + at * bs, as_ * br + at * bt)
def extract(z, j):
@@ -45,6 +42,7 @@ bm_params = {
(5000, 1000): (3, 350),
}
+
def bm_setup(params):
state = None
@@ -53,10 +51,10 @@ def bm_setup(params):
nloop, ndig = params
ndig = params[1]
for _ in range(nloop):
- state = None # free previous result
+ state = None # free previous result
state = gen_pi_digits(ndig)
def result():
- return params[0] * params[1], ''.join(str(d) for d in state)
+ return params[0] * params[1], "".join(str(d) for d in state)
return run, result
diff --git a/tests/perf_bench/misc_aes.py b/tests/perf_bench/misc_aes.py
index 5413a06b1..0743737cb 100644
--- a/tests/perf_bench/misc_aes.py
+++ b/tests/perf_bench/misc_aes.py
@@ -12,6 +12,7 @@
# discrete arithmetic routines, mostly from a precomputed table
# non-linear, invertible, substitution box
+# fmt: off
aes_s_box_table = bytes((
0x63,0x7c,0x77,0x7b,0xf2,0x6b,0x6f,0xc5,0x30,0x01,0x67,0x2b,0xfe,0xd7,0xab,0x76,
0xca,0x82,0xc9,0x7d,0xfa,0x59,0x47,0xf0,0xad,0xd4,0xa2,0xaf,0x9c,0xa4,0x72,0xc0,
@@ -30,31 +31,36 @@ aes_s_box_table = bytes((
0xe1,0xf8,0x98,0x11,0x69,0xd9,0x8e,0x94,0x9b,0x1e,0x87,0xe9,0xce,0x55,0x28,0xdf,
0x8c,0xa1,0x89,0x0d,0xbf,0xe6,0x42,0x68,0x41,0x99,0x2d,0x0f,0xb0,0x54,0xbb,0x16,
))
+# fmt: on
# multiplication of polynomials modulo x^8 + x^4 + x^3 + x + 1 = 0x11b
def aes_gf8_mul_2(x):
if x & 0x80:
- return (x << 1) ^ 0x11b
+ return (x << 1) ^ 0x11B
else:
return x << 1
+
def aes_gf8_mul_3(x):
return x ^ aes_gf8_mul_2(x)
+
# non-linear, invertible, substitution box
def aes_s_box(a):
- return aes_s_box_table[a & 0xff]
+ return aes_s_box_table[a & 0xFF]
+
# return 0x02^(a-1) in GF(2^8)
def aes_r_con(a):
ans = 1
while a > 1:
- ans <<= 1;
+ ans <<= 1
if ans & 0x100:
- ans ^= 0x11b
+ ans ^= 0x11B
a -= 1
return ans
+
##################################################################
# basic AES algorithm; see FIPS-197
@@ -63,6 +69,7 @@ def aes_add_round_key(state, w):
for i in range(16):
state[i] ^= w[i]
+
# combined sub_bytes, shift_rows, mix_columns, add_round_key
# all inputs must be size 16
def aes_sb_sr_mc_ark(state, w, w_idx, temp):
@@ -72,7 +79,7 @@ def aes_sb_sr_mc_ark(state, w, w_idx, temp):
x1 = aes_s_box_table[state[1 + ((i + 1) & 3) * 4]]
x2 = aes_s_box_table[state[2 + ((i + 2) & 3) * 4]]
x3 = aes_s_box_table[state[3 + ((i + 3) & 3) * 4]]
- temp[temp_idx] = aes_gf8_mul_2(x0) ^ aes_gf8_mul_3(x1) ^ x2 ^ x3 ^ w[w_idx]
+ temp[temp_idx] = aes_gf8_mul_2(x0) ^ aes_gf8_mul_3(x1) ^ x2 ^ x3 ^ w[w_idx]
temp[temp_idx + 1] = x0 ^ aes_gf8_mul_2(x1) ^ aes_gf8_mul_3(x2) ^ x3 ^ w[w_idx + 1]
temp[temp_idx + 2] = x0 ^ x1 ^ aes_gf8_mul_2(x2) ^ aes_gf8_mul_3(x3) ^ w[w_idx + 2]
temp[temp_idx + 3] = aes_gf8_mul_3(x0) ^ x1 ^ x2 ^ aes_gf8_mul_2(x3) ^ w[w_idx + 3]
@@ -81,6 +88,7 @@ def aes_sb_sr_mc_ark(state, w, w_idx, temp):
for i in range(16):
state[i] = temp[i]
+
# combined sub_bytes, shift_rows, add_round_key
# all inputs must be size 16
def aes_sb_sr_ark(state, w, w_idx, temp):
@@ -90,7 +98,7 @@ def aes_sb_sr_ark(state, w, w_idx, temp):
x1 = aes_s_box_table[state[1 + ((i + 1) & 3) * 4]]
x2 = aes_s_box_table[state[2 + ((i + 2) & 3) * 4]]
x3 = aes_s_box_table[state[3 + ((i + 3) & 3) * 4]]
- temp[temp_idx] = x0 ^ w[w_idx]
+ temp[temp_idx] = x0 ^ w[w_idx]
temp[temp_idx + 1] = x1 ^ w[w_idx + 1]
temp[temp_idx + 2] = x2 ^ w[w_idx + 2]
temp[temp_idx + 3] = x3 ^ w[w_idx + 3]
@@ -99,6 +107,7 @@ def aes_sb_sr_ark(state, w, w_idx, temp):
for i in range(16):
state[i] = temp[i]
+
# take state as input and change it to the next state in the sequence
# state and temp have size 16, w has size 16 * (Nr + 1), Nr >= 1
def aes_state(state, w, temp, nr):
@@ -109,6 +118,7 @@ def aes_state(state, w, temp, nr):
w_idx += 16
aes_sb_sr_ark(state, w, w_idx, temp)
+
# expand 'key' to 'w' for use with aes_state
# key has size 4 * Nk, w has size 16 * (Nr + 1), temp has size 16
def aes_key_expansion(key, w, temp, nk, nr):
@@ -132,9 +142,11 @@ def aes_key_expansion(key, w, temp, nk, nr):
for j in range(4):
w[w_idx + j] = w[w_idx + j - 4 * nk] ^ t[t_idx + j]
+
##################################################################
# simple use of AES algorithm, using output feedback (OFB) mode
+
class AES:
def __init__(self, keysize):
if keysize == 128:
@@ -160,7 +172,7 @@ class AES:
def set_iv(self, iv):
for i in range(16):
self.state[i] = iv[i]
- self.state_pos = 16;
+ self.state_pos = 16
def get_some_state(self, n_needed):
if self.state_pos >= 16:
@@ -182,6 +194,7 @@ class AES:
idx += ln
self.state_pos += n
+
###########################################################################
# Benchmark interface
@@ -192,6 +205,7 @@ bm_params = {
(5000, 1000): (20, 256),
}
+
def bm_setup(params):
nloop, datalen = params
diff --git a/tests/perf_bench/misc_mandel.py b/tests/perf_bench/misc_mandel.py
index a4b789136..fe26e3f4c 100644
--- a/tests/perf_bench/misc_mandel.py
+++ b/tests/perf_bench/misc_mandel.py
@@ -1,5 +1,6 @@
# Compute the Mandelbrot set, to test complex numbers
+
def mandelbrot(w, h):
def in_set(c):
z = 0
@@ -11,21 +12,23 @@ def mandelbrot(w, h):
img = bytearray(w * h)
- xscale = ((w - 1) / 2.4)
- yscale = ((h - 1) / 3.2)
+ xscale = (w - 1) / 2.4
+ yscale = (h - 1) / 3.2
for v in range(h):
- line = memoryview(img)[v * w:v * w + w]
+ line = memoryview(img)[v * w : v * w + w]
for u in range(w):
c = in_set(complex(v / yscale - 2.3, u / xscale - 1.2))
line[u] = c
return img
+
bm_params = {
(100, 100): (20, 20),
(1000, 1000): (80, 80),
(5000, 1000): (150, 150),
}
+
def bm_setup(ps):
return lambda: mandelbrot(ps[0], ps[1]), lambda: (ps[0] * ps[1], None)
diff --git a/tests/perf_bench/misc_pystone.py b/tests/perf_bench/misc_pystone.py
index 88626774b..26f91c7be 100644
--- a/tests/perf_bench/misc_pystone.py
+++ b/tests/perf_bench/misc_pystone.py
@@ -16,10 +16,9 @@ __version__ = "1.2"
[Ident1, Ident2, Ident3, Ident4, Ident5] = range(1, 6)
-class Record:
- def __init__(self, PtrComp = None, Discr = 0, EnumComp = 0,
- IntComp = 0, StringComp = 0):
+class Record:
+ def __init__(self, PtrComp=None, Discr=0, EnumComp=0, IntComp=0, StringComp=0):
self.PtrComp = PtrComp
self.Discr = Discr
self.EnumComp = EnumComp
@@ -27,12 +26,13 @@ class Record:
self.StringComp = StringComp
def copy(self):
- return Record(self.PtrComp, self.Discr, self.EnumComp,
- self.IntComp, self.StringComp)
+ return Record(self.PtrComp, self.Discr, self.EnumComp, self.IntComp, self.StringComp)
+
TRUE = 1
FALSE = 0
+
def Setup():
global IntGlob
global BoolGlob
@@ -43,10 +43,11 @@ def Setup():
IntGlob = 0
BoolGlob = FALSE
- Char1Glob = '\0'
- Char2Glob = '\0'
- Array1Glob = [0]*51
- Array2Glob = [x[:] for x in [Array1Glob]*51]
+ Char1Glob = "\0"
+ Char2Glob = "\0"
+ Array1Glob = [0] * 51
+ Array2Glob = [x[:] for x in [Array1Glob] * 51]
+
def Proc0(loops):
global IntGlob
@@ -82,16 +83,17 @@ def Proc0(loops):
IntLoc1 = IntLoc1 + 1
Proc8(Array1Glob, Array2Glob, IntLoc1, IntLoc3)
PtrGlb = Proc1(PtrGlb)
- CharIndex = 'A'
+ CharIndex = "A"
while CharIndex <= Char2Glob:
- if EnumLoc == Func1(CharIndex, 'C'):
+ if EnumLoc == Func1(CharIndex, "C"):
EnumLoc = Proc6(Ident1)
- CharIndex = chr(ord(CharIndex)+1)
+ CharIndex = chr(ord(CharIndex) + 1)
IntLoc3 = IntLoc2 * IntLoc1
IntLoc2 = IntLoc3 // IntLoc1
IntLoc2 = 7 * (IntLoc3 - IntLoc2) - IntLoc1
IntLoc1 = Proc2(IntLoc1)
+
def Proc1(PtrParIn):
PtrParIn.PtrComp = NextRecord = PtrGlb.copy()
PtrParIn.IntComp = 5
@@ -108,10 +110,11 @@ def Proc1(PtrParIn):
NextRecord.PtrComp = None
return PtrParIn
+
def Proc2(IntParIO):
IntLoc = IntParIO + 10
while 1:
- if Char1Glob == 'A':
+ if Char1Glob == "A":
IntLoc = IntLoc - 1
IntParIO = IntLoc - IntGlob
EnumLoc = Ident1
@@ -119,6 +122,7 @@ def Proc2(IntParIO):
break
return IntParIO
+
def Proc3(PtrParOut):
global IntGlob
@@ -129,20 +133,23 @@ def Proc3(PtrParOut):
PtrGlb.IntComp = Proc7(10, IntGlob)
return PtrParOut
+
def Proc4():
global Char2Glob
- BoolLoc = Char1Glob == 'A'
+ BoolLoc = Char1Glob == "A"
BoolLoc = BoolLoc or BoolGlob
- Char2Glob = 'B'
+ Char2Glob = "B"
+
def Proc5():
global Char1Glob
global BoolGlob
- Char1Glob = 'A'
+ Char1Glob = "A"
BoolGlob = FALSE
+
def Proc6(EnumParIn):
EnumParOut = EnumParIn
if not Func3(EnumParIn):
@@ -162,24 +169,27 @@ def Proc6(EnumParIn):
EnumParOut = Ident3
return EnumParOut
+
def Proc7(IntParI1, IntParI2):
IntLoc = IntParI1 + 2
IntParOut = IntParI2 + IntLoc
return IntParOut
+
def Proc8(Array1Par, Array2Par, IntParI1, IntParI2):
global IntGlob
IntLoc = IntParI1 + 5
Array1Par[IntLoc] = IntParI2
- Array1Par[IntLoc+1] = Array1Par[IntLoc]
- Array1Par[IntLoc+30] = IntLoc
- for IntIndex in range(IntLoc, IntLoc+2):
+ Array1Par[IntLoc + 1] = Array1Par[IntLoc]
+ Array1Par[IntLoc + 30] = IntLoc
+ for IntIndex in range(IntLoc, IntLoc + 2):
Array2Par[IntLoc][IntIndex] = IntLoc
- Array2Par[IntLoc][IntLoc-1] = Array2Par[IntLoc][IntLoc-1] + 1
- Array2Par[IntLoc+20][IntLoc] = Array1Par[IntLoc]
+ Array2Par[IntLoc][IntLoc - 1] = Array2Par[IntLoc][IntLoc - 1] + 1
+ Array2Par[IntLoc + 20][IntLoc] = Array1Par[IntLoc]
IntGlob = 5
+
def Func1(CharPar1, CharPar2):
CharLoc1 = CharPar1
CharLoc2 = CharLoc1
@@ -188,15 +198,16 @@ def Func1(CharPar1, CharPar2):
else:
return Ident2
+
def Func2(StrParI1, StrParI2):
IntLoc = 1
while IntLoc <= 1:
- if Func1(StrParI1[IntLoc], StrParI2[IntLoc+1]) == Ident1:
- CharLoc = 'A'
+ if Func1(StrParI1[IntLoc], StrParI2[IntLoc + 1]) == Ident1:
+ CharLoc = "A"
IntLoc = IntLoc + 1
- if CharLoc >= 'W' and CharLoc <= 'Z':
+ if CharLoc >= "W" and CharLoc <= "Z":
IntLoc = 7
- if CharLoc == 'X':
+ if CharLoc == "X":
return TRUE
else:
if StrParI1 > StrParI2:
@@ -205,9 +216,11 @@ def Func2(StrParI1, StrParI2):
else:
return FALSE
+
def Func3(EnumParIn):
EnumLoc = EnumParIn
- if EnumLoc == Ident3: return TRUE
+ if EnumLoc == Ident3:
+ return TRUE
return FALSE
@@ -221,6 +234,7 @@ bm_params = {
(5000, 10): (20000,),
}
+
def bm_setup(params):
Setup()
return lambda: Proc0(params[0]), lambda: (params[0], 0)
diff --git a/tests/perf_bench/misc_raytrace.py b/tests/perf_bench/misc_raytrace.py
index 76d4194bc..b51acacca 100644
--- a/tests/perf_bench/misc_raytrace.py
+++ b/tests/perf_bench/misc_raytrace.py
@@ -4,6 +4,7 @@
INF = 1e30
EPS = 1e-6
+
class Vec:
def __init__(self, x, y, z):
self.x, self.y, self.z = x, y, z
@@ -30,12 +31,15 @@ class Vec:
def dot(self, rhs):
return self.x * rhs.x + self.y * rhs.y + self.z * rhs.z
+
RGB = Vec
+
class Ray:
def __init__(self, p, d):
self.p, self.d = p, d
+
class View:
def __init__(self, width, height, depth, pos, xdir, ydir, zdir):
self.width = width
@@ -49,12 +53,14 @@ class View:
def calc_dir(self, dx, dy):
return (self.xdir * dx + self.ydir * dy + self.zdir * self.depth).normalise()
+
class Light:
def __init__(self, pos, colour, casts_shadows):
self.pos = pos
self.colour = colour
self.casts_shadows = casts_shadows
+
class Surface:
def __init__(self, diffuse, specular, spec_idx, reflect, transp, colour):
self.diffuse = diffuse
@@ -76,6 +82,7 @@ class Surface:
def transparent(colour):
return Surface(0.2, 0.9, 32, 0.0, 0.8, colour * 0.3)
+
class Sphere:
def __init__(self, surface, centre, radius):
self.surface = surface
@@ -99,6 +106,7 @@ class Sphere:
def surface_at(self, v):
return self.surface, (v - self.centre).normalise()
+
class Plane:
def __init__(self, surface, centre, normal):
self.surface = surface
@@ -116,12 +124,14 @@ class Plane:
def surface_at(self, p):
return self.surface, self.normal
+
class Scene:
def __init__(self, ambient, light, objs):
self.ambient = ambient
self.light = light
self.objs = objs
+
def trace_scene(canvas, view, scene, max_depth):
for v in range(canvas.height):
y = (-v + 0.5 * (canvas.height - 1)) * view.height / canvas.height
@@ -131,6 +141,7 @@ def trace_scene(canvas, view, scene, max_depth):
c = trace_ray(scene, ray, max_depth)
canvas.put_pix(u, v, c)
+
def trace_ray(scene, ray, depth):
# Find closest intersecting object
hit_t = INF
@@ -181,6 +192,7 @@ def trace_ray(scene, ray, depth):
return col
+
def trace_to_light(scene, ray, light_dist):
col = scene.light.colour
for obj in scene.objs:
@@ -189,6 +201,7 @@ def trace_to_light(scene, ray, light_dist):
col *= obj.surface.transp
return col
+
class Canvas:
def __init__(self, width, height):
self.width = width
@@ -202,10 +215,11 @@ class Canvas:
self.data[off + 2] = min(255, max(0, int(255 * c.z)))
def write_ppm(self, filename):
- with open(filename, 'wb') as f:
- f.write(bytes('P6 %d %d 255\n' % (self.width, self.height), 'ascii'))
+ with open(filename, "wb") as f:
+ f.write(bytes("P6 %d %d 255\n" % (self.width, self.height), "ascii"))
f.write(self.data)
+
def main(w, h, d):
canvas = Canvas(w, h)
view = View(32, 32, 64, Vec(0, 0, 50), Vec(1, 0, 0), Vec(0, 1, 0), Vec(0, 0, -1))
@@ -221,13 +235,14 @@ def main(w, h, d):
Sphere(Surface.shiny(RGB(1, 1, 1)), Vec(-5, -4, 3), 4),
Sphere(Surface.dull(RGB(0, 0, 1)), Vec(4, -5, 0), 4),
Sphere(Surface.transparent(RGB(0.2, 0.2, 0.2)), Vec(6, -1, 8), 4),
- ]
+ ],
)
trace_scene(canvas, view, scene, d)
return canvas
+
# For testing
-#main(256, 256, 4).write_ppm('rt.ppm')
+# main(256, 256, 4).write_ppm('rt.ppm')
###########################################################################
# Benchmark interface
@@ -238,5 +253,6 @@ bm_params = {
(5000, 100): (40, 40, 3),
}
+
def bm_setup(params):
return lambda: main(*params), lambda: (params[0] * params[1] * params[2], None)
diff --git a/tests/perf_bench/viper_call0.py b/tests/perf_bench/viper_call0.py
index 0f476b127..903e2b5e5 100644
--- a/tests/perf_bench/viper_call0.py
+++ b/tests/perf_bench/viper_call0.py
@@ -2,12 +2,14 @@
def f0():
pass
+
@micropython.native
def call(r):
f = f0
for _ in r:
f()
+
bm_params = {
(50, 10): (15000,),
(100, 10): (30000,),
@@ -15,5 +17,6 @@ bm_params = {
(5000, 10): (1500000,),
}
+
def bm_setup(params):
return lambda: call(range(params[0])), lambda: (params[0] // 1000, None)
diff --git a/tests/perf_bench/viper_call1a.py b/tests/perf_bench/viper_call1a.py
index 2bb4a28fd..76adef60a 100644
--- a/tests/perf_bench/viper_call1a.py
+++ b/tests/perf_bench/viper_call1a.py
@@ -2,12 +2,14 @@
def f1a(x):
return x
+
@micropython.native
def call(r):
f = f1a
for _ in r:
f(1)
+
bm_params = {
(50, 10): (15000,),
(100, 10): (30000,),
@@ -15,5 +17,6 @@ bm_params = {
(5000, 10): (1500000,),
}
+
def bm_setup(params):
return lambda: call(range(params[0])), lambda: (params[0] // 1000, None)
diff --git a/tests/perf_bench/viper_call1b.py b/tests/perf_bench/viper_call1b.py
index cda64007f..b52693c15 100644
--- a/tests/perf_bench/viper_call1b.py
+++ b/tests/perf_bench/viper_call1b.py
@@ -2,12 +2,14 @@
def f1b(x) -> int:
return int(x)
+
@micropython.native
def call(r):
f = f1b
for _ in r:
f(1)
+
bm_params = {
(50, 10): (15000,),
(100, 10): (30000,),
@@ -15,5 +17,6 @@ bm_params = {
(5000, 10): (1500000,),
}
+
def bm_setup(params):
return lambda: call(range(params[0])), lambda: (params[0] // 1000, None)
diff --git a/tests/perf_bench/viper_call1c.py b/tests/perf_bench/viper_call1c.py
index c653eb8d3..31578c5ba 100644
--- a/tests/perf_bench/viper_call1c.py
+++ b/tests/perf_bench/viper_call1c.py
@@ -1,13 +1,15 @@
@micropython.viper
-def f1c(x:int) -> int:
+def f1c(x: int) -> int:
return x
+
@micropython.native
def call(r):
f = f1c
for _ in r:
f(1)
+
bm_params = {
(50, 10): (15000,),
(100, 10): (30000,),
@@ -15,5 +17,6 @@ bm_params = {
(5000, 10): (1500000,),
}
+
def bm_setup(params):
return lambda: call(range(params[0])), lambda: (params[0] // 1000, None)
diff --git a/tests/perf_bench/viper_call2a.py b/tests/perf_bench/viper_call2a.py
index 6204f985f..d0520b46b 100644
--- a/tests/perf_bench/viper_call2a.py
+++ b/tests/perf_bench/viper_call2a.py
@@ -2,12 +2,14 @@
def f2a(x, y):
return x
+
@micropython.native
def call(r):
f = f2a
for _ in r:
f(1, 2)
+
bm_params = {
(50, 10): (15000,),
(100, 10): (30000,),
@@ -15,5 +17,6 @@ bm_params = {
(5000, 10): (1500000,),
}
+
def bm_setup(params):
return lambda: call(range(params[0])), lambda: (params[0] // 1000, None)
diff --git a/tests/perf_bench/viper_call2b.py b/tests/perf_bench/viper_call2b.py
index 087cf8ab0..1171b7d57 100644
--- a/tests/perf_bench/viper_call2b.py
+++ b/tests/perf_bench/viper_call2b.py
@@ -1,13 +1,15 @@
@micropython.viper
-def f2b(x:int, y:int) -> int:
+def f2b(x: int, y: int) -> int:
return x + y
+
@micropython.native
def call(r):
f = f2b
for _ in r:
f(1, 2)
+
bm_params = {
(50, 10): (15000,),
(100, 10): (30000,),
@@ -15,5 +17,6 @@ bm_params = {
(5000, 10): (1500000,),
}
+
def bm_setup(params):
return lambda: call(range(params[0])), lambda: (params[0] // 1000, None)