fix roundup of bitSize

dev
MITSUNARI Shigeo 8 years ago
parent 74620cb7a4
commit e46f2c2ab7
  1. 10
      include/mcl/fp.hpp
  2. 6
      src/gen.cpp
  3. 2
      test/fp_test.cpp

@ -105,6 +105,7 @@ public:
const Unit *getUnit() const { return v_; } const Unit *getUnit() const { return v_; }
static inline size_t getUnitSize() { return op_.N; } static inline size_t getUnitSize() { return op_.N; }
static inline size_t getBitSize() { return op_.bitSize; } static inline size_t getBitSize() { return op_.bitSize; }
static inline size_t getByteSize() { return (op_.bitSize + 7) / 8; }
static inline const fp::Op& getOp() { return op_; } static inline const fp::Op& getOp() { return op_; }
void dump() const void dump() const
{ {
@ -221,7 +222,7 @@ public:
void setStr(const std::string& str, int ioMode = 0) void setStr(const std::string& str, int ioMode = 0)
{ {
if (ioMode & (IoArray | IoArrayRaw)) { if (ioMode & (IoArray | IoArrayRaw)) {
const size_t n = getBitSize() / 8; const size_t n = getByteSize();
if (str.size() != n) throw cybozu::Exception("FpT:setStr:bad size") << str.size() << n << ioMode; if (str.size() != n) throw cybozu::Exception("FpT:setStr:bad size") << str.size() << n << ioMode;
if (ioMode & IoArrayRaw) { if (ioMode & IoArrayRaw) {
memcpy(v_, str.c_str(), n); memcpy(v_, str.c_str(), n);
@ -289,15 +290,16 @@ public:
} }
void getStr(std::string& str, int ioMode = 10) const void getStr(std::string& str, int ioMode = 10) const
{ {
const size_t n = getByteSize();
if (ioMode & IoArrayRaw) { if (ioMode & IoArrayRaw) {
str.resize(getBitSize() / 8); str.resize(n);
memcpy(&str[0], v_, str.size()); memcpy(&str[0], v_, str.size());
return; return;
} }
fp::Block b; fp::Block b;
getBlock(b); getBlock(b);
if (ioMode & IoArray) { if (ioMode & IoArray) {
str.resize(getBitSize() / 8); str.resize(n);
memcpy(&str[0], b.p, str.size()); memcpy(&str[0], b.p, str.size());
return; return;
} }
@ -380,7 +382,7 @@ public:
int ioMode = fp::detectIoMode(getIoMode(), is); int ioMode = fp::detectIoMode(getIoMode(), is);
std::string str; std::string str;
if (ioMode & (IoArray | IoArrayRaw)) { if (ioMode & (IoArray | IoArrayRaw)) {
str.resize(getBitSize() / 8); str.resize(getByteSize());
is.read(&str[0], str.size()); is.read(&str[0], str.size());
} else { } else {
is >> str; is >> str;

@ -796,9 +796,9 @@ struct Code : public mcl::Generator {
{ {
this->privateFuncList = &privateFuncList; this->privateFuncList = &privateFuncList;
gen_once(); gen_once();
uint32_t end = ((maxBitSize + unit - 1) / unit) * unit; uint32_t end = ((maxBitSize + unit - 1) / unit);
for (uint32_t i = 64; i <= end; i += unit) { for (uint32_t n = 1; n <= end; n++) {
setBit(i); setBit(n * unit);
gen_all(); gen_all();
gen_addsub(); gen_addsub();
gen_mul(); gen_mul();

@ -176,7 +176,7 @@ void ioModeTest()
} }
std::stringstream ss; std::stringstream ss;
ss << x; ss << x;
CYBOZU_TEST_EQUAL(ss.str().size(), Fp::getBitSize() / 8); CYBOZU_TEST_EQUAL(ss.str().size(), Fp::getByteSize());
Fp y; Fp y;
ss >> y; ss >> y;
CYBOZU_TEST_EQUAL(x, y); CYBOZU_TEST_EQUAL(x, y);

Loading…
Cancel
Save