aboutsummaryrefslogtreecommitdiff
path: root/src/pnf.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/pnf.c')
-rw-r--r--src/pnf.c84
1 files changed, 84 insertions, 0 deletions
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
4u64
5pdnf_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
24void
25pdnf_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
38u64
39pcnf_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
59void
60pcnf_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
73bool
74pdnf_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}