Skip to content

Commit 4ed00e1

Browse files
authored
Merge pull request #8716 from tautschnig/use-sign_bit
Use bv_utilst::sign_bit
2 parents 46f9d54 + 3fd45d8 commit 4ed00e1

File tree

3 files changed

+19
-21
lines changed

3 files changed

+19
-21
lines changed

src/solvers/flattening/boolbv.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ literalt boolbvt::convert_rest(const exprt &expr)
407407
CHECK_RETURN(!bv.empty());
408408
const irep_idt type_id = op.type().id();
409409
if(type_id == ID_signedbv || type_id == ID_fixedbv || type_id == ID_floatbv)
410-
return bv[bv.size()-1];
410+
return bv_utils.sign_bit(bv);
411411
if(type_id == ID_unsignedbv)
412412
return const_literal(false);
413413
}

src/solvers/flattening/bv_utils.cpp

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -432,12 +432,11 @@ literalt bv_utilst::overflow_add(
432432
// An overflow occurs if the signs of the two operands are the same
433433
// and the sign of the sum is the opposite.
434434

435-
literalt old_sign=op0[op0.size()-1];
436-
literalt sign_the_same=prop.lequal(op0[op0.size()-1], op1[op1.size()-1]);
435+
literalt old_sign = sign_bit(op0);
436+
literalt sign_the_same = prop.lequal(sign_bit(op0), sign_bit(op1));
437437

438438
bvt result=add(op0, op1);
439-
return
440-
prop.land(sign_the_same, prop.lxor(result[result.size()-1], old_sign));
439+
return prop.land(sign_the_same, prop.lxor(sign_bit(result), old_sign));
441440
}
442441
else if(rep==representationt::UNSIGNED)
443442
{
@@ -457,7 +456,7 @@ literalt bv_utilst::overflow_sub(
457456
// x is negative, always representable, and
458457
// thus not an overflow.
459458
literalt op1_is_int_min=is_int_min(op1);
460-
literalt op0_is_negative=op0[op0.size()-1];
459+
literalt op0_is_negative = sign_bit(op0);
461460

462461
return
463462
prop.lselect(op1_is_int_min,
@@ -557,7 +556,7 @@ bvt bv_utilst::shift(const bvt &src, const shiftt s, std::size_t dist)
557556
case shiftt::SHIFT_ARIGHT:
558557
// src.size()-i won't underflow as i<src.size()
559558
// Then, if dist<src.size()-i, then i+dist<src.size()
560-
l=(dist<src.size()-i?src[i+dist]:src[src.size()-1]); // sign bit
559+
l = dist < src.size() - i ? src[i + dist] : sign_bit(src);
561560
break;
562561

563562
case shiftt::SHIFT_LRIGHT:
@@ -608,7 +607,7 @@ literalt bv_utilst::overflow_negate(const bvt &bv)
608607
bvt should_be_zeros(bv);
609608
should_be_zeros.pop_back();
610609

611-
return prop.land(bv[bv.size() - 1], !prop.lor(should_be_zeros));
610+
return prop.land(sign_bit(bv), !prop.lor(should_be_zeros));
612611
}
613612

614613
void bv_utilst::incrementer(
@@ -1005,8 +1004,8 @@ bvt bv_utilst::signed_multiplier(const bvt &op0, const bvt &op1)
10051004
if(op0.empty() || op1.empty())
10061005
return bvt();
10071006

1008-
literalt sign0=op0[op0.size()-1];
1009-
literalt sign1=op1[op1.size()-1];
1007+
literalt sign0 = sign_bit(op0);
1008+
literalt sign1 = sign_bit(op1);
10101009

10111010
bvt neg0=cond_negate(op0, sign0);
10121011
bvt neg1=cond_negate(op1, sign1);
@@ -1034,7 +1033,7 @@ bvt bv_utilst::cond_negate(const bvt &bv, const literalt cond)
10341033
bvt bv_utilst::absolute_value(const bvt &bv)
10351034
{
10361035
PRECONDITION(!bv.empty());
1037-
return cond_negate(bv, bv[bv.size()-1]);
1036+
return cond_negate(bv, sign_bit(bv));
10381037
}
10391038

10401039
bvt bv_utilst::cond_negate_no_overflow(const bvt &bv, literalt cond)
@@ -1051,15 +1050,15 @@ bvt bv_utilst::signed_multiplier_no_overflow(
10511050
if(op0.empty() || op1.empty())
10521051
return bvt();
10531052

1054-
literalt sign0=op0[op0.size()-1];
1055-
literalt sign1=op1[op1.size()-1];
1053+
literalt sign0 = sign_bit(op0);
1054+
literalt sign1 = sign_bit(op1);
10561055

10571056
bvt neg0=cond_negate_no_overflow(op0, sign0);
10581057
bvt neg1=cond_negate_no_overflow(op1, sign1);
10591058

10601059
bvt result=unsigned_multiplier_no_overflow(neg0, neg1);
10611060

1062-
prop.l_set_to_false(result[result.size() - 1]);
1061+
prop.l_set_to_false(sign_bit(result));
10631062

10641063
literalt result_sign=prop.lxor(sign0, sign1);
10651064

@@ -1112,8 +1111,8 @@ void bv_utilst::signed_divider(
11121111

11131112
bvt _op0(op0), _op1(op1);
11141113

1115-
literalt sign_0=_op0[_op0.size()-1];
1116-
literalt sign_1=_op1[_op1.size()-1];
1114+
literalt sign_0 = sign_bit(_op0);
1115+
literalt sign_1 = sign_bit(_op1);
11171116

11181117
bvt neg_0=negate(_op0), neg_1=negate(_op1);
11191118

@@ -1409,9 +1408,6 @@ literalt bv_utilst::lt_or_le(
14091408
{
14101409
PRECONDITION(bv0.size() == bv1.size());
14111410

1412-
literalt top0=bv0[bv0.size()-1],
1413-
top1=bv1[bv1.size()-1];
1414-
14151411
#ifdef COMPACT_LT_OR_LE
14161412
if(prop.has_set_to() && prop.cnf_handled_well())
14171413
{
@@ -1422,6 +1418,8 @@ literalt bv_utilst::lt_or_le(
14221418

14231419
if(rep == representationt::SIGNED)
14241420
{
1421+
literalt top0 = sign_bit(bv0), top1 = sign_bit(bv1);
1422+
14251423
if(top0.is_false() && top1.is_true())
14261424
return const_literal(false);
14271425
else if(top0.is_true() && top1.is_false())
@@ -1524,7 +1522,7 @@ literalt bv_utilst::lt_or_le(
15241522
literalt result;
15251523

15261524
if(rep==representationt::SIGNED)
1527-
result=prop.lxor(prop.lequal(top0, top1), carry);
1525+
result = prop.lxor(prop.lequal(sign_bit(bv0), sign_bit(bv1)), carry);
15281526
else
15291527
{
15301528
INVARIANT(

src/solvers/flattening/bv_utils.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ class bv_utilst
137137

138138
static inline literalt sign_bit(const bvt &op)
139139
{
140-
return op[op.size()-1];
140+
return op.back();
141141
}
142142

143143
literalt is_zero(const bvt &op)

0 commit comments

Comments
 (0)