bitvector.h
Go to the documentation of this file.
/********************* */
#include "cvc4_public.h"
#ifndef __CVC4__BITVECTOR_H
#define __CVC4__BITVECTOR_H
#include <iostream>
#include "util/exception.h"
#include "util/integer.h"
namespace CVC4 {
class CVC4_PUBLIC BitVector {
private:
/*
Class invariants:
* no overflows: 2^d_size < d_value
* no negative numbers: d_value >= 0
*/
unsigned d_size;
Integer d_value;
Integer toSignedInt() const {
// returns Integer corresponding to two's complement interpretation of bv
unsigned size = d_size;
Integer sign_bit = d_value.extractBitRange(1,size-1);
Integer val = d_value.extractBitRange(size-1, 0);
Integer res = Integer(-1) * sign_bit.multiplyByPow2(size - 1) + val;
return res;
}
public:
BitVector(unsigned size, const Integer& val):
d_size(size),
d_value(val.modByPow2(size))
{}
BitVector(unsigned size = 0)
: d_size(size), d_value(0) {}
BitVector(unsigned size, unsigned int z)
: d_size(size), d_value(z) {
d_value = d_value.modByPow2(size);
}
BitVector(unsigned size, unsigned long int z)
: d_size(size), d_value(z) {
d_value = d_value.modByPow2(size);
}
BitVector(unsigned size, const BitVector& q)
: d_size(size), d_value(q.d_value) {}
BitVector(const std::string& num, unsigned base = 2);
~BitVector() {}
Integer toInteger() const {
return d_value;
}
BitVector& operator =(const BitVector& x) {
if(this == &x)
return *this;
d_size = x.d_size;
d_value = x.d_value;
return *this;
}
bool operator ==(const BitVector& y) const {
if (d_size != y.d_size) return false;
return d_value == y.d_value;
}
bool operator !=(const BitVector& y) const {
if (d_size != y.d_size) return true;
return d_value != y.d_value;
}
BitVector concat (const BitVector& other) const {
return BitVector(d_size + other.d_size, (d_value.multiplyByPow2(other.d_size)) + other.d_value);
}
BitVector extract(unsigned high, unsigned low) const {
return BitVector(high - low + 1, d_value.extractBitRange(high - low + 1, low));
}
/*
Bitwise operations on BitVectors
*/
// xor
BitVector operator ^(const BitVector& y) const {
CheckArgument(d_size == y.d_size, y);
return BitVector(d_size, d_value.bitwiseXor(y.d_value));
}
// or
BitVector operator |(const BitVector& y) const {
CheckArgument(d_size == y.d_size, y);
return BitVector(d_size, d_value.bitwiseOr(y.d_value));
}
// and
BitVector operator &(const BitVector& y) const {
CheckArgument(d_size == y.d_size, y);
return BitVector(d_size, d_value.bitwiseAnd(y.d_value));
}
// not
BitVector operator ~() const {
return BitVector(d_size, d_value.bitwiseNot());
}
/*
Arithmetic operations on BitVectors
*/
bool operator <(const BitVector& y) const {
return d_value < y.d_value;
}
bool operator >(const BitVector& y) const {
return d_value > y.d_value ;
}
bool operator <=(const BitVector& y) const {
return d_value <= y.d_value;
}
bool operator >=(const BitVector& y) const {
return d_value >= y.d_value ;
}
BitVector operator +(const BitVector& y) const {
CheckArgument(d_size == y.d_size, y);
Integer sum = d_value + y.d_value;
return BitVector(d_size, sum);
}
BitVector operator -(const BitVector& y) const {
CheckArgument(d_size == y.d_size, y);
// to maintain the invariant that we are only adding BitVectors of the
// same size
BitVector one(d_size, Integer(1));
return *this + ~y + one;
}
BitVector operator -() const {
BitVector one(d_size, Integer(1));
return ~(*this) + one;
}
BitVector operator *(const BitVector& y) const {
CheckArgument(d_size == y.d_size, y);
Integer prod = d_value * y.d_value;
return BitVector(d_size, prod);
}
BitVector setBit(uint32_t i) const {
CheckArgument(i < d_size, i);
Integer res = d_value.setBit(i);
return BitVector(d_size, res);
}
bool isBitSet(uint32_t i) const {
CheckArgument(i < d_size, i);
return d_value.isBitSet(i);
}
BitVector unsignedDivTotal (const BitVector& y) const {
CheckArgument(d_size == y.d_size, y);
if (y.d_value == 0) {
// under division by zero return -1
return BitVector(d_size, Integer(1).oneExtend(1, d_size - 1));
}
CheckArgument(d_value >= 0, this);
CheckArgument(y.d_value > 0, y);
return BitVector(d_size, d_value.floorDivideQuotient(y.d_value));
}
BitVector unsignedRemTotal(const BitVector& y) const {
CheckArgument(d_size == y.d_size, y);
if (y.d_value == 0) {
return BitVector(d_size, d_value);
}
CheckArgument(d_value >= 0, this);
CheckArgument(y.d_value > 0, y);
return BitVector(d_size, d_value.floorDivideRemainder(y.d_value));
}
bool signedLessThan(const BitVector& y) const {
CheckArgument(d_size == y.d_size, y);
CheckArgument(d_value >= 0, this);
CheckArgument(y.d_value >= 0, y);
Integer a = (*this).toSignedInt();
Integer b = y.toSignedInt();
return a < b;
}
bool unsignedLessThan(const BitVector& y) const {
CheckArgument(d_size == y.d_size, y);
CheckArgument(d_value >= 0, this);
CheckArgument(y.d_value >= 0, y);
return d_value < y.d_value;
}
bool signedLessThanEq(const BitVector& y) const {
CheckArgument(d_size == y.d_size, y);
CheckArgument(d_value >= 0, this);
CheckArgument(y.d_value >= 0, y);
Integer a = (*this).toSignedInt();
Integer b = y.toSignedInt();
return a <= b;
}
bool unsignedLessThanEq(const BitVector& y) const {
CheckArgument(d_size == y.d_size, this);
CheckArgument(d_value >= 0, this);
CheckArgument(y.d_value >= 0, y);
return d_value <= y.d_value;
}
/*
Extend operations
*/
BitVector zeroExtend(unsigned amount) const {
return BitVector(d_size + amount, d_value);
}
BitVector signExtend(unsigned amount) const {
Integer sign_bit = d_value.extractBitRange(1, d_size -1);
if(sign_bit == Integer(0)) {
return BitVector(d_size + amount, d_value);
} else {
Integer val = d_value.oneExtend(d_size, amount);
return BitVector(d_size+ amount, val);
}
}
/*
Shifts on BitVectors
*/
BitVector leftShift(const BitVector& y) const {
if (y.d_value > Integer(d_size)) {
return BitVector(d_size, Integer(0));
}
if (y.d_value == 0) {
return *this;
}
// making sure we don't lose information casting
CheckArgument(y.d_value < Integer(1).multiplyByPow2(32), y);
uint32_t amount = y.d_value.toUnsignedInt();
Integer res = d_value.multiplyByPow2(amount);
return BitVector(d_size, res);
}
BitVector logicalRightShift(const BitVector& y) const {
if(y.d_value > Integer(d_size)) {
return BitVector(d_size, Integer(0));
}
// making sure we don't lose information casting
CheckArgument(y.d_value < Integer(1).multiplyByPow2(32), y);
uint32_t amount = y.d_value.toUnsignedInt();
Integer res = d_value.divByPow2(amount);
return BitVector(d_size, res);
}
BitVector arithRightShift(const BitVector& y) const {
Integer sign_bit = d_value.extractBitRange(1, d_size - 1);
if(y.d_value > Integer(d_size)) {
if(sign_bit == Integer(0)) {
return BitVector(d_size, Integer(0));
} else {
return BitVector(d_size, Integer(d_size).multiplyByPow2(d_size) -1 );
}
}
if (y.d_value == 0) {
return *this;
}
// making sure we don't lose information casting
CheckArgument(y.d_value < Integer(1).multiplyByPow2(32), y);
uint32_t amount = y.d_value.toUnsignedInt();
Integer rest = d_value.divByPow2(amount);
if(sign_bit == Integer(0)) {
return BitVector(d_size, rest);
}
Integer res = rest.oneExtend(d_size - amount, amount);
return BitVector(d_size, res);
}
/*
Convenience functions
*/
size_t hash() const {
return d_value.hash() + d_size;
}
std::string toString(unsigned int base = 2) const {
std::string str = d_value.toString(base);
if( base == 2 && d_size > str.size() ) {
std::string zeroes;
for( unsigned int i=0; i < d_size - str.size(); ++i ) {
zeroes.append("0");
}
return zeroes + str;
} else {
return str;
}
}
unsigned getSize() const {
return d_size;
}
const Integer& getValue() const {
return d_value;
}
unsigned isPow2() {
return d_value.isPow2();
}
};/* class BitVector */
inline BitVector::BitVector(const std::string& num, unsigned base) {
CheckArgument(base == 2 || base == 16, base);
if( base == 2 ) {
d_size = num.size();
} else {
d_size = num.size() * 4;
}
d_value = Integer(num, base);
}/* BitVector::BitVector() */
struct CVC4_PUBLIC BitVectorHashFunction {
inline size_t operator()(const BitVector& bv) const {
return bv.hash();
}
};/* struct BitVectorHashFunction */
struct CVC4_PUBLIC BitVectorExtract {
unsigned high;
unsigned low;
BitVectorExtract(unsigned high, unsigned low)
: high(high), low(low) {}
bool operator == (const BitVectorExtract& extract) const {
return high == extract.high && low == extract.low;
}
};/* struct BitVectorExtract */
struct CVC4_PUBLIC BitVectorExtractHashFunction {
size_t operator()(const BitVectorExtract& extract) const {
size_t hash = extract.low;
hash ^= extract.high + 0x9e3779b9 + (hash << 6) + (hash >> 2);
return hash;
}
};/* struct BitVectorExtractHashFunction */
struct CVC4_PUBLIC BitVectorBitOf {
unsigned bitIndex;
BitVectorBitOf(unsigned i)
: bitIndex(i) {}
bool operator == (const BitVectorBitOf& other) const {
return bitIndex == other.bitIndex;
}
};/* struct BitVectorBitOf */
struct CVC4_PUBLIC BitVectorBitOfHashFunction {
size_t operator()(const BitVectorBitOf& b) const {
return b.bitIndex;
}
};/* struct BitVectorBitOfHashFunction */
struct CVC4_PUBLIC BitVectorSize {
unsigned size;
BitVectorSize(unsigned size)
: size(size) {}
operator unsigned () const { return size; }
};/* struct BitVectorSize */
struct CVC4_PUBLIC BitVectorRepeat {
unsigned repeatAmount;
BitVectorRepeat(unsigned repeatAmount)
: repeatAmount(repeatAmount) {}
operator unsigned () const { return repeatAmount; }
};/* struct BitVectorRepeat */
struct CVC4_PUBLIC BitVectorZeroExtend {
unsigned zeroExtendAmount;
BitVectorZeroExtend(unsigned zeroExtendAmount)
: zeroExtendAmount(zeroExtendAmount) {}
operator unsigned () const { return zeroExtendAmount; }
};/* struct BitVectorZeroExtend */
struct CVC4_PUBLIC BitVectorSignExtend {
unsigned signExtendAmount;
BitVectorSignExtend(unsigned signExtendAmount)
: signExtendAmount(signExtendAmount) {}
operator unsigned () const { return signExtendAmount; }
};/* struct BitVectorSignExtend */
struct CVC4_PUBLIC BitVectorRotateLeft {
unsigned rotateLeftAmount;
BitVectorRotateLeft(unsigned rotateLeftAmount)
: rotateLeftAmount(rotateLeftAmount) {}
operator unsigned () const { return rotateLeftAmount; }
};/* struct BitVectorRotateLeft */
struct CVC4_PUBLIC BitVectorRotateRight {
unsigned rotateRightAmount;
BitVectorRotateRight(unsigned rotateRightAmount)
: rotateRightAmount(rotateRightAmount) {}
operator unsigned () const { return rotateRightAmount; }
};/* struct BitVectorRotateRight */
struct CVC4_PUBLIC IntToBitVector {
unsigned size;
IntToBitVector(unsigned size)
: size(size) {}
operator unsigned () const { return size; }
};/* struct IntToBitVector */
template <typename T>
struct CVC4_PUBLIC UnsignedHashFunction {
inline size_t operator()(const T& x) const {
return (size_t)x;
}
};/* struct UnsignedHashFunction */
inline std::ostream& operator <<(std::ostream& os, const BitVector& bv) CVC4_PUBLIC;
inline std::ostream& operator <<(std::ostream& os, const BitVector& bv) {
return os << bv.toString();
}
inline std::ostream& operator <<(std::ostream& os, const BitVectorExtract& bv) CVC4_PUBLIC;
inline std::ostream& operator <<(std::ostream& os, const BitVectorExtract& bv) {
return os << "[" << bv.high << ":" << bv.low << "]";
}
inline std::ostream& operator <<(std::ostream& os, const BitVectorBitOf& bv) CVC4_PUBLIC;
inline std::ostream& operator <<(std::ostream& os, const BitVectorBitOf& bv) {
return os << "[" << bv.bitIndex << "]";
}
inline std::ostream& operator <<(std::ostream& os, const IntToBitVector& bv) CVC4_PUBLIC;
inline std::ostream& operator <<(std::ostream& os, const IntToBitVector& bv) {
return os << "[" << bv.size << "]";
}
}/* CVC4 namespace */
#endif /* __CVC4__BITVECTOR_H */
CVC4::BitVector
Definition bitvector.h:29
CVC4::BitVector::concat
BitVector concat(const BitVector &other) const
Definition bitvector.h:100
CVC4::BitVector::zeroExtend
BitVector zeroExtend(unsigned amount) const
Definition bitvector.h:261
CVC4::BitVector::hash
size_t hash() const
Definition bitvector.h:338
CVC4::BitVector::getValue
const Integer & getValue() const
Definition bitvector.h:359
CVC4::BitVector::logicalRightShift
BitVector logicalRightShift(const BitVector &y) const
Definition bitvector.h:294
CVC4::BitVector::signedLessThan
bool signedLessThan(const BitVector &y) const
Definition bitvector.h:222
CVC4::BitVector::unsignedDivTotal
BitVector unsignedDivTotal(const BitVector &y) const
Total division function that returns 0 when the denominator is 0.
Definition bitvector.h:196
CVC4::BitVector::signExtend
BitVector signExtend(unsigned amount) const
Definition bitvector.h:265
CVC4::BitVector::toString
std::string toString(unsigned int base=2) const
Definition bitvector.h:342
CVC4::BitVector::toInteger
Integer toInteger() const
Definition bitvector.h:78
CVC4::BitVector::BitVector
BitVector(unsigned size, const BitVector &q)
Definition bitvector.h:71
CVC4::BitVector::leftShift
BitVector leftShift(const BitVector &y) const
Definition bitvector.h:279
CVC4::BitVector::signedLessThanEq
bool signedLessThanEq(const BitVector &y) const
Definition bitvector.h:239
CVC4::BitVector::getSize
unsigned getSize() const
Definition bitvector.h:355
CVC4::BitVector::unsignedLessThan
bool unsignedLessThan(const BitVector &y) const
Definition bitvector.h:232
CVC4::BitVector::~BitVector
~BitVector()
Definition bitvector.h:76
CVC4::BitVector::isPow2
unsigned isPow2()
Returns k is the integer is equal to 2^{k-1} and zero otherwise.
Definition bitvector.h:368
CVC4::BitVector::isBitSet
bool isBitSet(uint32_t i) const
Definition bitvector.h:188
CVC4::BitVector::unsignedRemTotal
BitVector unsignedRemTotal(const BitVector &y) const
Total division function that returns 0 when the denominator is 0.
Definition bitvector.h:211
CVC4::BitVector::BitVector
BitVector(unsigned size=0)
Definition bitvector.h:58
CVC4::BitVector::BitVector
BitVector(unsigned size, unsigned long int z)
Definition bitvector.h:66
CVC4::BitVector::setBit
BitVector setBit(uint32_t i) const
Definition bitvector.h:182
CVC4::BitVector::arithRightShift
BitVector arithRightShift(const BitVector &y) const
Definition bitvector.h:306
CVC4::BitVector::BitVector
BitVector(unsigned size, unsigned int z)
Definition bitvector.h:61
CVC4::BitVector::BitVector
BitVector(unsigned size, const Integer &val)
Definition bitvector.h:53
CVC4::BitVector::extract
BitVector extract(unsigned high, unsigned low) const
Definition bitvector.h:104
CVC4::BitVector::unsignedLessThanEq
bool unsignedLessThanEq(const BitVector &y) const
Definition bitvector.h:249
CVC4::Integer
Definition integer_gmp_imp.h:34
CVC4::Integer::floorDivideQuotient
Integer floorDivideQuotient(const Integer &y) const
Returns the floor(this / y)
Definition integer_cln_imp.h:212
CVC4::Integer::setBit
Integer setBit(uint32_t i) const
Definition integer_cln_imp.h:183
CVC4::Integer::bitwiseAnd
Integer bitwiseAnd(const Integer &y) const
Definition integer_cln_imp.h:158
CVC4::Integer::extractBitRange
Integer extractBitRange(uint32_t bitCount, uint32_t low) const
See CLN Documentation.
Definition integer_cln_imp.h:204
CVC4::Integer::hash
size_t hash() const
Computes the hash of the node from the first word of the numerator, the denominator.
Definition integer_cln_imp.h:450
CVC4::Integer::oneExtend
Integer oneExtend(uint32_t size, uint32_t amount) const
Definition integer_cln_imp.h:189
CVC4::Integer::bitwiseXor
Integer bitwiseXor(const Integer &y) const
Definition integer_cln_imp.h:162
CVC4::Integer::bitwiseNot
Integer bitwiseNot() const
Definition integer_cln_imp.h:166
CVC4::Integer::toString
std::string toString(int base=10) const
Definition integer_cln_imp.h:364
CVC4::Integer::divByPow2
Integer divByPow2(uint32_t exp) const
Definition integer_cln_imp.h:313
CVC4::Integer::isPow2
unsigned isPow2() const
Returns k if the integer is equal to 2^(k-1)
Definition integer_cln_imp.h:468
CVC4::Integer::multiplyByPow2
Integer multiplyByPow2(uint32_t pow) const
Return this*(2^pow).
Definition integer_cln_imp.h:174
CVC4::Integer::bitwiseOr
Integer bitwiseOr(const Integer &y) const
Definition integer_cln_imp.h:154
CVC4::Integer::toUnsignedInt
uint32_t toUnsignedInt() const
Definition integer_cln_imp.h:198
CVC4::Integer::floorDivideRemainder
Integer floorDivideRemainder(const Integer &y) const
Returns r == this - floor(this/y)*y.
Definition integer_cln_imp.h:219
CVC4::Integer::modByPow2
Integer modByPow2(uint32_t exp) const
Definition integer_cln_imp.h:308
CVC4::Integer::isBitSet
bool isBitSet(uint32_t i) const
Definition integer_cln_imp.h:179
cvc4_public.h
Macros that should be defined everywhere during the building of the libraries and driver binary,...
CVC4_PUBLIC
#define CVC4_PUBLIC
Definition cvc4_public.h:30
exception.h
CVC4's exception base class and some associated utilities.
CVC4
Definition expr.h:106
CVC4::operator<<
std::ostream & operator<<(std::ostream &out, const TypeCheckingException &e)
CVC4::CheckArgument
void CheckArgument(bool cond, const T &arg, const char *fmt,...)
Definition exception.h:140
CVC4::BitVectorBitOfHashFunction
Hash function for the BitVectorBitOf objects.
Definition bitvector.h:446
CVC4::BitVectorBitOfHashFunction::operator()
size_t operator()(const BitVectorBitOf &b) const
Definition bitvector.h:447
CVC4::BitVectorBitOf
The structure representing the extraction of one Boolean bit.
Definition bitvector.h:432
CVC4::BitVectorBitOf::BitVectorBitOf
BitVectorBitOf(unsigned i)
Definition bitvector.h:435
CVC4::BitVectorBitOf::bitIndex
unsigned bitIndex
The index of the bit.
Definition bitvector.h:434
CVC4::BitVectorExtractHashFunction
Hash function for the BitVectorExtract objects.
Definition bitvector.h:420
CVC4::BitVectorExtractHashFunction::operator()
size_t operator()(const BitVectorExtract &extract) const
Definition bitvector.h:421
CVC4::BitVectorExtract
The structure representing the extraction operation for bit-vectors.
Definition bitvector.h:403
CVC4::BitVectorExtract::high
unsigned high
The high bit of the range for this extract.
Definition bitvector.h:405
CVC4::BitVectorExtract::low
unsigned low
The low bit of the range for this extract.
Definition bitvector.h:407
CVC4::BitVectorExtract::BitVectorExtract
BitVectorExtract(unsigned high, unsigned low)
Definition bitvector.h:409
CVC4::BitVectorHashFunction
Hash function for the BitVector constants.
Definition bitvector.h:392
CVC4::BitVectorHashFunction::operator()
size_t operator()(const BitVector &bv) const
Definition bitvector.h:393
CVC4::BitVectorRepeat
Definition bitvector.h:461
CVC4::BitVectorRepeat::BitVectorRepeat
BitVectorRepeat(unsigned repeatAmount)
Definition bitvector.h:463
CVC4::BitVectorRepeat::repeatAmount
unsigned repeatAmount
Definition bitvector.h:462
CVC4::BitVectorRotateLeft
Definition bitvector.h:482
CVC4::BitVectorRotateLeft::rotateLeftAmount
unsigned rotateLeftAmount
Definition bitvector.h:483
CVC4::BitVectorRotateLeft::BitVectorRotateLeft
BitVectorRotateLeft(unsigned rotateLeftAmount)
Definition bitvector.h:484
CVC4::BitVectorRotateRight
Definition bitvector.h:489
CVC4::BitVectorRotateRight::BitVectorRotateRight
BitVectorRotateRight(unsigned rotateRightAmount)
Definition bitvector.h:491
CVC4::BitVectorRotateRight::rotateRightAmount
unsigned rotateRightAmount
Definition bitvector.h:490
CVC4::BitVectorSignExtend
Definition bitvector.h:475
CVC4::BitVectorSignExtend::BitVectorSignExtend
BitVectorSignExtend(unsigned signExtendAmount)
Definition bitvector.h:477
CVC4::BitVectorSignExtend::signExtendAmount
unsigned signExtendAmount
Definition bitvector.h:476
CVC4::BitVectorSize
Definition bitvector.h:454
CVC4::BitVectorSize::size
unsigned size
Definition bitvector.h:455
CVC4::BitVectorSize::BitVectorSize
BitVectorSize(unsigned size)
Definition bitvector.h:456
CVC4::BitVectorZeroExtend
Definition bitvector.h:468
CVC4::BitVectorZeroExtend::BitVectorZeroExtend
BitVectorZeroExtend(unsigned zeroExtendAmount)
Definition bitvector.h:470
CVC4::BitVectorZeroExtend::zeroExtendAmount
unsigned zeroExtendAmount
Definition bitvector.h:469
CVC4::IntToBitVector
Definition bitvector.h:496
CVC4::IntToBitVector::size
unsigned size
Definition bitvector.h:497
CVC4::IntToBitVector::IntToBitVector
IntToBitVector(unsigned size)
Definition bitvector.h:498
CVC4::UnsignedHashFunction
Definition bitvector.h:504
CVC4::UnsignedHashFunction::operator()
size_t operator()(const T &x) const
Definition bitvector.h:505