mpz_class.hpp — long long support for mpz_class.

This file provides gmpxll::mpz_class which inherits from the official mpz_class and is binary compatible with it.

This makes it possible to mix the two types quite freely. So, you might want to use gmpxxll::mpz_class where you perform (generic) conversion from/to standard integer types and use the regular mpz_class everywhere else:

#include "gmpxxll/mpz_class.hpp"

mpz_class n;
gmpxxll::mpz_class m = std::limits<long long>::max;

// Since gmpxxll::mpz_class is derived from mpz_class, it casts implicitly into it:
n = m;

// Conversely, there are explicit conversions:
m = n;
m = gmpxxll::mpz_class(n);

Class mpz_class

Adds long long functionality to mpz_class.

This class derives from GMP’s mpz_class. It is binary compatible with that class but adds some methods related to long long and unsigned long long. Note that on LP64 implementations of C++ both long and long long are 64 bits wide so this does not add anything and performance overhead should be negligible when optimizations are enabled. On LLP64 implementations, most notably Microsoft Windows, conversions actually need to take place. These conversions are probably not implemented very efficiently yet.

Constructors

(1) mpz_class(long long value)

(2) mpz_class(unsigned long long value)

Construct an mpz_class from an (unsigned) long long value.

All the constructors provided by GMP’s mpz_class are also available through a forwarding constructor.

Type Conversion

(1) long long get_sll() const

(2) unsigned long long get_ull() const

Return this integer as an (unsigned) long long value.

The behaviour is undefined when the integer does not fit into an (unsigned) long long.

Range Checks

(1) bool fits_slonglong_p() const

(2) bool fits_ulonglong_p() const

Return whether this integers is within the range of an (unsigned) long long.

Relational Operators

(1) bool operator==(const mpz_class& lhs, const long long rhs)

(2) bool operator<(const mpz_class& lhs, const long long rhs)

(3) bool operator>(const mpz_class& lhs, const long long rhs)

(4) bool operator==(const mpz_class& lhs, const unsigned long long rhs)

(5) bool operator<(const mpz_class& lhs, const unsigned long long rhs)

(6) bool operator>(const mpz_class& lhs, const unsigned long long rhs)

All the binary operators <, <=, ==, !=, >=, > support long long operands. Note that binary arithmetic operators are implicitly supported because there is an implicit constructor that can cast a long long to an gmpxxll::mpz_class.