forked from FFTW/fftw3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddchain.c
171 lines (149 loc) · 3.12 KB
/
addchain.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
/* addition-chain optimizer */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
static int verbose;
static int mulcost = 18;
static int ldcost = 2;
static int sqcost = 10;
static int reflcost = 8;
#define INFTY 100000
static int *answer;
static int best_so_far;
static void print_answer(int n, int t)
{
int i;
printf("| (%d, %d) -> [", n, t);
for (i = 0; i < t; ++i)
printf("%d;", answer[i]);
printf("] (* %d *)\n", best_so_far);
}
#define DO(i, j, k, cst) \
if (k < n) { \
int c = A[i] + A[j] + cst; \
if (c < A[k]) { \
A[k] = c; \
changed = 1; \
} \
}
#define DO3(i, j, l, k, cst) \
if (k < n) { \
int c = A[i] + A[j] + A[l] + cst; \
if (c < A[k]) { \
A[k] = c; \
changed = 1; \
} \
}
static int optimize(int n, int *A)
{
int i, j, k, changed, cst, cstmax;
do {
changed = 0;
for (i = 0; i < n; ++i) {
k = i + i;
DO(i, i, k, sqcost);
}
for (i = 0; i < n; ++i) {
for (j = 0; j <= i; ++j) {
k = i + j;
DO(i, j, k, mulcost);
k = i - j;
DO(i, j, k, mulcost);
k = i + j;
DO3(i, j, i - j, k, reflcost);
}
}
} while (changed);
cst = cstmax = 0;
for (i = 0; i < n; ++i) {
cst += A[i];
if (A[i] > cstmax) cstmax = A[i];
}
/* return cstmax; */
return cst;
}
static void search(int n, int t, int *A, int *B, int depth)
{
if (depth == 0) {
int i, tc;
for (i = 0; i < n; ++i)
A[i] = INFTY;
A[0] = 0; /* always free */
for (i = 1; i <= t; ++i)
A[B[-i]] = ldcost;
tc = optimize(n, A);
if (tc < best_so_far) {
best_so_far = tc;
for (i = 1; i <= t; ++i)
answer[t - i] = B[-i];
if (verbose)
print_answer(n, t);
}
} else {
for (B[0] = B[-1] + 1; B[0] < n; ++B[0])
search(n, t, A, B + 1, depth - 1);
}
}
static void doit(int n, int t)
{
int *A;
int *B;
A = malloc(n * sizeof(int));
B = malloc((t + 1) * sizeof(int));
answer = malloc(t * sizeof(int));
B[0] = 0;
best_so_far = INFTY;
search(n, t, A, B + 1, t);
print_answer(n, t);
free(A); free(B); free(answer);
}
int main(int argc, char *argv[])
{
int n = 32;
int t = 3;
int all;
int ch;
verbose = 0;
all = 0;
while ((ch = getopt(argc, argv, "n:t:m:l:r:s:va")) != -1) {
switch (ch) {
case 'n':
n = atoi(optarg);
break;
case 't':
t = atoi(optarg);
break;
case 'm':
mulcost = atoi(optarg);
break;
case 'l':
ldcost = atoi(optarg);
break;
case 's':
sqcost = atoi(optarg);
break;
case 'r':
reflcost = atoi(optarg);
break;
case 'v':
++verbose;
break;
case 'a':
++all;
break;
case '?':
fprintf(stderr, "use the source\n");
exit(1);
}
}
if (all) {
for (n = 4; n <= 64; n *= 2) {
int n1 = n - 1; if (n1 > 7) n1 = 7;
for (t = 1; t <= n1; ++t)
doit(n, t);
}
} else {
doit(n, t);
}
return 0;
}