refactor test of fp_generator

dev
MITSUNARI Shigeo 9 years ago
parent 8dbb713772
commit e733e523f1
  1. 1
      include/mcl/fp.hpp
  2. 36
      include/mcl/fp_generator.hpp
  3. 2
      src/fp.cpp
  4. 35
      test/fp_generator_test.cpp

@ -66,6 +66,7 @@ public:
const Unit *getUnit() const { return v_; }
static inline size_t getUnitSize() { return op_.N; }
static inline size_t getBitSize() { return op_.bitSize; }
static inline const fp::Op& getOp() { return op_; }
void dump() const
{
const size_t N = op_.N;

@ -161,14 +161,11 @@ struct FpGenerator : Xbyak::CodeGenerator {
typedef int (*int2op)(uint64_t*, const uint64_t*);
bool3op addNC_;
bool3op subNC_;
void3op add_;
void3op sub_;
void3op mul_;
void3u mul_;
uint3opI mulI_;
void *montRedRaw_;
void2op montRed_;
void2op sqr_;
void2op neg_;
void2op shr1_;
int2op preInv_;
FpGenerator()
@ -179,28 +176,24 @@ struct FpGenerator : Xbyak::CodeGenerator {
, isFullBit_(0)
, addNC_(0)
, subNC_(0)
, add_(0)
, sub_(0)
, mul_(0)
, mulI_(0)
, montRedRaw_(0)
, montRed_(0)
, neg_(0)
, shr1_(0)
, preInv_(0)
{
useMulx_ = cpu_.has(Xbyak::util::Cpu::tBMI2);
}
/*
@param p [in] pointer to prime
@param pn [in] length of prime
@param op [in] ; use op.p and op.N
*/
void init(Op& op, const uint64_t *p, int pn)
void init(Op& op)
{
if (pn < 2) throw cybozu::Exception("mcl:FpGenerator:small pn") << pn;
p_ = p;
rp_ = fp::getMontgomeryCoeff(p[0]);
pn_ = pn;
if (op.N < 2) throw cybozu::Exception("mcl:FpGenerator:small pn") << op.N;
p_ = op.p;
rp_ = fp::getMontgomeryCoeff(p_[0]);
pn_ = (int)op.N;
isFullBit_ = (p_[pn_ - 1] >> 63) != 0;
// printf("p=%p, pn_=%d, isFullBit_=%d\n", p_, pn_, isFullBit_);
@ -212,19 +205,20 @@ struct FpGenerator : Xbyak::CodeGenerator {
subNC_ = getCurr<bool3op>();
gen_addSubNC(false);
align(16);
add_ = getCurr<void3op>();
op.fp_add = getCurr<void3u>();
gen_addMod();
align(16);
sub_ = getCurr<void3op>();
op.fp_sub = getCurr<void3u>();
gen_sub();
align(16);
neg_ = getCurr<void2op>();
op.fp_neg = getCurr<void2u>();
gen_neg();
align(16);
mulI_ = getCurr<uint3opI>();
gen_mulI();
align(16);
mul_ = getCurr<void3op>();
mul_ = getCurr<void3u>();
op.fp_mul = mul_;
gen_mul();
align(16);
montRed_ = getCurr<void2op>();
@ -240,15 +234,11 @@ struct FpGenerator : Xbyak::CodeGenerator {
preInv_ = getCurr<int2op>();
gen_preInv();
op.fp_neg = Xbyak::CastTo<void2u>(neg_);
op.fp_add = Xbyak::CastTo<void3u>(add_);
op.fp_sub = Xbyak::CastTo<void3u>(sub_);
op.fp_addNC = Xbyak::CastTo<void3u>(addNC_);
op.fp_subNC = Xbyak::CastTo<void3u>(subNC_);
op.fp_mul = Xbyak::CastTo<void3u>(mul_);
op.fp_sqr = Xbyak::CastTo<void2u>(sqr_);
if (pn <= 4) {
if (op.N <= 4) {
if (montRed_) {
op.fp_mod = Xbyak::CastTo<void2u>(montRed_);
}

@ -363,7 +363,7 @@ static void initForMont(Op& op, const Unit *p, Mode mode)
#ifdef MCL_USE_XBYAK
FpGenerator *fg = op.fg;
if (fg == 0) return;
fg->init(op, p, (int)N);
fg->init(op);
if (N <= 4) {
op.fp_invOp = &invOpForMontC;

@ -93,10 +93,10 @@ static inline std::ostream& operator<<(std::ostream& os, const Int& x)
return os << x.getStr();
}
void testAddSub(const mcl::fp::FpGenerator& fg, int pn)
void testAddSub(const mcl::fp::Op& op)
{
Fp x, y;
Int mx(pn), my(pn);
Int mx(op.N), my(op.N);
x.setStr("0x8811aabb23427cc");
y.setStr("0x8811aabb23427cc11");
mx.set(x);
@ -104,24 +104,24 @@ void testAddSub(const mcl::fp::FpGenerator& fg, int pn)
for (int i = 0; i < 30; i++) {
CYBOZU_TEST_EQUAL(mx, x);
x += x;
fg.add_(mx.v, mx.v, mx.v);
op.fp_add(mx.v, mx.v, mx.v);
}
for (int i = 0; i < 30; i++) {
CYBOZU_TEST_EQUAL(mx, x);
x += y;
fg.add_(mx.v, mx.v, my.v);
op.fp_add(mx.v, mx.v, my.v);
}
for (int i = 0; i < 30; i++) {
CYBOZU_TEST_EQUAL(my, y);
y -= x;
fg.sub_(my.v, my.v, mx.v);
op.fp_sub(my.v, my.v, mx.v);
}
}
void testNeg(const mcl::fp::FpGenerator& fg, int pn)
void testNeg(const mcl::fp::Op& op)
{
Fp x;
Int mx(pn), my(pn);
Int mx(op.N), my(op.N);
const char *tbl[] = {
"0",
"0x12346",
@ -132,7 +132,7 @@ void testNeg(const mcl::fp::FpGenerator& fg, int pn)
x.setStr(tbl[i]);
mx.set(x);
x = -x;
fg.neg_(mx.v, mx.v);
op.fp_neg(mx.v, mx.v);
CYBOZU_TEST_EQUAL(mx, x);
}
}
@ -183,18 +183,13 @@ void testShr1(const mcl::fp::FpGenerator& fg, int pn)
void test(const char *pStr)
{
Fp::setModulo(pStr, 16);
uint64_t p[MAX_N];
strToArray(p, MAX_N, pStr);
const int pn = mcl::fp::getNonZeroArraySize(p, MAX_N);
printf("pn=%d\n", pn);
mcl::fp::FpGenerator fg;
mcl::fp::Op op;
fg.init(op, p, pn);
testAddSub(fg, pn);
testNeg(fg, pn);
testMulI(fg, pn);
testShr1(fg, pn);
Fp::setModulo(pStr, 16, mcl::fp::FP_XBYAK);
const mcl::fp::Op& op = Fp::getOp();
const int pn = (int)op.N;
testAddSub(op);
testNeg(op);
testMulI(*op.fg, pn);
testShr1(*op.fg, pn);
}
CYBOZU_TEST_AUTO(all)

Loading…
Cancel
Save