Compare commits
5 commits
master
...
developmen
Author | SHA1 | Date | |
---|---|---|---|
![]() |
551f93afbd | ||
![]() |
6d15af8b86 | ||
![]() |
971f13a59b | ||
![]() |
97125eaf07 | ||
![]() |
95282f7d1a |
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -1,4 +1,7 @@
|
||||||
.vs
|
.vs
|
||||||
|
.vscode
|
||||||
out
|
out
|
||||||
|
build
|
||||||
|
3rdparty
|
||||||
|
|
||||||
*.json
|
*.json
|
|
@ -1,9 +1,34 @@
|
||||||
# CMakeList.txt : Top-level CMake project file, do global configuration
|
cmake_minimum_required (VERSION 3.8)
|
||||||
# and include sub-projects here.
|
|
||||||
#
|
|
||||||
cmake_minimum_required (VERSION 3.8)
|
|
||||||
|
|
||||||
project ("GMP++")
|
project ("gm3p" CXX)
|
||||||
|
|
||||||
|
set(GMP_INCLUDE_DIR "" CACHE PATH "Path to the directory containing 'gmp.h'")
|
||||||
|
|
||||||
|
if(CMAKE_BUILD_TYPE EQUAL "DEBUG")
|
||||||
|
set(GMP_LIBRARY "" CACHE FILEPATH "Path to the gmp library")
|
||||||
|
else(CMAKE_BUILD_TYPE EQUAL "RELEASE")
|
||||||
|
set(GMP_LIBRARY "" CACHE FILEPATH "Path to the gmpDebug library")
|
||||||
|
endif()
|
||||||
|
|
||||||
|
set(ENABLE_TEST_PROJ FALSE CACHE BOOL "Enable the testing project (usually not needed)")
|
||||||
|
|
||||||
|
add_library(gm3p STATIC
|
||||||
|
"include/gm3p_int.hpp" "src/gm3p_int.cpp"
|
||||||
|
)
|
||||||
|
|
||||||
|
target_include_directories(gm3p PUBLIC
|
||||||
|
include
|
||||||
|
${GMP_INCLUDE_DIR}
|
||||||
|
)
|
||||||
|
|
||||||
|
target_link_libraries(gm3p PUBLIC
|
||||||
|
${GMP_LIBRARY}
|
||||||
|
)
|
||||||
|
|
||||||
# Include sub-projects.
|
# Include sub-projects.
|
||||||
add_subdirectory ("GMP++")
|
if(ENABLE_TEST_PROJ)
|
||||||
|
add_subdirectory(testapp)
|
||||||
|
endif()
|
||||||
|
|
||||||
|
install(TARGETS gm3p DESTINATION lib)
|
||||||
|
install(DIRECTORY include DESTINATION .)
|
3
include/gm3p.hpp
Normal file
3
include/gm3p.hpp
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "gm3p_int.hpp"
|
63
include/gm3p_int.hpp
Normal file
63
include/gm3p_int.hpp
Normal file
|
@ -0,0 +1,63 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
#include "gmp.h"
|
||||||
|
|
||||||
|
namespace gm3p
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* A wrapper for mpz_t
|
||||||
|
*/
|
||||||
|
class gInt
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
// CONSTRUCTORS
|
||||||
|
gInt();
|
||||||
|
gInt(mpz_t init);
|
||||||
|
gInt(mpq_t init);
|
||||||
|
gInt(mpf_t init);
|
||||||
|
gInt(long int init);
|
||||||
|
gInt(const char* init, int base = 10);
|
||||||
|
|
||||||
|
// DESTRUCTOR
|
||||||
|
~gInt();
|
||||||
|
|
||||||
|
// ASSIGNMENT/COPY OPERATORS
|
||||||
|
gInt& operator=(const gInt& other);
|
||||||
|
gInt(const gInt& other);
|
||||||
|
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);
|
||||||
|
friend gInt operator/(const gInt& left, const gInt& right);
|
||||||
|
|
||||||
|
gInt& operator+=(const gInt& right);
|
||||||
|
gInt& operator-=(const gInt& right);
|
||||||
|
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;
|
||||||
|
double ToDouble() const;
|
||||||
|
double ToDouble(long int* exp) const;
|
||||||
|
|
||||||
|
// STDIO OPERATORS
|
||||||
|
friend std::ostream& operator<<(std::ostream& os, const gInt& value);
|
||||||
|
|
||||||
|
private:
|
||||||
|
mpz_t value;
|
||||||
|
};
|
||||||
|
}
|
178
src/gm3p_int.cpp
Normal file
178
src/gm3p_int.cpp
Normal file
|
@ -0,0 +1,178 @@
|
||||||
|
#include "gm3p.hpp"
|
||||||
|
|
||||||
|
namespace gm3p
|
||||||
|
{
|
||||||
|
gInt::gInt()
|
||||||
|
{
|
||||||
|
mpz_init(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
gInt::gInt(mpz_t init)
|
||||||
|
{
|
||||||
|
mpz_init_set(value, init);
|
||||||
|
}
|
||||||
|
|
||||||
|
gInt::gInt(mpq_t init)
|
||||||
|
{
|
||||||
|
mpz_init(value);
|
||||||
|
mpz_set_q(value, init);
|
||||||
|
}
|
||||||
|
|
||||||
|
gInt::gInt(mpf_t init)
|
||||||
|
{
|
||||||
|
mpz_init(value);
|
||||||
|
mpz_set_f(value, init);
|
||||||
|
}
|
||||||
|
|
||||||
|
gInt::gInt(long int init) :
|
||||||
|
gInt::gInt()
|
||||||
|
{
|
||||||
|
mpz_init_set_si(value, init);
|
||||||
|
}
|
||||||
|
|
||||||
|
gInt::gInt(const char* init, int base) :
|
||||||
|
gInt::gInt()
|
||||||
|
{
|
||||||
|
mpz_init_set_str(value, init, base);
|
||||||
|
}
|
||||||
|
|
||||||
|
gInt::~gInt()
|
||||||
|
{
|
||||||
|
mpz_clear(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
gInt& gInt::operator=(const gInt& other)
|
||||||
|
{
|
||||||
|
mpz_set(*this, const_cast<gInt&>(other));
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
gInt::gInt(const gInt& other)
|
||||||
|
{
|
||||||
|
mpz_set(*this, const_cast<gInt&>(other));
|
||||||
|
}
|
||||||
|
|
||||||
|
gInt::gInt(gInt&& other)
|
||||||
|
{
|
||||||
|
mpz_set(*this, const_cast<gInt&>(other));
|
||||||
|
}
|
||||||
|
|
||||||
|
gInt operator-(const gInt& value)
|
||||||
|
{
|
||||||
|
return Neg(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
gInt operator+(const gInt& left, const gInt& right)
|
||||||
|
{
|
||||||
|
gInt ret;
|
||||||
|
mpz_add(ret, const_cast<gInt&>(left), const_cast<gInt&>(right));
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
gInt operator-(const gInt& left, const gInt& right)
|
||||||
|
{
|
||||||
|
gInt ret;
|
||||||
|
mpz_sub(ret, const_cast<gInt&>(left), const_cast<gInt&>(right));
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
gInt operator*(const gInt& left, const gInt& right)
|
||||||
|
{
|
||||||
|
gInt ret;
|
||||||
|
mpz_mul(ret, const_cast<gInt&>(left), const_cast<gInt&>(right));
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
gInt operator/(const gInt& left, const gInt& right)
|
||||||
|
{
|
||||||
|
gInt ret;
|
||||||
|
mpz_div(ret, const_cast<gInt&>(left), const_cast<gInt&>(right));
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
gInt& gInt::operator+=(const gInt& right)
|
||||||
|
{
|
||||||
|
mpz_add(value, value, const_cast<gInt&>(right));
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
gInt& gInt::operator-=(const gInt& right)
|
||||||
|
{
|
||||||
|
mpz_sub(value, value, const_cast<gInt&>(right));
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
gInt& gInt::operator*=(const gInt& right)
|
||||||
|
{
|
||||||
|
mpz_mul(value, value, right.value);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
gInt& gInt::operator/=(const gInt& right)
|
||||||
|
{
|
||||||
|
mpz_div(value, value, const_cast<gInt&>(right));
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
gInt Mul2Exp(const gInt& value, const mp_bitcnt_t& exp)
|
||||||
|
{
|
||||||
|
gInt ret;
|
||||||
|
mpz_mul_2exp(ret, const_cast<gInt&>(value), exp);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
gInt Neg(const gInt& value)
|
||||||
|
{
|
||||||
|
gInt ret;
|
||||||
|
mpz_neg(ret, const_cast<gInt&>(value));
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
gInt Abs(const gInt& value)
|
||||||
|
{
|
||||||
|
gInt ret;
|
||||||
|
mpz_abs(ret, const_cast<gInt&>(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);
|
||||||
|
}
|
||||||
|
|
||||||
|
char* gInt::ToString(char* buf, int base) const
|
||||||
|
{
|
||||||
|
return mpz_get_str(buf, base, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
double gInt::ToDouble() const
|
||||||
|
{
|
||||||
|
return mpz_get_d(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
double gInt::ToDouble(long int* exp) const
|
||||||
|
{
|
||||||
|
return mpz_get_d_2exp(exp, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
std::ostream& operator<<(std::ostream& os, const gInt& value)
|
||||||
|
{
|
||||||
|
os << value.ToString(NULL,
|
||||||
|
(os.flags() & std::ios_base::dec) != 0 ? 10 :
|
||||||
|
(os.flags() & std::ios_base::hex) != 0 ? 16 :
|
||||||
|
8
|
||||||
|
);
|
||||||
|
return os;
|
||||||
|
}
|
||||||
|
}
|
13
testapp/CMakeLists.txt
Normal file
13
testapp/CMakeLists.txt
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
add_executable(testapp
|
||||||
|
"main.cpp"
|
||||||
|
)
|
||||||
|
|
||||||
|
target_include_directories(testapp PUBLIC
|
||||||
|
${GMP_INCLUDE_DIR}
|
||||||
|
${CMAKE_SOURCE_DIR}/include
|
||||||
|
)
|
||||||
|
|
||||||
|
target_link_libraries(testapp PUBLIC
|
||||||
|
${GMP_LIBRARY}
|
||||||
|
gm3p
|
||||||
|
)
|
27
testapp/main.cpp
Normal file
27
testapp/main.cpp
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
#include "gm3p.hpp"
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#include <iomanip>
|
||||||
|
|
||||||
|
using namespace gm3p;
|
||||||
|
|
||||||
|
int main(int argc, char** argv)
|
||||||
|
{
|
||||||
|
gInt a("12");
|
||||||
|
gInt b = -5;
|
||||||
|
|
||||||
|
gInt c = a * b;
|
||||||
|
|
||||||
|
std::cout << c << std::endl << std::endl;
|
||||||
|
std::cout << -c << std::endl << std::endl;
|
||||||
|
|
||||||
|
int d = Abs(c);
|
||||||
|
std::cout << d << std::endl;
|
||||||
|
std::cout << c.ToString(NULL, 2) << std::endl << std::endl;
|
||||||
|
|
||||||
|
mpz_t& e = c;
|
||||||
|
mpz_mul_ui(e, e, 2);
|
||||||
|
std::cout << c << std::endl;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
Loading…
Reference in a new issue