From 3bb1ad5fa95a17a2594200ab5ac5b8be1a6ee740 Mon Sep 17 00:00:00 2001 From: MITSUNARI Shigeo Date: Tue, 11 Oct 2016 12:09:37 +0900 Subject: [PATCH] unify low_gmp.hpp to fp_proto.hpp --- src/fp.cpp | 10 ---- src/fp_proto.hpp | 120 ++++++++++++++++++++++++++++++++++++++-------- src/low_gmp.hpp | 96 ------------------------------------- test/low_test.cpp | 1 - 4 files changed, 101 insertions(+), 126 deletions(-) delete mode 100644 src/low_gmp.hpp diff --git a/src/fp.cpp b/src/fp.cpp index 7ff29fc..acafcda 100644 --- a/src/fp.cpp +++ b/src/fp.cpp @@ -5,7 +5,6 @@ #include "fp_generator.hpp" #endif #include "fp_proto.hpp" -#include "low_gmp.hpp" #ifdef _MSC_VER #pragma warning(disable : 4127) @@ -122,15 +121,6 @@ template<>const void3u MontRed::f = &mcl_fp_montRed ## n ## L; \ template<>const void4u DblAdd::f = &mcl_fpDbl_add ## n ## L; \ template<>const void4u DblSub::f = &mcl_fpDbl_sub ## n ## L; \ -// use Dbl_Mod of gmp -template -struct Dbl_Mod { - static const void3u f; -}; - -template -const void3u Dbl_Mod::f = Dbl_Mod::f; - MCL_DEF_LLVM_FUNC(1) MCL_DEF_LLVM_FUNC(2) MCL_DEF_LLVM_FUNC(3) diff --git a/src/fp_proto.hpp b/src/fp_proto.hpp index 68cca54..22fc812 100644 --- a/src/fp_proto.hpp +++ b/src/fp_proto.hpp @@ -10,28 +10,110 @@ namespace mcl { namespace fp { -struct Ltag; -struct Atag; +struct Gtag; // GMP +struct Ltag; // LLVM +struct Atag; // asm // (carry, z[N]) <- x[N] + y[N] -templatestruct AddNC { static const u3u f; }; +template +struct AddNC { + static inline Unit func(Unit *z, const Unit *x, const Unit *y) + { + return mpn_add_n((mp_limb_t*)z, (const mp_limb_t*)x, (const mp_limb_t*)y, N); + } + static const u3u f; +}; +template +const u3u AddNC::f = &AddNC::func; + // (carry, z[N]) <- x[N] - y[N] -templatestruct SubNC { static const u3u f; }; +template +struct SubNC { + static inline Unit func(Unit *z, const Unit *x, const Unit *y) + { + return mpn_sub_n((mp_limb_t*)z, (const mp_limb_t*)x, (const mp_limb_t*)y, N); + } + static const u3u f; +}; + +template +const u3u SubNC::f = &SubNC::func; + // z[N * 2] <- x[N] * y[N] -templatestruct MulPre { static const void3u f; }; +template +struct MulPre { + static inline void func(Unit *z, const Unit *x, const Unit *y) + { + return mpn_mul_n((mp_limb_t*)z, (const mp_limb_t*)x, (const mp_limb_t*)y, N); + } + static const void3u f; +}; + +template +const void3u MulPre::f = &MulPre::func; + // z[N * 2] <- x[N] * x[N] -templatestruct SqrPre { static const void2u f; }; +template +struct SqrPre { + static inline void func(Unit *y, const Unit *x) + { + return mpn_sqr((mp_limb_t*)y, (const mp_limb_t*)x, N); + } + static const void2u f; +}; + +template +const void2u SqrPre::f = &SqrPre::func; + // z[N + 1] <- x[N] * y -templatestruct Mul_UnitPre { static const void2uI f; }; +template +struct Mul_UnitPre { + static inline void func(Unit *z, const Unit *x, Unit y) + { + z[N] = mpn_mul_1((mp_limb_t*)z, (const mp_limb_t*)x, N, y); + } + static const void2uI f; +}; + +template +const void2uI Mul_UnitPre::f = &Mul_UnitPre::func; + // z[N] <- x[N + 1] % p[N] -templatestruct N1_Mod { static const void3u f; }; +template +struct N1_Mod { + static inline void func(Unit *y, const Unit *x, const Unit *p) + { + mp_limb_t q[2]; // not used + mpn_tdiv_qr(q, (mp_limb_t*)y, 0, (const mp_limb_t*)x, N + 1, (const mp_limb_t*)p, N); + } + static const void3u f; +}; + +template +const void3u N1_Mod::f = &N1_Mod::func; + // z[N] <- x[N * 2] % p[N] -templatestruct Dbl_Mod { static const void3u f; }; +template +struct Dbl_Mod { + static inline void func(Unit *y, const Unit *x, const Unit *p) + { + mp_limb_t q[N + 1]; // not used + mpn_tdiv_qr(q, (mp_limb_t*)y, 0, (const mp_limb_t*)x, N * 2, (const mp_limb_t*)p, N); + } + static const void3u f; +}; + +template +const void3u Dbl_Mod::f = &Dbl_Mod::func; + // z[N] <- MontRed(xy[N], p[N]) -templatestruct MontRed { static const void3u f; }; +template +struct MontRed { + static const void3u f; +}; // z[N] <- (x[N] + y[N]) % p[N] -template +template struct Add { static inline void func(Unit *z, const Unit *x, const Unit *y, const Unit *p) { @@ -51,7 +133,7 @@ template const void4u Add::f = Add::func; // z[N] <- (x[N] - y[N]) % p[N] -template +template struct Sub { static inline void func(Unit *z, const Unit *x, const Unit *y, const Unit *p) { @@ -66,7 +148,7 @@ template const void4u Sub::f = Sub::func; // z[N * 2] <- (x[N * 2] + y[N * 2]) mod p[N] << (N * UnitBitSize) -template +template struct DblAdd { static inline void func(Unit *z, const Unit *x, const Unit *y, const Unit *p) { @@ -86,7 +168,7 @@ template const void4u DblAdd::f = DblAdd::func; // z[N * 2] <- (x[N * 2] - y[N * 2]) mod p[N] << (N * UnitBitSize) -template +template struct DblSub { static inline void func(Unit *z, const Unit *x, const Unit *y, const Unit *p) { @@ -101,13 +183,13 @@ template const void4u DblSub::f = DblSub::func; // z[N] <- Montgomery(x[N], y[N], p[N]) -template +template struct Mont { static inline void func(Unit *z, const Unit *x, const Unit *y, const Unit *p) { #if 0 Unit xy[N * 2]; - MulPre::f(xy, x, y); + MulPre::f(xy, x, y); fpDbl_modMontC(z, xy, p); #else const Unit rp = p[-1]; @@ -144,7 +226,7 @@ template const void4u Mont::f = Mont::func; // z[N] <- Montgomery(x[N], x[N], p[N]) -template +template struct SqrMont { static inline void func(Unit *y, const Unit *x, const Unit *p) { @@ -162,7 +244,7 @@ template const void3u SqrMont::f = SqrMont::func; // z[N] <- (x[N] * y[N]) % p[N] -template +template struct Mul { static inline void func(Unit *z, const Unit *x, const Unit *y, const Unit *p) { @@ -176,7 +258,7 @@ template const void4u Mul::f = Mul::func; // y[N] <- (x[N] * x[N]) % p[N] -template +template struct Sqr { static inline void func(Unit *y, const Unit *x, const Unit *p) { diff --git a/src/low_gmp.hpp b/src/low_gmp.hpp deleted file mode 100644 index d11a30f..0000000 --- a/src/low_gmp.hpp +++ /dev/null @@ -1,96 +0,0 @@ -#pragma once -#include -#include "fp_proto.hpp" - -namespace mcl { namespace fp { - -struct Gtag; - -template -struct AddNC { - static inline Unit func(Unit *z, const Unit *x, const Unit *y) - { - return mpn_add_n((mp_limb_t*)z, (const mp_limb_t*)x, (const mp_limb_t*)y, N); - } - static const u3u f; -}; - -template -const u3u AddNC::f = &AddNC::func; - -template -struct SubNC { - static inline Unit func(Unit *z, const Unit *x, const Unit *y) - { - return mpn_sub_n((mp_limb_t*)z, (const mp_limb_t*)x, (const mp_limb_t*)y, N); - } - static const u3u f; -}; - -template -const u3u SubNC::f = &SubNC::func; - -template -struct MulPre { - static inline void func(Unit *z, const Unit *x, const Unit *y) - { - return mpn_mul_n((mp_limb_t*)z, (const mp_limb_t*)x, (const mp_limb_t*)y, N); - } - static const void3u f; -}; - -template -const void3u MulPre::f = &MulPre::func; - -template -struct SqrPre { - static inline void func(Unit *y, const Unit *x) - { - return mpn_sqr((mp_limb_t*)y, (const mp_limb_t*)x, N); - } - static const void2u f; -}; - -template -const void2u SqrPre::f = &SqrPre::func; - -template -struct Mul_UnitPre { - static inline void func(Unit *z, const Unit *x, Unit y) - { - z[N] = mpn_mul_1((mp_limb_t*)z, (const mp_limb_t*)x, N, y); - } - static const void2uI f; -}; - -template -const void2uI Mul_UnitPre::f = &Mul_UnitPre::func; - -template -struct N1_Mod { - static inline void func(Unit *y, const Unit *x, const Unit *p) - { - mp_limb_t q[2]; // not used - mpn_tdiv_qr(q, (mp_limb_t*)y, 0, (const mp_limb_t*)x, N + 1, (const mp_limb_t*)p, N); - } - static const void3u f; -}; - -template -const void3u N1_Mod::f = &N1_Mod::func; - -template -struct Dbl_Mod { - static inline void func(Unit *y, const Unit *x, const Unit *p) - { - mp_limb_t q[N + 1]; // not used - mpn_tdiv_qr(q, (mp_limb_t*)y, 0, (const mp_limb_t*)x, N * 2, (const mp_limb_t*)p, N); - } - static const void3u f; -}; - -template -const void3u Dbl_Mod::f = &Dbl_Mod::func; - -} } // mcl::fp - diff --git a/test/low_test.cpp b/test/low_test.cpp index e1ae420..2dbeae1 100644 --- a/test/low_test.cpp +++ b/test/low_test.cpp @@ -5,7 +5,6 @@ #include #include #include "../src/fp_proto.hpp" -#include "../src/low_gmp.hpp" #include cybozu::XorShift rg;