Newer
Older
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
/*************************************************************************
FILE: adder.cc
DESCR: BDD implementation of an N bit adder.
AUTH: Jorn Lind
DATE: feb 1998
*************************************************************************/
#include <stdlib.h>
#include <string.h>
#include "bdd.h"
int N;
bdd *ainp;
bdd *binp;
bdd *co;
bdd *xout;
/*************************************************************************
Adder
*************************************************************************/
void build_adder(void)
{
int n;
for (n=0 ; n<N ; n++)
{
if (n > 0)
{
xout[n] = ainp[n] ^ binp[n] ^ co[n-1];
co[n] = ainp[n] & binp[n] |
ainp[n] & co[n-1] |
binp[n] & co[n-1];
}
else
{
xout[n] = ainp[n] ^ binp[n];
co[n] = ainp[n] & binp[n];
}
}
}
int main(int argc, char **argv)
{
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
int method=BDD_REORDER_NONE;
int n;
if(argc < 2 || argc > 3)
{
cout << "usage: adder N R\n";
cout << " N number of bits\n";
cout << " R -> enable reordering if R is win2,win2ite,win3,win3ite,sift,siftite\n";
cout << " in this case 'adder' starts with a worst case ordering\n";
exit(1);
}
N = atoi(argv[1]);
if (N <= 0)
{
cout << "The number of bits must be more than zero\n";
exit(2);
}
if (argc == 3)
{
if (strcmp(argv[2], "win2") == 0)
method = BDD_REORDER_WIN2;
else
if (strcmp(argv[2], "win2ite") == 0)
method = BDD_REORDER_WIN2ITE;
else
if (strcmp(argv[2], "win3") == 0)
method = BDD_REORDER_WIN3;
else
if (strcmp(argv[2], "win3ite") == 0)
method = BDD_REORDER_WIN3ITE;
else
if (strcmp(argv[2], "sift") == 0)
method = BDD_REORDER_SIFT;
else
if (strcmp(argv[2], "siftite") == 0)
method = BDD_REORDER_SIFTITE;
else
if (strcmp(argv[2], "rand") == 0)
method = BDD_REORDER_RANDOM;
}
bdd_init(500,1000);
bdd_setvarnum(2*N);
ainp = new bdd[N];
binp = new bdd[N];
co = new bdd[N];
xout = new bdd[N];
for (n=0 ; n<N ; n++)
{
if (method == BDD_REORDER_NONE)
{
ainp[n] = bdd_ithvar(2*n);
binp[n] = bdd_ithvar(2*n+1);
}
else
{
ainp[n] = bdd_ithvar(n);
binp[n] = bdd_ithvar(N+n);
}
}
for (n=0 ; n<N ; n++)
{
bdd_addvarblock(ainp[n],1);
bdd_addvarblock(binp[n],1);
}
//bdd_autoreorder(method);
//bdd_reorder_verbose(2);
build_adder();
if (method != BDD_REORDER_NONE)
{
cout << "Sizes before reordering:\n";
for (n=0 ; n<N ; n++)
cout << "Out[" << n << "]: " << bdd_nodecount(xout[n]) << " nodes\n";
bdd_reorder(method);
cout << "Sizes after reordering:\n";
}
else
cout << "Sizes:\n";
for (n=0 ; n<N ; n++)
cout << "Out[" << n << "]: " << bdd_nodecount(xout[n]) << " nodes\n";
}
/*=== DEBUGING =========================================================*/
bdd setval(int val, int v)
{
bdd x = bddtrue;
for (int n=0 ; n<N ; n++)
{
if (val & 1)
{
if (v == 0)
x &= ainp[n];
else
x &= binp[n];
}
else
{
if (v == 0)
x &= !ainp[n];
else
x &= !binp[n];
}
val = val >> 1;
}
return x;
}
int test_vector(bdd av, bdd bv, int a, int b)
{
int res = a+b;
for (int n=0 ; n<N ; n++)
{
bdd resv = av & bv & xout[n];
if (resv == bddfalse && (res & 1) || resv != bddfalse && !(res & 1))
return 0;
res = res >> 1;
}
return 1;
}
int test_adder(void)
{
int m = 1 << N;
for (int a=0 ; a<m ; a++)
{
for (int b=0 ; b<m ; b++)
{
bdd av = setval(a,0);
bdd bv = setval(b,1);
if (test_vector(av,bv,a,b) == 0)
return 0;
}
}
return 1;
}
/* EOF */