1 /********************* */
4 ** Original author: Dejan Jovanović <dejan.jovanovic@gmail.com>
5 ** Major contributors: Morgan Deters <mdeters@cs.nyu.edu>, Liana Hadarean <lianahady@gmail.com>
6 ** Minor contributors (to current version): lianah <lianahady@gmail.com>, Christopher L. Conway <christopherleeconway@gmail.com>
7 ** This file is part of the CVC4 project.
8 ** Copyright (c) 2009-2013 New York University and The University of Iowa
9 ** See the file COPYING in the top-level source directory for licensing
10 ** information.\endverbatim
12 ** \brief [[ Add one-line brief description here ]]
14 ** [[ Add lengthier description here ]]
15 ** \todo document this file
18 #include "cvc4_public.h"
20 #ifndef __CVC4__BITVECTOR_H
21 #define __CVC4__BITVECTOR_H
24 #include "util/exception.h"
25 #include "util/integer.h"
29 class CVC4_PUBLIC BitVector
{
35 * no overflows: 2^d_size < d_value
36 * no negative numbers: d_value >= 0
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
;
53 BitVector(unsigned size
, const Integer
& val
):
55 d_value(val
.modByPow2(size
))
58 BitVector(unsigned size
= 0)
59 : d_size(size
), d_value(0) {}
61 BitVector(unsigned size
, unsigned int z
)
62 : d_size(size
), d_value(z
) {
63 d_value
= d_value
.modByPow2(size
);
66 BitVector(unsigned size
, unsigned long int z
)
67 : d_size(size
), d_value(z
) {
68 d_value
= d_value
.modByPow2(size
);
71 BitVector(unsigned size
, const BitVector
& q
)
72 : d_size(size
), d_value(q
.d_value
) {}
74 BitVector(const std::string
& num
, unsigned base
= 2);
78 Integer
toInteger() const {
82 BitVector
& operator =(const BitVector
& x
) {
90 bool operator ==(const BitVector
& y
) const {
91 if (d_size
!= y
.d_size
) return false;
92 return d_value
== y
.d_value
;
95 bool operator !=(const BitVector
& y
) const {
96 if (d_size
!= y
.d_size
) return true;
97 return d_value
!= y
.d_value
;
100 BitVector
concat (const BitVector
& other
) const {
101 return BitVector(d_size
+ other
.d_size
, (d_value
.multiplyByPow2(other
.d_size
)) + other
.d_value
);
104 BitVector
extract(unsigned high
, unsigned low
) const {
105 return BitVector(high
- low
+ 1, d_value
.extractBitRange(high
- low
+ 1, low
));
109 Bitwise operations on BitVectors
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
));
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
));
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
));
131 BitVector
operator ~() const {
132 return BitVector(d_size
, d_value
.bitwiseNot());
136 Arithmetic operations on BitVectors
140 bool operator <(const BitVector
& y
) const {
141 return d_value
< y
.d_value
;
144 bool operator >(const BitVector
& y
) const {
145 return d_value
> y
.d_value
;
148 bool operator <=(const BitVector
& y
) const {
149 return d_value
<= y
.d_value
;
152 bool operator >=(const BitVector
& y
) const {
153 return d_value
>= y
.d_value
;
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
);
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
167 BitVector
one(d_size
, Integer(1));
168 return *this + ~y
+ one
;
171 BitVector
operator -() const {
172 BitVector
one(d_size
, Integer(1));
173 return ~(*this) + one
;
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
);
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
);
188 bool isBitSet(uint32_t i
) const {
189 CheckArgument(i
< d_size
, i
);
190 return d_value
.isBitSet(i
);
194 * Total division function that returns 0 when the denominator is 0.
196 BitVector
unsignedDivTotal (const BitVector
& y
) const {
198 CheckArgument(d_size
== y
.d_size
, y
);
199 if (y
.d_value
== 0) {
200 return BitVector(d_size
, 0u);
202 CheckArgument(d_value
>= 0, this);
203 CheckArgument(y
.d_value
> 0, y
);
204 return BitVector(d_size
, d_value
.floorDivideQuotient(y
.d_value
));
208 * Total division function that returns 0 when the denominator is 0.
210 BitVector
unsignedRemTotal(const BitVector
& y
) const {
211 CheckArgument(d_size
== y
.d_size
, y
);
212 if (y
.d_value
== 0) {
213 return BitVector(d_size
, 0u);
215 CheckArgument(d_value
>= 0, this);
216 CheckArgument(y
.d_value
> 0, y
);
217 return BitVector(d_size
, d_value
.floorDivideRemainder(y
.d_value
));
221 bool signedLessThan(const BitVector
& y
) const {
222 CheckArgument(d_size
== y
.d_size
, y
);
223 CheckArgument(d_value
>= 0, this);
224 CheckArgument(y
.d_value
>= 0, y
);
225 Integer a
= (*this).toSignedInt();
226 Integer b
= y
.toSignedInt();
231 bool unsignedLessThan(const BitVector
& y
) const {
232 CheckArgument(d_size
== y
.d_size
, y
);
233 CheckArgument(d_value
>= 0, this);
234 CheckArgument(y
.d_value
>= 0, y
);
235 return d_value
< y
.d_value
;
238 bool signedLessThanEq(const BitVector
& y
) const {
239 CheckArgument(d_size
== y
.d_size
, y
);
240 CheckArgument(d_value
>= 0, this);
241 CheckArgument(y
.d_value
>= 0, y
);
242 Integer a
= (*this).toSignedInt();
243 Integer b
= y
.toSignedInt();
248 bool unsignedLessThanEq(const BitVector
& y
) const {
249 CheckArgument(d_size
== y
.d_size
, this);
250 CheckArgument(d_value
>= 0, this);
251 CheckArgument(y
.d_value
>= 0, y
);
252 return d_value
<= y
.d_value
;
260 BitVector
zeroExtend(unsigned amount
) const {
261 return BitVector(d_size
+ amount
, d_value
);
264 BitVector
signExtend(unsigned amount
) const {
265 Integer sign_bit
= d_value
.extractBitRange(1, d_size
-1);
266 if(sign_bit
== Integer(0)) {
267 return BitVector(d_size
+ amount
, d_value
);
269 Integer val
= d_value
.oneExtend(d_size
, amount
);
270 return BitVector(d_size
+ amount
, val
);
278 BitVector
leftShift(const BitVector
& y
) const {
279 if (y
.d_value
> Integer(d_size
)) {
280 return BitVector(d_size
, Integer(0));
282 if (y
.d_value
== 0) {
286 // making sure we don't lose information casting
287 CheckArgument(y
.d_value
< Integer(1).multiplyByPow2(32), y
);
288 uint32_t amount
= y
.d_value
.toUnsignedInt();
289 Integer res
= d_value
.multiplyByPow2(amount
);
290 return BitVector(d_size
, res
);
293 BitVector
logicalRightShift(const BitVector
& y
) const {
294 if(y
.d_value
> Integer(d_size
)) {
295 return BitVector(d_size
, Integer(0));
298 // making sure we don't lose information casting
299 CheckArgument(y
.d_value
< Integer(1).multiplyByPow2(32), y
);
300 uint32_t amount
= y
.d_value
.toUnsignedInt();
301 Integer res
= d_value
.divByPow2(amount
);
302 return BitVector(d_size
, res
);
305 BitVector
arithRightShift(const BitVector
& y
) const {
306 Integer sign_bit
= d_value
.extractBitRange(1, d_size
- 1);
307 if(y
.d_value
> Integer(d_size
)) {
308 if(sign_bit
== Integer(0)) {
309 return BitVector(d_size
, Integer(0));
311 return BitVector(d_size
, Integer(d_size
).multiplyByPow2(d_size
) -1 );
315 if (y
.d_value
== 0) {
319 // making sure we don't lose information casting
320 CheckArgument(y
.d_value
< Integer(1).multiplyByPow2(32), y
);
322 uint32_t amount
= y
.d_value
.toUnsignedInt();
323 Integer rest
= d_value
.divByPow2(amount
);
325 if(sign_bit
== Integer(0)) {
326 return BitVector(d_size
, rest
);
328 Integer res
= rest
.oneExtend(d_size
- amount
, amount
);
329 return BitVector(d_size
, res
);
334 Convenience functions
337 size_t hash() const {
338 return d_value
.hash() + d_size
;
341 std::string
toString(unsigned int base
= 2) const {
342 std::string str
= d_value
.toString(base
);
343 if( base
== 2 && d_size
> str
.size() ) {
345 for( unsigned int i
=0; i
< d_size
- str
.size(); ++i
) {
354 unsigned getSize() const {
358 const Integer
& getValue() const {
363 Returns k is the integer is equal to 2^{k-1} and zero
365 @return k if the integer is equal to 2^{k-1} and zero otherwise
368 return d_value
.isPow2();
371 };/* class BitVector */
375 inline BitVector::BitVector(const std::string
& num
, unsigned base
) {
376 CheckArgument(base
== 2 || base
== 16, base
);
381 d_size
= num
.size() * 4;
384 d_value
= Integer(num
, base
);
385 }/* BitVector::BitVector() */
389 * Hash function for the BitVector constants.
391 struct CVC4_PUBLIC BitVectorHashFunction
{
392 inline size_t operator()(const BitVector
& bv
) const {
395 };/* struct BitVectorHashFunction */
398 * The structure representing the extraction operation for bit-vectors. The
399 * operation map bit-vectors to bit-vector of size <code>high - low + 1</code>
400 * by taking the bits at indices <code>high ... low</code>
402 struct CVC4_PUBLIC BitVectorExtract
{
403 /** The high bit of the range for this extract */
405 /** The low bit of the range for this extract */
408 BitVectorExtract(unsigned high
, unsigned low
)
409 : high(high
), low(low
) {}
411 bool operator == (const BitVectorExtract
& extract
) const {
412 return high
== extract
.high
&& low
== extract
.low
;
414 };/* struct BitVectorExtract */
417 * Hash function for the BitVectorExtract objects.
419 struct CVC4_PUBLIC BitVectorExtractHashFunction
{
420 size_t operator()(const BitVectorExtract
& extract
) const {
421 size_t hash
= extract
.low
;
422 hash
^= extract
.high
+ 0x9e3779b9 + (hash
<< 6) + (hash
>> 2);
425 };/* struct BitVectorExtractHashFunction */
429 * The structure representing the extraction of one Boolean bit.
431 struct CVC4_PUBLIC BitVectorBitOf
{
432 /** The index of the bit */
434 BitVectorBitOf(unsigned i
)
437 bool operator == (const BitVectorBitOf
& other
) const {
438 return bitIndex
== other
.bitIndex
;
440 };/* struct BitVectorBitOf */
443 * Hash function for the BitVectorBitOf objects.
445 struct CVC4_PUBLIC BitVectorBitOfHashFunction
{
446 size_t operator()(const BitVectorBitOf
& b
) const {
449 };/* struct BitVectorBitOfHashFunction */
453 struct CVC4_PUBLIC BitVectorSize
{
455 BitVectorSize(unsigned size
)
457 operator unsigned () const { return size
; }
458 };/* struct BitVectorSize */
460 struct CVC4_PUBLIC BitVectorRepeat
{
461 unsigned repeatAmount
;
462 BitVectorRepeat(unsigned repeatAmount
)
463 : repeatAmount(repeatAmount
) {}
464 operator unsigned () const { return repeatAmount
; }
465 };/* struct BitVectorRepeat */
467 struct CVC4_PUBLIC BitVectorZeroExtend
{
468 unsigned zeroExtendAmount
;
469 BitVectorZeroExtend(unsigned zeroExtendAmount
)
470 : zeroExtendAmount(zeroExtendAmount
) {}
471 operator unsigned () const { return zeroExtendAmount
; }
472 };/* struct BitVectorZeroExtend */
474 struct CVC4_PUBLIC BitVectorSignExtend
{
475 unsigned signExtendAmount
;
476 BitVectorSignExtend(unsigned signExtendAmount
)
477 : signExtendAmount(signExtendAmount
) {}
478 operator unsigned () const { return signExtendAmount
; }
479 };/* struct BitVectorSignExtend */
481 struct CVC4_PUBLIC BitVectorRotateLeft
{
482 unsigned rotateLeftAmount
;
483 BitVectorRotateLeft(unsigned rotateLeftAmount
)
484 : rotateLeftAmount(rotateLeftAmount
) {}
485 operator unsigned () const { return rotateLeftAmount
; }
486 };/* struct BitVectorRotateLeft */
488 struct CVC4_PUBLIC BitVectorRotateRight
{
489 unsigned rotateRightAmount
;
490 BitVectorRotateRight(unsigned rotateRightAmount
)
491 : rotateRightAmount(rotateRightAmount
) {}
492 operator unsigned () const { return rotateRightAmount
; }
493 };/* struct BitVectorRotateRight */
495 template <typename T
>
496 struct CVC4_PUBLIC UnsignedHashFunction
{
497 inline size_t operator()(const T
& x
) const {
500 };/* struct UnsignedHashFunction */
502 inline std::ostream
& operator <<(std::ostream
& os
, const BitVector
& bv
) CVC4_PUBLIC
;
503 inline std::ostream
& operator <<(std::ostream
& os
, const BitVector
& bv
) {
504 return os
<< bv
.toString();
507 inline std::ostream
& operator <<(std::ostream
& os
, const BitVectorExtract
& bv
) CVC4_PUBLIC
;
508 inline std::ostream
& operator <<(std::ostream
& os
, const BitVectorExtract
& bv
) {
509 return os
<< "[" << bv
.high
<< ":" << bv
.low
<< "]";
512 inline std::ostream
& operator <<(std::ostream
& os
, const BitVectorBitOf
& bv
) CVC4_PUBLIC
;
513 inline std::ostream
& operator <<(std::ostream
& os
, const BitVectorBitOf
& bv
) {
514 return os
<< "[" << bv
.bitIndex
<< "]";
517 }/* CVC4 namespace */
519 #endif /* __CVC4__BITVECTOR_H */