From 055f35e74601bea2223a1555c15b029ee90a9117 Mon Sep 17 00:00:00 2001 From: Comrade Martin Miroslavovich Michalec Date: Sat, 25 Nov 2023 21:59:53 +0300 Subject: publish --- src/bdd.c | 50 +++++++++++++++++++++++++++++++ src/bdd.h | 9 ++++++ src/main.c | 97 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/pnf.c | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/pnf.h | 22 ++++++++++++++ src/pzh.c | 75 +++++++++++++++++++++++++++++++++++++++++++++++ src/pzh.h | 16 ++++++++++ src/utils.c | 58 ++++++++++++++++++++++++++++++++++++ src/utils.h | 40 +++++++++++++++++++++++++ 9 files changed, 451 insertions(+) create mode 100644 src/bdd.c create mode 100644 src/bdd.h create mode 100644 src/main.c create mode 100644 src/pnf.c create mode 100644 src/pnf.h create mode 100644 src/pzh.c create mode 100644 src/pzh.h create mode 100644 src/utils.c create mode 100644 src/utils.h (limited to 'src') diff --git a/src/bdd.c b/src/bdd.c new file mode 100644 index 0000000..a4ef15b --- /dev/null +++ b/src/bdd.c @@ -0,0 +1,50 @@ +#include "bdd.h" + +enum node { + Z1 = 0b000, + Y1 = 0b001, + Y2 = 0b010, + X1 = 0b011, + X2 = 0b100, + W1 = 0b101, + W2 = 0b110, + TT = 0b111, +}; + +enum var { + X = 0b11, + Y = 0b10, + Z = 0b01, + W = 0b00, +}; + +#define PACK(B0, B1, VAR) ((B0) | (B1) << 3 | (VAR) << 6) +static u8 bdd[] = { + // 0 1 var + [Z1] = PACK (Y1, Y2, Z), + [Y1] = PACK (Y1, X1, Y), + [Y2] = PACK (X2, W2, Y), + [X1] = PACK (W2, W1, X), + [X2] = PACK (TT, W2, X), + [W1] = PACK (TT, W1, W), + [W2] = PACK (W2, TT, W), +}; + +#define UNPACK_BRANCH1(ROW) (bdd[ROW] >> 3 & 0b111) +#define UNPACK_BRANCH0(ROW) (bdd[ROW] >> 0 & 0b111) +#define UNPACK_VARMASK(ROW) (1 << (bdd[ROW] >> 6)) +bool +bdd_eval (u8 xyzw) +{ + enum node curr = 0; + enum node prev; + + loop: + prev = curr; + curr = xyzw & UNPACK_VARMASK (curr) + ? UNPACK_BRANCH1 (curr) + : UNPACK_BRANCH0 (curr); + if (curr == prev) return false; + if (curr == TT ) return true; + goto loop; +} diff --git a/src/bdd.h b/src/bdd.h new file mode 100644 index 0000000..d242acf --- /dev/null +++ b/src/bdd.h @@ -0,0 +1,9 @@ +#ifndef BDD_H +#define BDD_H + +#include +#include "utils.h" + +bool bdd_eval (u8 xyzw); + +#endif diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..23a7b85 --- /dev/null +++ b/src/main.c @@ -0,0 +1,97 @@ +#include +#include +#include + +#include "utils.h" + +#include "bdd.h" +#include "pnf.h" +#include "pzh.h" + +#define REPL(PROMPT, EVAL) \ + for (;;) { \ + char *xyzw_str; \ + \ + while (!input ((PROMPT), "^([01]{4}|done)$", "%ms", &xyzw_str)) \ + puts ("Ввод не соответствует регулярному выражению!"); \ + \ + if (!strcmp (xyzw_str, "done")) \ + break; \ + \ + u8 xyzw = strtol (xyzw_str, NULL, 2); \ + free (xyzw_str); \ + \ + printf ("> %b\n", (EVAL)); \ + } + +#define CHECK(NAME, EVAL) ({ \ + puts ("Проверка:"); \ + puts ("xyzw | f | "NAME" f"); \ + bool is_correct = true; \ + for (u8 xyzw = 0b0000; xyzw <= 0b1111; ++xyzw) { \ + bool res_f = f << xyzw >> 0b1111 & 1; \ + bool res_pdnf = pdnf_eval (pdnf, POPCOUNT (f), xyzw); \ + if (res_f != res_pdnf) \ + is_correct = false; \ + printf ("%04b | %c | %c\n", \ + xyzw, \ + '0' + res_f, \ + '0' + res_pdnf); \ + } puts (is_correct ? "Проверка сошлась!" : "Проверка не сошлась!");}) + +u16 f = 0b0011010100011001; + +static void +section_f (void) +{ + printf ("f = (%016b)\n", f); +} + +static void +section_bdd (void) +{ + REPL ("Введите xyzw для вычисления значения функции f по БДР, или done, " + "чтобы перейти к следующему пункту", bdd_eval (xyzw)); +} + +static void +section_pnf (void) +{ + u64 pdnf = pdnf_compute (f); + puts ("СДНФ f:"); + pdnf_print (pdnf, POPCOUNT (f)); + CHECK ("СДНФ", pdnf_eval (pdnf, POPCOUNT (f), xyzw)); + NEWLINE (); + + u64 pcnf = pcnf_compute (f); + puts ("СКНФ f:"); + pcnf_print (pcnf, 16 - POPCOUNT (f)); + CHECK ("СКНФ", pcnf_eval (pcnf, 16 - POPCOUNT (f), xyzw)); + NEWLINE (); + + REPL ("Введите xyzw для вычисления значения функции f по СДНФ, или done, " + "чтобы перейти к следующему пункту", pdnf_eval (pdnf, POPCOUNT (f), xyzw)); +} + +static void +section_pzh (void) +{ + u16 pzh = pzh_compute (f); + puts ("Полином Жигалкина:"); + pzh_print (pzh); + NEWLINE (); + + REPL ("Введите xyzw для вычисления значения функции f по ПЖ, или done, " + "чтобы завершить программу", pzh_eval (pzh, xyzw)); +} + +int +main (void) +{ + section_f (); PAGEBREAK (); + section_bdd (); PAGEBREAK (); + section_pnf (); PAGEBREAK (); + section_pzh (); + + return EXIT_SUCCESS; +} diff --git a/src/pnf.c b/src/pnf.c new file mode 100644 index 0000000..773ace7 --- /dev/null +++ b/src/pnf.c @@ -0,0 +1,84 @@ +#include "pnf.h" +#include + +u64 +pdnf_compute (u16 f) +{ + u64 pdnf = 0; + u8 xyzw = 0b1111; + + while (f) { + xyzw -= CTZ (f); + f >>= CTZ (f); + + pdnf <<= 4; + pdnf |= xyzw; + + xyzw -= 1; + f >>= 1; + } + + return pdnf; +} + +void +pdnf_print (u64 pdnf, u8 numones) +{ + for (u8 i = numones; i; --i) { + printf ("%cx && %cy && %cz && %cw%s\n", + pdnf & 1 << 3 ? ' ' : '!', + pdnf & 1 << 2 ? ' ' : '!', + pdnf & 1 << 1 ? ' ' : '!', + pdnf & 1 << 0 ? ' ' : '!', + i > 1 ? " ||" : ""); + pdnf >>= 4; + } +} + +u64 +pcnf_compute (u16 f) +{ + u64 pcnf = 0; + u8 xyzw = 0b1111; + + f = ~f; + while (f) { + xyzw -= CTZ (f); + f >>= CTZ (f); + + pcnf <<= 4; + pcnf |= xyzw; + + xyzw -= 1; + f >>= 1; + } + + return ~pcnf; +} + +void +pcnf_print (u64 pcnf, u8 numzeros) +{ + for (u8 i = numzeros; i; --i) { + printf ("(%cx || %cy || %cz || %cw)%s\n", + pcnf & 1 << 3 ? ' ' : '!', + pcnf & 1 << 2 ? ' ' : '!', + pcnf & 1 << 1 ? ' ' : '!', + pcnf & 1 << 0 ? ' ' : '!', + i > 1 ? " &&" : ""); + pcnf >>= 4; + } +} + +bool +pdnf_eval (u64 pdnf, u8 numones, u8 xyzw) +{ + for (u8 i = 0; i < numones; ++i) { + u8 mask = pdnf & (1 << 4) - 1; + if (xyzw == mask) + return true; + pdnf >>= 4; + } + + return false; +} diff --git a/src/pnf.h b/src/pnf.h new file mode 100644 index 0000000..ccac3f4 --- /dev/null +++ b/src/pnf.h @@ -0,0 +1,22 @@ +#ifndef PNF_H +#define PNF_H + +#include +#include "utils.h" + +u64 +pdnf_compute (u16 f); + +void +pdnf_print (u64 pdnf, u8 numones); + +u64 +pcnf_compute (u16 f); + +void +pcnf_print (u64 pcnf, u8 numzeros); + +bool +pdnf_eval (u64 pdnf, u8 numones, u8 xyzw); + +#endif diff --git a/src/pzh.c b/src/pzh.c new file mode 100644 index 0000000..73a536c --- /dev/null +++ b/src/pzh.c @@ -0,0 +1,75 @@ +#include "pzh.h" +#include + +// Разворачивается в 92 инструкции исключительно над регистрами! +u16 +pzh_compute (u16 f) +{ + u16 pzh = 0; + + for (u16 i = 1; i; i <<= 1) { + pzh >>= 1; + pzh |= f & 1 << 8*sizeof f - 1; + f ^= f << 1; + } + + return pzh; +} + +void +pzh_print (u16 pzh) +{ + if (!pzh) { + puts ("1 != 1"); + return; + } + + for (u16 i = 1; i; i <<= 1) { + if (pzh & i) { + u8 n = CTZ (i); + switch (POPCOUNT (n)) { + case 0: + printf ("1"); + break; + case 1: + printf ("%c", "wzyx"[CTZ (n)]); + break; + case 2: + printf ("(%c && %c)", + "xyz"[CLZ (n) - (1 << sizeof (CLZ (n)) + 1) + 4], + "wzy"[CTZ (n)]); + break; + case 3: + printf ("(%c && %c && %c)", + "xy"[CLZ (n) - (1 << sizeof (CLZ (n)) + 1) + 4], + "zy"[CTZ (n)], + "wz"[CTZ (n)]); + break; + case 4: + printf ("(x && y && z && w)"); + break; + } + printf ("%s", pzh & ~((i << 1) - 1) ? " + " : "\n"); + } + } +} + + +// 25, hot 13 +bool +pzh_eval (u16 pzh, u8 xyzw) +{ + bool res = 0; + + for (u8 i = 0; pzh;) { + i += CTZ (pzh); + pzh >>= CTZ (pzh); + + res ^= (i & xyzw) == i; + + i += 1; + pzh >>= 1; + } + + return res; +} diff --git a/src/pzh.h b/src/pzh.h new file mode 100644 index 0000000..e2b1fc6 --- /dev/null +++ b/src/pzh.h @@ -0,0 +1,16 @@ +#ifndef PZH_H +#define PZH_H + +#include +#include "utils.h" + +u16 +pzh_compute (u16 f); + +void +pzh_print (u16 pzh); + +bool +pzh_eval (u16 pzh, u8 xyzw); + +#endif diff --git a/src/utils.c b/src/utils.c new file mode 100644 index 0000000..574a80e --- /dev/null +++ b/src/utils.c @@ -0,0 +1,58 @@ +#include "utils.h" +#include +#include +#include +#include +#include + +int +input (const char *message, const char *regex, const char *template, ...) +{ + char *line = NULL; + size_t line_size = 0; + regex_t compiled; + int matches = 0; + int c; + va_list ap; + + va_start (ap, template); + vprintf (message, ap); + printf (" (\"%s\"): ", regex); + getline (&line, &line_size, stdin); + line[strlen (line) - 1] = '\0'; + + regcomp (&compiled, regex, REG_EXTENDED | REG_NOSUB); + if (regexec (&compiled, line, 0, NULL, 0)) + goto defer; + + matches = vsscanf (line, template, ap); + va_end (ap); + + defer: + free (line); + return matches; +} + +uint +log10ui (uint ui) +{ + return ((ui >= 1000000000u) ? 9 : + (ui >= 100000000u) ? 8 : + (ui >= 10000000u) ? 7 : + (ui >= 1000000u) ? 6 : + (ui >= 100000u) ? 5 : + (ui >= 10000u) ? 4 : + (ui >= 1000u) ? 3 : + (ui >= 100u) ? 2 : + (ui >= 10u) ? 1 : 0u); +} + +u16 +rev16 (u16 b) +{ + b = (b & 0xFF00) >> 8 | (b & 0x00FF) << 8; + b = (b & 0xF0F0) >> 4 | (b & 0x0F0F) << 4; + b = (b & 0xCCCC) >> 2 | (b & 0x3333) << 2; + b = (b & 0xAAAA) >> 1 | (b & 0x5555) << 1; + return b; +} diff --git a/src/utils.h b/src/utils.h new file mode 100644 index 0000000..8cdbe46 --- /dev/null +++ b/src/utils.h @@ -0,0 +1,40 @@ +#ifndef UTILS_H +#define UTILS_H + +#include + +#define MAX(a, b) ({ \ + __typeof__ (a) _a = (a); \ + __typeof__ (b) _b = (b); \ + _a > _b ? _a : _b; \ + }) + +#define MIN(a, b) ({ \ + __typeof__ (a) _a = (a); \ + __typeof__ (b) _b = (b); \ + _a < _b ? _a : _b; \ + }) + +#define CLZ(X) __builtin_clz (X) +#define CTZ(X) __builtin_ctz (X) +#define FFS(X) __builtin_ffs (X) +#define POPCOUNT(X) __builtin_popcount (X) + +#define PAGEBREAK() puts ("\n ") +#define NEWLINE() putchar ('\n') + +typedef unsigned int uint; +typedef uint8_t u8; +typedef uint16_t u16; +typedef uint64_t u64; + +int +input (const char *message, const char *regex, const char *template, ...); + +uint +log10ui (uint ui); + +u16 +rev16 (u16 b); + +#endif -- cgit v1.3.1