Skip to content

Commit 9b7dd1a

Browse files
committed
Refactor iteration over bitvector in convert_unary_minus()
1 parent e15844c commit 9b7dd1a

File tree

1 file changed

+11
-22
lines changed

1 file changed

+11
-22
lines changed

src/solvers/flattening/boolbv_unary_minus.cpp

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ Author: Daniel Kroening, [email protected]
1212

1313
#include <solvers/floatbv/float_utils.h>
1414

15+
#include <algorithm>
16+
#include <iterator>
17+
1518
#include "boolbv_type.h"
1619

1720
bvt boolbvt::convert_unary_minus(const unary_exprt &expr)
@@ -48,42 +51,28 @@ bvt boolbvt::convert_unary_minus(const unary_exprt &expr)
4851
"total bitvector width needs to be a multiple of the component bitvector "
4952
"widths");
5053

51-
std::size_t size=width/sub_width;
5254
bvt bv;
53-
bv.resize(width);
5455

55-
for(std::size_t i=0; i<size; i++)
56+
for(std::size_t sub_idx = 0; sub_idx < width; sub_idx += sub_width)
5657
{
5758
bvt tmp_op;
58-
tmp_op.resize(sub_width);
59-
60-
for(std::size_t j=0; j<tmp_op.size(); j++)
61-
{
62-
INVARIANT(
63-
i * sub_width + j < op_bv.size(), "bit index shall be within bounds");
64-
tmp_op[j]=op_bv[i*sub_width+j];
65-
}
6659

67-
bvt tmp_result;
60+
const auto sub_it = std::next(op_bv.begin(), sub_idx);
61+
std::copy_n(sub_it, sub_width, std::back_inserter(tmp_op));
6862

69-
if(type.subtype().id()==ID_floatbv)
63+
if(type.subtype().id() == ID_floatbv)
7064
{
7165
float_utilst float_utils(prop, to_floatbv_type(subtype));
72-
tmp_result=float_utils.negate(tmp_op);
66+
tmp_op = float_utils.negate(tmp_op);
7367
}
7468
else
75-
tmp_result=bv_utils.negate(tmp_op);
69+
tmp_op = bv_utils.negate(tmp_op);
7670

7771
INVARIANT(
78-
tmp_result.size() == sub_width,
72+
tmp_op.size() == sub_width,
7973
"bitvector after negation shall have same bit width");
8074

81-
for(std::size_t j=0; j<tmp_result.size(); j++)
82-
{
83-
INVARIANT(
84-
i * sub_width + j < bv.size(), "bit index shall be within bounds");
85-
bv[i*sub_width+j]=tmp_result[j];
86-
}
75+
std::copy(tmp_op.begin(), tmp_op.end(), std::back_inserter(bv));
8776
}
8877

8978
return bv;

0 commit comments

Comments
 (0)