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 
224  void adder(
225  bvt &sum,
226  const bvt &op,
227  literalt carry_in,
229 
230  void adder_no_overflow(
231  bvt &sum,
232  const bvt &op,
233  bool subtract,
234  representationt rep);
235 
236  void adder_no_overflow(bvt &sum, const bvt &op);
237 
239  const bvt &op0, const bvt &op1);
240 
242  const bvt &op0, const bvt &op1);
243 
244  bvt cond_negate_no_overflow(const bvt &bv, const literalt cond);
245 
246  bvt wallace_tree(const std::vector<bvt> &pps);
247 };
248 
249 #endif // CPROVER_SOLVERS_FLATTENING_BV_UTILS_H
bv_utilst::shiftt::SHIFT_LEFT
@ SHIFT_LEFT
dstringt
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:36
bv_utilst::cond_negate_no_overflow
bvt cond_negate_no_overflow(const bvt &bv, const literalt cond)
Definition: bv_utils.cpp:839
mp_integer
BigInt mp_integer
Definition: smt_terms.h:17
bv_utilst::zero_extension
static bvt zero_extension(const bvt &bv, std::size_t new_size)
Definition: bv_utils.h:187
mp_arith.h
bv_utilst::extract_lsb
static bvt extract_lsb(const bvt &a, std::size_t n)
Definition: bv_utils.cpp:66
bv_utilst::cond_implies_equal
void cond_implies_equal(literalt cond, const bvt &a, const bvt &b)
Definition: bv_utils.cpp:1370
bv_utilst::carry
literalt carry(literalt a, literalt b, literalt c)
Definition: bv_utils.cpp:227
bv_utilst::full_adder
literalt full_adder(const literalt a, const literalt b, const literalt carry_in, literalt &carry_out)
Definition: bv_utils.cpp:136
bv_utilst::inc
bvt inc(const bvt &op)
Definition: bv_utils.h:33
bv_utilst::is_not_zero
literalt is_not_zero(const bvt &op)
Definition: bv_utils.h:146
bv_utilst::carry_out
literalt carry_out(const bvt &op0, const bvt &op1, literalt carry_in)
Definition: bv_utils.cpp:309
bvt
std::vector< literalt > bvt
Definition: literal.h:201
bv_utilst::negate_no_overflow
bvt negate_no_overflow(const bvt &op)
Definition: bv_utils.cpp:607
bv_utilst::adder_no_overflow
void adder_no_overflow(bvt &sum, const bvt &op, bool subtract, representationt rep)
Definition: bv_utils.cpp:482
bv_utilst::signed_multiplier
bvt signed_multiplier(const bvt &op0, const bvt &op1)
Definition: bv_utils.cpp:802
bv_utilst::shiftt::SHIFT_ARIGHT
@ SHIFT_ARIGHT
bv_utilst::zeros
static bvt zeros(std::size_t new_size)
Definition: bv_utils.h:192
bv_utilst::representationt::UNSIGNED
@ UNSIGNED
bv_utilst::set_equal
void set_equal(const bvt &a, const bvt &b)
Definition: bv_utils.cpp:31
bv_utilst::signed_divider
void signed_divider(const bvt &op0, const bvt &op1, bvt &res, bvt &rem)
Definition: bv_utils.cpp:898
bv_utilst::sign_extension
static bvt sign_extension(const bvt &bv, std::size_t new_size)
Definition: bv_utils.h:182
propt::lor
virtual literalt lor(literalt a, literalt b)=0
bv_utilst::prop
propt & prop
Definition: bv_utils.h:222
bv_utilst::incrementer
bvt incrementer(const bvt &op, literalt carry_in)
Definition: bv_utils.cpp:639
bv_utilst::inverted
static bvt inverted(const bvt &op)
Definition: bv_utils.cpp:647
bv_utilst::absolute_value
bvt absolute_value(const bvt &op)
Definition: bv_utils.cpp:833
bv_utilst::unsigned_multiplier
bvt unsigned_multiplier(const bvt &op0, const bvt &op1)
Definition: bv_utils.cpp:701
propt::land
virtual literalt land(literalt a, literalt b)=0
bv_utilst::select
bvt select(literalt s, const bvt &a, const bvt &b)
If s is true, selects a otherwise selects b.
Definition: bv_utils.cpp:92
bv_utilst
Definition: bv_utils.h:23
bv_utilst::shift
static bvt shift(const bvt &op, const shiftt shift, std::size_t distance)
Definition: bv_utils.cpp:547
bv_utilst::unsigned_less_than
literalt unsigned_less_than(const bvt &bv0, const bvt &bv1)
Definition: bv_utils.cpp:1318
bv_utilst::unsigned_divider
void unsigned_divider(const bvt &op0, const bvt &op1, bvt &res, bvt &rem)
Definition: bv_utils.cpp:951
bv_utilst::is_int_min
literalt is_int_min(const bvt &op)
Definition: bv_utils.h:149
bv_utilst::add
bvt add(const bvt &op0, const bvt &op1)
Definition: bv_utils.h:66
bv_utilst::representationt::SIGNED
@ SIGNED
bv_utilst::representationt
representationt
Definition: bv_utils.h:28
bv_utilst::cond_negate
bvt cond_negate(const bvt &bv, const literalt cond)
Definition: bv_utils.cpp:820
bv_utilst::overflow_sub
literalt overflow_sub(const bvt &op0, const bvt &op1, representationt rep)
Definition: bv_utils.cpp:457
prop.h
const_literal
literalt const_literal(bool value)
Definition: literal.h:188
bv_utilst::overflow_add
literalt overflow_add(const bvt &op0, const bvt &op1, representationt rep)
Definition: bv_utils.cpp:433
bv_utilst::signed_multiplier_no_overflow
bvt signed_multiplier_no_overflow(const bvt &op0, const bvt &op1)
Definition: bv_utils.cpp:846
bv_utilst::bv_utilst
bv_utilst(propt &_prop)
Definition: bv_utils.h:26
bv_utilst::unsigned_multiplier_no_overflow
bvt unsigned_multiplier_no_overflow(const bvt &op0, const bvt &op1)
Definition: bv_utils.cpp:763
bv_utilst::concatenate
static bvt concatenate(const bvt &a, const bvt &b)
Definition: bv_utils.cpp:76
bv_utilst::lt_or_le
literalt lt_or_le(bool or_equal, const bvt &bv0, const bvt &bv1, representationt rep)
Definition: bv_utils.cpp:1200
bv_utilst::add_sub
bvt add_sub(const bvt &op0, const bvt &op1, bool subtract)
Definition: bv_utils.cpp:335
bv_utilst::wallace_tree
bvt wallace_tree(const std::vector< bvt > &pps)
Definition: bv_utils.cpp:655
bv_utilst::remainder
bvt remainder(const bvt &op0, const bvt &op1, representationt rep)
Definition: bv_utils.h:96
bv_utilst::multiplier_no_overflow
bvt multiplier_no_overflow(const bvt &op0, const bvt &op1, representationt rep)
Definition: bv_utils.cpp:882
bv_utilst::build_constant
static bvt build_constant(const mp_integer &i, std::size_t width)
Definition: bv_utils.cpp:11
bv_utilst::rel
literalt rel(const bvt &bv0, irep_idt id, const bvt &bv1, representationt rep)
Definition: bv_utils.cpp:1337
bv_utilst::shiftt
shiftt
Definition: bv_utils.h:73
propt
TO_BE_DOCUMENTED.
Definition: prop.h:22
bv_utilst::negate
bvt negate(const bvt &op)
Definition: bv_utils.cpp:599
bv_utilst::is_all_ones
literalt is_all_ones(const bvt &op)
Definition: bv_utils.h:158
bv_utilst::overflow_negate
literalt overflow_negate(const bvt &op)
Definition: bv_utils.cpp:613
bv_utilst::shiftt::ROTATE_RIGHT
@ ROTATE_RIGHT
bv_utilst::sign_bit
static literalt sign_bit(const bvt &op)
Definition: bv_utils.h:138
bv_utilst::extension
static bvt extension(const bvt &bv, std::size_t new_size, representationt rep)
Definition: bv_utils.cpp:105
literalt
Definition: literal.h:25
bv_utilst::divider
bvt divider(const bvt &op0, const bvt &op1, representationt rep)
Definition: bv_utils.h:89
bv_utilst::extract
static bvt extract(const bvt &a, std::size_t first, std::size_t last)
Definition: bv_utils.cpp:38
bv_utilst::signed_less_than
literalt signed_less_than(const bvt &bv0, const bvt &bv1)
Definition: bv_utils.cpp:1330
bv_utilst::shiftt::SHIFT_LRIGHT
@ SHIFT_LRIGHT
bv_utilst::multiplier
bvt multiplier(const bvt &op0, const bvt &op1, representationt rep)
Definition: bv_utils.cpp:868
bv_utilst::extract_msb
static bvt extract_msb(const bvt &a, std::size_t n)
Definition: bv_utils.cpp:54
bv_utilst::verilog_bv_normal_bits
static bvt verilog_bv_normal_bits(const bvt &)
Definition: bv_utils.cpp:1408
bv_utilst::adder
void adder(bvt &sum, const bvt &op, literalt carry_in, literalt &carry_out)
Definition: bv_utils.cpp:293
bv_utilst::add_sub_no_overflow
bvt add_sub_no_overflow(const bvt &op0, const bvt &op1, bool subtract, representationt rep)
Definition: bv_utils.cpp:324
bv_utilst::equal
literalt equal(const bvt &op0, const bvt &op1)
Bit-blasting ID_equal and use in other encodings.
Definition: bv_utils.cpp:1165
bv_utilst::is_zero
literalt is_zero(const bvt &op)
Definition: bv_utils.h:143
bv_utilst::shiftt::ROTATE_LEFT
@ ROTATE_LEFT
bv_utilst::saturating_add_sub
bvt saturating_add_sub(const bvt &op0, const bvt &op1, bool subtract, representationt rep)
Definition: bv_utils.cpp:363
bv_utilst::verilog_bv_has_x_or_z
literalt verilog_bv_has_x_or_z(const bvt &)
Definition: bv_utils.cpp:1393
bv_utilst::is_one
literalt is_one(const bvt &op)
Definition: bv_utils.cpp:22
bv_utilst::sub
bvt sub(const bvt &op0, const bvt &op1)
Definition: bv_utils.h:67
bv_utilst::is_constant
static bool is_constant(const bvt &bv)
Definition: bv_utils.cpp:1359