Compare commits

...

1 commit

Author SHA1 Message Date
19a36e120f needs more ram 2025-08-05 12:03:02 -05:00
2 changed files with 184 additions and 146 deletions

View file

@ -28,6 +28,7 @@
gnumake gnumake
readline.dev readline.dev
python3 python3
valgrind
]; ];
}; };
packages.default = pkgs.stdenv.mkDerivation rec { packages.default = pkgs.stdenv.mkDerivation rec {

329
main.c
View file

@ -189,19 +189,24 @@ static void parse_args(int argc, char* const* argv)
} }
} }
#define W 0b0101010101010101 #define V 0b01010101010101010101010101010101
#define X 0b0011001100110011 #define W 0b00110011001100110011001100110011
#define Y 0b0000111100001111 #define X 0b00001111000011110000111100001111
#define Z 0b0000000011111111 #define Y 0b00000000111111110000000011111111
#define M 0b1111111111111111 #define Z 0b00000000000000001111111111111111
#define M 0b11111111111111111111111111111111
#define N (65536) #define N (1UL << (1UL << 5UL))
#define NN (65536)
enum kind { enum kind {
ek_unreachable, ek_unreachable,
ek_0, ek_0,
ek_1, ek_1,
ek_V,
ek_W, ek_W,
ek_X, ek_X,
ek_Y, ek_Y,
@ -231,13 +236,15 @@ enum kind {
struct expr { struct expr {
enum kind kind; enum kind kind;
uint16_t cond, left, right; uint32_t cond, left, right;
int cost; int cost;
} lookup[N]; } lookup[N];
static void print(uint16_t truthtable, int depth) static void print(uint32_t truthtable, int depth)
{ {
assert(!"TODO");
#if 0
#define LITERAL_ESCAPE "\e[38;2;200;200;100m" #define LITERAL_ESCAPE "\e[38;2;200;200;100m"
#define VARIABLE_ESCAPE "\e[38;2;100;100;200m" #define VARIABLE_ESCAPE "\e[38;2;100;100;200m"
#define RESET_ESCAPE "\e[0m" #define RESET_ESCAPE "\e[0m"
@ -378,79 +385,102 @@ static void print(uint16_t truthtable, int depth)
break; break;
} }
} }
#endif
} }
// heap of truthtables; key = cost
struct todo {
uint32_t data[N];
uint32_t n;
// truthtable -> index in 'todo'
uint32_t reverse[N];
} *todo;
// create a list of the "done" truthtables
struct done {
uint64_t headtails[N], next[N], head;
} *done;
void calculate_simplifications(void) void calculate_simplifications(void)
{ {
// init 'lookup': // init 'lookup':
for (uint64_t i = 0; i < N; i++)
{ {
for (int i = 0; i < N; i++) if (i % 42949672 == 0)
{ {
lookup[i].kind = ek_unreachable; printf("init lookup: %.2f%%\n", (double) i * 100 / N);
lookup[i].cost = INT_MAX;
} }
lookup[i].kind = ek_unreachable;
lookup[i].cost = INT_MAX;
} }
// heap of truthtables; key = cost
struct {
uint16_t data[N];
int n;
// truthtable -> index in 'todo'
int reverse[N];
} todo;
// init 'todo': // init 'todo':
{ {
todo.n = 0; todo->n = 0;
for (int i = 0; i < N; i++) for (uint64_t i = 0; i < N; i++)
{ {
todo.reverse[i] = -1; if (i % 42949672 == 0)
{
printf("init todo: %.2f%%\n", (double) i * 100 / N);
}
todo->reverse[i] = (uint32_t) -1;
} }
} }
uint16_t pop(void) uint32_t pop(void)
{ {
assert(todo.n > 0); printf(" pop()\n");
uint16_t retval = todo.data[0]; assert(todo->n > 0);
todo.reverse[retval] = -1; uint32_t retval = todo->data[0];
uint16_t moving = todo.data[todo.n-- - 1]; todo->reverse[retval] = (uint32_t) -1;
uint32_t moving = todo->data[todo->n-- - 1];
int cost = lookup[moving].cost; int cost = lookup[moving].cost;
int index = 0; uint64_t index = 0;
again: again:
{ {
int left = index * 2 + 1; printf(" index = %lu\n", index);
int right = index * 2 + 2;
uint16_t smallest = moving; uint64_t left = index * 2 + 1;
uint64_t right = index * 2 + 2;
if (left < todo.n && lookup[todo.data[left]].cost < cost) uint32_t smallest = moving;
smallest = todo.data[left];
if (right < todo.n && lookup[todo.data[right]].cost < lookup[smallest].cost) if (left < todo->n && lookup[todo->data[left]].cost < cost)
smallest = todo.data[right]; smallest = todo->data[left];
if (right < todo->n && lookup[todo->data[right]].cost < lookup[smallest].cost)
smallest = todo->data[right];
if (smallest == moving) if (smallest == moving)
{ {
todo.data[index] = moving; todo->data[index] = moving;
todo.reverse[moving] = index; assert(index < 4294967296);
todo->reverse[moving] = (uint32_t) index;
} }
else else
{ {
int new = todo.reverse[smallest]; uint32_t new = todo->reverse[smallest];
todo.data[index] = smallest; todo->data[index] = smallest;
todo.reverse[smallest] = index; assert(index < 4294967296);
todo->reverse[smallest] = (uint32_t) index;
index = new; index = new;
@ -458,156 +488,163 @@ void calculate_simplifications(void)
} }
} }
printf(" return 0b%032b\n", retval);
return retval; return retval;
} }
void append(uint16_t truthtable, int cost) void append(uint32_t truthtable, int cost)
{ {
assert(todo.reverse[truthtable] == -1); printf("append(truthtable = 0b%032b, cost = %i)\n", truthtable, cost);
int index = todo.n++, new_index; assert(todo->reverse[truthtable] == (uint32_t) -1);
while (index > 0 && lookup[todo.data[new_index = (index - 1) / 2]].cost > cost) assert(todo->n < N);
uint32_t index = (uint32_t) todo->n++, new_index;
printf(" index = %i\n", index);
while (index > 0 && lookup[todo->data[new_index = (index - 1) / 2]].cost > cost)
{ {
todo.data[index] = todo.data[new_index]; todo->data[index] = todo->data[new_index];
todo.reverse[todo.data[new_index]] = index; todo->reverse[todo->data[new_index]] = index;
index = new_index; index = new_index;
printf(" index = %i\n", index);
} }
todo.data[index] = truthtable; todo->data[index] = truthtable;
todo.reverse[truthtable] = index; todo->reverse[truthtable] = index;
lookup[truthtable].cost = cost; lookup[truthtable].cost = cost;
} }
void update(uint16_t truthtable, int cost) void update(uint16_t truthtable, int cost)
{ {
assert(todo.reverse[truthtable] != -1); assert(!"TODO");
#if 0
assert(todo->reverse[truthtable] != -1);
assert(cost < lookup[truthtable].cost); assert(cost < lookup[truthtable].cost);
int index =todo. reverse[truthtable], new_index; int index =todo-> reverse[truthtable], new_index;
while (index > 0 && lookup[todo.data[new_index = (index - 1) / 2]].cost > cost) while (index > 0 && lookup[todo->data[new_index = (index - 1) / 2]].cost > cost)
{ {
todo.data[index] = todo.data[new_index]; todo->data[index] = todo->data[new_index];
todo.reverse[todo.data[new_index]] = index; todo->reverse[todo->data[new_index]] = index;
index = new_index; index = new_index;
} }
todo.data[index] = truthtable; todo->data[index] = truthtable;
todo.reverse[truthtable] = index; todo->reverse[truthtable] = index;
lookup[truthtable].cost = cost; lookup[truthtable].cost = cost;
}
// create a list of the "done" truthtables
struct {
int headtails[N], next[N], head;
// for debugging:
#if ZDEBUG
bool in[N];
#endif #endif
} done = {}; }
// init 'done': // init 'done':
{ {
done.head = -1; done->head = (uint64_t) -1;
for (int i = 0; i < N; i++) for (uint64_t i = 0; i < N; i++)
{ {
done.headtails[i] = -1; done->headtails[i] = (uint64_t) -1;
#if ZDEBUG if (i % 42949672 == 0)
done.in[i] = false; {
#endif printf("init done: %.2f%%\n", (double) i * 100 / N);
}
} }
} }
void insert(int index) void insert(uint32_t index)
{ {
#if ZDEBUG printf(" insert(0b%032b)\n", index);
assert(!done.in[index]);
#endif
int head = index & 1 ? index & ~(index & -index) : index, prevhead = -1; assert(!"TODO");
#if 0
uint32_t head = index & 1 ? index & ~(index & -index) : index;
while (done.headtails[head] == -1 || index < done.headtails[head]) uint32_t prevhead = (uint32_t) -1;
while (false
|| done->headtails[head] == (uint32_t) -1
|| index < done->headtails[head])
{ {
done.headtails[head] = index; done->headtails[head] = index;
prevhead = head, head = head & ~(head & -head); prevhead = head, head = head & ~(head & -head);
} }
if (done.headtails[head] < index) printf(" head = %lu\n", head);
{
if (prevhead == -1)
{
assert(done.headtails[head] == head);
done.next[head] = index; if (done->headtails[head] < index)
{
if (prevhead == (uint32_t) -1)
{
assert(done->headtails[head] == head);
done->next[head] = index;
} }
else else
{ {
int tophalftail = done.headtails[prevhead - 1]; uint32_t tophalftail = done->headtails[prevhead - 1];
assert(tophalftail != -1); assert(tophalftail != (uint32_t) -1);
done.next[tophalftail] = index; done->next[tophalftail] = index;
} }
} }
else else
{ {
done.head = index; done->head = index;
} }
int n = ~index & M; uint32_t n = ~index & M;
int tail = index & 1 ? index : index | (n & -n), prevtail = -1; uint32_t tail = index & 1 ? index : index | (n & -n);
uint32_t prevtail = (uint32_t) -1;
while (done.headtails[tail] == -1 || done.headtails[tail] < index) while (false
|| done->headtails[tail] == (uint32_t) -1
|| done->headtails[tail] < index)
{ {
done.headtails[tail] = index; done->headtails[tail] = index;
prevtail = tail, tail = tail | (n = ~tail & M, n & -n); prevtail = tail, tail = tail | (n = ~tail & M, n & -n);
} }
if (index < done.headtails[tail]) printf(" tail = %lu\n", tail);
if (index < done->headtails[tail])
{ {
if (prevtail == -1) if (prevtail == (uint32_t) -1)
{ {
assert(done.headtails[tail] == tail); assert(done->headtails[tail] == tail);
#if ZDEBUG done->next[index] = tail;
assert(done.in[tail]);
#endif
done.next[index] = tail;
} }
else else
{ {
int bottomhalfhead = done.headtails[prevtail + 1]; int bottomhalfhead = done->headtails[prevtail + 1];
assert(bottomhalfhead != -1); assert(bottomhalfhead != (uint32_t) -1);
done.next[index] = bottomhalfhead; done->next[index] = bottomhalfhead;
} }
} }
else else
{ {
done.next[index] = -1; done->next[index] = (uint32_t) -1;
} }
#if ZDEBUG
done.in[index] = true;
#endif #endif
} }
append(V, 0), lookup[V].kind = ek_V;
append(W, 0), lookup[W].kind = ek_W; append(W, 0), lookup[W].kind = ek_W;
append(X, 0), lookup[X].kind = ek_X; append(X, 0), lookup[X].kind = ek_X;
append(Y, 0), lookup[Y].kind = ek_Y; append(Y, 0), lookup[Y].kind = ek_Y;
@ -616,38 +653,24 @@ void calculate_simplifications(void)
append(0, 1), lookup[0].kind = ek_0; append(0, 1), lookup[0].kind = ek_0;
append(M, 1), lookup[M].kind = ek_1; append(M, 1), lookup[M].kind = ek_1;
// disable terminal autowrap: for (uint64_t iterations = 1; iterations <= N; iterations++)
if (verbose && print_with_color)
{ {
printf("\e[?7l"); printf("iterations = %lu\n", iterations);
}
for (int iterations = 1; todo.n && iterations <= N; iterations++) assert(todo->n);
{
uint16_t truthtable = pop(); uint32_t truthtable = pop();
insert(truthtable); insert(truthtable);
int cost = lookup[truthtable].cost; int cost = lookup[truthtable].cost;
printf("%lu of %lu (%.2f%%): [%i] ",
iterations, N, (100.0 * (double) iterations / N), cost);
if (verbose) print(truthtable, 0), puts("");
{
if (print_with_color)
{
printf("\e[2K");
}
printf("%i of %i (%.2f%%): [%i] ",
iterations, N, (100.0 * iterations / N), cost);
print(truthtable, 0), puts("");
if (print_with_color)
{
printf("\e[1A");
}
}
assert(!"TODO");
#if 0
// consider NOT: // consider NOT:
if (use_operators.not) if (use_operators.not)
{ {
@ -657,7 +680,7 @@ void calculate_simplifications(void)
if (not_cost < lookup[not_truthtable].cost) if (not_cost < lookup[not_truthtable].cost)
{ {
if (todo.reverse[not_truthtable] == -1) if (todo->reverse[not_truthtable] == -1)
{ {
append(not_truthtable, not_cost); append(not_truthtable, not_cost);
} }
@ -673,7 +696,7 @@ void calculate_simplifications(void)
#define BINARY_OPERATOR(ekind, function) \ #define BINARY_OPERATOR(ekind, function) \
{ \ { \
for (int i = done.head; i != -1; i = done.next[i]) \ for (int i = done->head; i != -1; i = done->next[i]) \
{ \ { \
{ \ { \
uint16_t bin_truthtable = function(truthtable, i) & M; \ uint16_t bin_truthtable = function(truthtable, i) & M; \
@ -682,7 +705,7 @@ void calculate_simplifications(void)
\ \
if (bin_cost < lookup[bin_truthtable].cost) \ if (bin_cost < lookup[bin_truthtable].cost) \
{ \ { \
if (todo.reverse[bin_truthtable] == -1) \ if (todo->reverse[bin_truthtable] == -1) \
append(bin_truthtable, bin_cost); \ append(bin_truthtable, bin_cost); \
else \ else \
update(bin_truthtable, bin_cost); \ update(bin_truthtable, bin_cost); \
@ -700,7 +723,7 @@ void calculate_simplifications(void)
\ \
if (bin_cost < lookup[bin_truthtable].cost) \ if (bin_cost < lookup[bin_truthtable].cost) \
{ \ { \
if (todo.reverse[bin_truthtable] == -1) \ if (todo->reverse[bin_truthtable] == -1) \
append(bin_truthtable, bin_cost); \ append(bin_truthtable, bin_cost); \
else \ else \
update(bin_truthtable, bin_cost); \ update(bin_truthtable, bin_cost); \
@ -790,9 +813,9 @@ void calculate_simplifications(void)
if (use_operators.ternary) if (use_operators.ternary)
{ {
for (int i = done.head; i != -1; i = done.next[i]) for (int i = done->head; i != -1; i = done->next[i])
{ {
for (int j = done.head; j != -1; j = done.next[j]) for (int j = done->head; j != -1; j = done->next[j])
{ {
int ternary_cost = 1 + cost + lookup[i].cost + lookup[j].cost; int ternary_cost = 1 + cost + lookup[i].cost + lookup[j].cost;
@ -803,7 +826,7 @@ void calculate_simplifications(void)
\ \
if (ternary_cost < lookup[ternary_truthtable].cost) \ if (ternary_cost < lookup[ternary_truthtable].cost) \
{ \ { \
if (todo.reverse[ternary_truthtable] == -1) \ if (todo->reverse[ternary_truthtable] == -1) \
{ \ { \
append(ternary_truthtable, ternary_cost); \ append(ternary_truthtable, ternary_cost); \
} \ } \
@ -825,17 +848,13 @@ void calculate_simplifications(void)
} }
} }
} }
} #endif
if (verbose && print_with_color)
{
printf("\e[2K" "\e[?7h");
} }
} }
void get_simplifications(void) void get_simplifications(void)
{ {
char path[PATH_MAX] = ".simplifier-cache-2"; char path[PATH_MAX] = ".simplifier-cache-5-var";
if (use_operators.not) if (use_operators.not)
strcat(path, "-not"); strcat(path, "-not");
@ -948,6 +967,8 @@ void get_simplifications(void)
uint16_t evaluate(const char* text) uint16_t evaluate(const char* text)
{ {
assert(!"TODO");
#if 0
enum { enum {
tk_uninitialized, tk_uninitialized,
@ -1435,16 +1456,22 @@ uint16_t evaluate(const char* text)
} }
return truthtable; return truthtable;
#endif
} }
int main(int argc, char* const* argv) int main(int argc, char* const* argv)
{ {
parse_args(argc, argv); parse_args(argc, argv);
todo = malloc(sizeof(*todo));
done = malloc(sizeof(*done));
get_simplifications(); get_simplifications();
if (print_all_and_quit) if (print_all_and_quit)
{ {
assert(!"TODO");
#if 0
for (int i = 0; i < N; i++) for (int i = 0; i < N; i++)
{ {
int cost = lookup[i].cost; int cost = lookup[i].cost;
@ -1454,9 +1481,12 @@ int main(int argc, char* const* argv)
printf("0b%016b: [%2i]: ", i, cost), print(i, 0), puts(""); printf("0b%016b: [%2i]: ", i, cost), print(i, 0), puts("");
} }
} }
#endif
} }
else if (print_stats) else if (print_stats)
{ {
assert(!"TODO");
#if 0
printf("statistics:" "\n"); printf("statistics:" "\n");
uintmax_t total_cost = 0; uintmax_t total_cost = 0;
@ -1509,9 +1539,12 @@ int main(int argc, char* const* argv)
{ {
puts("everything unreachable: no statistics to give"); puts("everything unreachable: no statistics to give");
} }
#endif
} }
else if (command) else if (command)
{ {
assert(!"TODO");
#if 0
uint16_t truthtable = evaluate(command); uint16_t truthtable = evaluate(command);
// printf("truthtable = 0b%016b\n", truthtable); // printf("truthtable = 0b%016b\n", truthtable);
@ -1524,9 +1557,12 @@ int main(int argc, char* const* argv)
{ {
printf("%2i: ", lookup[truthtable].cost), print(truthtable, 0), puts(""); printf("%2i: ", lookup[truthtable].cost), print(truthtable, 0), puts("");
} }
#endif
} }
else else
{ {
assert(!"TODO");
#if 0
if (!quiet) if (!quiet)
{ {
puts("Use C-style syntax for boolean operators and expressions."); puts("Use C-style syntax for boolean operators and expressions.");
@ -1556,6 +1592,7 @@ int main(int argc, char* const* argv)
print(truthtable, 0), puts(""); print(truthtable, 0), puts("");
} }
} }
#endif
} }
return 0; return 0;