forked from JackHack96/logic-synthesis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhill.c
234 lines (207 loc) · 6.41 KB
/
hill.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
229
230
231
232
233
234
/*
* Symbolic encoding program for compiling a symbolic
* description into a binary representation. Target
* is multi-level logic synthesis
*
* History:
*
* Bill Lin
* University of California, Berkeley
* Comments to [email protected]
*
* Copyright (c) 1989 Bill Lin, UC Berkeley CAD Group
* Permission is granted to do anything with this
* code except sell it or remove this message.
*/
#include "jedi.h"
#include "rp.h"
#define DISPLACE1 0 /* flag for displacement */
#define DISPLACE2 1 /* flag for displacement */
#define INTERCHANGE 2 /* flag for interchange */
/*
* global variables
*/
int move_type; /* move type for SA */
int random1; /* random variable for SA */
int random2; /* random variable for SA */
int currentEnumtype; /* states the current type being encoded */
/*
* external declarations
*/
double rint(); /* math.h */
extern char *int_to_binary(); /* util.c */
extern int binary_to_int(); /* util.c */
/*
* forward declarations
*/
int opt_embedding();
int cost_function();
int generate_function();
int accept_function();
int reject_function();
opt_embedding()
{
int i;
for (i=0; i<ne; i++) {
if (verboseFlag) {
(void) fprintf(stderr, "\nencoding type (%s) ...\n\n",
enumtypes[i].name);
}
/*
* encode the type if it has weights on it
*/
if (enumtypes[i].input_flag || enumtypes[i].output_flag) {
currentEnumtype = i;
(void) combinatorial_optimize(cost_function, generate_function,
accept_function, reject_function, stdin, stderr, stderr,
beginningStates, endingStates, startingTemperature,
maximumTemperature, enumtypes[i].ns-1, verboseFlag);
}
}
} /* end of opt_embedding */
cost_function(ret_cost)
double *ret_cost;
{ int i, j, k, m;
int cost;
int inc_cost;
int dist;
int we;
int ns;
cost = 0;
ns = enumtypes[currentEnumtype].ns;
for (k=0; k<ns; k++) {
for (m=0; m<ns; m++) {
if (k < m) {
i = enumtypes[currentEnumtype].symbols[k].code_ptr;
j = enumtypes[currentEnumtype].symbols[m].code_ptr;
dist = enumtypes[currentEnumtype].distances[i][j];
we = enumtypes[currentEnumtype].links[k][m].weight;
inc_cost = we * (dist - 1);
cost += inc_cost;
}
}
}
*ret_cost = (double) cost;
return SA_OK;
} /* end of cost_function */
generate_function(range)
int range;
{
Boolean continue_flag = TRUE;
Boolean cond1, cond2, cond3;
char *tmp_code;
int num_codes;
int num_bits;
int ran;
int c, d;
int tmp_symbol_ptr;
/*
* set num_codes and num_bits for the
* current enum type
*/
num_codes = enumtypes[currentEnumtype].nc;
num_bits = enumtypes[currentEnumtype].nb;
/*
* pick two valid codes to exchange
*/
while (continue_flag) {
random1 = int_random_generator(0, num_codes-1);
if (range == SA_FULL_RANGE) {
random2 = int_random_generator(0, num_codes-1);
} else {
/*
* short range just toggle one bit
*/
tmp_code = int_to_binary(random1, num_bits);
ran = int_random_generator(0, num_bits-1);
if (tmp_code[ran] == '1') {
tmp_code[ran] = '0';
} else if (tmp_code[ran] == '0') {
tmp_code[ran] = '1';
} else {
(void) exit(-1);
}
random2 = binary_to_int(tmp_code, num_bits);
FREE(tmp_code);
}
cond1 = (random1 == random2);
cond2 = (!enumtypes[currentEnumtype].codes[random1].assigned);
cond3 = (!enumtypes[currentEnumtype].codes[random2].assigned);
continue_flag = (cond1 || (cond2 && cond3));
}
if (!enumtypes[currentEnumtype].codes[random1].assigned) {
/*
* displace into random code 1 if
* it is unassigned yet
*/
move_type = DISPLACE1;
enumtypes[currentEnumtype].codes[random1].assigned = TRUE;
c = enumtypes[currentEnumtype].codes[random1].symbol_ptr =
enumtypes[currentEnumtype].codes[random2].symbol_ptr;
enumtypes[currentEnumtype].symbols[c].code_ptr = random1;
enumtypes[currentEnumtype].codes[random2].assigned = FALSE;
enumtypes[currentEnumtype].codes[random2].symbol_ptr = 0;
} else if (!enumtypes[currentEnumtype].codes[random2].assigned) {
/*
* displace into random code 2 if
* it is unassigned yet
*/
move_type = DISPLACE2;
enumtypes[currentEnumtype].codes[random2].assigned = TRUE;
c = enumtypes[currentEnumtype].codes[random2].symbol_ptr =
enumtypes[currentEnumtype].codes[random1].symbol_ptr;
enumtypes[currentEnumtype].symbols[c].code_ptr = random2;
enumtypes[currentEnumtype].codes[random1].assigned = FALSE;
enumtypes[currentEnumtype].codes[random1].symbol_ptr = 0;
} else {
/*
* interchange the two code assignments
*/
move_type = INTERCHANGE;
tmp_symbol_ptr = enumtypes[currentEnumtype].codes[random1].symbol_ptr;
c = enumtypes[currentEnumtype].codes[random1].symbol_ptr =
enumtypes[currentEnumtype].codes[random2].symbol_ptr;
d = enumtypes[currentEnumtype].codes[random2].symbol_ptr =
tmp_symbol_ptr;
enumtypes[currentEnumtype].symbols[c].code_ptr = random1;
enumtypes[currentEnumtype].symbols[d].code_ptr = random2;
}
return SA_OK;
} /* end of generate_function */
accept_function()
{
/* do nothing */
return SA_OK;
} /* end of accept_function */
reject_function()
{
int c, d;
int tmp_symbol_ptr;
if (move_type == DISPLACE2) {
enumtypes[currentEnumtype].codes[random1].assigned = TRUE;
c = enumtypes[currentEnumtype].codes[random1].symbol_ptr =
enumtypes[currentEnumtype].codes[random2].symbol_ptr;
enumtypes[currentEnumtype].symbols[c].code_ptr = random1;
enumtypes[currentEnumtype].codes[random2].assigned = FALSE;
enumtypes[currentEnumtype].codes[random2].symbol_ptr = 0;
} else if (move_type == DISPLACE1) {
enumtypes[currentEnumtype].codes[random2].assigned = TRUE;
c = enumtypes[currentEnumtype].codes[random2].symbol_ptr =
enumtypes[currentEnumtype].codes[random1].symbol_ptr;
enumtypes[currentEnumtype].symbols[c].code_ptr = random2;
enumtypes[currentEnumtype].codes[random1].assigned = FALSE;
enumtypes[currentEnumtype].codes[random1].symbol_ptr = 0;
} else {
/*
* interchange back the encodings
*/
tmp_symbol_ptr = enumtypes[currentEnumtype].codes[random1].symbol_ptr;
c = enumtypes[currentEnumtype].codes[random1].symbol_ptr =
enumtypes[currentEnumtype].codes[random2].symbol_ptr;
d = enumtypes[currentEnumtype].codes[random2].symbol_ptr =
tmp_symbol_ptr;
enumtypes[currentEnumtype].symbols[c].code_ptr = random1;
enumtypes[currentEnumtype].symbols[d].code_ptr = random2;
}
return SA_OK;
} /* end of reject_function */