-
Notifications
You must be signed in to change notification settings - Fork 11
/
bestboth.c
228 lines (208 loc) · 6.69 KB
/
bestboth.c
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
/* bestboth.c
*
* by: David Canright
*
* for each input basis, and each of 4 transformation matrices,
* takes bit matrix and finds equivalent with minimum # of gates
* combining both input matrices, and both output matrices
* NOTE: matrix input order is: [A2X, X2A, X2S, S2X]
*
* input should have lines of the form:
hexstring num
* where hexstring contains all 4 matrices, num is an ID#, e.g.:
98F3F2480981A9FF64786E8C6829DE60582D9E0BDC0403248C7905EB12045153 4
* for which the output should be:
basis # 4:
A2X: 98F3F2480981A9FF S2X: 8C7905EB12045153
ncols = 8, gates = 42
A2Xb: 0000000000012804100810224008808001
S2Xb: 0028006200000100008800000102044010
[0,2], [0,3], [1,7], [2,10], [3,11], [4,7], [5,8], [6,10], [4,15],
ncols = 17, gates = 20
X2S: 582D9E0BDC040324 X2A: 64786E8C6829DE60
ncols = 8, gates = 38
X2Sb: 000000000000000040082480180002040100
X2Ab: 041000800021D00000000000000204080860
[0,4], [1,3], [1,7], [2,4], [2,8], [2,6], [3,13], [5,11], [6,9], [10,12],
ncols = 18, gates = 18
***bestgates 4 = 38 = 20 + 18
* which, for each matrix pair, shows the original versions (8 columns),
* the optimized versions, and a list of index pairs for precomputed XORs,
* which correspond to new columns. Also shown: # XOR gates required.
* Note: a "quick" test case is:
F1261450CA86D330C502A8BF412B3590352582D03974323C65C4836C69953380 0
*
* uses pruning algorithm to eliminate redundant cases; minimal memory copying
*/
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 8
/* gatematrix is a structure with an array of 16-bit columns,
list of indices (used in pairs), number of columns, and number of gates*/
typedef struct gatematrix {
uint32_t mat[128];
char ind[256];
int n;
int g;
} GateMat;
static uint32_t share[65536];
static GateMat test;
/* blockPrint prints columns and index pairs for matrix pair */
void blockPrint(GateMat* p, const char* tag1, const char* tag2) {
int i;
printf("%6s: ", tag1);
for (i = 0; i < p->n; i++) {
printf("%02X", (p->mat[i]) & 0XFF);
}
if ((p->n) > N) {
printf("\n");
}
printf("%6s: ", tag2);
for (i = 0; i < p->n; i++) {
printf("%02X", ((p->mat[i]) & 0XFF00) >> 8);
}
if ((p->n) > N)
printf("\n");
for (i = 0; i < (p->n) - N; i++) {
printf(" [%1d,%1d], ", p->ind[2 * i], p->ind[2 * i + 1]);
}
printf("\n ncols = %2d, gates = %2d\n", p->n, p->g);
} /* end blockPrint */
/* copyMat copies from one to another*/
void copyMat(GateMat* p, GateMat* q) {
int i, n;
n = q->n = p->n;
q->g = p->g;
memcpy(q->mat, p->mat, n * sizeof(uint32_t));
memcpy(q->ind, p->ind, (n - N) * 2);
} /* end copyMat */
/*
* bestgates is recursive:
* takes current matrix, tries all possibilities of adding a gate
* returns best # of gates
* p points to test matrix on input, and used to store output.
* tree search is pruned if this set of columns previously tried
*/
void bestgates() {
char indb[256];
int gb, nb, ci, cj;
int i, j, n, c, g, io, jo;
int nm, np, n2, n2p, t;
gb = 1024; /* best # gates, start high */
n = test.n;
g = test.g;
nm = n - 1;
np = n + 1;
n2 = 2 * (n - N);
n2p = n2 + 1;
if (n == N) {
io = jo = 0; /* if orig matrix, no "old" index pair */
} else {
io = (test.ind[n2 - 2]);
jo = (test.ind[n2 - 1]);
}
for (i = 0; i < nm; i++) /* for each pair of columns */
for (j = i + 1; j < n; j++) {
c = (test.mat[i]) & (test.mat[j]);
t = share[c];
if (t) { /* if can share a gate ????????????? */
if (i < io && j != io && j != jo && j < nm) {
// if prior, indep. pair, then been there, done that; skip to next j
continue;
}
test.n = np;
test.g = g - t;
ci = test.mat[i]; /* save current columns */
cj = test.mat[j];
test.mat[i] ^= c; /* update to new columns */
test.mat[j] ^= c;
test.mat[n] = c;
test.ind[n2] = i;
test.ind[n2p] = j;
bestgates(); /* recurse with new matrix */
test.mat[i] = ci; /* restore current columns */
test.mat[j] = cj;
if (test.g < gb) { /* if best yet, save data */
memcpy(indb, test.ind + n2, (test.n - n) * 2);
nb = test.n;
gb = test.g;
}
}
} /* end columns loop */
if (gb < 1024) { /* if improved, return best data */
memcpy(test.ind + n2, indb, (nb - n) * 2);
test.n = nb;
test.g = gb;
}
/* else {printf("%3d [%2d]",n,g); fflush(stdout);} */
}
/* bestmat reconstructs best matrix */
void bestmat(GateMat* p) {
int i, j, n, c;
int nm, np, n2, n2p, t;
GateMat best;
p->g = test.g;
for (i = 0; i < N; i++) test.mat[i] = p->mat[i];
for (n = 0; n < (test.n - N); n++) {
i = test.ind[n * 2];
j = test.ind[n * 2 + 1];
c = (test.mat[i]) & (test.mat[j]);
test.mat[i] ^= c;
test.mat[j] ^= c;
test.mat[n + N] = c;
}
}
int main(void) {
char line[256];
char name[4][4] = {
"A2X", "X2S", "S2X", "X2A",
};
char bname[4][5] = {
"A2Xb", "X2Sb", "S2Xb", "X2Ab",
};
long int i, j, k, n, nid, gt;
unsigned u;
int InitMat[32];
GateMat orig[2];
/* share[i] is initialized to 0 if # bits < 2 */
share[0] = 0;
for (i = 1; i < 65536; i++) {
k = 0;
for (j = i & 0xFFFF; j; j >>= 1) k += j & 1;
share[i] = k - 1;
}
while (fgets(line, 256, stdin) == line) {
for (i = 0; i < 32; i++) { /* read matrices, ID number */
sscanf(line + 2 * i, "%02X", &u);
InitMat[i] = u;
}
sscanf(line + 65, "%ld", &nid);
printf("\nbasis #%3ld:\n", nid);
/* NOTE: matrix input order is: [A2X, X2A, X2S, S2X] */
for (i = 0; i < 8; i++) { /* combine input pair; combine output pair */
(orig[0]).mat[i] = InitMat[8 * 0 + i] | (InitMat[8 * 3 + i] << 8);
(orig[1]).mat[i] = InitMat[8 * 2 + i] | (InitMat[8 * 1 + i] << 8);
}
gt = 0;
for (k = 0; k < 2; k++) { /* for each matrix pair */
(orig[k]).n = 8; /* initialize # columns, # gates */
for (i = j = 0; i < 8; i++) {
j += share[(orig[k]).mat[i]];
}
(orig[k]).g = j - 8;
blockPrint(&(orig[k]), name[k], name[k + 2]);
fflush(stdout);
copyMat(&(orig[k]), &test);
bestgates(); /* optimize */
bestmat(&(orig[k]));
blockPrint(&test, bname[k], bname[k + 2]);
fflush(stdout);
gt += test.g; /* total # gates */
}
printf("***bestgates %3ld = %5ld =%5d +%5d\n", nid, gt, (orig[0]).g, (orig[1]).g);
fflush(stdout);
}
return 0;
}