Skip to content

Commit

Permalink
Fix formatting of our Python scripts.
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexhuszagh committed Nov 1, 2024
1 parent cc04113 commit 478caac
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 33 deletions.
17 changes: 14 additions & 3 deletions lexical-benchmark/etc/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import argparse
import json
import os
import re

import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
Expand All @@ -21,6 +20,7 @@
etc = os.path.dirname(os.path.realpath(__file__))
home = os.path.dirname(etc)


def parse_args(argv=None):
'''Create and parse our command line arguments.'''

Expand All @@ -42,6 +42,7 @@ def parse_args(argv=None):
)
return parser.parse_args(argv)


def filename(basename, args):
'''Get a resilient name for the benchmark data.'''

Expand All @@ -52,6 +53,7 @@ def filename(basename, args):
name = f'{name}_features={args.features}'
return name


def format_time(time):
'''Format time to be a nice value.'''

Expand All @@ -71,14 +73,17 @@ def strip_zero(value):
time /= 1000
return f'{strip_zero(str(round(time, 3)))} s'


def float_sort_key(x):
'''Sort key for an float value.'''
return (x[0], int(x[1:]))


def integer_sort_key(x):
'''Sort key for an integral value.'''
return (x[0], int(x[1:]))


def plot_bar(
prefix=None,
xlabel=None,
Expand Down Expand Up @@ -115,13 +120,13 @@ def plot_ax(ax, xticks):
ax.legend(libraries, fancybox=True, framealpha=1, shadow=True, borderpad=1)

fig = plt.figure(figsize=(10, 8))
index = 1
ax = fig.add_subplot(1, 1, 1)
plot_ax(ax, xticks)

fig.savefig(path, format='svg')
fig.clf()


def plot_scatter(
prefix=None,
xlabel=None,
Expand All @@ -146,7 +151,7 @@ def plot_ax(ax, xticks):

for library in libraries:
ys = [data[f'{prefix}_{i}_{library}'] for i in xticks]
points = ax.semilogy(
_ = ax.semilogy(
xticks, ys, '-o', mec='k', ms=15,
mew=1, alpha=.8, label=library
)
Expand All @@ -170,6 +175,7 @@ def plot_ax(ax, xticks):
fig.savefig(path, format='svg')
fig.clf()


def plot_write_float(args):
'''Plot the write float dataset.'''

Expand Down Expand Up @@ -235,6 +241,7 @@ def plot_write_float(args):
title='Random Data: BigInts',
)


def plot_write_integer(args):
'''Plot the write integer dataset.'''

Expand Down Expand Up @@ -301,6 +308,7 @@ def plot_write_integer(args):
title='Random Data: Large Negative',
)


def plot_parse_float(args):
'''Plot the parse float dataset.'''

Expand Down Expand Up @@ -392,6 +400,7 @@ def plot_parse_float(args):
key=lambda x: int(x),
)


def plot_parse_integer(args):
'''Plot the parse integer dataset.'''

Expand Down Expand Up @@ -452,6 +461,7 @@ def plot_parse_integer(args):
title='Random Data: Large Negative',
)


def main(argv=None):
'''Entry point.'''

Expand All @@ -469,5 +479,6 @@ def main(argv=None):
else:
raise NotImplementedError


if __name__ == '__main__':
main()
7 changes: 7 additions & 0 deletions lexical-benchmark/etc/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
etc = os.path.dirname(os.path.realpath(__file__))
home = os.path.dirname(etc)


def parse_args(argv=None):
'''Create and parse our command line arguments.'''

Expand All @@ -42,6 +43,7 @@ def parse_args(argv=None):
)
return parser.parse_args(argv)


def filename(basename, args):
'''Get a resilient name for the benchmark data.'''

Expand All @@ -52,6 +54,7 @@ def filename(basename, args):
name = f'{name}_features={args.features}'
return name


@contextlib.contextmanager
def change_directory(path):
'''Change directory and return to the original directory afterwards.'''
Expand All @@ -63,6 +66,7 @@ def change_directory(path):
finally:
os.chdir(cwd)


def process_rust_benchmark(line):
'''Process the result of an individual Rust benchmark.'''

Expand All @@ -83,6 +87,7 @@ def process_rust_benchmark(line):

return group, name, speed


def run_benchmark(args):
'''Run a single benchmark.'''

Expand All @@ -109,6 +114,7 @@ def run_benchmark(args):

return data


def main(argv=None):
'''Entry point.'''

Expand All @@ -121,5 +127,6 @@ def main(argv=None):
with open(f'{home}/results/{filename(bench, args)}.json', 'w') as file:
json.dump(data, file)


if __name__ == '__main__':
main()
33 changes: 19 additions & 14 deletions lexical-parse-float/etc/correctness/test-parse-unittests/to_toml.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
# FLOAT HELPERS
# -------------


class FloatMixin:
'''Mixing for floating-point methods.'''

Expand Down Expand Up @@ -87,13 +88,13 @@ def mantissa(self):
class Float32(FloatMixin):
'''Wrapper around a 32-bit floating point value.'''

