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