diff --git a/GMP.pri b/GMP.pri new file mode 100644 index 0000000..e69de29 diff --git a/GMP.pro b/GMP.pro new file mode 100644 index 0000000..bdc2c10 --- /dev/null +++ b/GMP.pro @@ -0,0 +1,22 @@ +QT -= core gui + +TARGET = MiniGMP +TEMPLATE = lib + +DEFINES += MINIGMP_LIBRARY +DEFINES += QT_DEPRECATED_WARNINGS + +VERSION = 6.0.0 + +DISTFILES += \ + README \ + gen_export.sh + +HEADERS += \ + bigint.h \ + mini-gmp.h \ + minigmp_global.h + +SOURCES += \ + bigint.cpp \ + mini-gmp.c diff --git a/GMP.pro.user b/GMP.pro.user new file mode 100644 index 0000000..4dfc702 --- /dev/null +++ b/GMP.pro.user @@ -0,0 +1,335 @@ + + + + + + EnvironmentId + {b865c35b-cb42-4f89-95c4-ffb97a092c5e} + + + ProjectExplorer.Project.ActiveTarget + 0 + + + ProjectExplorer.Project.EditorSettings + + true + false + true + + Cpp + + CppGlobal + + + + QmlJS + + QmlJSGlobal + + + 2 + UTF-8 + false + 4 + false + 80 + true + true + 1 + true + false + 0 + true + true + 0 + 8 + true + 1 + true + true + true + false + + + + ProjectExplorer.Project.PluginSettings + + + true + + + + ProjectExplorer.Project.Target.0 + + Desktop Qt 5.13.0 GCC 64bit + Desktop Qt 5.13.0 GCC 64bit + qt.qt5.5130.gcc_64_kit + 0 + 0 + 0 + + /home/endrii/gitHub/build-GMP-Desktop_Qt_5_13_0_GCC_64bit-Debug + + + true + qmake + + QtProjectManager.QMakeBuildStep + true + + false + false + false + + + true + Сборка + + Qt4ProjectManager.MakeStep + + false + + + false + + 2 + Сборка + + ProjectExplorer.BuildSteps.Build + + + + true + Сборка + + Qt4ProjectManager.MakeStep + + true + clean + + false + + 1 + Очистка + + ProjectExplorer.BuildSteps.Clean + + 2 + false + + Отладка + Отладка + Qt4ProjectManager.Qt4BuildConfiguration + 2 + true + + + /home/endrii/gitHub/build-GMP-Desktop_Qt_5_13_0_GCC_64bit-Release + + + true + qmake + + QtProjectManager.QMakeBuildStep + false + + false + false + true + + + true + Сборка + + Qt4ProjectManager.MakeStep + + false + + + false + + 2 + Сборка + + ProjectExplorer.BuildSteps.Build + + + + true + Сборка + + Qt4ProjectManager.MakeStep + + true + clean + + false + + 1 + Очистка + + ProjectExplorer.BuildSteps.Clean + + 2 + false + + Выпуск + Выпуск + Qt4ProjectManager.Qt4BuildConfiguration + 0 + true + + + /home/endrii/gitHub/build-GMP-Desktop_Qt_5_13_0_GCC_64bit-Profile + + + true + qmake + + QtProjectManager.QMakeBuildStep + true + + false + true + true + + + true + Сборка + + Qt4ProjectManager.MakeStep + + false + + + false + + 2 + Сборка + + ProjectExplorer.BuildSteps.Build + + + + true + Сборка + + Qt4ProjectManager.MakeStep + + true + clean + + false + + 1 + Очистка + + ProjectExplorer.BuildSteps.Clean + + 2 + false + + Профилирование + Профилирование + Qt4ProjectManager.Qt4BuildConfiguration + 0 + true + + 3 + + + 0 + Установка + + ProjectExplorer.BuildSteps.Deploy + + 1 + Конфигурация установки + + ProjectExplorer.DefaultDeployConfiguration + + 1 + + + dwarf + + cpu-cycles + + + 250 + -F + true + 4096 + false + false + 1000 + + true + + false + false + false + false + true + 0.01 + 10 + true + kcachegrind + 1 + 25 + + 1 + true + false + true + valgrind + + 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 + 13 + 14 + + 2 + + + Особая программа + + ProjectExplorer.CustomExecutableRunConfiguration + + 3768 + false + true + false + false + true + + + + 1 + + + + ProjectExplorer.Project.TargetCount + 1 + + + ProjectExplorer.Project.Updater.FileVersion + 21 + + + Version + 21 + + diff --git a/bigint.cpp b/bigint.cpp new file mode 100644 index 0000000..52110ff --- /dev/null +++ b/bigint.cpp @@ -0,0 +1,185 @@ +#include "bigint.h" + +BigInt::BigInt() { + mpz_init(data); +} + +BigInt::BigInt(const BigInt &val): + BigInt() { + mpz_set(data, val.data); +} + +BigInt::BigInt(const char *str, int base): + BigInt() { + + mpz_set_str(data, str, base); +} + +BigInt::BigInt(std::string imput, int base): + BigInt(imput.c_str(), base) { +} + +BigInt::BigInt(int val): + BigInt() { + mpz_set_si(data, val); +} + +BigInt::BigInt(unsigned int val): + BigInt() { + mpz_set_ui(data, val); +} + +BigInt::BigInt(double val): + BigInt() { + mpz_set_d(data, val); +} + +std::string BigInt::getString(int base) const { + char *str = nullptr; + mpz_get_str(str, base, data); + + return str; +} + +BigInt::~BigInt() { + mpz_clear(data); +} + +BigInt operator +(BigInt left, unsigned int right) { + mpz_add_ui(left.data, left.data, right); + return left; +} + +BigInt operator +(BigInt left, const BigInt &right) { + mpz_add(left.data, left.data, right.data); + return left; +} + +BigInt& operator +=(BigInt &left, unsigned int right) { + mpz_add_ui(left.data, left.data, right); + return left; +} + +BigInt& operator +=(BigInt &left, const BigInt &right) { + mpz_add(left.data, left.data, right.data); + return left; +} + +BigInt operator *(BigInt left, const BigInt &right) { + mpz_mul(left.data, left.data, right.data); + return left; +} + +BigInt operator *(BigInt left, int right) { + mpz_mul_si(left.data, left.data, right); + return left; +} + +BigInt operator *(BigInt left, unsigned int right) { + mpz_mul_ui(left.data, left.data, right); + return left; +} + +BigInt& operator *=(BigInt &left, const BigInt &right) { + mpz_mul(left.data, left.data, right.data); + return left; +} + +BigInt& operator *=(BigInt &left, int right) { + mpz_mul_si(left.data, left.data, right); + return left; +} + +BigInt& operator *=(BigInt &left, unsigned int right) { + mpz_mul_ui(left.data, left.data, right); + return left; +} + +BigInt operator /(BigInt left, const BigInt &right) { + mpz_fdiv_r(left.data, left.data, right.data); + return left; +} + +BigInt operator /(BigInt left, unsigned int right) { + mpz_fdiv_r_ui(left.data, left.data, right); + return left; +} + +BigInt& operator /=(BigInt &left, const BigInt &right) { + mpz_fdiv_r(left.data, left.data, right.data); + return left; +} + +BigInt& operator /=(BigInt &left, unsigned int right) { + mpz_fdiv_r_ui(left.data, left.data, right); + return left; +} + +BigInt operator -(BigInt left, const BigInt &right) { + mpz_sub(left.data, left.data, right.data); + return left; +} + +BigInt operator -(BigInt left, unsigned int right) { + mpz_sub_ui(left.data, left.data, right); + return left; +} + +BigInt operator -(unsigned int left, BigInt right) { + mpz_ui_sub(right.data, left, right.data); + return right; +} + +BigInt& operator -=(BigInt &left, const BigInt &right) { + mpz_sub(left.data, left.data, right.data); + return left; +} + +BigInt& operator -=(BigInt &left, unsigned int right) { + mpz_sub_ui(left.data, left.data, right); + return left; +} + +BigInt operator %(BigInt left, const BigInt &right) { + mpz_mod(left.data, left.data, right.data); + return left; +} + +BigInt operator %(BigInt left, unsigned int right) { + mpz_mod_ui(left.data, left.data, right); + return left; +} + +BigInt& operator %=(BigInt& left, const BigInt &right) { + mpz_mod(left.data, left.data, right.data); + return left; +} + +BigInt& operator %=(BigInt& left, unsigned int right) { + mpz_mod_ui(left.data, left.data, right); + return left; +} + +bool operator == (const BigInt& left, const BigInt& right) { + return mpz_cmp(left.data, right.data) == 0; +} + +bool operator != (const BigInt &left, const BigInt& right) { + return mpz_cmp(left.data, right.data) != 0; +} + +bool operator < ( const BigInt &left, const BigInt& right) { + return mpz_cmp(left.data, right.data) < 0; +} + +bool operator > ( const BigInt &left, const BigInt& right) { + return mpz_cmp(left.data, right.data) > 0; +} + +bool operator <= ( const BigInt &left, const BigInt& right) { + return mpz_cmp(left.data, right.data) <= 0; +} + +bool operator >= ( const BigInt &left, const BigInt& right) { + return mpz_cmp(left.data, right.data) >= 0; +} diff --git a/bigint.h b/bigint.h new file mode 100644 index 0000000..87bdd35 --- /dev/null +++ b/bigint.h @@ -0,0 +1,68 @@ +#ifndef BIGINT_H +#define BIGINT_H +#include "mini-gmp.h" +#include +#include + +/** + * @brief The BigInt class - c++ minigmp wrapper + */ + +class BigInt +{ + mpz_t data; +public: + BigInt(); + BigInt(const BigInt& val ); + BigInt(const char *str, int base = 10); + BigInt(std::string imput, int base = 10); + BigInt(int val); + BigInt(unsigned int val); + BigInt(double val); + + std::string getString(int base = 10) const; + ~BigInt(); + + friend BigInt operator + ( BigInt left, const BigInt& right); + friend BigInt operator + ( BigInt left, unsigned int right); + friend BigInt& operator += ( BigInt &left, unsigned int right); + friend BigInt& operator += ( BigInt &left, const BigInt& right); + + friend BigInt operator - ( BigInt left, const BigInt& right); + friend BigInt operator - (unsigned int right, BigInt left); + friend BigInt operator - ( BigInt left, unsigned int right); + friend BigInt& operator -= ( BigInt &left, unsigned int right); + friend BigInt& operator -= ( BigInt &left, const BigInt& right); + + + friend BigInt operator / ( BigInt left, const BigInt& right); + friend BigInt operator / ( BigInt left, unsigned int right); + friend BigInt& operator /= ( BigInt &left, unsigned int right); + friend BigInt& operator /= ( BigInt &left, const BigInt& right); + + + friend BigInt operator * ( BigInt left, const BigInt& right); + friend BigInt operator * ( BigInt left, unsigned int right); + friend BigInt operator * ( BigInt left, int right); + friend BigInt& operator *= ( BigInt &left, unsigned int right); + friend BigInt& operator *= ( BigInt &left, const BigInt& right); + friend BigInt& operator *= ( BigInt &left, int right); + + + friend BigInt operator % ( BigInt left, const BigInt& right); + friend BigInt operator % ( BigInt left, unsigned int right); + friend BigInt& operator %= ( BigInt &left, unsigned int right); + friend BigInt& operator %= ( BigInt &left, const BigInt& right); + + + friend bool operator == ( const BigInt& left, const BigInt& right); + friend bool operator != ( const BigInt& left, const BigInt& right); + + friend bool operator < ( const BigInt& left, const BigInt& right); + friend bool operator > ( const BigInt& left, const BigInt& right); + + friend bool operator <= ( const BigInt& left, const BigInt& right); + friend bool operator >= ( const BigInt& left, const BigInt& right); +}; + +#endif // BIGINT_H diff --git a/eacsl_compat.sh b/eacsl_compat.sh deleted file mode 100644 index f8ae1c0..0000000 --- a/eacsl_compat.sh +++ /dev/null @@ -1,8 +0,0 @@ -#!/bin/sh - -EACSL='' -GMP='' - -list=$(cat "$EACSL/share/e-acsl/e_acsl_gmp.h" | grep -Poe 'extern.*?\K\b\w+(?=\()' | perl -n -e 'BEGIN{@r=();} chomp; push @r, $_; END{print join("\\b\\|\\b", @r);}') - -grep -e "$list" "$GMP/gmp.h" | perl -ne 'chomp; @s=split /\h/; print "$s[0] $s[2] $s[1]\n";' diff --git a/export.c b/export.c deleted file mode 100644 index 0af854e..0000000 --- a/export.c +++ /dev/null @@ -1,144 +0,0 @@ -#include -#include "mini-gmp.h" -EXPORT_SYMBOL_GPL(mp_set_memory_functions); -EXPORT_SYMBOL_GPL(mp_get_memory_functions); -EXPORT_SYMBOL_GPL(mpn_copyi); -EXPORT_SYMBOL_GPL(mpn_copyd); -EXPORT_SYMBOL_GPL(mpn_zero); -EXPORT_SYMBOL_GPL(mpn_cmp); -EXPORT_SYMBOL_GPL(mpn_zero_p); -EXPORT_SYMBOL_GPL(mpn_add_1); -EXPORT_SYMBOL_GPL(mpn_add_n); -EXPORT_SYMBOL_GPL(mpn_add); -EXPORT_SYMBOL_GPL(mpn_sub_1); -EXPORT_SYMBOL_GPL(mpn_sub_n); -EXPORT_SYMBOL_GPL(mpn_sub); -EXPORT_SYMBOL_GPL(mpn_mul_1); -EXPORT_SYMBOL_GPL(mpn_addmul_1); -EXPORT_SYMBOL_GPL(mpn_submul_1); -EXPORT_SYMBOL_GPL(mpn_mul); -EXPORT_SYMBOL_GPL(mpn_mul_n); -EXPORT_SYMBOL_GPL(mpn_sqr); -EXPORT_SYMBOL_GPL(mpn_perfect_square_p); -EXPORT_SYMBOL_GPL(mpn_sqrtrem); -EXPORT_SYMBOL_GPL(mpn_lshift); -EXPORT_SYMBOL_GPL(mpn_rshift); -EXPORT_SYMBOL_GPL(mpn_scan0); -EXPORT_SYMBOL_GPL(mpn_scan1); -EXPORT_SYMBOL_GPL(mpn_popcount); -EXPORT_SYMBOL_GPL(mpn_invert_3by2); -EXPORT_SYMBOL_GPL(mpn_get_str); -EXPORT_SYMBOL_GPL(mpn_set_str); -EXPORT_SYMBOL_GPL(mpz_init); -EXPORT_SYMBOL_GPL(mpz_init2); -EXPORT_SYMBOL_GPL(mpz_clear); -EXPORT_SYMBOL_GPL(mpz_sgn); -EXPORT_SYMBOL_GPL(mpz_cmp_si); -EXPORT_SYMBOL_GPL(mpz_cmp_ui); -EXPORT_SYMBOL_GPL(mpz_cmp); -EXPORT_SYMBOL_GPL(mpz_cmpabs_ui); -EXPORT_SYMBOL_GPL(mpz_cmpabs); -EXPORT_SYMBOL_GPL(mpz_abs); -EXPORT_SYMBOL_GPL(mpz_neg); -EXPORT_SYMBOL_GPL(mpz_swap); -EXPORT_SYMBOL_GPL(mpz_add_ui); -EXPORT_SYMBOL_GPL(mpz_add); -EXPORT_SYMBOL_GPL(mpz_sub_ui); -EXPORT_SYMBOL_GPL(mpz_ui_sub); -EXPORT_SYMBOL_GPL(mpz_sub); -EXPORT_SYMBOL_GPL(mpz_mul_si); -EXPORT_SYMBOL_GPL(mpz_mul_ui); -EXPORT_SYMBOL_GPL(mpz_mul); -EXPORT_SYMBOL_GPL(mpz_mul_2exp); -EXPORT_SYMBOL_GPL(mpz_addmul_ui); -EXPORT_SYMBOL_GPL(mpz_addmul); -EXPORT_SYMBOL_GPL(mpz_submul_ui); -EXPORT_SYMBOL_GPL(mpz_submul); -EXPORT_SYMBOL_GPL(mpz_cdiv_qr); -EXPORT_SYMBOL_GPL(mpz_fdiv_qr); -EXPORT_SYMBOL_GPL(mpz_tdiv_qr); -EXPORT_SYMBOL_GPL(mpz_cdiv_q); -EXPORT_SYMBOL_GPL(mpz_fdiv_q); -EXPORT_SYMBOL_GPL(mpz_tdiv_q); -EXPORT_SYMBOL_GPL(mpz_cdiv_r); -EXPORT_SYMBOL_GPL(mpz_fdiv_r); -EXPORT_SYMBOL_GPL(mpz_tdiv_r); -EXPORT_SYMBOL_GPL(mpz_cdiv_q_2exp); -EXPORT_SYMBOL_GPL(mpz_fdiv_q_2exp); -EXPORT_SYMBOL_GPL(mpz_tdiv_q_2exp); -EXPORT_SYMBOL_GPL(mpz_cdiv_r_2exp); -EXPORT_SYMBOL_GPL(mpz_fdiv_r_2exp); -EXPORT_SYMBOL_GPL(mpz_tdiv_r_2exp); -EXPORT_SYMBOL_GPL(mpz_mod); -EXPORT_SYMBOL_GPL(mpz_divexact); -EXPORT_SYMBOL_GPL(mpz_divisible_p); -EXPORT_SYMBOL_GPL(mpz_congruent_p); -EXPORT_SYMBOL_GPL(mpz_cdiv_qr_ui); -EXPORT_SYMBOL_GPL(mpz_fdiv_qr_ui); -EXPORT_SYMBOL_GPL(mpz_tdiv_qr_ui); -EXPORT_SYMBOL_GPL(mpz_cdiv_q_ui); -EXPORT_SYMBOL_GPL(mpz_fdiv_q_ui); -EXPORT_SYMBOL_GPL(mpz_tdiv_q_ui); -EXPORT_SYMBOL_GPL(mpz_cdiv_r_ui); -EXPORT_SYMBOL_GPL(mpz_fdiv_r_ui); -EXPORT_SYMBOL_GPL(mpz_tdiv_r_ui); -EXPORT_SYMBOL_GPL(mpz_cdiv_ui); -EXPORT_SYMBOL_GPL(mpz_fdiv_ui); -EXPORT_SYMBOL_GPL(mpz_tdiv_ui); -EXPORT_SYMBOL_GPL(mpz_mod_ui); -EXPORT_SYMBOL_GPL(mpz_divexact_ui); -EXPORT_SYMBOL_GPL(mpz_divisible_ui_p); -EXPORT_SYMBOL_GPL(mpz_gcd_ui); -EXPORT_SYMBOL_GPL(mpz_gcd); -EXPORT_SYMBOL_GPL(mpz_gcdext); -EXPORT_SYMBOL_GPL(mpz_lcm_ui); -EXPORT_SYMBOL_GPL(mpz_lcm); -EXPORT_SYMBOL_GPL(mpz_invert); -EXPORT_SYMBOL_GPL(mpz_sqrtrem); -EXPORT_SYMBOL_GPL(mpz_sqrt); -EXPORT_SYMBOL_GPL(mpz_perfect_square_p); -EXPORT_SYMBOL_GPL(mpz_pow_ui); -EXPORT_SYMBOL_GPL(mpz_ui_pow_ui); -EXPORT_SYMBOL_GPL(mpz_powm); -EXPORT_SYMBOL_GPL(mpz_powm_ui); -EXPORT_SYMBOL_GPL(mpz_rootrem); -EXPORT_SYMBOL_GPL(mpz_root); -EXPORT_SYMBOL_GPL(mpz_fac_ui); -EXPORT_SYMBOL_GPL(mpz_bin_uiui); -EXPORT_SYMBOL_GPL(mpz_probab_prime_p); -EXPORT_SYMBOL_GPL(mpz_tstbit); -EXPORT_SYMBOL_GPL(mpz_setbit); -EXPORT_SYMBOL_GPL(mpz_clrbit); -EXPORT_SYMBOL_GPL(mpz_combit); -EXPORT_SYMBOL_GPL(mpz_com); -EXPORT_SYMBOL_GPL(mpz_and); -EXPORT_SYMBOL_GPL(mpz_ior); -EXPORT_SYMBOL_GPL(mpz_xor); -EXPORT_SYMBOL_GPL(mpz_popcount); -EXPORT_SYMBOL_GPL(mpz_hamdist); -EXPORT_SYMBOL_GPL(mpz_scan0); -EXPORT_SYMBOL_GPL(mpz_scan1); -EXPORT_SYMBOL_GPL(mpz_fits_slong_p); -EXPORT_SYMBOL_GPL(mpz_fits_ulong_p); -EXPORT_SYMBOL_GPL(mpz_get_si); -EXPORT_SYMBOL_GPL(mpz_get_ui); -EXPORT_SYMBOL_GPL(mpz_size); -EXPORT_SYMBOL_GPL(mpz_getlimbn); -EXPORT_SYMBOL_GPL(mpz_realloc2); -EXPORT_SYMBOL_GPL(mpz_limbs_read); -EXPORT_SYMBOL_GPL(mpz_limbs_modify); -EXPORT_SYMBOL_GPL(mpz_limbs_write); -EXPORT_SYMBOL_GPL(mpz_limbs_finish); -EXPORT_SYMBOL_GPL(mpz_roinit_n); -EXPORT_SYMBOL_GPL(mpz_set_si); -EXPORT_SYMBOL_GPL(mpz_set_ui); -EXPORT_SYMBOL_GPL(mpz_set); -EXPORT_SYMBOL_GPL(mpz_init_set_si); -EXPORT_SYMBOL_GPL(mpz_init_set_ui); -EXPORT_SYMBOL_GPL(mpz_init_set); -EXPORT_SYMBOL_GPL(mpz_sizeinbase); -EXPORT_SYMBOL_GPL(mpz_get_str); -EXPORT_SYMBOL_GPL(mpz_set_str); -EXPORT_SYMBOL_GPL(mpz_init_set_str); -EXPORT_SYMBOL_GPL(mpz_import); -EXPORT_SYMBOL_GPL(mpz_export); diff --git a/gen_export.sh b/gen_export.sh deleted file mode 100755 index fe5a825..0000000 --- a/gen_export.sh +++ /dev/null @@ -1,9 +0,0 @@ -#!/bin/sh - -echo '#include ' > export.c -echo '#include "mini-gmp.h"' >> export.c - - -gcc -P -E -DKERNEL=1 ./mini-gmp.h | -perl -e 'my $all=join("",<>); my @d; push @d, "EXPORT_SYMBOL_GPL($1);" while $all =~ m/(\w++)\h*(\((?:[^()]++|(?2))*\))\s*;/g; print join("\n", @d) . "\n";' >> export.c - diff --git a/mini-gmp.c b/mini-gmp.c index 1f902af..b918695 100644 --- a/mini-gmp.c +++ b/mini-gmp.c @@ -2,7 +2,7 @@ Contributed to the GNU project by Niels Möller -Copyright 1991-1997, 1999-2015 Free Software Foundation, Inc. +Copyright 1991-1997, 1999-2014 Free Software Foundation, Inc. This file is part of the GNU MP Library. @@ -41,35 +41,15 @@ see https://www.gnu.org/licenses/. */ mpn/generic/sbpi1_div_qr.c, mpn/generic/sub_n.c, mpn/generic/submul_1.c. */ -#ifdef KERNEL -# include -# include -# include -# include -# include -#else -# include -# include -# include -# include -# include -# include -#endif +#include +#include +#include +#include +#include +#include #include "mini-gmp.h" -#ifdef KERNEL -# define CHAR_BIT BITS_PER_BYTE -# define assert BUG_ON -# define free(p) kfree(p) -# define malloc(s) kmalloc(s, GFP_KERNEL) -# define realloc(o,n) krealloc(o, n, GFP_KERNEL) -#endif - -#ifdef KERNEL -MODULE_LICENSE("GPL"); -#endif - /* Macros */ #define GMP_LIMB_BITS (sizeof(mp_limb_t) * CHAR_BIT) @@ -260,20 +240,12 @@ const int mp_bits_per_limb = GMP_LIMB_BITS; /* Memory allocation and other helper functions. */ -#ifdef KERNEL -static void -gmp_die (const char *msg) -{ - panic(msg); -} -#else static void gmp_die (const char *msg) { fprintf (stderr, "%s\n", msg); abort(); } -#endif static void * gmp_default_alloc (size_t size) @@ -292,12 +264,12 @@ gmp_default_alloc (size_t size) static void * gmp_default_realloc (void *old, size_t old_size, size_t new_size) { - void * p; + mp_ptr p; p = realloc (old, new_size); if (!p) - gmp_die("gmp_default_realloc: Virtual memory exhausted."); + gmp_die("gmp_default_realoc: Virtual memory exhausted."); return p; } @@ -350,14 +322,14 @@ mp_set_memory_functions (void *(*alloc_func) (size_t), static mp_ptr gmp_xalloc_limbs (mp_size_t size) { - return (mp_ptr) gmp_xalloc (size * sizeof (mp_limb_t)); + return gmp_xalloc (size * sizeof (mp_limb_t)); } static mp_ptr gmp_xrealloc_limbs (mp_ptr old, mp_size_t size) { assert (size > 0); - return (mp_ptr) (*gmp_reallocate_func) (old, 0, size * sizeof (mp_limb_t)); + return (*gmp_reallocate_func) (old, 0, size * sizeof (mp_limb_t)); } @@ -374,7 +346,7 @@ mpn_copyi (mp_ptr d, mp_srcptr s, mp_size_t n) void mpn_copyd (mp_ptr d, mp_srcptr s, mp_size_t n) { - while (--n >= 0) + while (n-- > 0) d[n] = s[n]; } @@ -401,22 +373,20 @@ mpn_cmp4 (mp_srcptr ap, mp_size_t an, mp_srcptr bp, mp_size_t bn) static mp_size_t mpn_normalized_size (mp_srcptr xp, mp_size_t n) { - while (n > 0 && xp[n-1] == 0) - --n; + for (; n > 0 && xp[n-1] == 0; n--) + ; return n; } -int -mpn_zero_p(mp_srcptr rp, mp_size_t n) -{ - return mpn_normalized_size (rp, n) == 0; -} +#define mpn_zero_p(xp, n) (mpn_normalized_size ((xp), (n)) == 0) void mpn_zero (mp_ptr rp, mp_size_t n) { - while (--n >= 0) - rp[n] = 0; + mp_size_t i; + + for (i = 0; i < n; i++) + rp[i] = 0; } mp_limb_t @@ -608,16 +578,17 @@ mpn_mul (mp_ptr rp, mp_srcptr up, mp_size_t un, mp_srcptr vp, mp_size_t vn) way. */ rp[un] = mpn_mul_1 (rp, up, un, vp[0]); + rp += 1, vp += 1, vn -= 1; /* Now accumulate the product of up[] and the next higher limb from vp[]. */ - while (--vn >= 1) + while (vn >= 1) { - rp += 1, vp += 1; rp[un] = mpn_addmul_1 (rp, up, un, vp[0]); + rp += 1, vp += 1, vn -= 1; } - return rp[un]; + return rp[un - 1]; } void @@ -637,6 +608,7 @@ mpn_lshift (mp_ptr rp, mp_srcptr up, mp_size_t n, unsigned int cnt) { mp_limb_t high_limb, low_limb; unsigned int tnc; + mp_size_t i; mp_limb_t retval; assert (n >= 1); @@ -651,7 +623,7 @@ mpn_lshift (mp_ptr rp, mp_srcptr up, mp_size_t n, unsigned int cnt) retval = low_limb >> tnc; high_limb = (low_limb << cnt); - while (--n != 0) + for (i = n; --i != 0;) { low_limb = *--up; *--rp = high_limb | (low_limb >> tnc); @@ -667,6 +639,7 @@ mpn_rshift (mp_ptr rp, mp_srcptr up, mp_size_t n, unsigned int cnt) { mp_limb_t high_limb, low_limb; unsigned int tnc; + mp_size_t i; mp_limb_t retval; assert (n >= 1); @@ -678,7 +651,7 @@ mpn_rshift (mp_ptr rp, mp_srcptr up, mp_size_t n, unsigned int cnt) retval = (high_limb << tnc); low_limb = high_limb >> cnt; - while (--n != 0) + for (i = n; --i != 0;) { high_limb = *up++; *rp++ = low_limb | (high_limb << tnc); @@ -903,7 +876,7 @@ mpn_div_qr_1_preinv (mp_ptr qp, mp_srcptr np, mp_size_t nn, d = inv->d1; di = inv->di; - while (--nn >= 0) + while (nn-- > 0) { mp_limb_t q; @@ -1341,7 +1314,7 @@ mpn_set_str_other (mp_ptr rp, const unsigned char *sp, size_t sn, j = 0; w = sp[j++]; - while (--k != 0) + for (; --k > 0; ) w = w * b + sp[j++]; rp[0] = w; @@ -1415,7 +1388,7 @@ mpz_clear (mpz_t r) gmp_free (r->_mp_d); } -static mp_ptr +static void * mpz_realloc (mpz_t r, mp_size_t size) { size = GMP_MAX (size, 1); @@ -1502,12 +1475,14 @@ mpz_fits_slong_p (const mpz_t u) { mp_size_t us = u->_mp_size; - if (us == 1) + if (us == 0) + return 1; + else if (us == 1) return u->_mp_d[0] < GMP_LIMB_HIGHBIT; else if (us == -1) return u->_mp_d[0] <= GMP_LIMB_HIGHBIT; else - return (us == 0); + return 0; } int @@ -1594,7 +1569,6 @@ mpz_roinit_n (mpz_t x, mp_srcptr xp, mp_size_t xs) return x; } -#ifndef KERNEL /* Conversions and comparison to double. */ void @@ -1736,7 +1710,6 @@ mpz_cmp_d (const mpz_t x, double d) return mpz_cmpabs_d (x, d); } } -#endif /* MPZ comparisons and the like. */ @@ -1823,14 +1796,18 @@ mpz_cmpabs (const mpz_t u, const mpz_t v) void mpz_abs (mpz_t r, const mpz_t u) { - mpz_set (r, u); + if (r != u) + mpz_set (r, u); + r->_mp_size = GMP_ABS (r->_mp_size); } void mpz_neg (mpz_t r, const mpz_t u) { - mpz_set (r, u); + if (r != u) + mpz_set (r, u); + r->_mp_size = -r->_mp_size; } @@ -2100,7 +2077,8 @@ mpz_mul_2exp (mpz_t r, const mpz_t u, mp_bitcnt_t bits) else mpn_copyd (rp + limbs, u->_mp_d, un); - mpn_zero (rp, limbs); + while (limbs > 0) + rp[--limbs] = 0; r->_mp_size = (u->_mp_size < 0) ? - rn : rn; } @@ -3096,7 +3074,9 @@ void mpz_ui_pow_ui (mpz_t r, unsigned long blimb, unsigned long e) { mpz_t b; - mpz_pow_ui (r, mpz_roinit_n (b, &blimb, 1), e); + mpz_init_set_ui (b, blimb); + mpz_pow_ui (r, b, e); + mpz_clear (b); } void @@ -3168,7 +3148,7 @@ mpz_powm (mpz_t r, const mpz_t b, const mpz_t e, const mpz_t m) } mpz_init_set_ui (tr, 1); - while (--en >= 0) + while (en-- > 0) { mp_limb_t w = e->_mp_d[en]; mp_limb_t bit; @@ -3208,7 +3188,9 @@ void mpz_powm_ui (mpz_t r, const mpz_t b, unsigned long elimb, const mpz_t m) { mpz_t e; - mpz_powm (r, b, mpz_roinit_n (e, &elimb, 1), m); + mpz_init_set_ui (e, elimb); + mpz_powm (r, b, e, m); + mpz_clear (e); } /* x=trunc(y^(1/z)), r=y-x^z */ @@ -3236,7 +3218,7 @@ mpz_rootrem (mpz_t x, mpz_t r, const mpz_t y, unsigned long z) { mp_bitcnt_t tb; tb = mpz_sizeinbase (y, 2) / z + 1; - mpz_init2 (t, tb + 1); + mpz_init2 (t, tb); mpz_setbit (t, tb); } @@ -3351,7 +3333,7 @@ void mpz_fac_ui (mpz_t x, unsigned long n) { mpz_set_ui (x, n + (n == 0)); - while (n > 2) + for (;n > 2;) mpz_mul_ui (x, x, --n); } @@ -3522,7 +3504,7 @@ mpz_tstbit (const mpz_t d, mp_bitcnt_t bit_index) must be complemented. */ if (shift > 0 && (w << (GMP_LIMB_BITS - shift)) > 0) return bit ^ 1; - while (--limb_index >= 0) + while (limb_index-- > 0) if (d->_mp_d[limb_index] > 0) return bit ^ 1; } @@ -3587,7 +3569,7 @@ mpz_abs_sub_bit (mpz_t d, mp_bitcnt_t bit_index) gmp_assert_nocarry (mpn_sub_1 (dp + limb_index, dp + limb_index, dn - limb_index, bit)); - dn = mpn_normalized_size (dp, dn); + dn -= (dp[dn-1] == 0); d->_mp_size = (d->_mp_size < 0) ? - dn : dn; } @@ -4083,7 +4065,7 @@ mpz_get_str (char *sp, int base, const mpz_t u) sn = 1 + mpz_sizeinbase (u, base); if (!sp) - sp = (char *) gmp_xalloc (1 + sn); + sp = gmp_xalloc (1 + sn); un = GMP_ABS (u->_mp_size); @@ -4165,7 +4147,7 @@ mpz_set_str (mpz_t r, const char *sp, int base) } sn = strlen (sp); - dp = (unsigned char *) gmp_xalloc (sn + (sn == 0)); + dp = gmp_xalloc (sn + (sn == 0)); for (sn = 0; *sp; sp++) { @@ -4223,7 +4205,6 @@ mpz_init_set_str (mpz_t r, const char *sp, int base) return mpz_set_str (r, sp, base); } -#ifndef KERNEL size_t mpz_out_str (FILE *stream, int base, const mpz_t x) { @@ -4236,7 +4217,6 @@ mpz_out_str (FILE *stream, int base, const mpz_t x) gmp_free (str); return len; } -#endif static int diff --git a/mini-gmp.h b/mini-gmp.h index 81bb77a..bc925e4 100644 --- a/mini-gmp.h +++ b/mini-gmp.h @@ -1,6 +1,6 @@ /* mini-gmp, a minimalistic implementation of a GNU GMP subset. -Copyright 2011-2015 Free Software Foundation, Inc. +Copyright 2011-2014 Free Software Foundation, Inc. This file is part of the GNU MP Library. @@ -38,14 +38,8 @@ see https://www.gnu.org/licenses/. */ #ifndef __MINI_GMP_H__ #define __MINI_GMP_H__ -#ifndef KERNEL /* For size_t */ -# include -#endif - -#if defined (__cplusplus) -extern "C" { -#endif +#include void mp_set_memory_functions (void *(*) (size_t), void *(*) (void *, size_t, size_t), @@ -84,7 +78,6 @@ void mpn_copyd (mp_ptr, mp_srcptr, mp_size_t); void mpn_zero (mp_ptr, mp_size_t); int mpn_cmp (mp_srcptr, mp_srcptr, mp_size_t); -int mpn_zero_p (mp_srcptr, mp_size_t); mp_limb_t mpn_add_1 (mp_ptr, mp_srcptr, mp_size_t, mp_limb_t); mp_limb_t mpn_add_n (mp_ptr, mp_srcptr, mp_srcptr, mp_size_t); @@ -131,10 +124,8 @@ int mpz_cmp_ui (const mpz_t, unsigned long); int mpz_cmp (const mpz_t, const mpz_t); int mpz_cmpabs_ui (const mpz_t, unsigned long); int mpz_cmpabs (const mpz_t, const mpz_t); -#ifndef KERNEL int mpz_cmp_d (const mpz_t, double); int mpz_cmpabs_d (const mpz_t, double); -#endif void mpz_abs (mpz_t, const mpz_t); void mpz_neg (mpz_t, const mpz_t); @@ -241,9 +232,7 @@ int mpz_fits_slong_p (const mpz_t); int mpz_fits_ulong_p (const mpz_t); long int mpz_get_si (const mpz_t); unsigned long int mpz_get_ui (const mpz_t); -#ifndef KERNEL double mpz_get_d (const mpz_t); -#endif size_t mpz_size (const mpz_t); mp_limb_t mpz_getlimbn (const mpz_t, mp_size_t); @@ -259,16 +248,12 @@ mpz_srcptr mpz_roinit_n (mpz_t, mp_srcptr, mp_size_t); void mpz_set_si (mpz_t, signed long int); void mpz_set_ui (mpz_t, unsigned long int); void mpz_set (mpz_t, const mpz_t); -#ifndef KERNEL void mpz_set_d (mpz_t, double); -#endif void mpz_init_set_si (mpz_t, signed long int); void mpz_init_set_ui (mpz_t, unsigned long int); void mpz_init_set (mpz_t, const mpz_t); -#ifndef KERNEL void mpz_init_set_d (mpz_t, double); -#endif size_t mpz_sizeinbase (const mpz_t, int); char *mpz_get_str (char *, int, const mpz_t); @@ -278,7 +263,7 @@ int mpz_init_set_str (mpz_t, const char *, int); /* This long list taken from gmp.h. */ /* For reference, "defined(EOF)" cannot be used here. In g++ 2.95.4, defines EOF but not FILE. */ -#if defined (FILE) && !defined(KERNEL) \ +#if defined (FILE) \ || defined (H_STDIO) \ || defined (_H_STDIO) /* AIX */ \ || defined (_STDIO_H) /* glibc, Sun, SCO */ \ @@ -299,27 +284,5 @@ size_t mpz_out_str (FILE *, int, const mpz_t); void mpz_import (mpz_t, size_t, int, size_t, int, size_t, const void *); void *mpz_export (void *, size_t *, int, size_t, int, size_t, const mpz_t); -#define __gmpz_add mpz_add -#define __gmpz_clear mpz_clear -#define __gmpz_cmp mpz_cmp -#define __gmpz_com mpz_com -#define __gmpz_get_si mpz_get_si -#define __gmpz_get_ui mpz_get_ui -#define __gmpz_init mpz_init -#define __gmpz_init_set mpz_init_set -#define __gmpz_init_set_si mpz_init_set_si -#define __gmpz_init_set_str mpz_init_set_str -#define __gmpz_init_set_ui mpz_init_set_ui -#define __gmpz_mul mpz_mul -#define __gmpz_neg mpz_neg -#define __gmpz_set mpz_set -#define __gmpz_set_si mpz_set_si -#define __gmpz_set_ui mpz_set_ui -#define __gmpz_sub mpz_sub -#define __gmpz_tdiv_q mpz_tdiv_q -#define __gmpz_tdiv_r mpz_tdiv_r -#if defined (__cplusplus) -} -#endif #endif /* __MINI_GMP_H__ */ diff --git a/minigmp_global.h b/minigmp_global.h new file mode 100644 index 0000000..dc9dba1 --- /dev/null +++ b/minigmp_global.h @@ -0,0 +1,10 @@ +#ifndef MINIGMP_GLOBAL_H +#define MINIGMP_GLOBAL_H + +#if defined(MINIGMP_LIBRARY) +# define UNTITLEDSHARED_EXPORT __declspec(dllexport) +#else +# define UNTITLEDSHARED_EXPORT __attribute__((visibility("default"))) +#endif + +#endif //MINIGMP_GLOBAL_H