added c implementation, which is MUCH faster!

This commit is contained in:
Zander Thannhauser 2025-06-10 20:51:47 -05:00
parent aeee25889e
commit 453cd8af9c
4 changed files with 1241 additions and 17 deletions

2
.gitignore vendored
View file

@ -1,3 +1,5 @@
simplifications.bin simplifications.bin
.simplifier-*.bin

1190
main.c Normal file

File diff suppressed because it is too large Load diff

46
main.py
View file

@ -21,6 +21,13 @@ def parse_args(argv):
This options allows use of: nor, nand, xor, nxor, andn, orn. This options allows use of: nor, nand, xor, nxor, andn, orn.
''') ''')
parser.add_argument('-E', '--extra-extended-operators', action='store_true', help='''
In addition to the operators of '--extended-operators' introduce
the ternary ('?:') operator.
This operator is very powerful but dramatically increases
simplification time by an nth degree.
''')
parser.add_argument('-o', '--custom-operators', help=''' parser.add_argument('-o', '--custom-operators', help='''
Pick and choose which operators the simplifier can use. Comma seperated. Pick and choose which operators the simplifier can use. Comma seperated.
You can refer to them using their names or their symbols. You can refer to them using their names or their symbols.
@ -91,6 +98,8 @@ def determine_available_operators(args):
raise BaseException(f"not an operator: '{o}'"); raise BaseException(f"not an operator: '{o}'");
return available_operators; return available_operators;
elif args.extra_extended_operators:
return set(standard + extended + ("?:", ));
else: else:
return set(standard); return set(standard);
@ -305,9 +314,10 @@ def calculate_simplifications(args, available_operators):
# consider unary operators: # consider unary operators:
for name, function in sorted(unary_operators.items()): for name, function in sorted(unary_operators.items()):
if name in available_operators: if name in available_operators:
unary_cost = my_cost + 1;
unary_truthtable = function(my_truthtable) & M; unary_truthtable = function(my_truthtable) & M;
unary_expression = (name, my_expression); unary_expression = (name, my_expression);
unary_cost = my_cost + 1;
consider(unary_truthtable, unary_expression, unary_cost); consider(unary_truthtable, unary_expression, unary_cost);
@ -315,11 +325,12 @@ def calculate_simplifications(args, available_operators):
for name, function in sorted(binary_operators.items()): for name, function in sorted(binary_operators.items()):
if name in available_operators: if name in available_operators:
for other_truthtable, other_expression in sorted(lookup.items()): for other_truthtable, other_expression in sorted(lookup.items()):
binary_cost = my_cost + 1 + costs[other_truthtable];
# x + y # x + y
binary_truthtable = function(my_truthtable, other_truthtable); binary_truthtable = function(my_truthtable, other_truthtable);
binary_truthtable = binary_truthtable & M binary_truthtable = binary_truthtable & M
binary_expression = (name, my_expression, other_expression); binary_expression = (name, my_expression, other_expression);
binary_cost = my_cost + 1 + costs[other_truthtable];
consider(binary_truthtable, binary_expression, binary_cost); consider(binary_truthtable, binary_expression, binary_cost);
@ -327,7 +338,6 @@ def calculate_simplifications(args, available_operators):
binary_truthtable = function(other_truthtable, my_truthtable); binary_truthtable = function(other_truthtable, my_truthtable);
binary_truthtable = binary_truthtable & M binary_truthtable = binary_truthtable & M
binary_expression = (name, other_expression, my_expression); binary_expression = (name, other_expression, my_expression);
binary_cost = my_cost + 1 + costs[other_truthtable];
consider(binary_truthtable, binary_expression, binary_cost); consider(binary_truthtable, binary_expression, binary_cost);
@ -335,25 +345,27 @@ def calculate_simplifications(args, available_operators):
for name, function in sorted(ternary_operators.items()): for name, function in sorted(ternary_operators.items()):
if name in available_operators: if name in available_operators:
s = sorted(lookup.items()); s = sorted(lookup.items());
for i, (a_truthtable, a_expression) in enumerate(s): for a_truthtable, a_expression in s:
# print(f'i = {i}');
for b_truthtable, b_expression in s: for b_truthtable, b_expression in s:
# x ? y : z ternary_cost = 1 + my_cost + costs[a_truthtable] + costs[b_truthtable];
ternary_truthtable = function(
my_truthtable, a_truthtable, b_truthtable) & M;
ternary_expression = (name,
my_expression, a_expression, b_expression);
ternary_cost = 1 + my_cost + \
costs[a_truthtable] + costs[b_truthtable];
consider(ternary_truthtable, ternary_expression, \ # x ? y : z
ternary_cost); ternary_truthtable = function(my_truthtable, a_truthtable, b_truthtable) & M;
ternary_expression = (name, my_expression, a_expression, b_expression);
consider(ternary_truthtable, ternary_expression, ternary_cost);
# y ? x : z # y ? x : z
# assert(not "TODO"); ternary_truthtable = function(a_truthtable, my_truthtable, b_truthtable) & M;
ternary_expression = (name, a_expression, my_expression, b_expression);
# z ? y : z consider(ternary_truthtable, ternary_expression, ternary_cost);
# assert(not "TODO");
# y ? z : x
ternary_truthtable = function(a_truthtable, b_truthtable, my_truthtable) & M;
ternary_expression = (name, a_expression, b_expression, my_expression);
consider(ternary_truthtable, ternary_expression, ternary_cost);
return costs, lookup return costs, lookup

20
makefile Normal file
View file

@ -0,0 +1,20 @@
# vim: noexpandtab tabstop=4 :
args =
cc = gcc
cppflags = -D _GNU_SOURCE
cflags = -Werror -Wall -Wextra -Wstrict-prototypes
cflags += -Wno-unused
ldflags += -lreadline
/tmp/4-variable-simplifier: main.c
$(cc) $(cppflags) $(cflags) $< -o $@ $(ldflags)
run: /tmp/4-variable-simplifier
$< $(args)