-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtracebit_mod2.h
172 lines (123 loc) · 3.46 KB
/
tracebit_mod2.h
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#ifndef TRACEBIT_MOD2
#define TRACEBIT_MOD2
#include <ostream>
// BitTracer using modulo-2 artithmetic
/* Stores a bit as a set of term with each term being a set of variables
* Because x+x = 0, each term is unique
* Becasue x.x = x, each factor within each term is unique
*
*/
typedef __uint128_t varset;
// TraceBit represents a single-bit, either constant or as a inhomogeneous linear equation
// (x2.x3 + x1.x4 + 1)
class TraceBit {
private:
// each term consist of the factors of distinct variables
// stores as the bits of uint64
// variable 0 is reserved for the constant 1
std::set<varset> terms;
char dbg[1024];
void setdbg() {
std::string s = to_str();
auto len = s.copy(dbg, 1024);
dbg[len] = 0;
}
void addTerm(varset v) {
if (terms.find(v) != terms.end())
terms.erase(v);
else
terms.insert(v);
}
bool isOne() const
{
return terms.size() == 1 && terms.find((varset)1) != terms.end();
}
public:
TraceBit(int variable)
{
terms.insert(((varset)1) | (((varset)1) << variable));
setdbg();
}
TraceBit() {
setdbg();
}
TraceBit operator&(const TraceBit &other) const {
TraceBit result;
// x AND y = xy
// the result is each combination of the terms of both sides
for(varset myterm : this->terms)
{
for(varset otherterm : other.terms)
{
result.addTerm(myterm | otherterm);
}
}
result.setdbg();
return result;
}
TraceBit operator^(const TraceBit &other) const {
TraceBit result = *this;
// x XOR y = x + y
for(varset term : other.terms)
result.addTerm(term);
result.setdbg();
return result;
}
TraceBit operator|(const TraceBit &other) const {
TraceBit result = *this & other;
for(varset term : other.terms)
result.addTerm(term);
for(varset term : this->terms)
result.addTerm(term);
result.setdbg();
return result;
}
TraceBit operator~() const {
TraceBit result = *this;
// add 1
result.addTerm(1);
result.setdbg();
return result;
}
int Value() const {
if (terms.empty())
return 0;
else if (isOne())
return 1;
else
return -1;
}
// return the bit as string or as mod2 formula
std::string to_str() const {
bool has_plusone = false;
if (terms.empty())
return "0";
else if (isOne())
return "1";
std::string result = "{";
for(varset term : terms)
{
if ((term&1)== 0)
throw "Term does not contain 1!";
if (term == 1)
has_plusone = true;
else
{
std::string sterm = "";
for(int n=1; n < 64; n++)
if (term & (((varset)1) << n))
{
sterm += "x" + std::to_string(n) +".";
}
result += sterm.substr(0,sterm.length()-1) + " + ";
}
}
if (result.length()>0)
result = result.substr(0, result.length()-3);
if (has_plusone)
result = result + " + 1";
result += "}";
return result;
}
};
#endif // TRACEBIT_MOD2