2013-07-16 20:11:24 +00:00
|
|
|
/*
|
2013-07-19 17:29:33 +00:00
|
|
|
Copyright (c) 2013 Microsoft Corporation. All rights reserved.
|
2013-07-16 20:11:24 +00:00
|
|
|
Released under Apache 2.0 license as described in the file LICENSE.
|
|
|
|
|
2013-07-19 17:29:33 +00:00
|
|
|
Author: Leonardo de Moura
|
2013-07-16 20:11:24 +00:00
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <iostream>
|
|
|
|
#include <gmp.h>
|
|
|
|
#include "debug.h"
|
2013-07-19 00:39:44 +00:00
|
|
|
#include "numeric_traits.h"
|
2013-07-16 20:11:24 +00:00
|
|
|
|
|
|
|
namespace lean {
|
2013-07-16 22:54:36 +00:00
|
|
|
class mpq;
|
2013-07-16 20:11:24 +00:00
|
|
|
|
2013-07-17 21:24:35 +00:00
|
|
|
/**
|
|
|
|
\brief Wrapper for GMP integers
|
|
|
|
*/
|
2013-07-16 20:11:24 +00:00
|
|
|
class mpz {
|
2013-07-16 22:54:36 +00:00
|
|
|
friend class mpq;
|
2013-07-16 20:11:24 +00:00
|
|
|
mpz_t m_val;
|
2013-07-16 22:54:36 +00:00
|
|
|
mpz(__mpz_struct const * v) { mpz_init_set(m_val, v); }
|
2013-07-16 20:11:24 +00:00
|
|
|
public:
|
|
|
|
mpz() { mpz_init(m_val); }
|
2013-07-21 21:25:56 +00:00
|
|
|
explicit mpz(char const * v) { mpz_init_set_str(m_val, const_cast<char*>(v), 10); }
|
|
|
|
explicit mpz(unsigned long int v) { mpz_init_set_ui(m_val, v); }
|
|
|
|
explicit mpz(long int v) { mpz_init_set_si(m_val, v); }
|
|
|
|
explicit mpz(unsigned int v) { mpz_init_set_ui(m_val, v); }
|
|
|
|
explicit mpz(int v) { mpz_init_set_si(m_val, v); }
|
2013-07-16 20:11:24 +00:00
|
|
|
mpz(mpz const & s) { mpz_init_set(m_val, s.m_val); }
|
|
|
|
mpz(mpz && s):mpz() { mpz_swap(m_val, s.m_val); }
|
|
|
|
~mpz() { mpz_clear(m_val); }
|
2013-07-17 01:26:33 +00:00
|
|
|
|
2013-07-19 03:21:41 +00:00
|
|
|
friend void swap(mpz & a, mpz & b) { mpz_swap(a.m_val, b.m_val); }
|
2013-07-17 01:26:33 +00:00
|
|
|
|
|
|
|
unsigned hash() const { return static_cast<unsigned>(mpz_get_si(m_val)); }
|
|
|
|
|
2013-07-16 20:11:24 +00:00
|
|
|
int sgn() const { return mpz_sgn(m_val); }
|
2013-07-16 22:54:36 +00:00
|
|
|
friend int sgn(mpz const & a) { return a.sgn(); }
|
2013-07-19 17:29:33 +00:00
|
|
|
bool is_pos() const { return sgn() > 0; }
|
2013-07-17 01:26:33 +00:00
|
|
|
bool is_neg() const { return sgn() < 0; }
|
|
|
|
bool is_zero() const { return sgn() == 0; }
|
|
|
|
bool is_nonpos() const { return !is_pos(); }
|
|
|
|
bool is_nonneg() const { return !is_neg(); }
|
2013-07-19 17:29:33 +00:00
|
|
|
|
2013-07-16 20:11:24 +00:00
|
|
|
void neg() { mpz_neg(m_val, m_val); }
|
2013-07-17 01:26:33 +00:00
|
|
|
friend mpz neg(mpz a) { a.neg(); return a; }
|
|
|
|
|
2013-07-16 20:11:24 +00:00
|
|
|
void abs() { mpz_abs(m_val, m_val); }
|
2013-07-16 22:54:36 +00:00
|
|
|
friend mpz abs(mpz a) { a.abs(); return a; }
|
2013-07-16 20:11:24 +00:00
|
|
|
|
|
|
|
bool even() const { return mpz_even_p(m_val) != 0; }
|
|
|
|
bool odd() const { return !even(); }
|
|
|
|
|
|
|
|
bool is_int() const { return mpz_fits_sint_p(m_val) != 0; }
|
|
|
|
bool is_unsigned_int() const { return mpz_fits_uint_p(m_val) != 0; }
|
|
|
|
bool is_long_int() const { return mpz_fits_slong_p(m_val) != 0; }
|
|
|
|
bool is_unsigned_long_int() const { return mpz_fits_ulong_p(m_val) != 0; }
|
|
|
|
|
|
|
|
long int get_long_int() const { lean_assert(is_long_int()); return mpz_get_si(m_val); }
|
|
|
|
int get_int() const { lean_assert(is_int()); return static_cast<int>(get_long_int()); }
|
|
|
|
unsigned long int get_unsigned_long_int() const { lean_assert(is_unsigned_long_int()); return mpz_get_ui(m_val); }
|
|
|
|
unsigned int get_unsigned_int() const { lean_assert(is_unsigned_int()); return static_cast<unsigned>(get_unsigned_long_int()); }
|
|
|
|
|
2013-07-17 19:43:05 +00:00
|
|
|
mpz & operator=(mpz const & v) { mpz_set(m_val, v.m_val); return *this; }
|
2013-07-19 03:21:41 +00:00
|
|
|
mpz & operator=(mpz && v) { swap(*this, v); return *this; }
|
2013-07-17 19:43:05 +00:00
|
|
|
mpz & operator=(char const * v) { mpz_set_str(m_val, v, 10); return *this; }
|
|
|
|
mpz & operator=(unsigned long int v) { mpz_set_ui(m_val, v); return *this; }
|
|
|
|
mpz & operator=(long int v) { mpz_set_si(m_val, v); return *this; }
|
|
|
|
mpz & operator=(unsigned int v) { return operator=(static_cast<unsigned long int>(v)); }
|
|
|
|
mpz & operator=(int v) { return operator=(static_cast<long int>(v)); }
|
|
|
|
|
2013-07-16 20:11:24 +00:00
|
|
|
friend int cmp(mpz const & a, mpz const & b) { return mpz_cmp(a.m_val, b.m_val); }
|
|
|
|
friend int cmp(mpz const & a, unsigned b) { return mpz_cmp_ui(a.m_val, b); }
|
|
|
|
friend int cmp(mpz const & a, int b) { return mpz_cmp_si(a.m_val, b); }
|
|
|
|
|
2013-07-19 17:29:33 +00:00
|
|
|
friend bool operator<(mpz const & a, mpz const & b) { return cmp(a, b) < 0; }
|
|
|
|
friend bool operator<(mpz const & a, unsigned b) { return cmp(a, b) < 0; }
|
|
|
|
friend bool operator<(mpz const & a, int b) { return cmp(a, b) < 0; }
|
|
|
|
friend bool operator<(unsigned a, mpz const & b) { return cmp(b, a) > 0; }
|
|
|
|
friend bool operator<(int a, mpz const & b) { return cmp(b, a) > 0; }
|
|
|
|
|
|
|
|
friend bool operator>(mpz const & a, mpz const & b) { return cmp(a, b) > 0; }
|
|
|
|
friend bool operator>(mpz const & a, unsigned b) { return cmp(a, b) > 0; }
|
|
|
|
friend bool operator>(mpz const & a, int b) { return cmp(a, b) > 0; }
|
|
|
|
friend bool operator>(unsigned a, mpz const & b) { return cmp(b, a) < 0; }
|
|
|
|
friend bool operator>(int a, mpz const & b) { return cmp(b, a) < 0; }
|
|
|
|
|
|
|
|
friend bool operator<=(mpz const & a, mpz const & b) { return cmp(a, b) <= 0; }
|
|
|
|
friend bool operator<=(mpz const & a, unsigned b) { return cmp(a, b) <= 0; }
|
|
|
|
friend bool operator<=(mpz const & a, int b) { return cmp(a, b) <= 0; }
|
|
|
|
friend bool operator<=(unsigned a, mpz const & b) { return cmp(b, a) >= 0; }
|
|
|
|
friend bool operator<=(int a, mpz const & b) { return cmp(b, a) >= 0; }
|
|
|
|
|
|
|
|
friend bool operator>=(mpz const & a, mpz const & b) { return cmp(a, b) >= 0; }
|
|
|
|
friend bool operator>=(mpz const & a, unsigned b) { return cmp(a, b) >= 0; }
|
|
|
|
friend bool operator>=(mpz const & a, int b) { return cmp(a, b) >= 0; }
|
|
|
|
friend bool operator>=(unsigned a, mpz const & b) { return cmp(b, a) <= 0; }
|
|
|
|
friend bool operator>=(int a, mpz const & b) { return cmp(b, a) <= 0; }
|
2013-07-17 01:26:33 +00:00
|
|
|
|
|
|
|
friend bool operator==(mpz const & a, mpz const & b) { return cmp(a, b) == 0; }
|
|
|
|
friend bool operator==(mpz const & a, unsigned b) { return cmp(a, b) == 0; }
|
|
|
|
friend bool operator==(mpz const & a, int b) { return cmp(a, b) == 0; }
|
|
|
|
friend bool operator==(unsigned a, mpz const & b) { return cmp(b, a) == 0; }
|
|
|
|
friend bool operator==(int a, mpz const & b) { return cmp(b, a) == 0; }
|
|
|
|
|
|
|
|
friend bool operator!=(mpz const & a, mpz const & b) { return cmp(a, b) != 0; }
|
|
|
|
friend bool operator!=(mpz const & a, unsigned b) { return cmp(a, b) != 0; }
|
|
|
|
friend bool operator!=(mpz const & a, int b) { return cmp(a, b) != 0; }
|
|
|
|
friend bool operator!=(unsigned a, mpz const & b) { return cmp(b, a) != 0; }
|
|
|
|
friend bool operator!=(int a, mpz const & b) { return cmp(b, a) != 0; }
|
2013-07-19 17:29:33 +00:00
|
|
|
|
2013-07-16 20:11:24 +00:00
|
|
|
mpz & operator+=(mpz const & o) { mpz_add(m_val, m_val, o.m_val); return *this; }
|
|
|
|
mpz & operator+=(unsigned u) { mpz_add_ui(m_val, m_val, u); return *this; }
|
|
|
|
mpz & operator+=(int u) { if (u >= 0) mpz_add_ui(m_val, m_val, u); else mpz_sub_ui(m_val, m_val, u); return *this; }
|
2013-07-17 01:26:33 +00:00
|
|
|
|
2013-07-16 20:11:24 +00:00
|
|
|
mpz & operator-=(mpz const & o) { mpz_sub(m_val, m_val, o.m_val); return *this; }
|
|
|
|
mpz & operator-=(unsigned u) { mpz_sub_ui(m_val, m_val, u); return *this; }
|
|
|
|
mpz & operator-=(int u) { if (u >= 0) mpz_sub_ui(m_val, m_val, u); else mpz_add_ui(m_val, m_val, u); return *this; }
|
2013-07-17 01:26:33 +00:00
|
|
|
|
2013-07-16 20:11:24 +00:00
|
|
|
mpz & operator*=(mpz const & o) { mpz_mul(m_val, m_val, o.m_val); return *this; }
|
|
|
|
mpz & operator*=(unsigned u) { mpz_mul_ui(m_val, m_val, u); return *this; }
|
|
|
|
mpz & operator*=(int u) { mpz_mul_si(m_val, m_val, u); return *this; }
|
2013-07-17 01:26:33 +00:00
|
|
|
|
2013-07-16 20:11:24 +00:00
|
|
|
mpz & operator/=(mpz const & o) { mpz_tdiv_q(m_val, m_val, o.m_val); return *this; }
|
|
|
|
mpz & operator/=(unsigned u) { mpz_tdiv_q_ui(m_val, m_val, u); return *this; }
|
2013-07-17 01:26:33 +00:00
|
|
|
|
2013-07-16 20:11:24 +00:00
|
|
|
friend mpz rem(mpz const & a, mpz const & b) { mpz r; mpz_tdiv_r(r.m_val, a.m_val, b.m_val); return r; }
|
|
|
|
mpz & operator%=(mpz const & o) { mpz r(*this % o); mpz_swap(m_val, r.m_val); return *this; }
|
2013-07-17 01:26:33 +00:00
|
|
|
|
2013-07-19 17:29:33 +00:00
|
|
|
friend mpz operator+(mpz a, mpz const & b) { return a += b; }
|
|
|
|
friend mpz operator+(mpz a, unsigned b) { return a += b; }
|
|
|
|
friend mpz operator+(mpz a, int b) { return a += b; }
|
|
|
|
friend mpz operator+(unsigned a, mpz b) { return b += a; }
|
|
|
|
friend mpz operator+(int a, mpz b) { return b += a; }
|
2013-07-17 01:26:33 +00:00
|
|
|
|
2013-07-19 17:29:33 +00:00
|
|
|
friend mpz operator-(mpz a, mpz const & b) { return a -= b; }
|
|
|
|
friend mpz operator-(mpz a, unsigned b) { return a -= b; }
|
|
|
|
friend mpz operator-(mpz a, int b) { return a -= b; }
|
|
|
|
friend mpz operator-(unsigned a, mpz b) { b.neg(); return b += a; }
|
|
|
|
friend mpz operator-(int a, mpz b) { b.neg(); return b += a; }
|
2013-07-17 01:26:33 +00:00
|
|
|
|
|
|
|
friend mpz operator*(mpz a, mpz const & b) { return a *= b; }
|
2013-07-19 17:29:33 +00:00
|
|
|
friend mpz operator*(mpz a, unsigned b) { return a *= b; }
|
|
|
|
friend mpz operator*(mpz a, int b) { return a *= b; }
|
|
|
|
friend mpz operator*(unsigned a, mpz b) { return b *= a; }
|
2013-07-17 01:26:33 +00:00
|
|
|
friend mpz operator*(int a, mpz b) { return b *= a; }
|
|
|
|
|
2013-07-19 17:29:33 +00:00
|
|
|
friend mpz operator/(mpz a, mpz const & b) { return a /= b; }
|
2013-07-17 01:26:33 +00:00
|
|
|
friend mpz operator/(mpz a, unsigned b) { return a /= b; }
|
|
|
|
friend mpz operator/(mpz a, int b) { return a /= b; }
|
2013-07-19 17:29:33 +00:00
|
|
|
friend mpz operator/(unsigned a, mpz const & b) { mpz r(a); return r /= b; }
|
2013-07-17 01:26:33 +00:00
|
|
|
friend mpz operator/(int a, mpz const & b) { mpz r(a); return r /= b; }
|
|
|
|
|
2013-07-16 22:54:36 +00:00
|
|
|
friend mpz operator%(mpz const & a, mpz const & b);
|
2013-07-16 20:11:24 +00:00
|
|
|
|
2013-07-17 00:20:24 +00:00
|
|
|
mpz & operator++() { return operator+=(1); }
|
|
|
|
mpz operator++(int) { mpz r(*this); ++(*this); return r; }
|
2013-07-17 01:26:33 +00:00
|
|
|
|
|
|
|
mpz & operator--() { return operator-=(1); }
|
2013-07-17 00:20:24 +00:00
|
|
|
mpz operator--(int) { mpz r(*this); --(*this); return r; }
|
|
|
|
|
2013-07-16 20:11:24 +00:00
|
|
|
mpz & operator&=(mpz const & o) { mpz_and(m_val, m_val, o.m_val); return *this; }
|
|
|
|
mpz & operator|=(mpz const & o) { mpz_ior(m_val, m_val, o.m_val); return *this; }
|
|
|
|
mpz & operator^=(mpz const & o) { mpz_xor(m_val, m_val, o.m_val); return *this; }
|
|
|
|
void comp() { mpz_com(m_val, m_val); }
|
|
|
|
|
2013-07-17 01:26:33 +00:00
|
|
|
friend mpz operator&(mpz a, mpz const & b) { return a &= b; }
|
|
|
|
friend mpz operator|(mpz a, mpz const & b) { return a |= b; }
|
|
|
|
friend mpz operator^(mpz a, mpz const & b) { return a ^= b; }
|
|
|
|
friend mpz operator~(mpz a) { a.comp(); return a; }
|
|
|
|
|
2013-07-16 20:11:24 +00:00
|
|
|
// this <- this + a*b
|
|
|
|
void addmul(mpz const & a, mpz const & b) { mpz_addmul(m_val, a.m_val, b.m_val); }
|
|
|
|
// this <- this - a*b
|
|
|
|
void submul(mpz const & a, mpz const & b) { mpz_submul(m_val, a.m_val, b.m_val); }
|
|
|
|
|
2013-07-17 19:43:05 +00:00
|
|
|
// a <- b * 2^k
|
|
|
|
friend void mul2k(mpz & a, mpz const & b, unsigned k) { mpz_mul_2exp(a.m_val, b.m_val, k); }
|
|
|
|
// a <- b / 2^k
|
|
|
|
friend void div2k(mpz & a, mpz const & b, unsigned k) { mpz_tdiv_q_2exp(a.m_val, b.m_val, k); }
|
|
|
|
|
2013-07-16 20:11:24 +00:00
|
|
|
/**
|
|
|
|
\brief Return the position of the most significant bit.
|
|
|
|
Return 0 if the number is negative
|
|
|
|
*/
|
|
|
|
unsigned log2() const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
\brief log2(-n)
|
|
|
|
Return 0 if the number is nonegative
|
|
|
|
*/
|
|
|
|
unsigned mlog2() const;
|
|
|
|
|
|
|
|
bool perfect_square() const { return mpz_perfect_square_p(m_val); }
|
|
|
|
|
|
|
|
bool is_power_of_two() const { return is_pos() && mpz_popcount(m_val) == 1; }
|
|
|
|
bool is_power_of_two(unsigned & shift) const;
|
2013-07-17 01:26:33 +00:00
|
|
|
/**
|
|
|
|
\brief Return largest k s.t. n is a multiple of 2^k
|
|
|
|
*/
|
|
|
|
unsigned power_of_two_multiple() const { return mpz_scan1(m_val, 0); }
|
2013-07-16 20:11:24 +00:00
|
|
|
|
2013-07-17 21:24:35 +00:00
|
|
|
friend void power(mpz & a, mpz const & b, unsigned k) { mpz_pow_ui(a.m_val, b.m_val, k); }
|
2013-07-19 23:58:56 +00:00
|
|
|
friend void _power(mpz & a, mpz const & b, unsigned k) { power(a, b, k); }
|
2013-07-17 21:24:35 +00:00
|
|
|
friend mpz power(mpz const & a, unsigned k) { mpz r; power(r, a, k); return r; }
|
2013-07-16 20:11:24 +00:00
|
|
|
|
|
|
|
friend void rootrem(mpz & root, mpz & rem, mpz const & a, unsigned k) { mpz_rootrem(root.m_val, rem.m_val, a.m_val, k); }
|
2013-07-17 21:24:35 +00:00
|
|
|
// root <- a^{1/k}, return true iff the result is an integer
|
|
|
|
friend bool root(mpz & root, mpz const & a, unsigned k);
|
2013-07-16 20:11:24 +00:00
|
|
|
friend mpz root(mpz const & a, unsigned k) { mpz r; root(r, a, k); return r; }
|
|
|
|
|
|
|
|
friend void gcd(mpz & g, mpz const & a, mpz const & b) { mpz_gcd(g.m_val, a.m_val, b.m_val); }
|
|
|
|
friend mpz gcd(mpz const & a, mpz const & b) { mpz r; gcd(r, a, b); return r; }
|
|
|
|
friend void gcdext(mpz & g, mpz & s, mpz & t, mpz const & a, mpz const & b) { mpz_gcdext(g.m_val, s.m_val, t.m_val, a.m_val, b.m_val); }
|
|
|
|
friend void lcm(mpz & l, mpz const & a, mpz const & b) { mpz_lcm(l.m_val, a.m_val, b.m_val); }
|
|
|
|
friend mpz lcm(mpz const & a, mpz const & b) { mpz l; lcm(l, a, b); return l; }
|
|
|
|
|
2013-07-19 17:29:33 +00:00
|
|
|
friend std::ostream & operator<<(std::ostream & out, mpz const & v);
|
2013-07-16 20:11:24 +00:00
|
|
|
};
|
|
|
|
|
2013-07-19 00:39:44 +00:00
|
|
|
template<>
|
|
|
|
class numeric_traits<mpz> {
|
|
|
|
public:
|
2013-07-19 17:42:19 +00:00
|
|
|
static bool precise() { return true; }
|
2013-07-19 00:39:44 +00:00
|
|
|
static bool is_zero(mpz const & v) { return v.is_zero(); }
|
|
|
|
static bool is_pos(mpz const & v) { return v.is_pos(); }
|
|
|
|
static bool is_neg(mpz const & v) { return v.is_neg(); }
|
|
|
|
static void set_rounding(bool plus_inf) {}
|
|
|
|
static void neg(mpz & v) { v.neg(); }
|
|
|
|
static void reset(mpz & v) { v = 0; }
|
2013-07-19 23:58:56 +00:00
|
|
|
// v <- v^k
|
|
|
|
static void power(mpz & v, unsigned k) { _power(v, v, k); }
|
2013-07-19 00:39:44 +00:00
|
|
|
};
|
|
|
|
|
2013-07-16 20:11:24 +00:00
|
|
|
}
|