integer_gmp_imp.h
Go to the documentation of this file.
/********************* */
#include "cvc4_public.h"
#ifndef __CVC4__INTEGER_H
#define __CVC4__INTEGER_H
#include <string>
#include <iostream>
#include <limits>
#include "util/gmp_util.h"
#include "util/exception.h"
namespace CVC4 {
class Rational;
class CVC4_PUBLIC Integer {
private:
mpz_class d_value;
const mpz_class& get_mpz() const { return d_value; }
Integer(const mpz_class& val) : d_value(val) {}
public:
Integer() : d_value(0){}
explicit Integer(const char* s, unsigned base = 10);
explicit Integer(const std::string& s, unsigned base = 10);
Integer(const Integer& q) : d_value(q.d_value) {}
Integer( signed int z) : d_value(z) {}
Integer(unsigned int z) : d_value(z) {}
Integer( signed long int z) : d_value(z) {}
Integer(unsigned long int z) : d_value(z) {}
#ifdef CVC4_NEED_INT64_T_OVERLOADS
Integer( int64_t z) : d_value(static_cast<long>(z)) {}
Integer(uint64_t z) : d_value(static_cast<unsigned long>(z)) {}
#endif /* CVC4_NEED_INT64_T_OVERLOADS */
~Integer() {}
Integer& operator=(const Integer& x){
if(this == &x) return *this;
d_value = x.d_value;
return *this;
}
bool operator==(const Integer& y) const {
return d_value == y.d_value;
}
Integer operator-() const {
return Integer(-(d_value));
}
bool operator!=(const Integer& y) const {
return d_value != y.d_value;
}
bool operator< (const Integer& y) const {
return d_value < y.d_value;
}
bool operator<=(const Integer& y) const {
return d_value <= y.d_value;
}
bool operator> (const Integer& y) const {
return d_value > y.d_value;
}
bool operator>=(const Integer& y) const {
return d_value >= y.d_value;
}
Integer operator+(const Integer& y) const {
return Integer( d_value + y.d_value );
}
Integer& operator+=(const Integer& y) {
d_value += y.d_value;
return *this;
}
Integer operator-(const Integer& y) const {
return Integer( d_value - y.d_value );
}
Integer& operator-=(const Integer& y) {
d_value -= y.d_value;
return *this;
}
Integer operator*(const Integer& y) const {
return Integer( d_value * y.d_value );
}
Integer& operator*=(const Integer& y) {
d_value *= y.d_value;
return *this;
}
Integer bitwiseOr(const Integer& y) const {
mpz_class result;
mpz_ior(result.get_mpz_t(), d_value.get_mpz_t(), y.d_value.get_mpz_t());
return Integer(result);
}
Integer bitwiseAnd(const Integer& y) const {
mpz_class result;
mpz_and(result.get_mpz_t(), d_value.get_mpz_t(), y.d_value.get_mpz_t());
return Integer(result);
}
Integer bitwiseXor(const Integer& y) const {
mpz_class result;
mpz_xor(result.get_mpz_t(), d_value.get_mpz_t(), y.d_value.get_mpz_t());
return Integer(result);
}
Integer bitwiseNot() const {
mpz_class result;
mpz_com(result.get_mpz_t(), d_value.get_mpz_t());
return Integer(result);
}
Integer multiplyByPow2(uint32_t pow) const{
mpz_class result;
mpz_mul_2exp(result.get_mpz_t(), d_value.get_mpz_t(), pow);
return Integer( result );
}
Integer setBit(uint32_t i) const {
mpz_class res = d_value;
mpz_setbit(res.get_mpz_t(), i);
return Integer(res);
}
bool isBitSet(uint32_t i) const {
return !extractBitRange(1, i).isZero();
}
Integer oneExtend(uint32_t size, uint32_t amount) const {
// check that the size is accurate
DebugCheckArgument((*this) < Integer(1).multiplyByPow2(size), size);
mpz_class res = d_value;
for (unsigned i = size; i < size + amount; ++i) {
mpz_setbit(res.get_mpz_t(), i);
}
return Integer(res);
}
uint32_t toUnsignedInt() const {
return mpz_get_ui(d_value.get_mpz_t());
}
Integer extractBitRange(uint32_t bitCount, uint32_t low) const {
// bitCount = high-low+1
uint32_t high = low + bitCount-1;
//— Function: void mpz_fdiv_r_2exp (mpz_t r, mpz_t n, mp_bitcnt_t b)
mpz_class rem, div;
mpz_fdiv_r_2exp(rem.get_mpz_t(), d_value.get_mpz_t(), high+1);
mpz_fdiv_q_2exp(div.get_mpz_t(), rem.get_mpz_t(), low);
return Integer(div);
}
Integer floorDivideQuotient(const Integer& y) const {
mpz_class q;
mpz_fdiv_q(q.get_mpz_t(), d_value.get_mpz_t(), y.d_value.get_mpz_t());
return Integer( q );
}
Integer floorDivideRemainder(const Integer& y) const {
mpz_class r;
mpz_fdiv_r(r.get_mpz_t(), d_value.get_mpz_t(), y.d_value.get_mpz_t());
return Integer( r );
}
static void floorQR(Integer& q, Integer& r, const Integer& x, const Integer& y) {
mpz_fdiv_qr(q.d_value.get_mpz_t(), r.d_value.get_mpz_t(), x.d_value.get_mpz_t(), y.d_value.get_mpz_t());
}
Integer ceilingDivideQuotient(const Integer& y) const {
mpz_class q;
mpz_cdiv_q(q.get_mpz_t(), d_value.get_mpz_t(), y.d_value.get_mpz_t());
return Integer( q );
}
Integer ceilingDivideRemainder(const Integer& y) const {
mpz_class r;
mpz_cdiv_r(r.get_mpz_t(), d_value.get_mpz_t(), y.d_value.get_mpz_t());
return Integer( r );
}
static void euclidianQR(Integer& q, Integer& r, const Integer& x, const Integer& y) {
// compute the floor and then fix the value up if needed.
floorQR(q,r,x,y);
if(r.strictlyNegative()){
// if r < 0
// abs(r) < abs(y)
// - abs(y) < r < 0, then 0 < r + abs(y) < abs(y)
// n = y * q + r
// n = y * q - abs(y) + r + abs(y)
if(r.sgn() >= 0){
// y = abs(y)
// n = y * q - y + r + y
// n = y * (q-1) + (r+y)
q -= 1;
r += y;
}else{
// y = -abs(y)
// n = y * q + y + r - y
// n = y * (q+1) + (r-y)
q += 1;
r -= y;
}
}
}
Integer euclidianDivideQuotient(const Integer& y) const {
Integer q,r;
euclidianQR(q,r, *this, y);
return q;
}
Integer euclidianDivideRemainder(const Integer& y) const {
Integer q,r;
euclidianQR(q,r, *this, y);
return r;
}
Integer exactQuotient(const Integer& y) const {
DebugCheckArgument(y.divides(*this), y);
mpz_class q;
mpz_divexact(q.get_mpz_t(), d_value.get_mpz_t(), y.d_value.get_mpz_t());
return Integer( q );
}
Integer modByPow2(uint32_t exp) const {
mpz_class res;
mpz_fdiv_r_2exp(res.get_mpz_t(), d_value.get_mpz_t(), exp);
return Integer(res);
}
Integer divByPow2(uint32_t exp) const {
mpz_class res;
mpz_fdiv_q_2exp(res.get_mpz_t(), d_value.get_mpz_t(), exp);
return Integer(res);
}
int sgn() const {
return mpz_sgn(d_value.get_mpz_t());
}
inline bool strictlyPositive() const {
return sgn() > 0;
}
inline bool strictlyNegative() const {
return sgn() < 0;
}
inline bool isZero() const {
return sgn() == 0;
}
bool isOne() const {
return mpz_cmp_si(d_value.get_mpz_t(), 1) == 0;
}
bool isNegativeOne() const {
return mpz_cmp_si(d_value.get_mpz_t(), -1) == 0;
}
Integer pow(unsigned long int exp) const {
mpz_class result;
mpz_pow_ui(result.get_mpz_t(),d_value.get_mpz_t(),exp);
return Integer( result );
}
Integer gcd(const Integer& y) const {
mpz_class result;
mpz_gcd(result.get_mpz_t(), d_value.get_mpz_t(), y.d_value.get_mpz_t());
return Integer(result);
}
Integer lcm(const Integer& y) const {
mpz_class result;
mpz_lcm(result.get_mpz_t(), d_value.get_mpz_t(), y.d_value.get_mpz_t());
return Integer(result);
}
bool divides(const Integer& y) const {
int res = mpz_divisible_p(y.d_value.get_mpz_t(), d_value.get_mpz_t());
return res != 0;
}
Integer abs() const {
return d_value >= 0 ? *this : -*this;
}
std::string toString(int base = 10) const{
return d_value.get_str(base);
}
bool fitsSignedInt() const;
bool fitsUnsignedInt() const;
signed int getSignedInt() const;
unsigned int getUnsignedInt() const;
long getLong() const {
long si = d_value.get_si();
// ensure there wasn't overflow
CheckArgument(mpz_cmp_si(d_value.get_mpz_t(), si) == 0, this,
"Overflow detected in Integer::getLong()");
return si;
}
unsigned long getUnsignedLong() const {
unsigned long ui = d_value.get_ui();
// ensure there wasn't overflow
CheckArgument(mpz_cmp_ui(d_value.get_mpz_t(), ui) == 0, this,
"Overflow detected in Integer::getUnsignedLong()");
return ui;
}
size_t hash() const {
return gmpz_hash(d_value.get_mpz_t());
}
bool testBit(unsigned n) const {
return mpz_tstbit(d_value.get_mpz_t(), n);
}
unsigned isPow2() const {
if (d_value <= 0) return 0;
// check that the number of ones in the binary representation is 1
if (mpz_popcount(d_value.get_mpz_t()) == 1) {
// return the index of the first one plus 1
return mpz_scan1(d_value.get_mpz_t(), 0) + 1;
}
return 0;
}
size_t length() const {
if(sgn() == 0){
return 1;
}else{
return mpz_sizeinbase(d_value.get_mpz_t(),2);
}
}
static void extendedGcd(Integer& g, Integer& s, Integer& t, const Integer& a, const Integer& b){
//see the documentation for:
//mpz_gcdext (mpz_t g, mpz_t s, mpz_t t, mpz_t a, mpz_t b);
mpz_gcdext (g.d_value.get_mpz_t(), s.d_value.get_mpz_t(), t.d_value.get_mpz_t(), a.d_value.get_mpz_t(), b.d_value.get_mpz_t());
}
static const Integer& min(const Integer& a, const Integer& b){
return (a <=b ) ? a : b;
}
static const Integer& max(const Integer& a, const Integer& b){
return (a >= b ) ? a : b;
}
friend class CVC4::Rational;
};/* class Integer */
struct IntegerHashFunction {
inline size_t operator()(const CVC4::Integer& i) const {
return i.hash();
}
};/* struct IntegerHashFunction */
inline std::ostream& operator<<(std::ostream& os, const Integer& n) {
return os << n.toString();
}
}/* CVC4 namespace */
#endif /* __CVC4__INTEGER_H */
CVC4::Integer
Definition integer_gmp_imp.h:34
CVC4::Integer::operator-
Integer operator-(const Integer &y) const
Definition integer_gmp_imp.h:125
CVC4::Integer::lcm
Integer lcm(const Integer &y) const
Return the least common multiple of this integer with another.
Definition integer_gmp_imp.h:395
CVC4::Integer::floorDivideQuotient
Integer floorDivideQuotient(const Integer &y) const
Returns the floor(this / y)
Definition integer_gmp_imp.h:224
CVC4::Integer::ceilingDivideQuotient
Integer ceilingDivideQuotient(const Integer &y) const
Returns the ceil(this / y)
Definition integer_gmp_imp.h:249
CVC4::Integer::Integer
Integer(unsigned long int z)
Definition integer_gmp_imp.h:72
CVC4::Integer::divides
bool divides(const Integer &y) const
Return true if *this exactly divides y.
Definition integer_cln_imp.h:352
CVC4::Integer::setBit
Integer setBit(uint32_t i) const
Returns the Integer obtained by setting the ith bit of the current Integer to 1.
Definition integer_gmp_imp.h:179
CVC4::Integer::bitwiseAnd
Integer bitwiseAnd(const Integer &y) const
Definition integer_gmp_imp.h:148
CVC4::Integer::euclidianQR
static void euclidianQR(Integer &q, Integer &r, const Integer &x, const Integer &y)
Computes a quoitent and remainder according to Boute's Euclidean definition.
Definition integer_gmp_imp.h:273
CVC4::Integer::operator<=
bool operator<=(const Integer &y) const
Definition integer_gmp_imp.h:104
CVC4::Integer::operator-=
Integer & operator-=(const Integer &y)
Definition integer_gmp_imp.h:128
CVC4::Integer::extractBitRange
Integer extractBitRange(uint32_t bitCount, uint32_t low) const
See GMP Documentation.
Definition integer_gmp_imp.h:210
CVC4::Integer::Integer
Integer(const Integer &q)
Definition integer_gmp_imp.h:67
CVC4::Integer::~Integer
~Integer()
Definition integer_gmp_imp.h:79
CVC4::Integer::fitsUnsignedInt
bool fitsUnsignedInt() const
CVC4::Integer::ceilingDivideRemainder
Integer ceilingDivideRemainder(const Integer &y) const
Returns the ceil(this / y)
Definition integer_gmp_imp.h:258
CVC4::Integer::hash
size_t hash() const
Computes the hash of the node from the first word of the numerator, the denominator.
Definition integer_gmp_imp.h:448
CVC4::Integer::operator=
Integer & operator=(const Integer &x)
Definition integer_gmp_imp.h:81
CVC4::Integer::operator>=
bool operator>=(const Integer &y) const
Definition integer_gmp_imp.h:112
CVC4::Integer::euclidianDivideRemainder
Integer euclidianDivideRemainder(const Integer &y) const
Returns the remainfing according to Boute's Euclidean definition.
Definition integer_gmp_imp.h:312
CVC4::Integer::operator-
Integer operator-() const
Definition integer_gmp_imp.h:91
CVC4::Integer::getUnsignedInt
unsigned int getUnsignedInt() const
CVC4::Integer::oneExtend
Integer oneExtend(uint32_t size, uint32_t amount) const
Returns the integer with the binary representation of size bits extended with amount 1's.
Definition integer_gmp_imp.h:193
CVC4::Integer::abs
Integer abs() const
Return the absolute value of this integer.
Definition integer_gmp_imp.h:413
CVC4::Integer::isOne
bool isOne() const
Definition integer_gmp_imp.h:364
CVC4::Integer::exactQuotient
Integer exactQuotient(const Integer &y) const
If y divides *this, then exactQuotient returns (this/y)
Definition integer_gmp_imp.h:322
CVC4::Integer::operator==
bool operator==(const Integer &y) const
Definition integer_gmp_imp.h:87
CVC4::Integer::getLong
long getLong() const
Definition integer_gmp_imp.h:429
CVC4::Integer::bitwiseXor
Integer bitwiseXor(const Integer &y) const
Definition integer_gmp_imp.h:154
CVC4::Integer::extendedGcd
static void extendedGcd(Integer &g, Integer &s, Integer &t, const Integer &a, const Integer &b)
Definition integer_gmp_imp.h:489
CVC4::Integer::strictlyPositive
bool strictlyPositive() const
Definition integer_gmp_imp.h:352
CVC4::Integer::strictlyNegative
bool strictlyNegative() const
Definition integer_cln_imp.h:402
CVC4::Integer::max
static const Integer & max(const Integer &a, const Integer &b)
Returns a reference to the maximum of two integers.
Definition integer_gmp_imp.h:501
CVC4::Integer::pow
Integer pow(unsigned long int exp) const
Raise this Integer to the power exp.
Definition integer_gmp_imp.h:377
CVC4::Integer::operator+
Integer operator+(const Integer &y) const
Definition integer_gmp_imp.h:117
CVC4::Integer::bitwiseNot
Integer bitwiseNot() const
Definition integer_gmp_imp.h:160
CVC4::Integer::operator*=
Integer & operator*=(const Integer &y)
Definition integer_gmp_imp.h:136
CVC4::Integer::Integer
Integer(const char *s, unsigned base=10)
Constructs a Integer from a C string.
CVC4::Integer::operator*
Integer operator*(const Integer &y) const
Definition integer_gmp_imp.h:133
CVC4::Integer::toString
std::string toString(int base=10) const
Definition integer_gmp_imp.h:417
CVC4::Integer::isZero
bool isZero() const
Definition integer_gmp_imp.h:360
CVC4::Integer::fitsSignedInt
bool fitsSignedInt() const
CVC4::Integer::getUnsignedLong
unsigned long getUnsignedLong() const
Definition integer_gmp_imp.h:436
CVC4::Integer::divByPow2
Integer divByPow2(uint32_t exp) const
Returns y / 2^exp.
Definition integer_gmp_imp.h:341
CVC4::Integer::isPow2
unsigned isPow2() const
Returns k if the integer is equal to 2^(k-1)
Definition integer_gmp_imp.h:466
CVC4::Integer::multiplyByPow2
Integer multiplyByPow2(uint32_t pow) const
Return this*(2^pow).
Definition integer_gmp_imp.h:169
CVC4::Integer::length
size_t length() const
If x != 0, returns the smallest n s.t.
Definition integer_gmp_imp.h:481
CVC4::Integer::Integer
Integer(signed long int z)
Definition integer_gmp_imp.h:71
CVC4::Integer::sgn
int sgn() const
Definition integer_cln_imp.h:392
CVC4::Integer::euclidianDivideQuotient
Integer euclidianDivideQuotient(const Integer &y) const
Returns the quoitent according to Boute's Euclidean definition.
Definition integer_gmp_imp.h:302
CVC4::Integer::Integer
Integer(unsigned int z)
Definition integer_gmp_imp.h:70
CVC4::Integer::floorQR
static void floorQR(Integer &q, Integer &r, const Integer &x, const Integer &y)
Computes a floor quotient and remainder for x divided by y.
Definition integer_gmp_imp.h:242
CVC4::Integer::bitwiseOr
Integer bitwiseOr(const Integer &y) const
Definition integer_gmp_imp.h:142
CVC4::Integer::Integer
Integer(const std::string &s, unsigned base=10)
CVC4::Integer::min
static const Integer & min(const Integer &a, const Integer &b)
Returns a reference to the minimum of two integers.
Definition integer_gmp_imp.h:496
CVC4::Integer::operator!=
bool operator!=(const Integer &y) const
Definition integer_gmp_imp.h:96
CVC4::Integer::Integer
Integer()
Constructs a rational with the value 0.
Definition integer_gmp_imp.h:56
CVC4::Integer::toUnsignedInt
uint32_t toUnsignedInt() const
Definition integer_gmp_imp.h:205
CVC4::Integer::getSignedInt
signed int getSignedInt() const
CVC4::Integer::floorDivideRemainder
Integer floorDivideRemainder(const Integer &y) const
Returns r == this - floor(this/y)*y.
Definition integer_gmp_imp.h:233
CVC4::Integer::Integer
Integer(signed int z)
Definition integer_gmp_imp.h:69
CVC4::Integer::modByPow2
Integer modByPow2(uint32_t exp) const
Returns y mod 2^exp.
Definition integer_gmp_imp.h:332
CVC4::Integer::gcd
Integer gcd(const Integer &y) const
Return the greatest common divisor of this integer with another.
Definition integer_gmp_imp.h:386
CVC4::Integer::isNegativeOne
bool isNegativeOne() const
Definition integer_gmp_imp.h:368
CVC4::Integer::operator+=
Integer & operator+=(const Integer &y)
Definition integer_gmp_imp.h:120
CVC4::Integer::isBitSet
bool isBitSet(uint32_t i) const
Definition integer_gmp_imp.h:185
CVC4::Integer::testBit
bool testBit(unsigned n) const
Returns true iff bit n is set.
Definition integer_gmp_imp.h:458
CVC4::Rational
A multi-precision rational constant.
Definition rational_gmp_imp.h:51
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.
gmp_util.h
[[ Add one-line brief description here ]]
CVC4
Definition expr.h:106
CVC4::gmpz_hash
size_t gmpz_hash(const mpz_t toHash)
Hashes the gmp integer primitive in a word by word fashion.
Definition gmp_util.h:28
CVC4::DebugCheckArgument
void DebugCheckArgument(bool cond, const T &arg, const char *fmt,...)
Definition exception.h:155
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::IntegerHashFunction
Definition integer_gmp_imp.h:508
CVC4::IntegerHashFunction::operator()
size_t operator()(const CVC4::Integer &i) const
Definition integer_gmp_imp.h:509