1 /********************* */
4 ** Original author: dejan
5 ** Major contributors: cconway, mdeters
6 ** Minor contributors (to current version): none
7 ** This file is part of the CVC4 prototype.
8 ** Copyright (c) 2009, 2010, 2011 The Analysis of Computer Systems Group (ACSys)
9 ** Courant Institute of Mathematical Sciences
10 ** New York University
11 ** See the file COPYING in the top-level source directory for licensing
12 ** information.\endverbatim
14 ** \brief [[ Add one-line brief description here ]]
16 ** [[ Add lengthier description here ]]
17 ** \todo document this file
20 #include "cvc4_public.h"
22 #ifndef __CVC4__BITVECTOR_H
23 #define __CVC4__BITVECTOR_H
26 #include "util/Assert.h"
27 #include "util/integer.h"
31 class CVC4_PUBLIC BitVector
{
37 * no overflows: 2^d_size < d_value
38 * no negative numbers: d_value >= 0
43 Integer
toSignedInt() const {
44 // returns Integer corresponding to two's complement interpretation of bv
45 unsigned size
= d_size
;
46 Integer sign_bit
= d_value
.extractBitRange(1,size
-1);
47 Integer val
= d_value
.extractBitRange(size
-1, 0);
48 Integer res
= Integer(-1) * sign_bit
.multiplyByPow2(size
- 1) + val
;
55 BitVector(unsigned size
, const Integer
& val
):
57 d_value(val
.modByPow2(size
))
60 BitVector(unsigned size
= 0)
61 : d_size(size
), d_value(0) {}
63 BitVector(unsigned size
, unsigned int z
)
64 : d_size(size
), d_value(z
) {
65 d_value
= d_value
.modByPow2(size
);
68 BitVector(unsigned size
, unsigned long int z
)
69 : d_size(size
), d_value(z
) {
70 d_value
= d_value
.modByPow2(size
);
73 BitVector(unsigned size
, const BitVector
& q
)
74 : d_size(size
), d_value(q
.d_value
) {}
76 BitVector(const std::string
& num
, unsigned base
= 2);
80 Integer
toInteger() const {
84 BitVector
& operator =(const BitVector
& x
) {
92 bool operator ==(const BitVector
& y
) const {
93 if (d_size
!= y
.d_size
) return false;
94 return d_value
== y
.d_value
;
97 bool operator !=(const BitVector
& y
) const {
98 if (d_size
!= y
.d_size
) return true;
99 return d_value
!= y
.d_value
;
102 BitVector
equals(const BitVector
& y
) const {
103 Assert(d_size
== y
.d_size
);
104 return d_value
== y
.d_value
;
107 BitVector
concat (const BitVector
& other
) const {
108 return BitVector(d_size
+ other
.d_size
, (d_value
.multiplyByPow2(other
.d_size
)) + other
.d_value
);
111 BitVector
extract(unsigned high
, unsigned low
) const {
112 return BitVector(high
- low
+ 1, d_value
.extractBitRange(high
- low
+ 1, low
));
116 Bitwise operations on BitVectors
120 BitVector
operator ^(const BitVector
& y
) const {
121 Assert (d_size
== y
.d_size
);
122 return BitVector(d_size
, d_value
.bitwiseXor(y
.d_value
));
126 BitVector
operator |(const BitVector
& y
) const {
127 Assert (d_size
== y
.d_size
);
128 return BitVector(d_size
, d_value
.bitwiseOr(y
.d_value
));
132 BitVector
operator &(const BitVector
& y
) const {
133 Assert (d_size
== y
.d_size
);
134 return BitVector(d_size
, d_value
.bitwiseAnd(y
.d_value
));
138 BitVector
operator ~() const {
139 return BitVector(d_size
, d_value
.bitwiseNot());
143 Arithmetic operations on BitVectors
147 bool operator <(const BitVector
& y
) const {
148 return d_value
< y
.d_value
;
151 bool operator >(const BitVector
& y
) const {
152 return d_value
> y
.d_value
;
155 bool operator <=(const BitVector
& y
) const {
156 return d_value
<= y
.d_value
;
159 bool operator >=(const BitVector
& y
) const {
160 return d_value
>= y
.d_value
;
164 BitVector
operator +(const BitVector
& y
) const {
165 Assert (d_size
== y
.d_size
);
166 Integer sum
= d_value
+ y
.d_value
;
167 return BitVector(d_size
, sum
);
170 BitVector
operator -(const BitVector
& y
) const {
171 Assert (d_size
== y
.d_size
);
172 // to maintain the invariant that we are only adding BitVectors of the
174 BitVector
one(d_size
, Integer(1));
175 return *this + ~y
+ one
;
178 BitVector
operator -() const {
179 BitVector
one(d_size
, Integer(1));
180 return ~(*this) + one
;
183 BitVector
operator *(const BitVector
& y
) const {
184 Assert (d_size
== y
.d_size
);
185 Integer prod
= d_value
* y
.d_value
;
186 return BitVector(d_size
, prod
);
189 BitVector
unsignedDiv (const BitVector
& y
) const {
190 Assert (d_size
== y
.d_size
);
191 Assert (d_value
>= 0 && y
.d_value
> 0);
192 return BitVector(d_size
, d_value
.floorDivideQuotient(y
.d_value
));
195 BitVector
unsignedRem(const BitVector
& y
) const {
196 Assert (d_size
== y
.d_size
);
197 Assert (d_value
>= 0 && y
.d_value
> 0);
198 return BitVector(d_size
, d_value
.floorDivideRemainder(y
.d_value
));
202 bool signedLessThan(const BitVector
& y
) const {
203 Assert(d_size
== y
.d_size
);
204 Assert(d_value
>= 0 && y
.d_value
>= 0);
205 Integer a
= (*this).toSignedInt();
206 Integer b
= y
.toSignedInt();
211 bool unsignedLessThan(const BitVector
& y
) const {
212 Assert(d_size
== y
.d_size
);
213 Assert(d_value
>= 0 && y
.d_value
>= 0);
214 return d_value
< y
.d_value
;
217 bool signedLessThanEq(const BitVector
& y
) const {
218 Assert(d_size
== y
.d_size
);
219 Assert(d_value
>= 0 && y
.d_value
>= 0);
220 Integer a
= (*this).toSignedInt();
221 Integer b
= y
.toSignedInt();
226 bool unsignedLessThanEq(const BitVector
& y
) const {
227 Assert(d_size
== y
.d_size
);
228 Assert(d_value
>= 0 && y
.d_value
>= 0);
229 return d_value
<= y
.d_value
;
237 BitVector
zeroExtend(unsigned amount
) const {
238 return BitVector(d_size
+ amount
, d_value
);
241 BitVector
signExtend(unsigned amount
) const {
242 Integer sign_bit
= d_value
.extractBitRange(1, d_size
-1);
243 if(sign_bit
== Integer(0)) {
244 return BitVector(d_size
+ amount
, d_value
);
246 Integer val
= d_value
.oneExtend(d_size
, amount
);
247 return BitVector(d_size
+ amount
, val
);
255 BitVector
leftShift(const BitVector
& y
) const {
256 if (y
.d_value
> Integer(d_size
)) {
257 return BitVector(d_size
, Integer(0));
259 if (y
.d_value
== 0) {
263 // making sure we don't lose information casting
264 Assert(y
.d_value
< Integer(1).multiplyByPow2(32));
265 uint32_t amount
= y
.d_value
.toUnsignedInt();
266 Integer res
= d_value
.multiplyByPow2(amount
);
267 return BitVector(d_size
, res
);
270 BitVector
logicalRightShift(const BitVector
& y
) const {
271 if(y
.d_value
> Integer(d_size
)) {
272 return BitVector(d_size
, Integer(0));
275 // making sure we don't lose information casting
276 Assert(y
.d_value
< Integer(1).multiplyByPow2(32));
277 uint32_t amount
= y
.d_value
.toUnsignedInt();
278 Integer res
= d_value
.divByPow2(amount
);
279 return BitVector(d_size
, res
);
282 BitVector
arithRightShift(const BitVector
& y
) const {
283 Integer sign_bit
= d_value
.extractBitRange(1, d_size
- 1);
284 if(y
.d_value
> Integer(d_size
)) {
285 if(sign_bit
== Integer(0)) {
286 return BitVector(d_size
, Integer(0));
288 return BitVector(d_size
, Integer(d_size
).multiplyByPow2(d_size
) -1 );
292 if (y
.d_value
== 0) {
296 // making sure we don't lose information casting
297 Assert(y
.d_value
< Integer(1).multiplyByPow2(32));
299 uint32_t amount
= y
.d_value
.toUnsignedInt();
300 Integer rest
= d_value
.divByPow2(amount
);
302 if(sign_bit
== Integer(0)) {
303 return BitVector(d_size
, rest
);
305 Integer res
= rest
.oneExtend(d_size
- amount
, amount
);
306 return BitVector(d_size
, res
);
311 Convenience functions
314 size_t hash() const {
315 return d_value
.hash() + d_size
;
318 std::string
toString(unsigned int base
= 2) const {
319 std::string str
= d_value
.toString(base
);
320 if( base
== 2 && d_size
> str
.size() ) {
322 for( unsigned int i
=0; i
< d_size
- str
.size(); ++i
) {
331 unsigned getSize() const {
335 const Integer
& getValue() const {
340 Returns k is the integer is equal to 2^k and zero
342 @return k if the integer is equal to 2^k and zero otherwise
345 return d_value
.isPow2();
348 };/* class BitVector */
352 inline BitVector::BitVector(const std::string
& num
, unsigned base
) {
353 AlwaysAssert( base
== 2 || base
== 16 );
357 } else if( base
== 16 ) {
358 d_size
= num
.size() * 4;
360 Unreachable("Unsupported base in BitVector(std::string&, unsigned int).");
363 d_value
= Integer(num
,base
);
364 }/* BitVector::BitVector() */
368 * Hash function for the BitVector constants.
370 struct CVC4_PUBLIC BitVectorHashStrategy
{
371 static inline size_t hash(const BitVector
& bv
) {
374 };/* struct BitVectorHashStrategy */
377 * The structure representing the extraction operation for bit-vectors. The
378 * operation map bit-vectors to bit-vector of size <code>high - low + 1</code>
379 * by taking the bits at indices <code>high ... low</code>
381 struct CVC4_PUBLIC BitVectorExtract
{
382 /** The high bit of the range for this extract */
384 /** The low bit of the range for this extract */
387 BitVectorExtract(unsigned high
, unsigned low
)
388 : high(high
), low(low
) {}
390 bool operator == (const BitVectorExtract
& extract
) const {
391 return high
== extract
.high
&& low
== extract
.low
;
393 };/* struct BitVectorExtract */
396 * Hash function for the BitVectorExtract objects.
398 class CVC4_PUBLIC BitVectorExtractHashStrategy
{
400 static size_t hash(const BitVectorExtract
& extract
) {
401 size_t hash
= extract
.low
;
402 hash
^= extract
.high
+ 0x9e3779b9 + (hash
<< 6) + (hash
>> 2);
405 };/* class BitVectorExtractHashStrategy */
409 * The structure representing the extraction of one Boolean bit.
411 struct CVC4_PUBLIC BitVectorBitOf
{
412 /** The index of the bit */
414 BitVectorBitOf(unsigned i
)
417 bool operator == (const BitVectorBitOf
& other
) const {
418 return bitIndex
== other
.bitIndex
;
420 };/* struct BitVectorBitOf */
423 * Hash function for the BitVectorBitOf objects.
425 class CVC4_PUBLIC BitVectorBitOfHashStrategy
{
427 static size_t hash(const BitVectorBitOf
& b
) {
430 };/* class BitVectorBitOfHashStrategy */
434 struct CVC4_PUBLIC BitVectorSize
{
436 BitVectorSize(unsigned size
)
438 operator unsigned () const { return size
; }
439 };/* struct BitVectorSize */
441 struct CVC4_PUBLIC BitVectorRepeat
{
442 unsigned repeatAmount
;
443 BitVectorRepeat(unsigned repeatAmount
)
444 : repeatAmount(repeatAmount
) {}
445 operator unsigned () const { return repeatAmount
; }
446 };/* struct BitVectorRepeat */
448 struct CVC4_PUBLIC BitVectorZeroExtend
{
449 unsigned zeroExtendAmount
;
450 BitVectorZeroExtend(unsigned zeroExtendAmount
)
451 : zeroExtendAmount(zeroExtendAmount
) {}
452 operator unsigned () const { return zeroExtendAmount
; }
453 };/* struct BitVectorZeroExtend */
455 struct CVC4_PUBLIC BitVectorSignExtend
{
456 unsigned signExtendAmount
;
457 BitVectorSignExtend(unsigned signExtendAmount
)
458 : signExtendAmount(signExtendAmount
) {}
459 operator unsigned () const { return signExtendAmount
; }
460 };/* struct BitVectorSignExtend */
462 struct CVC4_PUBLIC BitVectorRotateLeft
{
463 unsigned rotateLeftAmount
;
464 BitVectorRotateLeft(unsigned rotateLeftAmount
)
465 : rotateLeftAmount(rotateLeftAmount
) {}
466 operator unsigned () const { return rotateLeftAmount
; }
467 };/* struct BitVectorRotateLeft */
469 struct CVC4_PUBLIC BitVectorRotateRight
{
470 unsigned rotateRightAmount
;
471 BitVectorRotateRight(unsigned rotateRightAmount
)
472 : rotateRightAmount(rotateRightAmount
) {}
473 operator unsigned () const { return rotateRightAmount
; }
474 };/* struct BitVectorRotateRight */
476 template <typename T
>
477 struct CVC4_PUBLIC UnsignedHashStrategy
{
478 static inline size_t hash(const T
& x
) {
481 };/* struct UnsignedHashStrategy */
483 inline std::ostream
& operator <<(std::ostream
& os
, const BitVector
& bv
) CVC4_PUBLIC
;
484 inline std::ostream
& operator <<(std::ostream
& os
, const BitVector
& bv
) {
485 return os
<< bv
.toString();
488 inline std::ostream
& operator <<(std::ostream
& os
, const BitVectorExtract
& bv
) CVC4_PUBLIC
;
489 inline std::ostream
& operator <<(std::ostream
& os
, const BitVectorExtract
& bv
) {
490 return os
<< "[" << bv
.high
<< ":" << bv
.low
<< "]";
493 inline std::ostream
& operator <<(std::ostream
& os
, const BitVectorBitOf
& bv
) CVC4_PUBLIC
;
494 inline std::ostream
& operator <<(std::ostream
& os
, const BitVectorBitOf
& bv
) {
495 return os
<< "[" << bv
.bitIndex
<< "]";
500 }/* CVC4 namespace */
502 #endif /* __CVC4__BITVECTOR_H */