From 5b4c1d02ce9108be74e60e9993254a71c5fa9318 Mon Sep 17 00:00:00 2001 From: Thomas Voss Date: Sun, 28 Apr 2024 09:11:55 +0200 Subject: Move out functions into lib.py --- gen/prop/bool-props.py | 53 ++++---------------------------------------------- gen/prop/lib.py | 47 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 49 deletions(-) create mode 100644 gen/prop/lib.py (limited to 'gen/prop') diff --git a/gen/prop/bool-props.py b/gen/prop/bool-props.py index a913904..32a293e 100755 --- a/gen/prop/bool-props.py +++ b/gen/prop/bool-props.py @@ -1,55 +1,10 @@ #!/usr/bin/python3 -import functools import math import sys -from typing import Generator - - -def chunks[T](xs: list[T], n: int) -> Generator[list[T], None, None]: - for i in range(0, len(xs), n): - yield xs[i:i + n] - -def powers_of_2() -> Generator[int, None, None]: - i = 0 - while True: - yield 2 ** i - i += 1 - -def bytes_per_col(n: int) -> int: - xs = list(set(functools.reduce(list.__add__, ( - [i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0) - ))) - for x in sorted(xs, reverse=True): - y = 5 - y += x * 5 - y += x - 1 - if y <= 80: - return x - - raise ValueError - -def isize(x: int) -> int: - if x < 256: - return 1 - if x < 65535: - return 2 - if x < 4294967295: - return 3 - if x < 18446744073709551615: - return 4 - raise ValueError - -def typename(x: int) -> str: - if x < 256: - return "uint8_t" - if x < 65535: - return "uint16_t" - if x < 4294967295: - return "uint32_t" - if x < 18446744073709551615: - return "uint64_t" - raise ValueError + +from lib import * + def parse(file: str) -> list[bool]: xs = [False] * 0x110000 @@ -89,7 +44,7 @@ def genfile(cs: list[tuple[bool, ...]], blksize: int) -> None: print() bcnt = blksize // 8 - bpc = bytes_per_col(bcnt) + bpc = columns(bcnt) print(f'static constexpr unsigned char stage2[][{bcnt}] = {{') for c in cs: x = sum(map(lambda x: x[1] << x[0], enumerate(c))) diff --git a/gen/prop/lib.py b/gen/prop/lib.py new file mode 100644 index 0000000..b4c7e1e --- /dev/null +++ b/gen/prop/lib.py @@ -0,0 +1,47 @@ +import functools +from typing import Generator + +def chunks[T](xs: list[T], n: int) -> Generator[list[T], None, None]: + for i in range(0, len(xs), n): + yield xs[i:i + n] + +def powers_of_2() -> Generator[int, None, None]: + i = 0 + while True: + yield 2 ** i + i += 1 + +def columns(n: int) -> int: + xs = list(set(functools.reduce(list.__add__, ( + [i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0) + ))) + for x in sorted(xs, reverse=True): + y = 5 + y += x * 5 + y += x - 1 + if y <= 80: + return x + + raise ValueError + +def isize(x: int) -> int: + if x < 256: + return 1 + if x < 65535: + return 2 + if x < 4294967295: + return 3 + if x < 18446744073709551615: + return 4 + raise ValueError + +def typename(x: int) -> str: + if x < 256: + return "uint8_t" + if x < 65535: + return "uint16_t" + if x < 4294967295: + return "uint32_t" + if x < 18446744073709551615: + return "uint64_t" + raise ValueError -- cgit v1.2.3