@@ -130,30 +130,35 @@ symbol_exprt string_constraint_generatort::fresh_boolean(
130
130
return b;
131
131
}
132
132
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
+
133
148
// / Create a plus expression while adding extra constraints to axioms in order
134
149
// / to prevent overflows.
135
150
// / \param op1: First term of the sum
136
151
// / \param op2: Second term of the sum
137
152
// / \return A plus expression representing the sum of the arguments
153
+ // / \deprecated
138
154
plus_exprt string_constraint_generatort::plus_exprt_with_overflow_check (
139
155
const exprt &op1, const exprt &op2)
140
156
{
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));
149
158
// We prevent overflows by adding the following constraint:
150
159
// If the signs of the two operands are the same, then the sign of the sum
151
160
// 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)));
157
162
return sum;
158
163
}
159
164
0 commit comments