Skip to content

Commit 4391c90

Browse files
na
1 parent 9915378 commit 4391c90

9 files changed

+126
-56
lines changed

src/ast/sls/bv_sls.cpp

+38-20
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Module Name:
2121
#include "ast/sls/bv_sls.h"
2222
#include "ast/ast_pp.h"
2323
#include "ast/ast_ll_pp.h"
24+
#include "params/sls_params.hpp"
2425

2526
namespace bv {
2627

@@ -48,15 +49,17 @@ namespace bv {
4849

4950
void sls::reinit_eval() {
5051
std::function<bool(expr*, unsigned)> eval = [&](expr* e, unsigned i) {
52+
auto should_keep = [&]() {
53+
return m_rand() % 100 >= 95;
54+
};
5155
if (m.is_bool(e)) {
52-
if (m_eval.is_fixed0(e))
56+
if (m_eval.is_fixed0(e) || should_keep())
5357
return m_eval.bval0(e);
5458
}
5559
else if (bv.is_bv(e)) {
5660
auto& w = m_eval.wval0(e);
57-
if (w.get(w.fixed, i))
58-
return w.get(w.bits, i);
59-
61+
if (w.get(w.fixed, i) || should_keep())
62+
return w.get(w.bits, i);
6063
}
6164
return m_rand() % 2 == 0;
6265
};
@@ -77,51 +80,59 @@ namespace bv {
7780
unsigned index = m_rand(m_repair_down.size());
7881
e = m_terms.term(m_repair_down.elem_at(index));
7982
}
80-
else if (m_repair_up.empty()) {
83+
else if (!m_repair_up.empty()) {
8184
unsigned index = m_rand(m_repair_up.size());
8285
e = m_terms.term(m_repair_up.elem_at(index));
8386
}
8487
return { !m_repair_down.empty(), e };
8588
}
8689

8790
lbool sls::search() {
88-
// init and init_eval were invoked.
89-
unsigned& n = m_stats.m_moves;
90-
n = 0;
91-
for (; n < m_config.m_max_repairs && m.inc(); ++n) {
91+
// init and init_eval were invoked
92+
unsigned n = 0;
93+
for (; n++ < m_config.m_max_repairs && m.inc(); ) {
94+
++m_stats.m_moves;
9295
auto [down, e] = next_to_repair();
9396
if (!e)
9497
return l_true;
95-
IF_VERBOSE(20, verbose_stream() << (down ? "d " : "u ") << mk_bounded_pp(e, m, 1) << "\n");
98+
IF_VERBOSE(20, verbose_stream() << (down ? "d #" : "u #")
99+
<< e->get_id() << ": "
100+
<< mk_bounded_pp(e, m, 1) << " ";
101+
if (bv.is_bv(e)) verbose_stream() << m_eval.wval0(e);
102+
verbose_stream() << "\n");
96103
if (eval_is_correct(e)) {
97104
if (down)
98105
m_repair_down.remove(e->get_id());
99106
else
100107
m_repair_up.remove(e->get_id());
101108
}
102-
else if (down) {
103-
try_repair_down(e);
104-
}
109+
else if (down)
110+
try_repair_down(e);
105111
else
106112
try_repair_up(e);
107113
}
108114
return l_undef;
109115
}
110116

117+
void sls::trace() {
118+
IF_VERBOSE(2, verbose_stream()
119+
<< "(bvsls :restarts " << m_stats.m_restarts
120+
<< " :repair-down " << m_repair_down.size()
121+
<< " :repair-up " << m_repair_up.size() << ")\n");
122+
}
123+
111124
lbool sls::operator()() {
112125
lbool res = l_undef;
126+
m_stats.reset();
127+
m_stats.m_restarts = 0;
113128
do {
114-
if (!m.inc())
115-
return l_undef;
116-
117129
res = search();
118-
119130
if (res != l_undef)
120-
return res;
121-
131+
break;
132+
trace();
122133
reinit_eval();
123134
}
124-
while (m_stats.m_restarts++ < m_config.m_max_restarts);
135+
while (m.inc() && m_stats.m_restarts++ < m_config.m_max_restarts);
125136

126137
return res;
127138
}
@@ -162,6 +173,8 @@ namespace bv {
162173
}
163174

164175
bool sls::eval_is_correct(app* e) {
176+
if (!m_eval.can_eval1(e))
177+
return false;
165178
if (m.is_bool(e))
166179
return m_eval.bval0(e) == m_eval.bval1(e);
167180
if (bv.is_bv(e))
@@ -208,4 +221,9 @@ namespace bv {
208221
terms.reset();
209222
return out;
210223
}
224+
225+
void sls::updt_params(params_ref const& _p) {
226+
sls_params p(_p);
227+
m_config.m_max_restarts = p.max_restarts();
228+
}
211229
}

src/ast/sls/bv_sls.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ namespace bv {
3636

3737
struct config {
3838
unsigned m_max_restarts = 1000;
39-
unsigned m_max_repairs = 100000;
39+
unsigned m_max_repairs = 1000;
4040
};
4141

4242
ast_manager& m;
@@ -59,6 +59,7 @@ namespace bv {
5959

6060
lbool search();
6161
void reinit_eval();
62+
void trace();
6263

6364
public:
6465
sls(ast_manager& m);
@@ -85,7 +86,7 @@ namespace bv {
8586
*/
8687
lbool operator()();
8788

88-
void updt_params(params_ref const& p) {}
89+
void updt_params(params_ref const& p);
8990
void collect_statistics(statistics & st) const { m_stats.collect_statistics(st); }
9091
void reset_statistics() { m_stats.reset(); }
9192

src/ast/sls/bv_sls_eval.cpp

+38-4
Original file line numberDiff line numberDiff line change
@@ -171,15 +171,41 @@ namespace bv {
171171
auto const& vb = wval0(b);
172172
return va.eq(vb);
173173
}
174-
UNREACHABLE();
175-
break;
174+
return m.are_equal(a, b);
176175
}
177176
default:
178177
UNREACHABLE();
178+
break;
179179
}
180180
UNREACHABLE();
181181
return false;
182182
}
183+
184+
bool sls_eval::can_eval1(app* e) const {
185+
expr* x, * y, * z;
186+
if (m.is_eq(e, x, y))
187+
return m.is_bool(x) || bv.is_bv(x);
188+
if (m.is_ite(e, x, y, z))
189+
return m.is_bool(y) || bv.is_bv(y);
190+
if (e->get_family_id() == bv.get_fid()) {
191+
switch (e->get_decl_kind()) {
192+
case OP_BNEG_OVFL:
193+
case OP_BSADD_OVFL:
194+
case OP_BSDIV_OVFL:
195+
case OP_BSMUL_NO_OVFL:
196+
case OP_BSMUL_NO_UDFL:
197+
case OP_BSMUL_OVFL:
198+
return false;
199+
default:
200+
return true;
201+
}
202+
}
203+
if (e->get_family_id() == basic_family_id)
204+
return true;
205+
if (is_uninterp_const(e))
206+
return m.is_bool(e) || bv.is_bv(e);
207+
return false;
208+
}
183209

184210
bool sls_eval::bval1_bv(app* e) const {
185211
SASSERT(m.is_bool(e));
@@ -1182,8 +1208,8 @@ namespace bv {
11821208
if (i == 0) {
11831209
if (e.is_zero() && a.is_ones(a.fixed) && a.is_ones())
11841210
return false;
1185-
if (b.is_zero())
1186-
return true;
1211+
if (b.is_zero())
1212+
return false;
11871213
if (!e.is_ones()) {
11881214
for (unsigned i = 0; i < a.nw; ++i)
11891215
m_tmp[i] = ~a.fixed[i] | a.bits[i];
@@ -1215,11 +1241,19 @@ namespace bv {
12151241
a.set_repair(true, m_tmp3);
12161242
}
12171243
else {
1244+
if (e.is_one() && a.is_zero()) {
1245+
for (unsigned i = 0; i < a.nw; ++i)
1246+
m_tmp[i] = random_bits();
1247+
a.clear_overflow_bits(m_tmp);
1248+
b.set_repair(true, m_tmp);
1249+
return true;
1250+
}
12181251
if (e.is_one()) {
12191252
b.set(m_tmp, a.bits);
12201253
b.set_repair(true, m_tmp);
12211254
return true;
12221255
}
1256+
12231257
// e * b + r = a
12241258
// b = (a - r) udiv e
12251259
// random version of r:

src/ast/sls/bv_sls_eval.h

+1
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ namespace bv {
134134
* Retrieve evaluation based on immediate children.
135135
*/
136136
bool bval1(app* e) const;
137+
bool can_eval1(app* e) const;
137138

138139
svector<digit_t>& wval1(app* e) const;
139140

src/ast/sls/bv_sls_fixed.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Module Name:
1111
1212
--*/
1313

14+
#include "ast/ast_pp.h"
1415
#include "ast/sls/bv_sls_fixed.h"
1516
#include "ast/sls/bv_sls_eval.h"
1617

@@ -137,12 +138,12 @@ namespace bv {
137138
v.add_range(-b, a - b);
138139
}
139140
else if (!y) {
140-
if (mod(b + 1, rational::power_of_two(bv.get_bv_size(x))) == 1)
141+
if (mod(b + 1, rational::power_of_two(bv.get_bv_size(x))) == 0)
141142
return;
142143
auto& v = wval0(x);
143144
if (!sign)
144145
v.add_range(-a, b - a + 1);
145-
else
146+
else
146147
v.add_range(b - a + 1, -a);
147148
}
148149
else if (x == y) {

src/ast/sls/sls_stats.h

+5-2
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,16 @@ namespace bv {
3535
st.update("sls restarts", m_restarts);
3636
st.update("sls full evals", m_full_evals);
3737
st.update("sls incr evals", m_incr_evals);
38-
st.update("sls incr evals/sec", m_incr_evals / seconds);
38+
if (seconds > 0 && m_incr_evals > 0)
39+
st.update("sls incr evals/sec", m_incr_evals / seconds);
40+
if (seconds > 0 && m_moves > 0)
41+
st.update("sls moves/sec", m_moves / seconds);
3942
st.update("sls FLIP moves", m_flips);
4043
st.update("sls INC moves", m_incs);
4144
st.update("sls DEC moves", m_decs);
4245
st.update("sls INV moves", m_invs);
4346
st.update("sls moves", m_moves);
44-
st.update("sls moves/sec", m_moves / seconds);
47+
4548
}
4649

4750
};

src/ast/sls/sls_valuation.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,7 @@ namespace bv {
296296
h = mod(h, rational::power_of_two(bw));
297297
if (h == l)
298298
return;
299+
299300
if (eq(lo, hi)) {
300301
set_value(lo, l);
301302
set_value(hi, h);

src/ast/sls/sls_valuation.h

+15-8
Original file line numberDiff line numberDiff line change
@@ -192,19 +192,26 @@ namespace bv {
192192

193193
std::ostream& display(std::ostream& out) const {
194194
out << std::hex;
195-
for (unsigned i = 0; i < nw; ++i)
196-
out << bits[i];
195+
auto print_bits = [&](svector<digit_t> const& v) {
196+
bool nz = false;
197+
for (unsigned i = nw; i-- > 0;)
198+
if (nz)
199+
out << std::setw(8) << std::setfill('0') << v[i];
200+
else if (v[i] != 0)
201+
out << v[i], nz = true;
202+
if (!nz)
203+
out << "0";
204+
};
205+
206+
print_bits(bits);
197207
out << " ";
198-
for (unsigned i = 0; i < nw; ++i)
199-
out << fixed[i];
208+
print_bits(fixed);
200209

201210
if (!eq(lo, hi)) {
202211
out << " [";
203-
for (unsigned i = 0; i < nw; ++i)
204-
out << lo[i];
212+
print_bits(lo);
205213
out << ", ";
206-
for (unsigned i = 0; i < nw; ++i)
207-
out << hi[i];
214+
print_bits(hi);
208215
out << "[";
209216
}
210217
out << std::dec;

0 commit comments

Comments
 (0)