integer_cln_imp.h
Go to the documentation of this file.
/********************* */
#include "cvc4_public.h"
#ifndef __CVC4__INTEGER_H
#define __CVC4__INTEGER_H
#include <string>
#include <sstream>
#include <iostream>
#include <cln/integer.h>
#include <cln/input.h>
#include <cln/integer_io.h>
#include <limits>
#include "util/exception.h"
namespace CVC4 {
class Rational;
class CVC4_PUBLIC Integer {
private:
cln::cl_I d_value;
const cln::cl_I& get_cl_I() const { return d_value; }
Integer(const cln::cl_I& val) : d_value(val) {}
void readInt(const cln::cl_read_flags& flags, const std::string& s, unsigned base) throw(std::invalid_argument);
void parseInt(const std::string& s, unsigned base) throw(std::invalid_argument);
public:
Integer() : d_value(0){}
explicit Integer(const char* sp, unsigned base = 10) throw (std::invalid_argument) {
parseInt(std::string(sp), base);
}
explicit Integer(const std::string& s, unsigned base = 10) throw (std::invalid_argument) {
parseInt(s, base);
}
Integer(const Integer& q) : d_value(q.d_value) {}
Integer( signed int z) : d_value((signed long int)z) {}
Integer(unsigned int z) : d_value((unsigned long int)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 {
return Integer(cln::logior(d_value, y.d_value));
}
Integer bitwiseAnd(const Integer& y) const {
return Integer(cln::logand(d_value, y.d_value));
}
Integer bitwiseXor(const Integer& y) const {
return Integer(cln::logxor(d_value, y.d_value));
}
Integer bitwiseNot() const {
return Integer(cln::lognot(d_value));
}
Integer multiplyByPow2(uint32_t pow) const {
cln::cl_I ipow(pow);
return Integer( d_value << ipow);
}
bool isBitSet(uint32_t i) const {
return !extractBitRange(1, i).isZero();
}
Integer setBit(uint32_t i) const {
cln::cl_I mask(1);
mask = mask << i;
return Integer(cln::logior(d_value, mask));
}
Integer oneExtend(uint32_t size, uint32_t amount) const {
DebugCheckArgument((*this) < Integer(1).multiplyByPow2(size), size);
cln::cl_byte range(amount, size);
cln::cl_I allones = (cln::cl_I(1) << (size + amount))- 1; // 2^size - 1
Integer temp(allones);
return Integer(cln::deposit_field(allones, d_value, range));
}
uint32_t toUnsignedInt() const {
return cln::cl_I_to_uint(d_value);
}
Integer extractBitRange(uint32_t bitCount, uint32_t low) const {
cln::cl_byte range(bitCount, low);
return Integer(cln::ldb(d_value, range));
}
Integer floorDivideQuotient(const Integer& y) const {
return Integer( cln::floor1(d_value, y.d_value) );
}
Integer floorDivideRemainder(const Integer& y) const {
return Integer( cln::floor2(d_value, y.d_value).remainder );
}
static void floorQR(Integer& q, Integer& r, const Integer& x, const Integer& y) {
cln::cl_I_div_t res = cln::floor2(x.d_value, y.d_value);
q.d_value = res.quotient;
r.d_value = res.remainder;
}
Integer ceilingDivideQuotient(const Integer& y) const {
return Integer( cln::ceiling1(d_value, y.d_value) );
}
Integer ceilingDivideRemainder(const Integer& y) const {
return Integer( cln::ceiling2(d_value, y.d_value).remainder );
}
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);
return Integer( cln::exquo(d_value, y.d_value) );
}
Integer modByPow2(uint32_t exp) const {
cln::cl_byte range(exp, 0);
return Integer(cln::ldb(d_value, range));
}
Integer divByPow2(uint32_t exp) const {
return d_value >> exp;
}
Integer pow(unsigned long int exp) const {
if(exp > 0){
cln::cl_I result= cln::expt_pos(d_value,exp);
return Integer( result );
}else if(exp == 0){
return Integer( 1 );
}else{
throw Exception("Negative exponent in Integer::pow()");
}
}
Integer gcd(const Integer& y) const {
cln::cl_I result = cln::gcd(d_value, y.d_value);
return Integer(result);
}
Integer lcm(const Integer& y) const {
cln::cl_I result = cln::lcm(d_value, y.d_value);
return Integer(result);
}
bool divides(const Integer& y) const {
cln::cl_I result = cln::rem(y.d_value, d_value);
return cln::zerop(result);
}
Integer abs() const {
return d_value >= 0 ? *this : -*this;
}
std::string toString(int base = 10) const{
std::stringstream ss;
switch(base){
case 2:
fprintbinary(ss,d_value);
break;
case 8:
fprintoctal(ss,d_value);
break;
case 10:
fprintdecimal(ss,d_value);
break;
case 16:
fprinthexadecimal(ss,d_value);
break;
default:
throw Exception("Unhandled base in Integer::toString()");
}
std::string output = ss.str();
for( unsigned i = 0; i <= output.length(); ++i){
if(isalpha(output[i])){
output.replace(i, 1, 1, tolower(output[i]));
}
}
return output;
}
int sgn() const {
cln::cl_I sgn = cln::signum(d_value);
return cln::cl_I_to_int(sgn);
}
inline bool strictlyPositive() const {
return sgn() > 0;
}
inline bool strictlyNegative() const {
return sgn() < 0;
}
inline bool isZero() const {
return sgn() == 0;
}
inline bool isOne() const {
return d_value == 1;
}
inline bool isNegativeOne() const {
return d_value == -1;
}
bool fitsSignedInt() const;
bool fitsUnsignedInt() const;
int getSignedInt() const;
unsigned int getUnsignedInt() const;
long getLong() const {
// ensure there isn't overflow
CheckArgument(d_value <= std::numeric_limits<long>::max(), this,
"Overflow detected in Integer::getLong()");
CheckArgument(d_value >= std::numeric_limits<long>::min(), this,
"Overflow detected in Integer::getLong()");
return cln::cl_I_to_long(d_value);
}
unsigned long getUnsignedLong() const {
// ensure there isn't overflow
CheckArgument(d_value <= std::numeric_limits<unsigned long>::max(), this,
"Overflow detected in Integer::getUnsignedLong()");
CheckArgument(d_value >= std::numeric_limits<unsigned long>::min(), this,
"Overflow detected in Integer::getUnsignedLong()");
return cln::cl_I_to_ulong(d_value);
}
size_t hash() const {
return equal_hashcode(d_value);
}
bool testBit(unsigned n) const {
return cln::logbitp(n, d_value);
}
unsigned isPow2() const {
if (d_value <= 0) return 0;
// power2p returns n such that d_value = 2^(n-1)
return cln::power2p(d_value);
}
size_t length() const {
int s = sgn();
if(s == 0){
return 1;
}else if(s < 0){
size_t len = cln::integer_length(d_value);
/*If this is -2^n, return len+1 not len to stay consistent with the definition above!
* From CLN's documentation of integer_length:
* This is the smallest n >= 0 such that -2^n <= x < 2^n.
* If x > 0, this is the unique n > 0 such that 2^(n-1) <= x < 2^n.
*/
size_t ord2 = cln::ord2(d_value);
return (len == ord2) ? (len + 1) : len;
}else{
return cln::integer_length(d_value);
}
}
/* cl_I xgcd (const cl_I& a, const cl_I& b, cl_I* u, cl_I* v) */
/* This function ("extended gcd") returns the greatest common divisor g of a and b and at the same time the representation of g as an integral linear combination of a and b: u and v with u*a+v*b = g, g >= 0. u and v will be normalized to be of smallest possible absolute value, in the following sense: If a and b are non-zero, and abs(a) != abs(b), u and v will satisfy the inequalities abs(u) <= abs(b)/(2*g), abs(v) <= abs(a)/(2*g). */
static void extendedGcd(Integer& g, Integer& s, Integer& t, const Integer& a, const Integer& b){
g.d_value = cln::xgcd(a.d_value, b.d_value, &s.d_value, &t.d_value);
}
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::Exception
Definition exception.h:32
CVC4::Integer
Definition integer_gmp_imp.h:34
CVC4::Integer::operator-
Integer operator-(const Integer &y) const
Definition integer_cln_imp.h:137
CVC4::Integer::lcm
Integer lcm(const Integer &y) const
Return the least common multiple of this integer with another.
Definition integer_cln_imp.h:344
CVC4::Integer::floorDivideQuotient
Integer floorDivideQuotient(const Integer &y) const
Returns the floor(this / y)
Definition integer_cln_imp.h:212
CVC4::Integer::ceilingDivideQuotient
Integer ceilingDivideQuotient(const Integer &y) const
Returns the ceil(this / y)
Definition integer_cln_imp.h:234
CVC4::Integer::Integer
Integer(unsigned long int z)
Definition integer_cln_imp.h:84
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
Definition integer_cln_imp.h:183
CVC4::Integer::bitwiseAnd
Integer bitwiseAnd(const Integer &y) const
Definition integer_cln_imp.h:158
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_cln_imp.h:254
CVC4::Integer::operator<=
bool operator<=(const Integer &y) const
Definition integer_cln_imp.h:116
CVC4::Integer::operator-=
Integer & operator-=(const Integer &y)
Definition integer_cln_imp.h:140
CVC4::Integer::extractBitRange
Integer extractBitRange(uint32_t bitCount, uint32_t low) const
See CLN Documentation.
Definition integer_cln_imp.h:204
CVC4::Integer::Integer
Integer(const Integer &q)
Definition integer_cln_imp.h:79
CVC4::Integer::~Integer
~Integer()
Definition integer_cln_imp.h:91
CVC4::Integer::fitsUnsignedInt
bool fitsUnsignedInt() const
fits the C "unsigned int" primitive
CVC4::Integer::ceilingDivideRemainder
Integer ceilingDivideRemainder(const Integer &y) const
Returns the ceil(this / y)
Definition integer_cln_imp.h:241
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::operator=
Integer & operator=(const Integer &x)
Definition integer_cln_imp.h:93
CVC4::Integer::operator>=
bool operator>=(const Integer &y) const
Definition integer_cln_imp.h:124
CVC4::Integer::euclidianDivideRemainder
Integer euclidianDivideRemainder(const Integer &y) const
Returns the remainfing according to Boute's Euclidean definition.
Definition integer_cln_imp.h:294
CVC4::Integer::operator-
Integer operator-() const
Definition integer_cln_imp.h:103
CVC4::Integer::getUnsignedInt
unsigned int getUnsignedInt() const
CVC4::Integer::oneExtend
Integer oneExtend(uint32_t size, uint32_t amount) const
Definition integer_cln_imp.h:189
CVC4::Integer::abs
Integer abs() const
Return the absolute value of this integer.
Definition integer_cln_imp.h:360
CVC4::Integer::isOne
bool isOne() const
Definition integer_cln_imp.h:410
CVC4::Integer::exactQuotient
Integer exactQuotient(const Integer &y) const
If y divides *this, then exactQuotient returns (this/y)
Definition integer_cln_imp.h:303
CVC4::Integer::operator==
bool operator==(const Integer &y) const
Definition integer_cln_imp.h:99
CVC4::Integer::getLong
long getLong() const
Definition integer_cln_imp.h:428
CVC4::Integer::bitwiseXor
Integer bitwiseXor(const Integer &y) const
Definition integer_cln_imp.h:162
CVC4::Integer::extendedGcd
static void extendedGcd(Integer &g, Integer &s, Integer &t, const Integer &a, const Integer &b)
Definition integer_cln_imp.h:498
CVC4::Integer::strictlyPositive
bool strictlyPositive() const
Definition integer_cln_imp.h:398
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_cln_imp.h:508
CVC4::Integer::pow
Integer pow(unsigned long int exp) const
Raise this Integer to the power exp.
Definition integer_cln_imp.h:322
CVC4::Integer::getSignedInt
int getSignedInt() const
CVC4::Integer::operator+
Integer operator+(const Integer &y) const
Definition integer_cln_imp.h:129
CVC4::Integer::Integer
Integer(const char *sp, unsigned base=10)
Constructs a Integer from a C string.
Definition integer_cln_imp.h:71
CVC4::Integer::bitwiseNot
Integer bitwiseNot() const
Definition integer_cln_imp.h:166
CVC4::Integer::operator*=
Integer & operator*=(const Integer &y)
Definition integer_cln_imp.h:148
CVC4::Integer::operator*
Integer operator*(const Integer &y) const
Definition integer_cln_imp.h:145
CVC4::Integer::toString
std::string toString(int base=10) const
Definition integer_cln_imp.h:364
CVC4::Integer::isZero
bool isZero() const
Definition integer_cln_imp.h:406
CVC4::Integer::fitsSignedInt
bool fitsSignedInt() const
fits the C "signed int" primitive
CVC4::Integer::getUnsignedLong
unsigned long getUnsignedLong() const
Definition integer_cln_imp.h:437
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::length
size_t length() const
If x != 0, returns the unique n s.t.
Definition integer_cln_imp.h:478
CVC4::Integer::Integer
Integer(signed long int z)
Definition integer_cln_imp.h:83
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_cln_imp.h:284
CVC4::Integer::Integer
Integer(unsigned int z)
Definition integer_cln_imp.h:82
CVC4::Integer::floorQR
static void floorQR(Integer &q, Integer &r, const Integer &x, const Integer &y)
Computes a floor quoient and remainder for x divided by y.
Definition integer_cln_imp.h:225
CVC4::Integer::bitwiseOr
Integer bitwiseOr(const Integer &y) const
Definition integer_cln_imp.h:154
CVC4::Integer::Integer
Integer(const std::string &s, unsigned base=10)
Definition integer_cln_imp.h:75
CVC4::Integer::min
static const Integer & min(const Integer &a, const Integer &b)
Returns a reference to the minimum of two integers.
Definition integer_cln_imp.h:503
CVC4::Integer::operator!=
bool operator!=(const Integer &y) const
Definition integer_cln_imp.h:108
CVC4::Integer::Integer
Integer()
Constructs a rational with the value 0.
Definition integer_cln_imp.h:63
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::Integer
Integer(signed int z)
Definition integer_cln_imp.h:81
CVC4::Integer::modByPow2
Integer modByPow2(uint32_t exp) const
Definition integer_cln_imp.h:308
CVC4::Integer::gcd
Integer gcd(const Integer &y) const
Return the greatest common divisor of this integer with another.
Definition integer_cln_imp.h:336
CVC4::Integer::isNegativeOne
bool isNegativeOne() const
Definition integer_cln_imp.h:414
CVC4::Integer::operator+=
Integer & operator+=(const Integer &y)
Definition integer_cln_imp.h:132
CVC4::Integer::isBitSet
bool isBitSet(uint32_t i) const
Definition integer_cln_imp.h:179
CVC4::Integer::testBit
bool testBit(unsigned n) const
Returns true iff bit n is set.
Definition integer_cln_imp.h:460
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.
CVC4
Definition expr.h:106
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
std
STL namespace.
CVC4::IntegerHashFunction
Definition integer_gmp_imp.h:508
CVC4::IntegerHashFunction::operator()
size_t operator()(const CVC4::Integer &i) const
Definition integer_cln_imp.h:516