Skip to content

Commit 3ef24a2

Browse files
author
Sonny Martin
committed
Changes in response to initial comments
1 parent dce7f59 commit 3ef24a2

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

src/util/simplify_expr_int.cpp

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ bool simplify_exprt::simplify_bswap(bswap_exprt &expr)
4141

4242
// put back together, but backwards
4343
mp_integer new_value=0;
44+
45+
//May I suggest to move this out of the loop and instead check that at least as many elements are contained in bytes?
46+
// this invariant protects against someone modifying the loop
47+
// expression - a bit pointless for it to be outside the loop.
4448
for(std::size_t bit = 0; bit < width; bit += bits_per_byte)
4549
{
4650
INVARIANT(
@@ -858,27 +862,22 @@ bool simplify_exprt::simplify_bitwise(exprt &expr)
858862
bool simplify_exprt::simplify_extractbit(exprt &expr)
859863
{
860864
PRECONDITION(expr.id() == ID_extractbit);
861-
auto &extractbit_expr = to_extractbit_expr(expr);
865+
const auto &extractbit_expr = to_extractbit_expr(expr);
862866

863867
const typet &src_type = extractbit_expr.src().type();
864868

865869
if(!is_bitvector_type(src_type))
866870
return true;
867871

868-
std::size_t src_bit_width = to_bitvector_type(src_type).get_width();
872+
const std::size_t src_bit_width = to_bitvector_type(src_type).get_width();
869873

870-
if(extractbit_expr.index().id() != ID_constant)
871-
{
872-
return true;
873-
}
874-
875-
auto index_converted_to_int =
874+
const auto index_converted_to_int =
876875
numeric_cast<mp_integer>(extractbit_expr.index());
877876
if(!index_converted_to_int.has_value())
878877
{
879878
return true;
880879
}
881-
mp_integer index_as_int = index_converted_to_int.has_value();
880+
const mp_integer index_as_int = index_converted_to_int.value();
882881
if(!extractbit_expr.src().is_constant())
883882
return true;
884883

@@ -888,12 +887,15 @@ bool simplify_exprt::simplify_extractbit(exprt &expr)
888887
const irep_idt &src_value =
889888
to_constant_expr(extractbit_expr.src()).get_value();
890889

891-
if(src_value.size() != src_bit_width)
890+
891+
std::string src_value_as_string=id2string(src_value);
892+
893+
if(src_value_as_string.size() != src_bit_width)
892894
return true;
893895

894-
bool bit =
895-
(id2string(src_value)
896-
[src_bit_width - numeric_cast_v<std::size_t>(index_as_int) - 1] == '1');
896+
const bool bit =
897+
(src_value_as_string
898+
[src_bit_width - numeric_cast_v<std::size_t>(index_as_int) - 1]=='1');
897899

898900
expr.make_bool(bit);
899901

0 commit comments

Comments
 (0)