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/pzh.c | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 src/pzh.c (limited to 'src/pzh.c') 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; +} -- cgit v1.3.1