4 * Created on: Mar 31, 2010
8 #ifndef __CVC4__BITVECTOR_H_
9 #define __CVC4__BITVECTOR_H_
13 #include "util/gmp_util.h"
24 BitVector(unsigned size
, const mpz_class
& val
) : d_size(size
), d_value(val
) {}
28 BitVector(unsigned size
= 0)
29 : d_size(size
), d_value(0) {}
31 BitVector(unsigned size
, unsigned int z
)
32 : d_size(size
), d_value(z
) {}
34 BitVector(unsigned size
, unsigned long int z
)
35 : d_size(size
), d_value(z
) {}
37 BitVector(unsigned size
, const BitVector
& q
)
38 : d_size(size
), d_value(q
.d_value
) {}
42 BitVector
& operator =(const BitVector
& x
) {
49 bool operator ==(const BitVector
& y
) const {
50 if (d_size
!= y
.d_size
) return false;
51 return d_value
== y
.d_value
;
54 bool operator !=(const BitVector
& y
) const {
55 if (d_size
== y
.d_size
) return false;
56 return d_value
!= y
.d_value
;
59 BitVector
operator +(const BitVector
& y
) const {
60 return BitVector(std::max(d_size
, y
.d_size
), d_value
+ y
.d_value
);
63 BitVector
operator -(const BitVector
& y
) const {
64 return *this + ~y
+ 1;
67 BitVector
operator -() const {
71 BitVector
operator *(const BitVector
& y
) const {
72 return BitVector(d_size
, d_value
* y
.d_value
);
75 BitVector
operator ~() const {
76 return BitVector(d_size
, d_value
);
80 return gmpz_hash(d_value
.get_mpz_t());
83 std::string
toString(unsigned int base
= 2) const {
84 return d_value
.get_str(base
);
89 * Hash function for the BitVector constants.
91 struct BitVectorHashStrategy
{
92 static inline size_t hash(const BitVector
& bv
) {
98 * The structure representing the extraction operation for bit-vectors. The
99 * operation map bit-vectors to bit-vector of size <code>high - low + 1</code>
100 * by taking the bits at indices <code>high ... low</code>
102 struct BitVectorExtract
{
103 /** The high bit of the range for this extract */
105 /** The low bit of the range for this extract */
108 BitVectorExtract(unsigned high
, unsigned low
)
109 : high(high
), low(low
) {}
111 bool operator == (const BitVectorExtract
& extract
) const {
112 return high
== extract
.high
&& low
== extract
.low
;
117 * Hash function for the BitVectorExtract objects.
119 class BitVectorExtractHashStrategy
{
121 static size_t hash(const BitVectorExtract
& extract
) {
122 size_t hash
= extract
.low
;
123 hash
^= extract
.high
+ 0x9e3779b9 + (hash
<< 6) + (hash
>> 2);
128 struct BitVectorSize
{
130 BitVectorSize(unsigned size
)
132 operator unsigned () const { return size
; }
135 struct BitVectorRepeat
{
136 unsigned repeatAmount
;
137 BitVectorRepeat(unsigned repeatAmount
)
138 : repeatAmount(repeatAmount
) {}
139 operator unsigned () const { return repeatAmount
; }
142 struct BitVectorZeroExtend
{
143 unsigned zeroExtendAmount
;
144 BitVectorZeroExtend(unsigned zeroExtendAmount
)
145 : zeroExtendAmount(zeroExtendAmount
) {}
146 operator unsigned () const { return zeroExtendAmount
; }
149 struct BitVectorSignExtend
{
150 unsigned signExtendAmount
;
151 BitVectorSignExtend(unsigned signExtendAmount
)
152 : signExtendAmount(signExtendAmount
) {}
153 operator unsigned () const { return signExtendAmount
; }
156 struct BitVectorRotateLeft
{
157 unsigned rotateLeftAmount
;
158 BitVectorRotateLeft(unsigned rotateLeftAmount
)
159 : rotateLeftAmount(rotateLeftAmount
) {}
160 operator unsigned () const { return rotateLeftAmount
; }
163 struct BitVectorRotateRight
{
164 unsigned rotateRightAmount
;
165 BitVectorRotateRight(unsigned rotateRightAmount
)
166 : rotateRightAmount(rotateRightAmount
) {}
167 operator unsigned () const { return rotateRightAmount
; }
170 template <typename T
>
171 struct UnsignedHashStrategy
{
172 static inline size_t hash(const T
& x
) {
177 std::ostream
& operator <<(std::ostream
& os
, const BitVector
& bv
);
178 std::ostream
& operator <<(std::ostream
& os
, const BitVectorExtract
& bv
);
182 #endif /* __CVC4__BITVECTOR_H_ */