SIGN_MASK = np.uint32(0x80000000)
EXPONENT_MASK = np.uint32(0x7F800000)
HIDDEN_BIT_MASK = np.uint32(0x00800000)
MANTISSA_MASK = np.uint32(0x007FFFFF)
MANTISSA_SIZE = np.int32(23)
EXPONENT_BIAS = np.int32(127 + MANTISSA_SIZE)
DENORMAL_EXPONENT = np.int32(1 - EXPONENT_BIAS)
SIGN_MASK = np.uint32(0x80000000) # noqa
EXPONENT_MASK = np.uint32(0x7F800000) # noqa
HIDDEN_BIT_MASK = np.uint32(0x00800000) # noqa
MANTISSA_MASK = np.uint32(0x007FFFFF) # noqa
MANTISSA_SIZE = np.int32(23) # noqa
EXPONENT_BIAS = np.int32(127 + MANTISSA_SIZE) # noqa
DENORMAL_EXPONENT = np.int32(1 - EXPONENT_BIAS) # noqa

def __init__(self, value):
self.value = np.float32(value)
Expand All @@ -103,13 +104,13 @@ def __init__(self, value):
class Float64(FloatMixin):
'''Wrapper around a 64-bit floating point value.'''

SIGN_MASK = np.uint64(0x8000000000000000)
EXPONENT_MASK = np.uint64(0x7FF0000000000000)
HIDDEN_BIT_MASK = np.uint64(0x0010000000000000)
MANTISSA_MASK = np.uint64(0x000FFFFFFFFFFFFF)
MANTISSA_SIZE = np.int32(52)
EXPONENT_BIAS = np.int32(1023 + MANTISSA_SIZE)
DENORMAL_EXPONENT = np.int32(1 - EXPONENT_BIAS)
SIGN_MASK = np.uint64(0x8000000000000000) # noqa
EXPONENT_MASK = np.uint64(0x7FF0000000000000) # noqa
HIDDEN_BIT_MASK = np.uint64(0x0010000000000000) # noqa
MANTISSA_MASK = np.uint64(0x000FFFFFFFFFFFFF) # noqa
MANTISSA_SIZE = np.int32(52) # noqa
EXPONENT_BIAS = np.int32(1023 + MANTISSA_SIZE) # noqa
DENORMAL_EXPONENT = np.int32(1 - EXPONENT_BIAS) # noqa

def __init__(self, value):
self.value = np.float64(value)
Expand Down Expand Up @@ -180,6 +181,7 @@ def test_mantissa(self):
float32 = Float32("1e-45")
self.assertEqual(float32.mantissa(), np.uint32(1))


class TestFloat64(unittest.TestCase):

def test_to_bits(self):
Expand Down Expand Up @@ -249,6 +251,7 @@ def run_tests():
'''Run unittest suite.'''
unittest.main(argv=sys.argv[:1])


def create_test(test):
'''Create conversion test table.'''

Expand All @@ -263,6 +266,7 @@ def create_test(test):

return conversion_test


def main(source, destination):
'''Run main script.'''

Expand All @@ -284,6 +288,7 @@ def main(source, destination):
with open(destination, 'w') as fout:
print(tomlkit.dumps(document), file=fout)


if __name__ == '__main__':
args = parser.parse_args()
if args.test:
Expand Down
25 changes: 14 additions & 11 deletions lexical-parse-float/etc/powers_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,19 +81,19 @@ def print_large(radix, max_exp):
print(f'pub const LARGE_POW{radix}: [u32; {len(limb32)}] = [')
for value in limb32:
print(f' {value},')
print(f'];')
print(f'')
print('];')
print('')

print(f'/// Pre-computed large power-of-{radix} for 64-bit limbs.')
print('#[cfg(all(target_pointer_width = "64", not(target_arch = "sparc")))]')
print(f'pub const LARGE_POW{radix}: [u64; {len(limb64)}] = [')
for value in limb64:
print(f' {value},')
print(f'];')
print(f'')
print('];')
print('')
print(f'/// Step for large power-of-{radix} for 32-bit limbs.')
print(f'pub const LARGE_POW{radix}_STEP: u32 = {5 * max_exp};')
print(f'')
print('')


def print_tables(radix, f64_pow_limit, f32_exp_limit, f64_exp_limit):
Expand All @@ -106,11 +106,11 @@ def print_tables(radix, f64_pow_limit, f32_exp_limit, f64_exp_limit):

def f32_exponent_limit(radix):
return {
3 : (-15, 15),
5 : (-10, 10),
6 : (-15, 15),
7 : (-8, 8),
9 : (-7, 7),
3 : (-15, 15), # noqa
5 : (-10, 10), # noqa
6 : (-15, 15), # noqa
7 : (-8, 8), # noqa
9 : (-7, 7), # noqa
11: (-6, 6),
12: (-15, 15),
13: (-6, 6),
Expand Down Expand Up @@ -206,7 +206,10 @@ def f64_power_limit(radix):
}[radix]


radixes = [3, 5, 6, 7, 9, 11, 12, 13, 14, 15, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 33, 34, 35, 36]
radixes = [
3, 5, 6, 7, 9, 11, 12, 13, 14, 15, 17, 18, 19, 20, 21,
22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 33, 34, 35, 36
]
for radix in radixes:
f64_pow_limit = f64_power_limit(radix)
f32_exp_limit = f32_exponent_limit(radix)[1]
Expand Down
Loading

0 comments on commit 478caac

Please sign in to comment.