Merge remote-tracking branch 'main-repo/1.0.x' into 1.0.x
[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 * Total division function that returns 0 when the denominator is 0.
183 */
184 BitVector unsignedDivTotal (const BitVector& y) const {
185 CheckArgument(d_size == y.d_size, y);
186 if (y.d_value == 0) {
187 return BitVector(d_size, 0u);
188 }
189 CheckArgument(d_value >= 0, this);
190 CheckArgument(y.d_value > 0, y);
191 return BitVector(d_size, d_value.floorDivideQuotient(y.d_value));
192 }
193 /**
194 * Total division function that returns 0 when the denominator is 0.
195 */
196 BitVector unsignedRemTotal(const BitVector& y) const {
197 CheckArgument(d_size == y.d_size, y);
198 if (y.d_value == 0) {
199 return BitVector(d_size, 0u);
200 }
201 CheckArgument(d_value >= 0, this);
202 CheckArgument(y.d_value > 0, y);
203 return BitVector(d_size, d_value.floorDivideRemainder(y.d_value));
204 }
205
206
207 bool signedLessThan(const BitVector& y) const {
208 CheckArgument(d_size == y.d_size, y);
209 CheckArgument(d_value >= 0, this);
210 CheckArgument(y.d_value >= 0, y);
211 Integer a = (*this).toSignedInt();
212 Integer b = y.toSignedInt();
213
214 return a < b;
215 }
216
217 bool unsignedLessThan(const BitVector& y) const {
218 CheckArgument(d_size == y.d_size, y);
219 CheckArgument(d_value >= 0, this);
220 CheckArgument(y.d_value >= 0, y);
221 return d_value < y.d_value;
222 }
223
224 bool signedLessThanEq(const BitVector& y) const {
225 CheckArgument(d_size == y.d_size, y);
226 CheckArgument(d_value >= 0, this);
227 CheckArgument(y.d_value >= 0, y);
228 Integer a = (*this).toSignedInt();
229 Integer b = y.toSignedInt();
230
231 return a <= b;
232 }
233
234 bool unsignedLessThanEq(const BitVector& y) const {
235 CheckArgument(d_size == y.d_size, this);
236 CheckArgument(d_value >= 0, this);
237 CheckArgument(y.d_value >= 0, y);
238 return d_value <= y.d_value;
239 }
240
241
242 /*
243 Extend operations
244 */
245
246 BitVector zeroExtend(unsigned amount) const {
247 return BitVector(d_size + amount, d_value);
248 }
249
250 BitVector signExtend(unsigned amount) const {
251 Integer sign_bit = d_value.extractBitRange(1, d_size -1);
252 if(sign_bit == Integer(0)) {
253 return BitVector(d_size + amount, d_value);
254 } else {
255 Integer val = d_value.oneExtend(d_size, amount);
256 return BitVector(d_size+ amount, val);
257 }
258 }
259
260 /*
261 Shifts on BitVectors
262 */
263
264 BitVector leftShift(const BitVector& y) const {
265 if (y.d_value > Integer(d_size)) {
266 return BitVector(d_size, Integer(0));
267 }
268 if (y.d_value == 0) {
269 return *this;
270 }
271
272 // making sure we don't lose information casting
273 CheckArgument(y.d_value < Integer(1).multiplyByPow2(32), y);
274 uint32_t amount = y.d_value.toUnsignedInt();
275 Integer res = d_value.multiplyByPow2(amount);
276 return BitVector(d_size, res);
277 }
278
279 BitVector logicalRightShift(const BitVector& y) const {
280 if(y.d_value > Integer(d_size)) {
281 return BitVector(d_size, Integer(0));
282 }
283
284 // making sure we don't lose information casting
285 CheckArgument(y.d_value < Integer(1).multiplyByPow2(32), y);
286 uint32_t amount = y.d_value.toUnsignedInt();
287 Integer res = d_value.divByPow2(amount);
288 return BitVector(d_size, res);
289 }
290
291 BitVector arithRightShift(const BitVector& y) const {
292 Integer sign_bit = d_value.extractBitRange(1, d_size - 1);
293 if(y.d_value > Integer(d_size)) {
294 if(sign_bit == Integer(0)) {
295 return BitVector(d_size, Integer(0));
296 } else {
297 return BitVector(d_size, Integer(d_size).multiplyByPow2(d_size) -1 );
298 }
299 }
300
301 if (y.d_value == 0) {
302 return *this;
303 }
304
305 // making sure we don't lose information casting
306 CheckArgument(y.d_value < Integer(1).multiplyByPow2(32), y);
307
308 uint32_t amount = y.d_value.toUnsignedInt();
309 Integer rest = d_value.divByPow2(amount);
310
311 if(sign_bit == Integer(0)) {
312 return BitVector(d_size, rest);
313 }
314 Integer res = rest.oneExtend(d_size - amount, amount);
315 return BitVector(d_size, res);
316 }
317
318
319 /*
320 Convenience functions
321 */
322
323 size_t hash() const {
324 return d_value.hash() + d_size;
325 }
326
327 std::string toString(unsigned int base = 2) const {
328 std::string str = d_value.toString(base);
329 if( base == 2 && d_size > str.size() ) {
330 std::string zeroes;
331 for( unsigned int i=0; i < d_size - str.size(); ++i ) {
332 zeroes.append("0");
333 }
334 return zeroes + str;
335 } else {
336 return str;
337 }
338 }
339
340 unsigned getSize() const {
341 return d_size;
342 }
343
344 const Integer& getValue() const {
345 return d_value;
346 }
347
348 /**
349 Returns k is the integer is equal to 2^{k-1} and zero
350 otherwise
351 @return k if the integer is equal to 2^{k-1} and zero otherwise
352 */
353 unsigned isPow2() {
354 return d_value.isPow2();
355 }
356
357 };/* class BitVector */
358
359
360
361 inline BitVector::BitVector(const std::string& num, unsigned base) {
362 CheckArgument(base == 2 || base == 16, base);
363
364 if( base == 2 ) {
365 d_size = num.size();
366 } else {
367 d_size = num.size() * 4;
368 }
369
370 d_value = Integer(num, base);
371 }/* BitVector::BitVector() */
372
373
374 /**
375 * Hash function for the BitVector constants.
376 */
377 struct CVC4_PUBLIC BitVectorHashFunction {
378 inline size_t operator()(const BitVector& bv) const {
379 return bv.hash();
380 }
381 };/* struct BitVectorHashFunction */
382
383 /**
384 * The structure representing the extraction operation for bit-vectors. The
385 * operation map bit-vectors to bit-vector of size <code>high - low + 1</code>
386 * by taking the bits at indices <code>high ... low</code>
387 */
388 struct CVC4_PUBLIC BitVectorExtract {
389 /** The high bit of the range for this extract */
390 unsigned high;
391 /** The low bit of the range for this extract */
392 unsigned low;
393
394 BitVectorExtract(unsigned high, unsigned low)
395 : high(high), low(low) {}
396
397 bool operator == (const BitVectorExtract& extract) const {
398 return high == extract.high && low == extract.low;
399 }
400 };/* struct BitVectorExtract */
401
402 /**
403 * Hash function for the BitVectorExtract objects.
404 */
405 struct CVC4_PUBLIC BitVectorExtractHashFunction {
406 size_t operator()(const BitVectorExtract& extract) const {
407 size_t hash = extract.low;
408 hash ^= extract.high + 0x9e3779b9 + (hash << 6) + (hash >> 2);
409 return hash;
410 }
411 };/* struct BitVectorExtractHashFunction */
412
413
414 /**
415 * The structure representing the extraction of one Boolean bit.
416 */
417 struct CVC4_PUBLIC BitVectorBitOf {
418 /** The index of the bit */
419 unsigned bitIndex;
420 BitVectorBitOf(unsigned i)
421 : bitIndex(i) {}
422
423 bool operator == (const BitVectorBitOf& other) const {
424 return bitIndex == other.bitIndex;
425 }
426 };/* struct BitVectorBitOf */
427
428 /**
429 * Hash function for the BitVectorBitOf objects.
430 */
431 struct CVC4_PUBLIC BitVectorBitOfHashFunction {
432 size_t operator()(const BitVectorBitOf& b) const {
433 return b.bitIndex;
434 }
435 };/* struct BitVectorBitOfHashFunction */
436
437
438
439 struct CVC4_PUBLIC BitVectorSize {
440 unsigned size;
441 BitVectorSize(unsigned size)
442 : size(size) {}
443 operator unsigned () const { return size; }
444 };/* struct BitVectorSize */
445
446 struct CVC4_PUBLIC BitVectorRepeat {
447 unsigned repeatAmount;
448 BitVectorRepeat(unsigned repeatAmount)
449 : repeatAmount(repeatAmount) {}
450 operator unsigned () const { return repeatAmount; }
451 };/* struct BitVectorRepeat */
452
453 struct CVC4_PUBLIC BitVectorZeroExtend {
454 unsigned zeroExtendAmount;
455 BitVectorZeroExtend(unsigned zeroExtendAmount)
456 : zeroExtendAmount(zeroExtendAmount) {}
457 operator unsigned () const { return zeroExtendAmount; }
458 };/* struct BitVectorZeroExtend */
459
460 struct CVC4_PUBLIC BitVectorSignExtend {
461 unsigned signExtendAmount;
462 BitVectorSignExtend(unsigned signExtendAmount)
463 : signExtendAmount(signExtendAmount) {}
464 operator unsigned () const { return signExtendAmount; }
465 };/* struct BitVectorSignExtend */
466
467 struct CVC4_PUBLIC BitVectorRotateLeft {
468 unsigned rotateLeftAmount;
469 BitVectorRotateLeft(unsigned rotateLeftAmount)
470 : rotateLeftAmount(rotateLeftAmount) {}
471 operator unsigned () const { return rotateLeftAmount; }
472 };/* struct BitVectorRotateLeft */
473
474 struct CVC4_PUBLIC BitVectorRotateRight {
475 unsigned rotateRightAmount;
476 BitVectorRotateRight(unsigned rotateRightAmount)
477 : rotateRightAmount(rotateRightAmount) {}
478 operator unsigned () const { return rotateRightAmount; }
479 };/* struct BitVectorRotateRight */
480
481 template <typename T>
482 struct CVC4_PUBLIC UnsignedHashFunction {
483 inline size_t operator()(const T& x) const {
484 return (size_t)x;
485 }
486 };/* struct UnsignedHashFunction */
487
488 inline std::ostream& operator <<(std::ostream& os, const BitVector& bv) CVC4_PUBLIC;
489 inline std::ostream& operator <<(std::ostream& os, const BitVector& bv) {
490 return os << bv.toString();
491 }
492
493 inline std::ostream& operator <<(std::ostream& os, const BitVectorExtract& bv) CVC4_PUBLIC;
494 inline std::ostream& operator <<(std::ostream& os, const BitVectorExtract& bv) {
495 return os << "[" << bv.high << ":" << bv.low << "]";
496 }
497
498 inline std::ostream& operator <<(std::ostream& os, const BitVectorBitOf& bv) CVC4_PUBLIC;
499 inline std::ostream& operator <<(std::ostream& os, const BitVectorBitOf& bv) {
500 return os << "[" << bv.bitIndex << "]";
501 }
502
503 }/* CVC4 namespace */
504
505 #endif /* __CVC4__BITVECTOR_H */