Skip to content

Commit 77f20b8

Browse files
Refactoring in boolbvt::convert_mult
1 parent 4092dff commit 77f20b8

File tree

1 file changed

+17
-42
lines changed

1 file changed

+17
-42
lines changed

src/solvers/flattening/boolbv_mult.cpp

+17-42
Original file line numberDiff line numberDiff line change
@@ -12,53 +12,40 @@ Author: Daniel Kroening, [email protected]
1212

1313
bvt boolbvt::convert_mult(const exprt &expr)
1414
{
15-
std::size_t width=boolbv_width(expr.type());
16-
15+
const std::size_t width = boolbv_width(expr.type());
1716
if(width==0)
1817
return conversion_failed(expr);
19-
2018
bvt bv;
2119
bv.resize(width);
2220

2321
const exprt::operandst &operands=expr.operands();
24-
if(operands.empty())
25-
throw "mult without operands";
26-
22+
DATA_INVARIANT(!operands.empty(), "mult must have operands");
2723
const exprt &op0=expr.op0();
28-
29-
bool no_overflow=expr.id()=="no-overflow-mult";
24+
const bool no_overflow = expr.id() == "no-overflow-mult";
3025

3126
if(expr.type().id()==ID_fixedbv)
3227
{
33-
if(op0.type()!=expr.type())
34-
throw "multiplication with mixed types";
35-
28+
DATA_INVARIANT(op0.type() == expr.type(), "multiplication with mixed types");
3629
bv=convert_bv(op0);
3730

38-
if(bv.size()!=width)
39-
throw "convert_mult: unexpected operand width";
40-
41-
std::size_t fraction_bits=
31+
DATA_INVARIANT(bv.size() == width, "convert_mult: unexpected operand width");
32+
const std::size_t fraction_bits =
4233
to_fixedbv_type(expr.type()).get_fraction_bits();
4334

4435
for(exprt::operandst::const_iterator it=operands.begin()+1;
4536
it!=operands.end(); it++)
4637
{
47-
if(it->type()!=expr.type())
48-
throw "multiplication with mixed types";
38+
DATA_INVARIANT(
39+
it->type() == expr.type(),
40+
"multiplication should have uniform operand types");
4941

5042
// do a sign extension by fraction_bits bits
5143
bv=bv_utils.sign_extension(bv, bv.size()+fraction_bits);
52-
5344
bvt op=convert_bv(*it);
5445

55-
if(op.size()!=width)
56-
throw "convert_mult: unexpected operand width";
57-
46+
INVARIANT(op.size() == width, "convert_mult: unexpected operand width");
5847
op=bv_utils.sign_extension(op, bv.size());
59-
6048
bv=bv_utils.signed_multiplier(bv, op);
61-
6249
// cut it down again
6350
bv.erase(bv.begin(), bv.begin()+fraction_bits);
6451
}
@@ -68,37 +55,25 @@ bvt boolbvt::convert_mult(const exprt &expr)
6855
else if(expr.type().id()==ID_unsignedbv ||
6956
expr.type().id()==ID_signedbv)
7057
{
71-
if(op0.type()!=expr.type())
72-
throw "multiplication with mixed types";
73-
58+
DATA_INVARIANT(
59+
op0.type() == expr.type(), "multiplication with mixed types");
7460
bv_utilst::representationt rep=
7561
expr.type().id()==ID_signedbv?bv_utilst::representationt::SIGNED:
7662
bv_utilst::representationt::UNSIGNED;
7763

7864
bv=convert_bv(op0);
79-
80-
if(bv.size()!=width)
81-
throw "convert_mult: unexpected operand width";
82-
65+
INVARIANT(bv.size() == width, "convert_mult: unexpected operand width");
8366
for(exprt::operandst::const_iterator it=operands.begin()+1;
8467
it!=operands.end(); it++)
8568
{
86-
if(it->type()!=expr.type())
87-
throw "multiplication with mixed types";
88-
69+
INVARIANT(it->type() == expr.type(), "multiplication with mixed types");
8970
const bvt &op=convert_bv(*it);
71+
CHECK_RETURN(op.size() == width);
9072

91-
if(op.size()!=width)
92-
throw "convert_mult: unexpected operand width";
93-
94-
if(no_overflow)
95-
bv=bv_utils.multiplier_no_overflow(bv, op, rep);
96-
else
97-
bv=bv_utils.multiplier(bv, op, rep);
73+
bv = no_overflow ? bv_utils.multiplier_no_overflow(bv, op, rep)
74+
: bv_utils.multiplier(bv, op, rep);
9875
}
99-
10076
return bv;
10177
}
102-
10378
return conversion_failed(expr);
10479
}

0 commit comments

Comments
 (0)