CBMC
bv_utils.h
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module:
4 
5 Author: 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 
23 class bv_utilst
24 {
25 public:
26  explicit bv_utilst(propt &_prop):prop(_prop) { }
27 
28  enum class representationt { SIGNED, UNSIGNED };
29 
30  static bvt build_constant(const mp_integer &i, std::size_t width);
31 
32  bvt incrementer(const bvt &op, literalt carry_in);
33  bvt inc(const bvt &op) { return incrementer(op, const_literal(true)); }
34  void incrementer(bvt &op, literalt carry_in, literalt &carry_out);
35 
36  bvt negate(const bvt &op);
37  bvt negate_no_overflow(const bvt &op);
38  bvt absolute_value(const bvt &op);
39 
40  // returns true iff unary minus will overflow
41  literalt overflow_negate(const bvt &op);
42 
43  // bit-wise negation
44  static bvt inverted(const bvt &op);
45 
47  const literalt a,
48  const literalt b,
49  const literalt carry_in,
52 
53  bvt add_sub(const bvt &op0, const bvt &op1, bool subtract);
54  bvt add_sub(const bvt &op0, const bvt &op1, literalt subtract);
56  const bvt &op0,
57  const bvt &op1,
58  bool subtract,
59  representationt rep);
61  const bvt &op0,
62  const bvt &op1,
63  bool subtract,
64  representationt rep);
65 
66  bvt add(const bvt &op0, const bvt &op1) { return add_sub(op0, op1, false); }
67  bvt sub(const bvt &op0, const bvt &op1) { return add_sub(op0, op1, true); }
68 
69  literalt overflow_add(const bvt &op0, const bvt &op1, representationt rep);
70  literalt overflow_sub(const bvt &op0, const bvt &op1, representationt rep);
71  literalt carry_out(const bvt &op0, const bvt &op1, literalt carry_in);
72 
73  enum class shiftt
74  {
76  };
77 
78  static bvt shift(const bvt &op, const shiftt shift, std::size_t distance);
79  bvt shift(const bvt &op, const shiftt shift, const bvt &distance);
80 
81  bvt unsigned_multiplier(const bvt &op0, const bvt &op1);
82  bvt signed_multiplier(const bvt &op0, const bvt &op1);
83  bvt multiplier(const bvt &op0, const bvt &op1, representationt rep);
85  const bvt &op0,
86  const bvt &op1,
87  representationt rep);
88 
89  bvt divider(const bvt &op0, const bvt &op1, representationt rep)
90  {
91  bvt res, rem;
92  divider(op0, op1, res, rem, rep);
93  return res;
94  }
95 
96  bvt remainder(const bvt &op0, const bvt &op1, representationt rep)
97  {
98  bvt res, rem;
99  divider(op0, op1, res, rem, rep);
100  return rem;
101  }
102 
103  void divider(
104  const bvt &op0,
105  const bvt &op1,
106  bvt &res,
107  bvt &rem,
108  representationt rep);
109 
110  void signed_divider(
111  const bvt &op0,
112  const bvt &op1,
113  bvt &res,
114  bvt &rem);
115 
116  void unsigned_divider(
117  const bvt &op0,
118  const bvt &op1,
119  bvt &res,
120  bvt &rem);
121 
122  #ifdef COMPACT_EQUAL_CONST
123  typedef std::set<bvt> equal_const_registeredt;
124  equal_const_registeredt equal_const_registered;
125  void equal_const_register(const bvt &var);
126 
127  typedef std::pair<bvt, bvt> var_constant_pairt;
128  typedef std::map<var_constant_pairt, literalt> equal_const_cachet;
129  equal_const_cachet equal_const_cache;
130 
131  literalt equal_const_rec(bvt &var, bvt &constant);
132  literalt equal_const(const bvt &var, const bvt &constant);
133  #endif
134 
135 
136  literalt equal(const bvt &op0, const bvt &op1);
137 
138  static inline literalt sign_bit(const bvt &op)
139  {
140  return op[op.size()-1];
141  }
142 
143  literalt is_zero(const bvt &op)
144  { return !prop.lor(op); }
145 
147  { return prop.lor(op); }
148 
150  {
151  bvt tmp=op;
152  tmp[tmp.size()-1]=!tmp[tmp.size()-1];
153  return is_zero(tmp);
154  }
155 
156  literalt is_one(const bvt &op);
157 
159  { return prop.land(op); }
160 
162  bool or_equal,
163  const bvt &bv0,
164  const bvt &bv1,
165  representationt rep);
166 
167  // id is one of ID_lt, le, gt, ge, equal, notequal
168  literalt rel(
169  const bvt &bv0,
170  irep_idt id,
171  const bvt &bv1,
172  representationt rep);
173 
174  literalt unsigned_less_than(const bvt &bv0, const bvt &bv1);
175  literalt signed_less_than(const bvt &bv0, const bvt &bv1);
176 
177  static bool is_constant(const bvt &bv);
178 
179  static bvt
180  extension(const bvt &bv, std::size_t new_size, representationt rep);
181 
182  static bvt sign_extension(const bvt &bv, std::size_t new_size)
183  {
184  return extension(bv, new_size, representationt::SIGNED);
185  }
186 
187  static bvt zero_extension(const bvt &bv, std::size_t new_size)
188  {
189  return extension(bv, new_size, representationt::UNSIGNED);
190  }
191 
192  static bvt zeros(std::size_t new_size)
193  {
194  return bvt(new_size, const_literal(false));
195  }
196 
197  void set_equal(const bvt &a, const bvt &b);
198 
199  // if cond holds, a has to be equal to b
200  void cond_implies_equal(literalt cond, const bvt &a, const bvt &b);
201 
202  bvt cond_negate(const bvt &bv, const literalt cond);
203 
204  bvt select(literalt s, const bvt &a, const bvt &b);
205 
206  // computes a[last:first]
207  static bvt extract(const bvt &a, std::size_t first, std::size_t last);
208 
209  // extracts the n most significant bits
210  static bvt extract_msb(const bvt &a, std::size_t n);
211 
212  // extracts the n least significant bits
213  static bvt extract_lsb(const bvt &a, std::size_t n);
214 
215  // put a and b together, where a comes first (lower indices)
216  static bvt concatenate(const bvt &a, const bvt &b);
217 
219  static bvt verilog_bv_normal_bits(const bvt &);
220 
221 protected:
223 
226  [[nodiscard]] std::pair<bvt, literalt>
227  adder(const bvt &op0, const bvt &op1, literalt carry_in);
228 
229  [[nodiscard]] bvt adder_no_overflow(
230  const bvt &op0,
231  const bvt &op1,
232  bool subtract,
233  representationt rep);
234 
235  [[nodiscard]] bvt adder_no_overflow(const bvt &op0, const bvt &op1);
236 
238  const bvt &op0, const bvt &op1);
239 
241  const bvt &op0, const bvt &op1);
242 
243  bvt cond_negate_no_overflow(const bvt &bv, const literalt cond);
244 
245  bvt wallace_tree(const std::vector<bvt> &pps);
246  bvt dadda_tree(const std::vector<bvt> &pps);
247 };
248 
249 #endif // CPROVER_SOLVERS_FLATTENING_BV_UTILS_H
static bvt inverted(const bvt &op)
Definition: bv_utils.cpp:637
literalt signed_less_than(const bvt &bv0, const bvt &bv1)
Definition: bv_utils.cpp:1550
static bool is_constant(const bvt &bv)
Definition: bv_utils.cpp:1579
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:297
literalt is_all_ones(const bvt &op)
Definition: bv_utils.h:158
bvt wallace_tree(const std::vector< bvt > &pps)
Definition: bv_utils.cpp:645
bv_utilst(propt &_prop)
Definition: bv_utils.h:26
literalt is_not_zero(const bvt &op)
Definition: bv_utils.h:146
static bvt verilog_bv_normal_bits(const bvt &)
Definition: bv_utils.cpp:1628
literalt is_int_min(const bvt &op)
Definition: bv_utils.h:149
static bvt extract_msb(const bvt &a, std::size_t n)
Definition: bv_utils.cpp:57
void set_equal(const bvt &a, const bvt &b)
Definition: bv_utils.cpp:34
static literalt sign_bit(const bvt &op)
Definition: bv_utils.h:138
bvt multiplier_no_overflow(const bvt &op0, const bvt &op1, representationt rep)
Definition: bv_utils.cpp:1083
bvt add_sub_no_overflow(const bvt &op0, const bvt &op1, bool subtract, representationt rep)
Definition: bv_utils.cpp:328
bvt add(const bvt &op0, const bvt &op1)
Definition: bv_utils.h:66
static bvt zero_extension(const bvt &bv, std::size_t new_size)
Definition: bv_utils.h:187
bvt absolute_value(const bvt &op)
Definition: bv_utils.cpp:1034
bvt select(literalt s, const bvt &a, const bvt &b)
If s is true, selects a otherwise selects b.
Definition: bv_utils.cpp:95
static bvt build_constant(const mp_integer &i, std::size_t width)
Definition: bv_utils.cpp:14
void signed_divider(const bvt &op0, const bvt &op1, bvt &res, bvt &rem)
Definition: bv_utils.cpp:1099
literalt is_zero(const bvt &op)
Definition: bv_utils.h:143
literalt equal(const bvt &op0, const bvt &op1)
Bit-blasting ID_equal and use in other encodings.
Definition: bv_utils.cpp:1364
bvt signed_multiplier(const bvt &op0, const bvt &op1)
Definition: bv_utils.cpp:1003
literalt overflow_add(const bvt &op0, const bvt &op1, representationt rep)
Definition: bv_utils.cpp:427
literalt is_one(const bvt &op)
Definition: bv_utils.cpp:25
bvt cond_negate_no_overflow(const bvt &bv, const literalt cond)
Definition: bv_utils.cpp:1040
literalt overflow_sub(const bvt &op0, const bvt &op1, representationt rep)
Definition: bv_utils.cpp:451
void cond_implies_equal(literalt cond, const bvt &a, const bvt &b)
Definition: bv_utils.cpp:1590
literalt unsigned_less_than(const bvt &bv0, const bvt &bv1)
Definition: bv_utils.cpp:1538
bvt signed_multiplier_no_overflow(const bvt &op0, const bvt &op1)
Definition: bv_utils.cpp:1047
bvt remainder(const bvt &op0, const bvt &op1, representationt rep)
Definition: bv_utils.h:96
bvt incrementer(const bvt &op, literalt carry_in)
Definition: bv_utils.cpp:629
bvt add_sub(const bvt &op0, const bvt &op1, bool subtract)
Definition: bv_utils.cpp:337
static bvt shift(const bvt &op, const shiftt shift, std::size_t distance)
Definition: bv_utils.cpp:537
literalt lt_or_le(bool or_equal, const bvt &bv0, const bvt &bv1, representationt rep)
Definition: bv_utils.cpp:1399
literalt overflow_negate(const bvt &op)
Definition: bv_utils.cpp:603
static bvt concatenate(const bvt &a, const bvt &b)
Definition: bv_utils.cpp:79
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:139
bvt negate_no_overflow(const bvt &op)
Definition: bv_utils.cpp:597
bvt sub(const bvt &op0, const bvt &op1)
Definition: bv_utils.h:67
static bvt extract(const bvt &a, std::size_t first, std::size_t last)
Definition: bv_utils.cpp:41
bvt inc(const bvt &op)
Definition: bv_utils.h:33
bvt unsigned_multiplier(const bvt &op0, const bvt &op1)
Definition: bv_utils.cpp:919
bvt divider(const bvt &op0, const bvt &op1, representationt rep)
Definition: bv_utils.h:89
void unsigned_divider(const bvt &op0, const bvt &op1, bvt &res, bvt &rem)
Definition: bv_utils.cpp:1152
bvt multiplier(const bvt &op0, const bvt &op1, representationt rep)
Definition: bv_utils.cpp:1069
bvt cond_negate(const bvt &bv, const literalt cond)
Definition: bv_utils.cpp:1021
bvt dadda_tree(const std::vector< bvt > &pps)
Definition: bv_utils.cpp:692
literalt carry_out(const bvt &op0, const bvt &op1, literalt carry_in)
Definition: bv_utils.cpp:313
bvt negate(const bvt &op)
Definition: bv_utils.cpp:589
static bvt extension(const bvt &bv, std::size_t new_size, representationt rep)
Definition: bv_utils.cpp:108
static bvt extract_lsb(const bvt &a, std::size_t n)
Definition: bv_utils.cpp:69
literalt verilog_bv_has_x_or_z(const bvt &)
Definition: bv_utils.cpp:1613
literalt carry(literalt a, literalt b, literalt c)
Definition: bv_utils.cpp:230
bvt unsigned_multiplier_no_overflow(const bvt &op0, const bvt &op1)
Definition: bv_utils.cpp:964
static bvt zeros(std::size_t new_size)
Definition: bv_utils.h:192
literalt rel(const bvt &bv0, irep_idt id, const bvt &bv1, representationt rep)
Definition: bv_utils.cpp:1557
propt & prop
Definition: bv_utils.h:222
bvt adder_no_overflow(const bvt &op0, const bvt &op1, bool subtract, representationt rep)
Definition: bv_utils.cpp:476
static bvt sign_extension(const bvt &bv, std::size_t new_size)
Definition: bv_utils.h:182
bvt saturating_add_sub(const bvt &op0, const bvt &op1, bool subtract, representationt rep)
Definition: bv_utils.cpp:358
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