Skip to content

Commit c52c813

Browse files
Add a sum_overflows function
Detects when an overflow happens in the sum of two integers. This will be used in buitin functions for dealing with the overflow case.
1 parent 0d03591 commit c52c813

File tree

2 files changed

+21
-13
lines changed

2 files changed

+21
-13
lines changed

src/solvers/refinement/string_constraint_generator.h

+3
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,9 @@ exprt minimum(const exprt &a, const exprt &b);
447447
/// \return expression representing the maximum of two expressions
448448
exprt maximum(const exprt &a, const exprt &b);
449449

450+
/// \return Boolean true when the sum of the two expressions overflows
451+
exprt sum_overflows(const plus_exprt &sum);
452+
450453
exprt length_constraint_for_concat_char(
451454
const array_string_exprt &res,
452455
const array_string_exprt &s1);

src/solvers/refinement/string_constraint_generator_main.cpp

+18-13
Original file line numberDiff line numberDiff line change
@@ -130,30 +130,35 @@ symbol_exprt string_constraint_generatort::fresh_boolean(
130130
return b;
131131
}
132132

133+
exprt sum_overflows(const plus_exprt &sum)
134+
{
135+
PRECONDITION(sum.operands().size() == 2);
136+
const exprt zero = from_integer(0, sum.op0().type());
137+
const binary_relation_exprt op0_negative(sum.op0(), ID_lt, zero);
138+
const binary_relation_exprt op1_negative(sum.op1(), ID_lt, zero);
139+
const binary_relation_exprt sum_negative(sum, ID_lt, zero);
140+
141+
// overflow happens when we add two values of same sign but their sum has a
142+
// different sign
143+
return and_exprt(
144+
equal_exprt(op0_negative, op1_negative),
145+
notequal_exprt(op1_negative, sum_negative));
146+
}
147+
133148
/// Create a plus expression while adding extra constraints to axioms in order
134149
/// to prevent overflows.
135150
/// \param op1: First term of the sum
136151
/// \param op2: Second term of the sum
137152
/// \return A plus expression representing the sum of the arguments
153+
/// \deprecated
138154
plus_exprt string_constraint_generatort::plus_exprt_with_overflow_check(
139155
const exprt &op1, const exprt &op2)
140156
{
141-
plus_exprt sum(plus_exprt(op1, op2));
142-
143-
exprt zero=from_integer(0, op1.type());
144-
145-
binary_relation_exprt neg1(op1, ID_lt, zero);
146-
binary_relation_exprt neg2(op2, ID_lt, zero);
147-
binary_relation_exprt neg_sum(sum, ID_lt, zero);
148-
157+
const plus_exprt sum(plus_exprt(op1, op2));
149158
// We prevent overflows by adding the following constraint:
150159
// If the signs of the two operands are the same, then the sign of the sum
151160
// should also be the same.
152-
implies_exprt no_overflow(equal_exprt(neg1, neg2),
153-
equal_exprt(neg1, neg_sum));
154-
155-
lemmas.push_back(no_overflow);
156-
161+
lemmas.push_back(not_exprt(sum_overflows(sum)));
157162
return sum;
158163
}
159164

0 commit comments

Comments
 (0)