CBMC
Loading...
Searching...
No Matches
bv_utils.h
Go to the documentation of this file.
1/*******************************************************************\
2
3Module:
4
5Author: Daniel Kroening, kroening@kroening.com
6
7\*******************************************************************/
8
9
10#ifndef CPROVER_SOLVERS_FLATTENING_BV_UTILS_H
11#define CPROVER_SOLVERS_FLATTENING_BV_UTILS_H
12
13#include <util/mp_arith.h>
14
15#include <solvers/prop/prop.h>
16
17// Shares variables between var == const tests for registered variables.
18// Gives ~15% memory savings on some programs using constant arrays
19// but seems to give a run-time penalty.
20// #define COMPACT_EQUAL_CONST
21
22
24{
25public:
26 explicit bv_utilst(propt &_prop):prop(_prop) { }
27
29
30 static bvt build_constant(const mp_integer &i, std::size_t width);
31
35 static mp_integer from_constant(const bvt &bv);
36
38 bvt inc(const bvt &op) { return incrementer(op, const_literal(true)); }
40
41 bvt negate(const bvt &op);
42 bvt negate_no_overflow(const bvt &op);
43 bvt absolute_value(const bvt &op);
44
45 // returns true iff unary minus will overflow
46 literalt overflow_negate(const bvt &op);
47
48 // bit-wise negation
49 static bvt inverted(const bvt &op);
50
52 const literalt a,
53 const literalt b,
54 const literalt carry_in,
57
58 bvt add_sub(const bvt &op0, const bvt &op1, bool subtract);
59 bvt add_sub(const bvt &op0, const bvt &op1, literalt subtract);
61 const bvt &op0,
62 const bvt &op1,
63 bool subtract,
64 representationt rep);
66 const bvt &op0,
67 const bvt &op1,
68 bool subtract,
69 representationt rep);
70
71 bvt add(const bvt &op0, const bvt &op1) { return add_sub(op0, op1, false); }
72 bvt sub(const bvt &op0, const bvt &op1) { return add_sub(op0, op1, true); }
73
74 literalt overflow_add(const bvt &op0, const bvt &op1, representationt rep);
75 literalt overflow_sub(const bvt &op0, const bvt &op1, representationt rep);
76 literalt carry_out(const bvt &op0, const bvt &op1, literalt carry_in);
77
82
83 static bvt shift(const bvt &op, const shiftt shift, std::size_t distance);
84 bvt shift(const bvt &op, const shiftt shift, const bvt &distance);
85
86 bvt unsigned_multiplier(const bvt &op0, const bvt &op1);
87 bvt signed_multiplier(const bvt &op0, const bvt &op1);
88 bvt multiplier(const bvt &op0, const bvt &op1, representationt rep);
90 const bvt &op0,
91 const bvt &op1,
92 representationt rep);
93
94 bvt divider(const bvt &op0, const bvt &op1, representationt rep)
95 {
96 bvt res, rem;
97 divider(op0, op1, res, rem, rep);
98 return res;
99 }
100
101 bvt remainder(const bvt &op0, const bvt &op1, representationt rep)
102 {
103 bvt res, rem;
104 divider(op0, op1, res, rem, rep);
105 return rem;
106 }
107
108 void divider(
109 const bvt &op0,
110 const bvt &op1,
111 bvt &res,
112 bvt &rem,
113 representationt rep);
114
115 void signed_divider(
116 const bvt &op0,
117 const bvt &op1,
118 bvt &res,
119 bvt &rem);
120
121 void unsigned_divider(
122 const bvt &op0,
123 const bvt &op1,
124 bvt &res,
125 bvt &rem);
126
127 #ifdef COMPACT_EQUAL_CONST
128 typedef std::set<bvt> equal_const_registeredt;
130 void equal_const_register(const bvt &var);
131
132 typedef std::pair<bvt, bvt> var_constant_pairt;
133 typedef std::map<var_constant_pairt, literalt> equal_const_cachet;
135
136 literalt equal_const_rec(bvt &var, bvt &constant);
137 literalt equal_const(const bvt &var, const bvt &constant);
138 #endif
139
140
141 literalt equal(const bvt &op0, const bvt &op1);
142
143 static inline literalt sign_bit(const bvt &op)
144 {
145 return op.back();
146 }
147
149 { return !prop.lor(op); }
150
152 { return prop.lor(op); }
153
155 {
156 bvt tmp=op;
157 tmp[tmp.size()-1]=!tmp[tmp.size()-1];
158 return is_zero(tmp);
159 }
160
161 literalt is_one(const bvt &op);
162
164 { return prop.land(op); }
165
167 bool or_equal,
168 const bvt &bv0,
169 const bvt &bv1,
170 representationt rep);
171
172 // id is one of ID_lt, le, gt, ge, equal, notequal
174 const bvt &bv0,
175 irep_idt id,
176 const bvt &bv1,
177 representationt rep);
178
179 literalt unsigned_less_than(const bvt &bv0, const bvt &bv1);
180 literalt signed_less_than(const bvt &bv0, const bvt &bv1);
181
182 static bool is_constant(const bvt &bv);
183
184 static bvt
185 extension(const bvt &bv, std::size_t new_size, representationt rep);
186
187 static bvt sign_extension(const bvt &bv, std::size_t new_size)
188 {
190 }
191
192 static bvt zero_extension(const bvt &bv, std::size_t new_size)
193 {
195 }
196
197 static bvt zeros(std::size_t new_size)
198 {
199 return bvt(new_size, const_literal(false));
200 }
201
202 void set_equal(const bvt &a, const bvt &b);
203
204 // if cond holds, a has to be equal to b
205 void cond_implies_equal(literalt cond, const bvt &a, const bvt &b);
206
207 bvt cond_negate(const bvt &bv, const literalt cond);
208
209 bvt select(literalt s, const bvt &a, const bvt &b);
210
211 // computes a[last:first]
212 static bvt extract(const bvt &a, std::size_t first, std::size_t last);
213
214 // extracts the n most significant bits
215 static bvt extract_msb(const bvt &a, std::size_t n);
216
217 // extracts the n least significant bits
218 static bvt extract_lsb(const bvt &a, std::size_t n);
219
220 // put a and b together, where a comes first (lower indices)
221 static bvt concatenate(const bvt &a, const bvt &b);
222
224 static bvt verilog_bv_normal_bits(const bvt &);
225
230 bvt popcount(const bvt &bv);
231
232protected:
234
237 [[nodiscard]] std::pair<bvt, literalt>
238 adder(const bvt &op0, const bvt &op1, literalt carry_in);
239
241 const bvt &op0,
242 const bvt &op1,
243 bool subtract,
244 representationt rep);
245
246 [[nodiscard]] bvt adder_no_overflow(const bvt &op0, const bvt &op1);
247
249 const bvt &op0, const bvt &op1);
250
252 const bvt &op0, const bvt &op1);
253
254 bvt cond_negate_no_overflow(const bvt &bv, const literalt cond);
255
256 bvt wallace_tree(const std::vector<bvt> &pps);
257 bvt dadda_tree(const std::vector<bvt> &pps);
258};
259
260#endif // CPROVER_SOLVERS_FLATTENING_BV_UTILS_H
ait supplies three of the four components needed: an abstract interpreter (in this case handling func...
Definition ai.h:566
static bvt inverted(const bvt &op)
Definition bv_utils.cpp:648
literalt signed_less_than(const bvt &bv0, const bvt &bv1)
static bool is_constant(const bvt &bv)
std::pair< bvt, literalt > adder(const bvt &op0, const bvt &op1, literalt carry_in)
Return the sum and carry-out when adding op0 and op1 under initial carry carry_in.
Definition bv_utils.cpp:309
literalt is_all_ones(const bvt &op)
Definition bv_utils.h:163
bvt wallace_tree(const std::vector< bvt > &pps)
Definition bv_utils.cpp:656
bv_utilst(propt &_prop)
Definition bv_utils.h:26
literalt is_not_zero(const bvt &op)
Definition bv_utils.h:151
static bvt verilog_bv_normal_bits(const bvt &)
literalt is_int_min(const bvt &op)
Definition bv_utils.h:154
static bvt extract_msb(const bvt &a, std::size_t n)
Definition bv_utils.cpp:69
static mp_integer from_constant(const bvt &bv)
Returns the unsigned integer value of the constant bitvector bv.
Definition bv_utils.cpp:27
void set_equal(const bvt &a, const bvt &b)
Definition bv_utils.cpp:46
static literalt sign_bit(const bvt &op)
Definition bv_utils.h:143
bvt multiplier_no_overflow(const bvt &op0, const bvt &op1, representationt rep)
bvt add_sub_no_overflow(const bvt &op0, const bvt &op1, bool subtract, representationt rep)
Definition bv_utils.cpp:340
bvt add(const bvt &op0, const bvt &op1)
Definition bv_utils.h:71
static bvt zero_extension(const bvt &bv, std::size_t new_size)
Definition bv_utils.h:192
bvt absolute_value(const bvt &op)
bvt select(literalt s, const bvt &a, const bvt &b)
If s is true, selects a otherwise selects b.
Definition bv_utils.cpp:107
static bvt build_constant(const mp_integer &i, std::size_t width)
Definition bv_utils.cpp:16
void signed_divider(const bvt &op0, const bvt &op1, bvt &res, bvt &rem)
literalt is_zero(const bvt &op)
Definition bv_utils.h:148
literalt equal(const bvt &op0, const bvt &op1)
Bit-blasting ID_equal and use in other encodings.
bvt popcount(const bvt &bv)
Symbolic implementation of popcount (count of 1 bits in a bit vector) Based on the pop0 algorithm fro...
bvt signed_multiplier(const bvt &op0, const bvt &op1)
literalt overflow_add(const bvt &op0, const bvt &op1, representationt rep)
Definition bv_utils.cpp:439
literalt is_one(const bvt &op)
Definition bv_utils.cpp:37
bvt cond_negate_no_overflow(const bvt &bv, const literalt cond)
literalt overflow_sub(const bvt &op0, const bvt &op1, representationt rep)
Definition bv_utils.cpp:462
void cond_implies_equal(literalt cond, const bvt &a, const bvt &b)
literalt unsigned_less_than(const bvt &bv0, const bvt &bv1)
bvt signed_multiplier_no_overflow(const bvt &op0, const bvt &op1)
bvt remainder(const bvt &op0, const bvt &op1, representationt rep)
Definition bv_utils.h:101
bvt incrementer(const bvt &op, literalt carry_in)
Definition bv_utils.cpp:640
bvt add_sub(const bvt &op0, const bvt &op1, bool subtract)
Definition bv_utils.cpp:349
static bvt shift(const bvt &op, const shiftt shift, std::size_t distance)
Definition bv_utils.cpp:548
literalt lt_or_le(bool or_equal, const bvt &bv0, const bvt &bv1, representationt rep)
literalt overflow_negate(const bvt &op)
Definition bv_utils.cpp:614
static bvt concatenate(const bvt &a, const bvt &b)
Definition bv_utils.cpp:91
representationt
Definition bv_utils.h:28
literalt full_adder(const literalt a, const literalt b, const literalt carry_in, literalt &carry_out)
Definition bv_utils.cpp:151
bvt negate_no_overflow(const bvt &op)
Definition bv_utils.cpp:608
bvt sub(const bvt &op0, const bvt &op1)
Definition bv_utils.h:72
static bvt extract(const bvt &a, std::size_t first, std::size_t last)
Definition bv_utils.cpp:53
bvt inc(const bvt &op)
Definition bv_utils.h:38
bvt unsigned_multiplier(const bvt &op0, const bvt &op1)
Definition bv_utils.cpp:930
bvt divider(const bvt &op0, const bvt &op1, representationt rep)
Definition bv_utils.h:94
void unsigned_divider(const bvt &op0, const bvt &op1, bvt &res, bvt &rem)
bvt multiplier(const bvt &op0, const bvt &op1, representationt rep)
bvt cond_negate(const bvt &bv, const literalt cond)
bvt dadda_tree(const std::vector< bvt > &pps)
Definition bv_utils.cpp:703
literalt carry_out(const bvt &op0, const bvt &op1, literalt carry_in)
Definition bv_utils.cpp:325
bvt negate(const bvt &op)
Definition bv_utils.cpp:600
static bvt extension(const bvt &bv, std::size_t new_size, representationt rep)
Definition bv_utils.cpp:120
static bvt extract_lsb(const bvt &a, std::size_t n)
Definition bv_utils.cpp:81
literalt verilog_bv_has_x_or_z(const bvt &)
literalt carry(literalt a, literalt b, literalt c)
Definition bv_utils.cpp:242
bvt unsigned_multiplier_no_overflow(const bvt &op0, const bvt &op1)
Definition bv_utils.cpp:975
static bvt zeros(std::size_t new_size)
Definition bv_utils.h:197
literalt rel(const bvt &bv0, irep_idt id, const bvt &bv1, representationt rep)
propt & prop
Definition bv_utils.h:233
bvt adder_no_overflow(const bvt &op0, const bvt &op1, bool subtract, representationt rep)
Definition bv_utils.cpp:487
static bvt sign_extension(const bvt &bv, std::size_t new_size)
Definition bv_utils.h:187
bvt saturating_add_sub(const bvt &op0, const bvt &op1, bool subtract, representationt rep)
Definition bv_utils.cpp:370
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition dstring.h:38
TO_BE_DOCUMENTED.
Definition prop.h:25
virtual literalt land(literalt a, literalt b)=0
virtual literalt lor(literalt a, literalt b)=0
std::vector< literalt > bvt
Definition literal.h:201
literalt const_literal(bool value)
Definition literal.h:188
BigInt mp_integer
Definition smt_terms.h:17