aboutsummaryrefslogtreecommitdiff
path: root/src/pnf.c
blob: 773ace794e6b4a015d0986264f0980a11b2f6df3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include "pnf.h"
#include <stdio.h>

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;
}