diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/bdd.c | 50 | ||||
| -rw-r--r-- | src/bdd.h | 9 | ||||
| -rw-r--r-- | src/main.c | 97 | ||||
| -rw-r--r-- | src/pnf.c | 84 | ||||
| -rw-r--r-- | src/pnf.h | 22 | ||||
| -rw-r--r-- | src/pzh.c | 75 | ||||
| -rw-r--r-- | src/pzh.h | 16 | ||||
| -rw-r--r-- | src/utils.c | 58 | ||||
| -rw-r--r-- | src/utils.h | 40 |
9 files changed, 451 insertions, 0 deletions
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 @@ | |||
| 1 | #include "bdd.h" | ||
| 2 | |||
| 3 | enum node { | ||
| 4 | Z1 = 0b000, | ||
| 5 | Y1 = 0b001, | ||
| 6 | Y2 = 0b010, | ||
| 7 | X1 = 0b011, | ||
| 8 | X2 = 0b100, | ||
| 9 | W1 = 0b101, | ||
| 10 | W2 = 0b110, | ||
| 11 | TT = 0b111, | ||
| 12 | }; | ||
| 13 | |||
| 14 | enum var { | ||
| 15 | X = 0b11, | ||
| 16 | Y = 0b10, | ||
| 17 | Z = 0b01, | ||
| 18 | W = 0b00, | ||
| 19 | }; | ||
| 20 | |||
| 21 | #define PACK(B0, B1, VAR) ((B0) | (B1) << 3 | (VAR) << 6) | ||
| 22 | static u8 bdd[] = { | ||
| 23 | // 0 1 var | ||
| 24 | [Z1] = PACK (Y1, Y2, Z), | ||
| 25 | [Y1] = PACK (Y1, X1, Y), | ||
| 26 | [Y2] = PACK (X2, W2, Y), | ||
| 27 | [X1] = PACK (W2, W1, X), | ||
| 28 | [X2] = PACK (TT, W2, X), | ||
| 29 | [W1] = PACK (TT, W1, W), | ||
| 30 | [W2] = PACK (W2, TT, W), | ||
| 31 | }; | ||
| 32 | |||
| 33 | #define UNPACK_BRANCH1(ROW) (bdd[ROW] >> 3 & 0b111) | ||
| 34 | #define UNPACK_BRANCH0(ROW) (bdd[ROW] >> 0 & 0b111) | ||
| 35 | #define UNPACK_VARMASK(ROW) (1 << (bdd[ROW] >> 6)) | ||
| 36 | bool | ||
| 37 | bdd_eval (u8 xyzw) | ||
| 38 | { | ||
| 39 | enum node curr = 0; | ||
| 40 | enum node prev; | ||
| 41 | |||
| 42 | loop: | ||
| 43 | prev = curr; | ||
| 44 | curr = xyzw & UNPACK_VARMASK (curr) | ||
| 45 | ? UNPACK_BRANCH1 (curr) | ||
| 46 | : UNPACK_BRANCH0 (curr); | ||
| 47 | if (curr == prev) return false; | ||
| 48 | if (curr == TT ) return true; | ||
| 49 | goto loop; | ||
| 50 | } | ||
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 @@ | |||
| 1 | #ifndef BDD_H | ||
| 2 | #define BDD_H | ||
| 3 | |||
| 4 | #include <stdbool.h> | ||
| 5 | #include "utils.h" | ||
| 6 | |||
| 7 | bool bdd_eval (u8 xyzw); | ||
| 8 | |||
| 9 | #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 @@ | |||
| 1 | #include <stdlib.h> | ||
| 2 | #include <stdio.h> | ||
| 3 | #include <string.h> | ||
| 4 | |||
| 5 | #include "utils.h" | ||
| 6 | |||
| 7 | #include "bdd.h" | ||
| 8 | #include "pnf.h" | ||
| 9 | #include "pzh.h" | ||
| 10 | |||
| 11 | #define REPL(PROMPT, EVAL) \ | ||
| 12 | for (;;) { \ | ||
| 13 | char *xyzw_str; \ | ||
| 14 | \ | ||
| 15 | while (!input ((PROMPT), "^([01]{4}|done)$", "%ms", &xyzw_str)) \ | ||
| 16 | puts ("Ввод не соответствует регулярному выражению!"); \ | ||
| 17 | \ | ||
| 18 | if (!strcmp (xyzw_str, "done")) \ | ||
| 19 | break; \ | ||
| 20 | \ | ||
| 21 | u8 xyzw = strtol (xyzw_str, NULL, 2); \ | ||
| 22 | free (xyzw_str); \ | ||
| 23 | \ | ||
| 24 | printf ("> %b\n", (EVAL)); \ | ||
| 25 | } | ||
| 26 | |||
| 27 | #define CHECK(NAME, EVAL) ({ \ | ||
| 28 | puts ("Проверка:"); \ | ||
| 29 | puts ("xyzw | f | "NAME" f"); \ | ||
| 30 | bool is_correct = true; \ | ||
| 31 | for (u8 xyzw = 0b0000; xyzw <= 0b1111; ++xyzw) { \ | ||
| 32 | bool res_f = f << xyzw >> 0b1111 & 1; \ | ||
| 33 | bool res_pdnf = pdnf_eval (pdnf, POPCOUNT (f), xyzw); \ | ||
| 34 | if (res_f != res_pdnf) \ | ||
| 35 | is_correct = false; \ | ||
| 36 | printf ("%04b | %c | %c\n", \ | ||
| 37 | xyzw, \ | ||
| 38 | '0' + res_f, \ | ||
| 39 | '0' + res_pdnf); \ | ||
| 40 | } puts (is_correct ? "Проверка сошлась!" : "Проверка не сошлась!");}) | ||
| 41 | |||
| 42 | u16 f = 0b0011010100011001; | ||
| 43 | |||
| 44 | static void | ||
| 45 | section_f (void) | ||
| 46 | { | ||
| 47 | printf ("f = (%016b)\n", f); | ||
| 48 | } | ||
| 49 | |||
| 50 | static void | ||
| 51 | section_bdd (void) | ||
| 52 | { | ||
| 53 | REPL ("Введите xyzw для вычисления значения функции f по БДР, или done, " | ||
| 54 | "чтобы перейти к следующему пункту", bdd_eval (xyzw)); | ||
| 55 | } | ||
| 56 | |||
| 57 | static void | ||
| 58 | section_pnf (void) | ||
| 59 | { | ||
| 60 | u64 pdnf = pdnf_compute (f); | ||
| 61 | puts ("СДНФ f:"); | ||
| 62 | pdnf_print (pdnf, POPCOUNT (f)); | ||
| 63 | CHECK ("СДНФ", pdnf_eval (pdnf, POPCOUNT (f), xyzw)); | ||
| 64 | NEWLINE (); | ||
| 65 | |||
| 66 | u64 pcnf = pcnf_compute (f); | ||
| 67 | puts ("СКНФ f:"); | ||
| 68 | pcnf_print (pcnf, 16 - POPCOUNT (f)); | ||
| 69 | CHECK ("СКНФ", pcnf_eval (pcnf, 16 - POPCOUNT (f), xyzw)); | ||
| 70 | NEWLINE (); | ||
| 71 | |||
| 72 | REPL ("Введите xyzw для вычисления значения функции f по СДНФ, или done, " | ||
| 73 | "чтобы перейти к следующему пункту", pdnf_eval (pdnf, POPCOUNT (f), xyzw)); | ||
| 74 | } | ||
| 75 | |||
| 76 | static void | ||
| 77 | section_pzh (void) | ||
| 78 | { | ||
| 79 | u16 pzh = pzh_compute (f); | ||
| 80 | puts ("Полином Жигалкина:"); | ||
| 81 | pzh_print (pzh); | ||
| 82 | NEWLINE (); | ||
| 83 | |||
| 84 | REPL ("Введите xyzw для вычисления значения функции f по ПЖ, или done, " | ||
| 85 | "чтобы завершить программу", pzh_eval (pzh, xyzw)); | ||
| 86 | } | ||
| 87 | |||
| 88 | int | ||
| 89 | main (void) | ||
| 90 | { | ||
| 91 | section_f (); PAGEBREAK (); | ||
| 92 | section_bdd (); PAGEBREAK (); | ||
| 93 | section_pnf (); PAGEBREAK (); | ||
| 94 | section_pzh (); | ||
| 95 | |||
| 96 | return EXIT_SUCCESS; | ||
| 97 | } | ||
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 @@ | |||
| 1 | #include "pnf.h" | ||
| 2 | #include <stdio.h> | ||
| 3 | |||
| 4 | u64 | ||
| 5 | pdnf_compute (u16 f) | ||
| 6 | { | ||
| 7 | u64 pdnf = 0; | ||
| 8 | u8 xyzw = 0b1111; | ||
| 9 | |||
| 10 | while (f) { | ||
| 11 | xyzw -= CTZ (f); | ||
| 12 | f >>= CTZ (f); | ||
| 13 | |||
| 14 | pdnf <<= 4; | ||
| 15 | pdnf |= xyzw; | ||
| 16 | |||
| 17 | xyzw -= 1; | ||
| 18 | f >>= 1; | ||
| 19 | } | ||
| 20 | |||
| 21 | return pdnf; | ||
| 22 | } | ||
| 23 | |||
| 24 | void | ||
| 25 | pdnf_print (u64 pdnf, u8 numones) | ||
| 26 | { | ||
| 27 | for (u8 i = numones; i; --i) { | ||
| 28 | printf ("%cx && %cy && %cz && %cw%s\n", | ||
| 29 | pdnf & 1 << 3 ? ' ' : '!', | ||
| 30 | pdnf & 1 << 2 ? ' ' : '!', | ||
| 31 | pdnf & 1 << 1 ? ' ' : '!', | ||
| 32 | pdnf & 1 << 0 ? ' ' : '!', | ||
| 33 | i > 1 ? " ||" : ""); | ||
| 34 | pdnf >>= 4; | ||
| 35 | } | ||
| 36 | } | ||
| 37 | |||
| 38 | u64 | ||
| 39 | pcnf_compute (u16 f) | ||
| 40 | { | ||
| 41 | u64 pcnf = 0; | ||
| 42 | u8 xyzw = 0b1111; | ||
| 43 | |||
| 44 | f = ~f; | ||
| 45 | while (f) { | ||
| 46 | xyzw -= CTZ (f); | ||
| 47 | f >>= CTZ (f); | ||
| 48 | |||
| 49 | pcnf <<= 4; | ||
| 50 | pcnf |= xyzw; | ||
| 51 | |||
| 52 | xyzw -= 1; | ||
| 53 | f >>= 1; | ||
| 54 | } | ||
| 55 | |||
| 56 | return ~pcnf; | ||
| 57 | } | ||
| 58 | |||
| 59 | void | ||
| 60 | pcnf_print (u64 pcnf, u8 numzeros) | ||
| 61 | { | ||
| 62 | for (u8 i = numzeros; i; --i) { | ||
| 63 | printf ("(%cx || %cy || %cz || %cw)%s\n", | ||
| 64 | pcnf & 1 << 3 ? ' ' : '!', | ||
| 65 | pcnf & 1 << 2 ? ' ' : '!', | ||
| 66 | pcnf & 1 << 1 ? ' ' : '!', | ||
| 67 | pcnf & 1 << 0 ? ' ' : '!', | ||
| 68 | i > 1 ? " &&" : ""); | ||
| 69 | pcnf >>= 4; | ||
| 70 | } | ||
| 71 | } | ||
| 72 | |||
| 73 | bool | ||
| 74 | pdnf_eval (u64 pdnf, u8 numones, u8 xyzw) | ||
| 75 | { | ||
| 76 | for (u8 i = 0; i < numones; ++i) { | ||
| 77 | u8 mask = pdnf & (1 << 4) - 1; | ||
| 78 | if (xyzw == mask) | ||
| 79 | return true; | ||
| 80 | pdnf >>= 4; | ||
| 81 | } | ||
| 82 | |||
| 83 | return false; | ||
| 84 | } | ||
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 @@ | |||
| 1 | #ifndef PNF_H | ||
| 2 | #define PNF_H | ||
| 3 | |||
| 4 | #include <stdbool.h> | ||
| 5 | #include "utils.h" | ||
| 6 | |||
| 7 | u64 | ||
| 8 | pdnf_compute (u16 f); | ||
| 9 | |||
| 10 | void | ||
| 11 | pdnf_print (u64 pdnf, u8 numones); | ||
| 12 | |||
| 13 | u64 | ||
| 14 | pcnf_compute (u16 f); | ||
| 15 | |||
| 16 | void | ||
| 17 | pcnf_print (u64 pcnf, u8 numzeros); | ||
| 18 | |||
| 19 | bool | ||
| 20 | pdnf_eval (u64 pdnf, u8 numones, u8 xyzw); | ||
| 21 | |||
| 22 | #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 @@ | |||
| 1 | #include "pzh.h" | ||
| 2 | #include <stdio.h> | ||
| 3 | |||
| 4 | // Разворачивается в 92 инструкции исключительно над регистрами! | ||
| 5 | u16 | ||
| 6 | pzh_compute (u16 f) | ||
| 7 | { | ||
| 8 | u16 pzh = 0; | ||
| 9 | |||
| 10 | for (u16 i = 1; i; i <<= 1) { | ||
| 11 | pzh >>= 1; | ||
| 12 | pzh |= f & 1 << 8*sizeof f - 1; | ||
| 13 | f ^= f << 1; | ||
| 14 | } | ||
| 15 | |||
| 16 | return pzh; | ||
| 17 | } | ||
| 18 | |||
| 19 | void | ||
| 20 | pzh_print (u16 pzh) | ||
| 21 | { | ||
| 22 | if (!pzh) { | ||
| 23 | puts ("1 != 1"); | ||
| 24 | return; | ||
| 25 | } | ||
| 26 | |||
| 27 | for (u16 i = 1; i; i <<= 1) { | ||
| 28 | if (pzh & i) { | ||
| 29 | u8 n = CTZ (i); | ||
| 30 | switch (POPCOUNT (n)) { | ||
| 31 | case 0: | ||
| 32 | printf ("1"); | ||
| 33 | break; | ||
| 34 | case 1: | ||
| 35 | printf ("%c", "wzyx"[CTZ (n)]); | ||
| 36 | break; | ||
| 37 | case 2: | ||
| 38 | printf ("(%c && %c)", | ||
| 39 | "xyz"[CLZ (n) - (1 << sizeof (CLZ (n)) + 1) + 4], | ||
| 40 | "wzy"[CTZ (n)]); | ||
| 41 | break; | ||
| 42 | case 3: | ||
| 43 | printf ("(%c && %c && %c)", | ||
| 44 | "xy"[CLZ (n) - (1 << sizeof (CLZ (n)) + 1) + 4], | ||
| 45 | "zy"[CTZ (n)], | ||
| 46 | "wz"[CTZ (n)]); | ||
| 47 | break; | ||
| 48 | case 4: | ||
| 49 | printf ("(x && y && z && w)"); | ||
| 50 | break; | ||
| 51 | } | ||
| 52 | printf ("%s", pzh & ~((i << 1) - 1) ? " + " : "\n"); | ||
| 53 | } | ||
| 54 | } | ||
| 55 | } | ||
| 56 | |||
| 57 | |||
| 58 | // 25, hot 13 | ||
| 59 | bool | ||
| 60 | pzh_eval (u16 pzh, u8 xyzw) | ||
| 61 | { | ||
| 62 | bool res = 0; | ||
| 63 | |||
| 64 | for (u8 i = 0; pzh;) { | ||
| 65 | i += CTZ (pzh); | ||
| 66 | pzh >>= CTZ (pzh); | ||
| 67 | |||
| 68 | res ^= (i & xyzw) == i; | ||
| 69 | |||
| 70 | i += 1; | ||
| 71 | pzh >>= 1; | ||
| 72 | } | ||
| 73 | |||
| 74 | return res; | ||
| 75 | } | ||
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 @@ | |||
| 1 | #ifndef PZH_H | ||
| 2 | #define PZH_H | ||
| 3 | |||
| 4 | #include <stdbool.h> | ||
| 5 | #include "utils.h" | ||
| 6 | |||
| 7 | u16 | ||
| 8 | pzh_compute (u16 f); | ||
| 9 | |||
| 10 | void | ||
| 11 | pzh_print (u16 pzh); | ||
| 12 | |||
| 13 | bool | ||
| 14 | pzh_eval (u16 pzh, u8 xyzw); | ||
| 15 | |||
| 16 | #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 @@ | |||
| 1 | #include "utils.h" | ||
| 2 | #include <stdlib.h> | ||
| 3 | #include <stdio.h> | ||
| 4 | #include <stdarg.h> | ||
| 5 | #include <regex.h> | ||
| 6 | #include <string.h> | ||
| 7 | |||
| 8 | int | ||
| 9 | input (const char *message, const char *regex, const char *template, ...) | ||
| 10 | { | ||
| 11 | char *line = NULL; | ||
| 12 | size_t line_size = 0; | ||
| 13 | regex_t compiled; | ||
| 14 | int matches = 0; | ||
| 15 | int c; | ||
| 16 | va_list ap; | ||
| 17 | |||
| 18 | va_start (ap, template); | ||
| 19 | vprintf (message, ap); | ||
| 20 | printf (" (\"%s\"): ", regex); | ||
| 21 | getline (&line, &line_size, stdin); | ||
| 22 | line[strlen (line) - 1] = '\0'; | ||
| 23 | |||
| 24 | regcomp (&compiled, regex, REG_EXTENDED | REG_NOSUB); | ||
| 25 | if (regexec (&compiled, line, 0, NULL, 0)) | ||
| 26 | goto defer; | ||
| 27 | |||
| 28 | matches = vsscanf (line, template, ap); | ||
| 29 | va_end (ap); | ||
| 30 | |||
| 31 | defer: | ||
| 32 | free (line); | ||
| 33 | return matches; | ||
| 34 | } | ||
| 35 | |||
| 36 | uint | ||
| 37 | log10ui (uint ui) | ||
| 38 | { | ||
| 39 | return ((ui >= 1000000000u) ? 9 : | ||
| 40 | (ui >= 100000000u) ? 8 : | ||
| 41 | (ui >= 10000000u) ? 7 : | ||
| 42 | (ui >= 1000000u) ? 6 : | ||
| 43 | (ui >= 100000u) ? 5 : | ||
| 44 | (ui >= 10000u) ? 4 : | ||
| 45 | (ui >= 1000u) ? 3 : | ||
| 46 | (ui >= 100u) ? 2 : | ||
| 47 | (ui >= 10u) ? 1 : 0u); | ||
| 48 | } | ||
| 49 | |||
| 50 | u16 | ||
| 51 | rev16 (u16 b) | ||
| 52 | { | ||
| 53 | b = (b & 0xFF00) >> 8 | (b & 0x00FF) << 8; | ||
| 54 | b = (b & 0xF0F0) >> 4 | (b & 0x0F0F) << 4; | ||
| 55 | b = (b & 0xCCCC) >> 2 | (b & 0x3333) << 2; | ||
| 56 | b = (b & 0xAAAA) >> 1 | (b & 0x5555) << 1; | ||
| 57 | return b; | ||
| 58 | } | ||
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 @@ | |||
| 1 | #ifndef UTILS_H | ||
| 2 | #define UTILS_H | ||
| 3 | |||
| 4 | #include <stdint.h> | ||
| 5 | |||
| 6 | #define MAX(a, b) ({ \ | ||
| 7 | __typeof__ (a) _a = (a); \ | ||
| 8 | __typeof__ (b) _b = (b); \ | ||
| 9 | _a > _b ? _a : _b; \ | ||
| 10 | }) | ||
| 11 | |||
| 12 | #define MIN(a, b) ({ \ | ||
| 13 | __typeof__ (a) _a = (a); \ | ||
| 14 | __typeof__ (b) _b = (b); \ | ||
| 15 | _a < _b ? _a : _b; \ | ||
| 16 | }) | ||
| 17 | |||
| 18 | #define CLZ(X) __builtin_clz (X) | ||
| 19 | #define CTZ(X) __builtin_ctz (X) | ||
| 20 | #define FFS(X) __builtin_ffs (X) | ||
| 21 | #define POPCOUNT(X) __builtin_popcount (X) | ||
| 22 | |||
| 23 | #define PAGEBREAK() puts ("\n") | ||
| 24 | #define NEWLINE() putchar ('\n') | ||
| 25 | |||
| 26 | typedef unsigned int uint; | ||
| 27 | typedef uint8_t u8; | ||
| 28 | typedef uint16_t u16; | ||
| 29 | typedef uint64_t u64; | ||
| 30 | |||
| 31 | int | ||
| 32 | input (const char *message, const char *regex, const char *template, ...); | ||
| 33 | |||
| 34 | uint | ||
| 35 | log10ui (uint ui); | ||
| 36 | |||
| 37 | u16 | ||
| 38 | rev16 (u16 b); | ||
| 39 | |||
| 40 | #endif | ||
