From 6d15af8b86e44b3465d89e7bb6af995211beb9ef Mon Sep 17 00:00:00 2001 From: Robert Date: Sun, 19 Jul 2020 17:01:23 +0200 Subject: [PATCH] Added more operations --- include/gm3p_int.hpp | 14 +++++++-- src/gm3p_int.cpp | 68 ++++++++++++++++++++++++++------------------ testapp/main.cpp | 12 ++++---- 3 files changed, 56 insertions(+), 38 deletions(-) diff --git a/include/gm3p_int.hpp b/include/gm3p_int.hpp index b72e611..f974cdf 100644 --- a/include/gm3p_int.hpp +++ b/include/gm3p_int.hpp @@ -6,6 +6,9 @@ namespace gm3p { + /** + * A wrapper for mpz_t + */ class gInt { public: @@ -14,10 +17,8 @@ namespace gm3p gInt(mpz_t init); gInt(mpq_t init); gInt(mpf_t init); - gInt(unsigned long int init); gInt(long int init); - gInt(const char* init, int base); - gInt(double init); + gInt(const char* init, int base = 10); // DESTRUCTOR ~gInt(); @@ -28,6 +29,8 @@ namespace gm3p gInt(gInt&& other); // ARITHMETIC OPERATORS + friend gInt operator-(const gInt& value); + friend gInt operator+(const gInt& left, const gInt& right); friend gInt operator-(const gInt& left, const gInt& right); friend gInt operator*(const gInt& left, const gInt& right); @@ -38,8 +41,13 @@ namespace gm3p gInt& operator*=(const gInt& right); gInt& operator/=(const gInt& right); + friend gInt Mul2Exp(const gInt& value, const mp_bitcnt_t& exp); + friend gInt Neg(const gInt& value); + friend gInt Abs(const gInt& value); + // CONVERSION OPERATORS operator long int() const; + operator mpz_t&(); unsigned long int ToUnsigned() const; char* ToString(char* buf, int base) const; diff --git a/src/gm3p_int.cpp b/src/gm3p_int.cpp index 347cf0b..91b528e 100644 --- a/src/gm3p_int.cpp +++ b/src/gm3p_int.cpp @@ -24,12 +24,6 @@ namespace gm3p mpz_set_f(value, init); } - gInt::gInt(unsigned long int init) : - gInt::gInt() - { - mpz_init_set_ui(value, init); - } - gInt::gInt(long int init) : gInt::gInt() { @@ -42,77 +36,69 @@ namespace gm3p mpz_init_set_str(value, init, base); } - gInt::gInt(double init) - { - mpz_init_set_d(value, init); - } - - gInt::~gInt() { mpz_clear(value); } - - - gInt& gInt::operator=(const gInt& other) { - mpz_set(this->value, other.value); + mpz_set(*this, const_cast(other)); return *this; } gInt::gInt(const gInt& other) { - mpz_set(this->value, other.value); + mpz_set(*this, const_cast(other)); } gInt::gInt(gInt&& other) { - mpz_set(this->value, other.value); + mpz_set(*this, const_cast(other)); } - + gInt operator-(const gInt& value) + { + return Neg(value); + } gInt operator+(const gInt& left, const gInt& right) { gInt ret; - mpz_add(ret.value, left.value, right.value); + mpz_add(ret, const_cast(left), const_cast(right)); return ret; } gInt operator-(const gInt& left, const gInt& right) { gInt ret; - mpz_sub(ret.value, left.value, right.value); + mpz_sub(ret, const_cast(left), const_cast(right)); return ret; } gInt operator*(const gInt& left, const gInt& right) { gInt ret; - mpz_mul(ret.value, left.value, right.value); + mpz_mul(ret, const_cast(left), const_cast(right)); return ret; } gInt operator/(const gInt& left, const gInt& right) { gInt ret; - mpz_div(ret.value, left.value, right.value); + mpz_div(ret, const_cast(left), const_cast(right)); return ret; } - - gInt& gInt::operator+=(const gInt& right) { - mpz_add(value, value, right.value); + mpz_add(value, value, const_cast(right)); return *this; } gInt& gInt::operator-=(const gInt& right) { - mpz_sub(value, value, right.value); + mpz_sub(value, value, const_cast(right)); return *this; } @@ -124,15 +110,41 @@ namespace gm3p gInt& gInt::operator/=(const gInt& right) { - mpz_div(value, value, right.value); + mpz_div(value, value, const_cast(right)); return *this; } + gInt Mul2Exp(const gInt& value, const mp_bitcnt_t& exp) + { + gInt ret; + mpz_mul_2exp(ret, const_cast(value), exp); + return ret; + } + + gInt Neg(const gInt& value) + { + gInt ret; + mpz_neg(ret, const_cast(value)); + return ret; + } + + gInt Abs(const gInt& value) + { + gInt ret; + mpz_abs(ret, const_cast(value)); + return ret; + } + gInt::operator long int() const { return mpz_get_si(value); } + gInt::operator mpz_t&() + { + return value; + } + unsigned long int gInt::ToUnsigned() const { return mpz_get_ui(value); diff --git a/testapp/main.cpp b/testapp/main.cpp index d51f8ac..071b693 100644 --- a/testapp/main.cpp +++ b/testapp/main.cpp @@ -7,17 +7,15 @@ using namespace gm3p; int main(int argc, char** argv) { - gInt a("12234234234095780349859034869034635235", 10); - gInt b("123534622305820985092875207398734234753", 10); + gInt a("12"); + gInt b = -5; - a *= b; gInt c = a * b; - std::cout << std::dec << a << " * " << b << " = " << c << std::endl; - std::cout << std::hex << a << " * " << b << " = " << c << std::endl; - std::cout << std::oct << a << " * " << b << " = " << c << std::endl << std::endl << std::dec; + std::cout << c << std::endl << std::endl; + std::cout << -c << std::endl << std::endl; - int d = c; + int d = Abs(c); std::cout << d << std::endl; std::cout << c.ToString(NULL, 2) << std::endl;