-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompute.cpp
362 lines (331 loc) · 11.1 KB
/
compute.cpp
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
// 逻辑完备集的计算
// 电路中的对称门与非对称门相关的内容会不会有什么启发?
// https://en.wikipedia.org/wiki/Functional_completeness
// https://blog.csdn.net/u013795675/article/details/44480789
#include <bits/stdc++.h>
std::vector<std::string> basic_input2{
"0101",
"0011",
"0000",
"1111"};
std::vector<std::string> basic_input3{
"01010101",
"00110011",
"00001111"
"00000000",
"11111111"};
std::unordered_map<std::string, std::string> gate_map2{
{"0000", "const0"},
{"0001", "and2"},
{"0010", "us2_1"},
{"0011", "us2_2"},
{"0100", "us2_3"},
{"0101", "us2_4"},
{"0110", "xor2"},
{"0111", "or2"},
{"1000", "nor2"},
{"1001", "xnor2"},
{"1010", "us2_5"},
{"1011", "us2_6"},
{"1100", "us2_7"},
{"1101", "us2_8"},
{"1110", "nand2"},
{"1111", "const1"},
};
std::unordered_set<std::string> basic_symmetry_gates{
{"0001"},
{"0110"},
{"0111"},
{"1000"},
{"1001"},
{"1110"}};
std::string uint_2_string(uint32_t u, uint8_t length)
{
std::string res;
for (int i = length - 1; i >= 0; --i)
{
res += u & (1 << i) ? '1' : '0';
}
return res;
}
/**
* @brief apply the inverter for the given gate
*/
std::string apply_inverter(std::string entry)
{
std::string res;
uint i = 0;
for (i = 0u; i < entry.size(); ++i)
{
if (entry[i] == '0')
res += '1';
else
res += '0';
}
return res;
}
/**
* @brief compute the truth table for the given gate
* @param entry the Boolean function (truth table) of the given gate
* @param a the input truth table
* @param b the input truth table
* @return the truth table of the given gate
*/
std::string compute_new_tt_2(std::string entry, const std::string &a, const std::string &b)
{
std::string res;
uint i = 0;
for (i = 0u; i < 4; ++i)
{
if (a[i] == '0' && b[i] == '0')
res += entry[0];
if (a[i] == '0' && b[i] == '1')
res += entry[1];
if (a[i] == '1' && b[i] == '0')
res += entry[2];
if (a[i] == '1' && b[i] == '1')
res += entry[3];
}
return res;
}
std::string compute_new_tt_3(std::string entry, const std::string &a, const std::string &b, const std::string &c)
{
std::string res;
uint i = 0;
for (i = 0u; i < 8; ++i)
{
if (a[i] == '0' && b[i] == '0' && c[i] == '0')
res += entry[0];
if (a[i] == '0' && b[i] == '0' && c[i] == '1')
res += entry[1];
if (a[i] == '0' && b[i] == '1' && c[i] == '0')
res += entry[2];
if (a[i] == '0' && b[i] == '1' && c[i] == '1')
res += entry[3];
if (a[i] == '1' && b[i] == '0' && c[i] == '0')
res += entry[4];
if (a[i] == '1' && b[i] == '0' && c[i] == '1')
res += entry[5];
if (a[i] == '1' && b[i] == '1' && c[i] == '0')
res += entry[6];
if (a[i] == '1' && b[i] == '1' && c[i] == '1')
res += entry[7];
}
return res;
}
void explore_2_1()
{
uint i = 0u, j = 0u, k = 0u;
bool added = true;
std::vector<std::string> fcs_s;
std::vector<std::string> fcs_us;
for (i = 0u; i < 16u; ++i)
{
std::string entry = uint_2_string(i, 4);
std::vector<std::string> tmp_basic_input2 = basic_input2;
std::set<std::string> tmp_set(tmp_basic_input2.begin(), tmp_basic_input2.end());
added = true;
while (added)
{
added = false;
for (j = 0; j < tmp_basic_input2.size(); ++j)
{
for (k = 0; k < tmp_basic_input2.size(); ++k)
{
std::string newTT = compute_new_tt_2(entry, tmp_basic_input2[j], tmp_basic_input2[k]);
if (tmp_set.find(newTT) == tmp_set.end())
{
tmp_basic_input2.emplace_back(newTT);
tmp_set.insert(newTT);
added = true;
}
}
}
}
if (tmp_basic_input2.size() == 16u)
{
if (basic_symmetry_gates.find(entry) != basic_symmetry_gates.end())
fcs_s.emplace_back(entry);
else
fcs_us.emplace_back(entry);
}
}
std::cout << "symmetry gates:" << std::endl;
for (auto fc : fcs_s)
{
std::cout << "\t" << gate_map2[fc] << std::endl;
}
std::cout << "unsymmetry gates:" << std::endl;
for (auto fc : fcs_us)
{
std::cout << "\t" << gate_map2[fc] << std::endl;
}
}
void explore_2_2()
{
uint i = 0u, j = 0u, k = 0u, l = 0u;
bool added = true;
std::set<std::tuple<std::string, std::string>> fcs;
for (i = 0u; i < 16u; ++i)
{
for (l = i + 1; l < 16u; ++l)
{
std::string entry1 = uint_2_string(i, 4);
std::string entry2 = uint_2_string(l, 4);
std::vector<std::string> tmp_basic_input2 = basic_input2;
std::set<std::string> tmp_set(tmp_basic_input2.begin(), tmp_basic_input2.end());
added = true;
while (added)
{
added = false;
for (j = 0; j < tmp_basic_input2.size(); ++j)
{
for (k = 0; k < tmp_basic_input2.size(); ++k)
{
std::string newTT1 = compute_new_tt_2(entry1, tmp_basic_input2[j], tmp_basic_input2[k]);
std::string newTT2 = compute_new_tt_2(entry2, tmp_basic_input2[j], tmp_basic_input2[k]);
if (tmp_set.find(newTT1) == tmp_set.end())
{
tmp_basic_input2.emplace_back(newTT1);
tmp_set.insert(newTT1);
added = true;
}
if (tmp_set.find(newTT2) == tmp_set.end())
{
tmp_basic_input2.emplace_back(newTT2);
tmp_set.insert(newTT2);
added = true;
}
}
}
}
if (tmp_basic_input2.size() == 16u)
{
std::vector<std::string> entries({entry1, entry2});
std::sort(entries.begin(), entries.end());
fcs.insert(std::make_tuple(entries[0], entries[1]));
}
}
}
for (auto fc : fcs)
{
std::cout << "\t" << gate_map2[std::get<0>(fc)] << ", " << gate_map2[std::get<1>(fc)] << std::endl;
}
}
void explore_2_3()
{
uint i = 0u, l = 0u, m = 0u, j = 0u, k = 0u, u = 0u;
bool added = true;
std::set<std::tuple<std::string, std::string, std::string>> fcs;
for (i = 0u; i < 16u; ++i)
{
for (l = i + 1; l < 16u; ++l)
{
for (m = l + 1; m < 16u; ++m)
{
std::string entry1 = uint_2_string(i, 4);
std::string entry2 = uint_2_string(l, 4);
std::string entry3 = uint_2_string(m, 4);
std::vector<std::string> tmp_basic_input2 = basic_input2;
std::set<std::string> tmp_set(tmp_basic_input2.begin(), tmp_basic_input2.end());
added = true;
while (added)
{
added = false;
for (j = 0; j < tmp_basic_input2.size(); ++j)
{
for (k = 0; k < tmp_basic_input2.size(); ++k)
{
std::string newTT1 = compute_new_tt_2(entry1, tmp_basic_input2[j], tmp_basic_input2[k]);
std::string newTT2 = compute_new_tt_2(entry2, tmp_basic_input2[j], tmp_basic_input2[k]);
std::string newTT3 = compute_new_tt_2(entry3, tmp_basic_input2[j], tmp_basic_input2[k]);
if (tmp_set.find(newTT1) == tmp_set.end())
{
tmp_basic_input2.emplace_back(newTT1);
tmp_set.insert(newTT1);
added = true;
}
if (tmp_set.find(newTT2) == tmp_set.end())
{
tmp_basic_input2.emplace_back(newTT2);
tmp_set.insert(newTT2);
added = true;
}
if (tmp_set.find(newTT3) == tmp_set.end())
{
tmp_basic_input2.emplace_back(newTT3);
tmp_set.insert(newTT3);
added = true;
}
}
}
}
if (tmp_basic_input2.size() == 16u)
{
std::vector<std::string> entries({entry1, entry2, entry3});
std::sort(entries.begin(), entries.end());
fcs.insert(std::make_tuple(entries[0], entries[1], entries[2]));
}
}
}
}
for (auto fc : fcs)
{
std::cout << "\t" << gate_map2[std::get<0>(fc)] << ", " << gate_map2[std::get<1>(fc)] << ", " << gate_map2[std::get<2>(fc)] << std::endl;
}
}
void explore_3_1()
{
uint i = 0u, j = 0u, k = 0u, l = 0u;
bool added = true;
std::vector<std::string> fcs;
for (i = 0u; i < 256u; ++i)
{
std::string entry = uint_2_string(i, 8);
std::vector<std::string> tmp_basic_input3 = basic_input3;
std::set<std::string> tmp_set(tmp_basic_input3.begin(), tmp_basic_input3.end());
std::cout << "try entry: " << entry << std::endl;
added = true;
while (added)
{
added = false;
for (j = 0; j < tmp_basic_input3.size(); ++j)
{
for (k = 0; k < tmp_basic_input3.size(); ++k)
{
for (l = 0; l < tmp_basic_input3.size(); ++l)
{
std::string newTT = compute_new_tt_3(entry, tmp_basic_input3[j], tmp_basic_input3[k], tmp_basic_input3[l]);
if (tmp_set.find(newTT) == tmp_set.end())
{
tmp_basic_input3.emplace_back(newTT);
tmp_set.insert(newTT);
added = true;
}
}
}
}
}
if (tmp_basic_input3.size() == 16u)
{
fcs.emplace_back(entry);
}
}
for (auto fc : fcs)
{
std::cout << fc << std::endl;
}
}
int main(int argc, char **argv)
{
std::cout << "Funational Completeness exploration for 2-inputs gate:" << std::endl;
std::cout << "One gate:" << std::endl;
explore_2_1();
std::cout << "Two gates:" << std::endl;
explore_2_2();
std::cout << "Three gates:" << std::endl;
explore_2_3();
// std::cout << "Funational Completeness exploration for 3-inputs gate:" << std::endl;
// explore_3_1();
return 1;
}