ported my bv-core branch from svn to git
[cvc5.git] / src / util / bitvector.h
1 /********************* */
2 /*! \file bitvector.h
3 ** \verbatim
4 ** Original author: dejan
5 ** Major contributors: mdeters, lianah
6 ** Minor contributors (to current version): cconway
7 ** This file is part of the CVC4 prototype.
8 ** Copyright (c) 2009-2012 New York University and The University of Iowa
9 ** See the file COPYING in the top-level source directory for licensing
10 ** information.\endverbatim
11 **
12 ** \brief [[ Add one-line brief description here ]]
13 **
14 ** [[ Add lengthier description here ]]
15 ** \todo document this file
16 **/
17
18 #include "cvc4_public.h"
19
20 #ifndef __CVC4__BITVECTOR_H
21 #define __CVC4__BITVECTOR_H
22
23 #include <iostream>
24 #include "util/exception.h"
25 #include "util/integer.h"
26
27 namespace CVC4 {
28
29 class CVC4_PUBLIC BitVector {
30
31 private:
32
33 /*
34 Class invariants:
35 * no overflows: 2^d_size < d_value
36 * no negative numbers: d_value >= 0
37 */
38 unsigned d_size;
39 Integer d_value;
40
41 Integer toSignedInt() const {
42 // returns Integer corresponding to two's complement interpretation of bv
43 unsigned size = d_size;
44 Integer sign_bit = d_value.extractBitRange(1,size-1);
45 Integer val = d_value.extractBitRange(size-1, 0);
46 Integer res = Integer(-1) * sign_bit.multiplyByPow2(size - 1) + val;
47 return res;
48 }
49
50
51 public:
52
53 BitVector(unsigned size, const Integer& val):
54 d_size(size),
55 d_value(val.modByPow2(size))
56 {}
57
58 BitVector(unsigned size = 0)
59 : d_size(size), d_value(0) {}
60
61 BitVector(unsigned size, unsigned int z)
62 : d_size(size), d_value(z) {
63 d_value = d_value.modByPow2(size);
64 }
65
66 BitVector(unsigned size, unsigned long int z)
67 : d_size(size), d_value(z) {
68 d_value = d_value.modByPow2(size);
69 }
70
71 BitVector(unsigned size, const BitVector& q)
72 : d_size(size), d_value(q.d_value) {}
73
74 BitVector(const std::string& num, unsigned base = 2);
75
76 ~BitVector() {}
77
78 Integer toInteger() const {
79 return d_value;
80 }
81
82 BitVector& operator =(const BitVector& x) {
83 if(this == &x)
84 return *this;
85 d_size = x.d_size;
86 d_value = x.d_value;
87 return *this;
88 }
89
90 bool operator ==(const BitVector& y) const {
91 if (d_size != y.d_size) return false;
92 return d_value == y.d_value;
93 }
94
95 bool operator !=(const BitVector& y) const {
96 if (d_size != y.d_size) return true;
97 return d_value != y.d_value;
98 }
99
100 BitVector concat (const BitVector& other) const {
101 return BitVector(d_size + other.d_size, (d_value.multiplyByPow2(other.d_size)) + other.d_value);
102 }
103
104 BitVector extract(unsigned high, unsigned low) const {
105 return BitVector(high - low + 1, d_value.extractBitRange(high - low + 1, low));
106 }
107
108 /*
109 Bitwise operations on BitVectors
110 */
111
112 // xor
113 BitVector operator ^(const BitVector& y) const {
114 CheckArgument(d_size == y.d_size, y);
115 return BitVector(d_size, d_value.bitwiseXor(y.d_value));
116 }
117
118 // or
119 BitVector operator |(const BitVector& y) const {
120 CheckArgument(d_size == y.d_size, y);
121 return BitVector(d_size, d_value.bitwiseOr(y.d_value));
122 }
123
124 // and
125 BitVector operator &(const BitVector& y) const {
126 CheckArgument(d_size == y.d_size, y);
127 return BitVector(d_size, d_value.bitwiseAnd(y.d_value));
128 }
129
130 // not
131 BitVector operator ~() const {
132 return BitVector(d_size, d_value.bitwiseNot());
133 }
134
135 /*
136 Arithmetic operations on BitVectors
137 */
138
139
140 bool operator <(const BitVector& y) const {
141 return d_value < y.d_value;
142 }
143
144 bool operator >(const BitVector& y) const {
145 return d_value > y.d_value ;
146 }
147
148 bool operator <=(const BitVector& y) const {
149 return d_value <= y.d_value;
150 }
151
152 bool operator >=(const BitVector& y) const {
153 return d_value >= y.d_value ;
154 }
155
156
157 BitVector operator +(const BitVector& y) const {
158 CheckArgument(d_size == y.d_size, y);
159 Integer sum = d_value + y.d_value;
160 return BitVector(d_size, sum);
161 }
162
163 BitVector operator -(const BitVector& y) const {
164 CheckArgument(d_size == y.d_size, y);
165 // to maintain the invariant that we are only adding BitVectors of the
166 // same size
167 BitVector one(d_size, Integer(1));
168 return *this + ~y + one;
169 }
170
171 BitVector operator -() const {
172 BitVector one(d_size, Integer(1));
173 return ~(*this) + one;
174 }
175
176 BitVector operator *(const BitVector& y) const {
177 CheckArgument(d_size == y.d_size, y);
178 Integer prod = d_value * y.d_value;
179 return BitVector(d_size, prod);
180 }
181
182 BitVector setBit(uint32_t i) const {
183 CheckArgument(i < d_size, i);
184 Integer res = d_value.setBit(i);
185 return BitVector(d_size, res);
186 }
187
188 bool isBitSet(uint32_t i) const {
189 CheckArgument(i < d_size, i);
190 return d_value.isBitSet(i);
191 }
192
193 BitVector unsignedDiv (const BitVector& y) const {
194 CheckArgument(d_size == y.d_size, y);
195 // TODO: decide whether we really want these semantics
196 if (y.d_value == 0) {
197 return BitVector(d_size, Integer(0));
198 }
199 CheckArgument(d_value >= 0, this);
200 CheckArgument(y.d_value > 0, y);
201 return BitVector(d_size, d_value.floorDivideQuotient(y.d_value));
202 }
203
204 BitVector unsignedRem(const BitVector& y) const {
205 CheckArgument(d_size == y.d_size, y);
206 // TODO: decide whether we really want these semantics
207 if (y.d_value == 0) {
208 return BitVector(d_size, d_value);
209 }
210 CheckArgument(d_value >= 0, this);
211 CheckArgument(y.d_value > 0, y);
212 return BitVector(d_size, d_value.floorDivideRemainder(y.d_value));
213 }
214
215
216 bool signedLessThan(const BitVector& y) const {
217 CheckArgument(d_size == y.d_size, y);
218 CheckArgument(d_value >= 0, this);
219 CheckArgument(y.d_value >= 0, y);
220 Integer a = (*this).toSignedInt();
221 Integer b = y.toSignedInt();
222
223 return a < b;
224 }
225
226 bool unsignedLessThan(const BitVector& y) const {
227 CheckArgument(d_size == y.d_size, y);
228 CheckArgument(d_value >= 0, this);
229 CheckArgument(y.d_value >= 0, y);
230 return d_value < y.d_value;
231 }
232
233 bool signedLessThanEq(const BitVector& y) const {
234 CheckArgument(d_size == y.d_size, y);
235 CheckArgument(d_value >= 0, this);
236 CheckArgument(y.d_value >= 0, y);
237 Integer a = (*this).toSignedInt();
238 Integer b = y.toSignedInt();
239
240 return a <= b;
241 }
242
243 bool unsignedLessThanEq(const BitVector& y) const {
244 CheckArgument(d_size == y.d_size, this);
245 CheckArgument(d_value >= 0, this);
246 CheckArgument(y.d_value >= 0, y);
247 return d_value <= y.d_value;
248 }
249
250
251 /*
252 Extend operations
253 */
254
255 BitVector zeroExtend(unsigned amount) const {
256 return BitVector(d_size + amount, d_value);
257 }
258
259 BitVector signExtend(unsigned amount) const {
260 Integer sign_bit = d_value.extractBitRange(1, d_size -1);
261 if(sign_bit == Integer(0)) {
262 return BitVector(d_size + amount, d_value);
263 } else {
264 Integer val = d_value.oneExtend(d_size, amount);
265 return BitVector(d_size+ amount, val);
266 }
267 }
268
269 /*
270 Shifts on BitVectors
271 */
272
273 BitVector leftShift(const BitVector& y) const {
274 if (y.d_value > Integer(d_size)) {
275 return BitVector(d_size, Integer(0));
276 }
277 if (y.d_value == 0) {
278 return *this;
279 }
280
281 // making sure we don't lose information casting
282 CheckArgument(y.d_value < Integer(1).multiplyByPow2(32), y);
283 uint32_t amount = y.d_value.toUnsignedInt();
284 Integer res = d_value.multiplyByPow2(amount);
285 return BitVector(d_size, res);
286 }
287
288 BitVector logicalRightShift(const BitVector& y) const {
289 if(y.d_value > Integer(d_size)) {
290 return BitVector(d_size, Integer(0));
291 }
292
293 // making sure we don't lose information casting
294 CheckArgument(y.d_value < Integer(1).multiplyByPow2(32), y);
295 uint32_t amount = y.d_value.toUnsignedInt();
296 Integer res = d_value.divByPow2(amount);
297 return BitVector(d_size, res);
298 }
299
300 BitVector arithRightShift(const BitVector& y) const {
301 Integer sign_bit = d_value.extractBitRange(1, d_size - 1);
302 if(y.d_value > Integer(d_size)) {
303 if(sign_bit == Integer(0)) {
304 return BitVector(d_size, Integer(0));
305 } else {
306 return BitVector(d_size, Integer(d_size).multiplyByPow2(d_size) -1 );
307 }
308 }
309
310 if (y.d_value == 0) {
311 return *this;
312 }
313
314 // making sure we don't lose information casting
315 CheckArgument(y.d_value < Integer(1).multiplyByPow2(32), y);
316
317 uint32_t amount = y.d_value.toUnsignedInt();
318 Integer rest = d_value.divByPow2(amount);
319
320 if(sign_bit == Integer(0)) {
321 return BitVector(d_size, rest);
322 }
323 Integer res = rest.oneExtend(d_size - amount, amount);
324 return BitVector(d_size, res);
325 }
326
327
328 /*
329 Convenience functions
330 */
331
332 size_t hash() const {
333 return d_value.hash() + d_size;
334 }
335
336 std::string toString(unsigned int base = 2) const {
337 std::string str = d_value.toString(base);
338 if( base == 2 && d_size > str.size() ) {
339 std::string zeroes;
340 for( unsigned int i=0; i < d_size - str.size(); ++i ) {
341 zeroes.append("0");
342 }
343 return zeroes + str;
344 } else {
345 return str;
346 }
347 }
348
349 unsigned getSize() const {
350 return d_size;
351 }
352
353 const Integer& getValue() const {
354 return d_value;
355 }
356
357 /**
358 Returns k is the integer is equal to 2^{k-1} and zero
359 otherwise
360 @return k if the integer is equal to 2^{k-1} and zero otherwise
361 */
362 unsigned isPow2() {
363 return d_value.isPow2();
364 }
365
366 };/* class BitVector */
367
368
369
370 inline BitVector::BitVector(const std::string& num, unsigned base) {
371 CheckArgument(base == 2 || base == 16, base);
372
373 if( base == 2 ) {
374 d_size = num.size();
375 } else {
376 d_size = num.size() * 4;
377 }
378
379 d_value = Integer(num, base);
380 }/* BitVector::BitVector() */
381
382
383 /**
384 * Hash function for the BitVector constants.
385 */
386 struct CVC4_PUBLIC BitVectorHashFunction {
387 inline size_t operator()(const BitVector& bv) const {
388 return bv.hash();
389 }
390 };/* struct BitVectorHashFunction */
391
392 /**
393 * The structure representing the extraction operation for bit-vectors. The
394 * operation map bit-vectors to bit-vector of size <code>high - low + 1</code>
395 * by taking the bits at indices <code>high ... low</code>
396 */
397 struct CVC4_PUBLIC BitVectorExtract {
398 /** The high bit of the range for this extract */
399 unsigned high;
400 /** The low bit of the range for this extract */
401 unsigned low;
402
403 BitVectorExtract(unsigned high, unsigned low)
404 : high(high), low(low) {}
405
406 bool operator == (const BitVectorExtract& extract) const {
407 return high == extract.high && low == extract.low;
408 }
409 };/* struct BitVectorExtract */
410
411 /**
412 * Hash function for the BitVectorExtract objects.
413 */
414 struct CVC4_PUBLIC BitVectorExtractHashFunction {
415 size_t operator()(const BitVectorExtract& extract) const {
416 size_t hash = extract.low;
417 hash ^= extract.high + 0x9e3779b9 + (hash << 6) + (hash >> 2);
418 return hash;
419 }
420 };/* struct BitVectorExtractHashFunction */
421
422
423 /**
424 * The structure representing the extraction of one Boolean bit.
425 */
426 struct CVC4_PUBLIC BitVectorBitOf {
427 /** The index of the bit */
428 unsigned bitIndex;
429 BitVectorBitOf(unsigned i)
430 : bitIndex(i) {}
431
432 bool operator == (const BitVectorBitOf& other) const {
433 return bitIndex == other.bitIndex;
434 }
435 };/* struct BitVectorBitOf */
436
437 /**
438 * Hash function for the BitVectorBitOf objects.
439 */
440 struct CVC4_PUBLIC BitVectorBitOfHashFunction {
441 size_t operator()(const BitVectorBitOf& b) const {
442 return b.bitIndex;
443 }
444 };/* struct BitVectorBitOfHashFunction */
445
446
447
448 struct CVC4_PUBLIC BitVectorSize {
449 unsigned size;
450 BitVectorSize(unsigned size)
451 : size(size) {}
452 operator unsigned () const { return size; }
453 };/* struct BitVectorSize */
454
455 struct CVC4_PUBLIC BitVectorRepeat {
456 unsigned repeatAmount;
457 BitVectorRepeat(unsigned repeatAmount)
458 : repeatAmount(repeatAmount) {}
459 operator unsigned () const { return repeatAmount; }
460 };/* struct BitVectorRepeat */
461
462 struct CVC4_PUBLIC BitVectorZeroExtend {
463 unsigned zeroExtendAmount;
464 BitVectorZeroExtend(unsigned zeroExtendAmount)
465 : zeroExtendAmount(zeroExtendAmount) {}
466 operator unsigned () const { return zeroExtendAmount; }
467 };/* struct BitVectorZeroExtend */
468
469 struct CVC4_PUBLIC BitVectorSignExtend {
470 unsigned signExtendAmount;
471 BitVectorSignExtend(unsigned signExtendAmount)
472 : signExtendAmount(signExtendAmount) {}
473 operator unsigned () const { return signExtendAmount; }
474 };/* struct BitVectorSignExtend */
475
476 struct CVC4_PUBLIC BitVectorRotateLeft {
477 unsigned rotateLeftAmount;
478 BitVectorRotateLeft(unsigned rotateLeftAmount)
479 : rotateLeftAmount(rotateLeftAmount) {}
480 operator unsigned () const { return rotateLeftAmount; }
481 };/* struct BitVectorRotateLeft */
482
483 struct CVC4_PUBLIC BitVectorRotateRight {
484 unsigned rotateRightAmount;
485 BitVectorRotateRight(unsigned rotateRightAmount)
486 : rotateRightAmount(rotateRightAmount) {}
487 operator unsigned () const { return rotateRightAmount; }
488 };/* struct BitVectorRotateRight */
489
490 template <typename T>
491 struct CVC4_PUBLIC UnsignedHashFunction {
492 inline size_t operator()(const T& x) const {
493 return (size_t)x;
494 }
495 };/* struct UnsignedHashFunction */
496
497 inline std::ostream& operator <<(std::ostream& os, const BitVector& bv) CVC4_PUBLIC;
498 inline std::ostream& operator <<(std::ostream& os, const BitVector& bv) {
499 return os << bv.toString();
500 }
501
502 inline std::ostream& operator <<(std::ostream& os, const BitVectorExtract& bv) CVC4_PUBLIC;
503 inline std::ostream& operator <<(std::ostream& os, const BitVectorExtract& bv) {
504 return os << "[" << bv.high << ":" << bv.low << "]";
505 }
506
507 inline std::ostream& operator <<(std::ostream& os, const BitVectorBitOf& bv) CVC4_PUBLIC;
508 inline std::ostream& operator <<(std::ostream& os, const BitVectorBitOf& bv) {
509 return os << "[" << bv.bitIndex << "]";
510 }
511
512 }/* CVC4 namespace */
513
514 #endif /* __CVC4__BITVECTOR_H */