diff --git a/.gitignore b/.gitignore index 32dcdee..cf2580d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ .vs out +3rdparty *.json \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 33ce1c4..89d258b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,9 +1,31 @@ -# CMakeList.txt : Top-level CMake project file, do global configuration -# and include sub-projects here. -# -cmake_minimum_required (VERSION 3.8) +cmake_minimum_required (VERSION 3.8) -project ("GMP++") +project ("GM3p") + +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. -add_subdirectory ("GMP++") +if(ENABLE_TEST_PROJ) + add_subdirectory(testapp) +endif() \ No newline at end of file diff --git a/include/gm3p.hpp b/include/gm3p.hpp new file mode 100644 index 0000000..48fb703 --- /dev/null +++ b/include/gm3p.hpp @@ -0,0 +1,4 @@ +#pragma once +#include + +#include "gm3p_int.hpp" \ No newline at end of file diff --git a/include/gm3p_int.hpp b/include/gm3p_int.hpp new file mode 100644 index 0000000..d150fdc --- /dev/null +++ b/include/gm3p_int.hpp @@ -0,0 +1,28 @@ +#pragma once + +#include "gmp.h" + +namespace gm3p +{ + class gInt + { + public: + gInt(); + gInt(mpz_t init); + gInt(long int init); + gInt(const char* init, int base); + ~gInt(); + + gInt& operator=(const gInt& other); + + 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); + + friend std::ostream& operator<<(std::ostream& os, const gInt& value); + + private: + mpz_t value; + }; +} \ No newline at end of file diff --git a/src/gm3p_int.cpp b/src/gm3p_int.cpp new file mode 100644 index 0000000..e8daff3 --- /dev/null +++ b/src/gm3p_int.cpp @@ -0,0 +1,71 @@ +#include "gm3p.hpp" + +namespace gm3p +{ + gInt::gInt() + { + mpz_init(value); + } + + gInt::gInt(mpz_t init) + : gInt::gInt() + { + mpz_set(value, init); + } + + gInt::gInt(long int init) : + gInt::gInt() + { + mpz_set_si(value, init); + } + + gInt::gInt(const char* init, int base) : + gInt::gInt() + { + mpz_set_str(value, init, base); + } + + gInt::~gInt() + { + mpz_clear(value); + } + + gInt& gInt::operator=(const gInt& other) + { + mpz_set(this->value, other.value); + } + + gInt operator+(const gInt& left, const gInt& right) + { + gInt ret; + mpz_add(ret.value, left.value, right.value); + return ret; + } + + gInt operator-(const gInt& left, const gInt& right) + { + gInt ret; + mpz_sub(ret.value, left.value, right.value); + return ret; + } + + gInt operator*(const gInt& left, const gInt& right) + { + gInt ret; + mpz_mul(ret.value, left.value, right.value); + return ret; + } + + gInt operator/(const gInt& left, const gInt& right) + { + gInt ret; + mpz_div(ret.value, left.value, right.value); + return ret; + } + std::ostream& operator<<(std::ostream& os, const gInt& value) + { + static char* buf; + gmp_asprintf(&buf, "%Zd", value); + os << buf; + } +} \ No newline at end of file diff --git a/testapp/CMakeLists.txt b/testapp/CMakeLists.txt new file mode 100644 index 0000000..970d34c --- /dev/null +++ b/testapp/CMakeLists.txt @@ -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 +) \ No newline at end of file diff --git a/testapp/main.cpp b/testapp/main.cpp new file mode 100644 index 0000000..2f3cf9b --- /dev/null +++ b/testapp/main.cpp @@ -0,0 +1,17 @@ +#include "gm3p.hpp" + +#include + +using namespace gm3p; + +int main(int argc, char** argv) +{ + gInt a("12234234234095780349859034869034635235", 10); + gInt b("123534622305820985092875207398734234753", 10); + + gInt c = a * b; + + std::cout << a << " * " << b << " = " << c << std::endl; + + return 0; +} \ No newline at end of file