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
363
364
365
366
/*++
Copyright (c) 2021 Microsoft Corporation
Module Name:
dd_fdd
Abstract:
Finite domain abstraction for using BDDs as sets of integers, inspired by BuDDy's fdd module.
Author:
Nikolaj Bjorner (nbjorner) 2021-04-20
Jakob Rath 2021-04-20
--*/
#include "math/dd/dd_fdd.h"
namespace dd {
fdd::fdd(bdd_manager& manager, unsigned_vector&& vars)
: m_pos2var(std::move(vars))
, m_var2pos()
, m(&manager)
, m_var(manager.mk_var(m_pos2var))
{
for (unsigned pos = 0; pos < m_pos2var.size(); ++pos) {
unsigned const var = m_pos2var[pos];
while (var >= m_var2pos.size())
m_var2pos.push_back(UINT_MAX);
m_var2pos[var] = pos;
}
}
bdd fdd::non_zero() const {
bdd non_zero = m->mk_false();
for (unsigned var : m_pos2var) {
non_zero |= m->mk_var(var);
}
return non_zero;
}
unsigned fdd::var2pos(unsigned var) const {
return var < m_var2pos.size() ? m_var2pos[var] : UINT_MAX;
}
bool fdd::contains(bdd b, rational const& val) const {
while (!b.is_const()) {
unsigned const pos = var2pos(b.var());
SASSERT(pos != UINT_MAX && "Unexpected BDD variable");
b = val.get_bit(pos) ? b.hi() : b.lo();
}
return b.is_true();
}
find_t fdd::find(bdd b, rational& out_val) const {
return find_hint(b, rational::zero(), out_val);
}
find_t fdd::find_hint(bdd b, rational const& hint, rational& out_val) const {
out_val = 0;
if (b.is_false())
return find_t::empty;
bool is_unique = true;
bool hint_ok = !hint.is_zero(); // since we choose the 'lo' branch by default, we don't need to check the hint when it is 0.
unsigned num_vars = 0;
while (!b.is_true()) {
++num_vars;
unsigned const pos = var2pos(b.var());
SASSERT(pos != UINT_MAX && "Unexpected BDD variable");
bool go_hi = false;
if (b.lo().is_false()) {
go_hi = true;
if (hint_ok && !hint.get_bit(pos))
hint_ok = false;
}
else if (b.hi().is_false()) {
if (hint_ok && hint.get_bit(pos))
hint_ok = false;
}
else {
// This is the only case where we have a choice
// => follow the hint
SASSERT(!b.lo().is_false() && !b.hi().is_false());
is_unique = false;
if (hint_ok && hint.get_bit(pos))
go_hi = true;
}
if (go_hi) {
out_val += rational::power_of_two(pos);
b = b.hi();
}
else
b = b.lo();
}
if (num_vars != num_bits())
is_unique = false;
// If a variable corresponding to a 1-bit in hint does not appear in the BDD,
// out_val is wrong at this point, so we set it explicitly.
if (hint_ok)
out_val = hint;
// TODO: instead of computing out_val incrementally, we could mark the visited 'hi'-positions and only compute out_val from the marks when !hint_ok.
return is_unique ? find_t::singleton : find_t::multiple;
}
std::ostream& operator<<(std::ostream& out, find_t x) {
switch (x) {
case find_t::empty:
return out << "empty";
case find_t::singleton:
return out << "singleton";
case find_t::multiple:
return out << "multiple";
}
UNREACHABLE();
return out;
}
bool fdd::contains(bdd const& x, bool_vector const& value) const {
bdd b = x;
while (!b.is_true()) {
unsigned const pos = var2pos(b.var());
SASSERT(pos != UINT_MAX && "Unexpected BDD variable");
if (value[pos] && b.hi().is_false())
return false;
if (!value[pos] && b.lo().is_false())
return false;
if (value[pos])
b = b.hi();
else
b = b.lo();
}
return true;
}
// subtract one from x
static void dec(bool_vector& x) {
for (auto& b : x) {
b = !b;
if (!b)
break;
}
}
// add one to x
static void inc(bool_vector& x) {
for (auto& b : x) {
b = !b;
if (b)
break;
}
}
static void reset(bool_vector& x, bool value) {
for (auto& b : x)
b = value;
}
bool fdd::sup(bdd const& x, bool_vector& lo) const {
SASSERT(lo.size() == num_bits());
//
// Assumption: common case is that high-order bits are before lower-order bits also
// after re-ordering. Then co-factoring is relatively cheap.
//
if (!contains(x, lo))
return false;
//
// find minimal index where b is false for some
// value larger than lo.
//
// Let ua(x lo) be shorthand for "unbounded-above" of variable
// x with bit-string lo.
//
// we have the following identities:
// ua(_ []) = true
// ua(x 1 ++ lo) = hi(x) = T or ua(hi(x), lo)
// ua(x 0 ++ lo) = hi(x) = T and ua(lo(x), lo)
//
// the least significant bit where ua is false
// represents the position where the smallest number above
// lo resides that violates x.
unsigned idx = UINT_MAX;
vector<bdd> trail;
bdd b = x;
for (unsigned i = lo.size(); i-- > 0; ) {
trail.push_back(b);
unsigned v = m_pos2var[i];
bdd w = m->mk_var(v);
bdd hi = b.cofactor(w);
if (lo[i]) {
if (hi.is_true())
break;
SASSERT(!hi.is_false());
b = hi;
}
else {
if (!hi.is_true())
idx = i;
b = b.cofactor(m->mk_nvar(v));
}
}
if (idx == UINT_MAX) {
// all values above lo satisfy x
reset(lo, true);
return true;
}
SASSERT(!lo[idx]);
lo[idx] = true;
unsigned v = m_pos2var[idx];
b = trail[lo.size() - idx - 1].cofactor(m->mk_var(v));
for (unsigned i = idx; i-- > 0; ) {
SASSERT(!b.is_true());
if (b.is_false()) {
for (unsigned j = 0; j <= i; ++j)
lo[j] = false;
break;
}
lo[i] = b.lo().is_true();
if (lo[i])
b = b.hi();
else
b = b.lo();
}
dec(lo);
return true;
}
bool fdd::inf(bdd const& x, bool_vector& hi) const {
SASSERT(hi.size() == num_bits());
if (!contains(x, hi))
return false;
// Let ub(x hi) be shorthand for "unbounded-below" of variable
// x with bit-string hi.
//
// we have the following identities:
// ub(_ []) = true
// ub(x 0 ++ hi) = lo(x) = T or ub(lo(x), hi)
// ub(x 1 ++ hi) = lo(x) = T and ub(hi(x), hi)
//
unsigned idx = UINT_MAX;
vector<bdd> trail;
bdd b = x;
for (unsigned i = hi.size(); i-- > 0; ) {
trail.push_back(b);
unsigned v = m_pos2var[i];
bdd nw = m->mk_nvar(v);
bdd lo = b.cofactor(nw);
if (!hi[i]) {
if (lo.is_true())
break;
SASSERT(!lo.is_false());
b = lo;
}
else {
if (!lo.is_true())
idx = i;
b = b.cofactor(m->mk_var(v));
}
}
if (idx == UINT_MAX) {
// all values below hi satisfy x
reset(hi, false);
return true;
}
SASSERT(hi[idx]);
hi[idx] = false;
unsigned v = m_pos2var[idx];
b = trail[hi.size() - idx - 1].cofactor(m->mk_nvar(v));
for (unsigned i = idx; i-- > 0; ) {
SASSERT(!b.is_true());
if (b.is_false()) {
for (unsigned j = 0; j <= i; ++j)
hi[j] = true;
break;
}
hi[i] = !b.hi().is_true();
if (!hi[i])
b = b.lo();
else
b = b.hi();
}
inc(hi);
return true;
}
bool_vector fdd::rational2bits(rational const& r) const {
bool_vector result;
for (unsigned i = 0; i < num_bits(); ++i)
result.push_back(r.get_bit(i));
return result;
}
rational fdd::bits2rational(bool_vector const& v) const {
rational result(0);
for (unsigned i = 0; i < num_bits(); ++i)
if (v[i])
result += rational::power_of_two(i);
return result;
}
bool fdd::sup(bdd const& b, rational& _lo) const {
bool_vector lo = rational2bits(_lo);
if (!sup(b, lo))
return false;
_lo = bits2rational(lo);
return true;
}
bool fdd::inf(bdd const& b, rational& _hi) const {
bool_vector hi = rational2bits(_hi);
if (!inf(b, hi))
return false;
_hi = bits2rational(hi);
return true;
}
rational fdd::max(bdd b) const {
SASSERT(!b.is_false());
rational result(0);
for (unsigned i = num_bits(); i-- > 0; ) {
unsigned v = m_pos2var[i];
bdd w = m->mk_var(v);
bdd hi = b.cofactor(w);
if (!hi.is_false()) {
b = hi;
result += rational::power_of_two(i);
}
}
return result;
}
rational fdd::min(bdd b) const {
SASSERT(!b.is_false());
rational result(0);
for (unsigned i = num_bits(); i-- > 0; ) {
unsigned v = m_pos2var[i];
bdd nw = m->mk_nvar(v);
bdd lo = b.cofactor(nw);
if (lo.is_false())
result += rational::power_of_two(i);
else
b = lo;
}
return result;
}
}