From 9d2327edd4c3b03dbfc58e0bd1d1ae3eaa66a260 Mon Sep 17 00:00:00 2001 From: MITSUNARI Shigeo Date: Sat, 19 May 2018 10:32:27 +0900 Subject: [PATCH] conversion.hpp to include/mcl --- {src => include/mcl}/conversion.hpp | 24 ++++++++++++++++++++++-- include/mcl/fp.hpp | 5 +++-- include/mcl/op.hpp | 6 ------ sample/bench.cpp | 2 +- src/fp.cpp | 19 +------------------ test/base_test.cpp | 2 +- test/conversion_test.cpp | 2 +- test/fp_util_test.cpp | 2 +- 8 files changed, 30 insertions(+), 32 deletions(-) rename {src => include/mcl}/conversion.hpp (93%) diff --git a/src/conversion.hpp b/include/mcl/conversion.hpp similarity index 93% rename from src/conversion.hpp rename to include/mcl/conversion.hpp index 3c5a036..e4a4844 100644 --- a/src/conversion.hpp +++ b/include/mcl/conversion.hpp @@ -5,7 +5,7 @@ #include /** @file - @brief convertion from T[] to str2, str16 + @brief convertion bin/dec/hex <=> array @author MITSUNARI Shigeo(@herumi) @license modified new BSD license http://opensource.org/licenses/BSD-3-Clause @@ -320,4 +320,24 @@ inline size_t decToArray(UT *_x, size_t maxN, const char *buf, size_t bufSize) return xn / (sizeof(UT) / 4); } -} } // mcp::fp +/* + return retavl is written size if success else 0 + REMARK : the top of string is buf + bufSize - retval +*/ +template +size_t arrayToStr(char *buf, size_t bufSize, const UT *x, size_t n, int base, bool withPrefix) +{ + switch (base) { + case 0: + case 10: + return arrayToDec(buf, bufSize, x, n); + case 16: + return arrayToHex(buf, bufSize, x, n, withPrefix); + case 2: + return arrayToBin(buf, bufSize, x, n, withPrefix); + default: + return 0; + } +} + +} } // mcl::fp diff --git a/include/mcl/fp.hpp b/include/mcl/fp.hpp index d05f380..7fb076e 100644 --- a/include/mcl/fp.hpp +++ b/include/mcl/fp.hpp @@ -29,6 +29,7 @@ #include #include #include +#include namespace mcl { @@ -305,9 +306,9 @@ public: } fp::Block b; getBlock(b); - // use low 8-bit ioMode for Fp + // use low 8-bit ioMode for (base, withPrefix) char buf[2048]; - size_t len = fp::arrayToStr(buf, sizeof(buf), b.p, b.n, ioMode & 255); + size_t len = mcl::fp::arrayToStr(buf, sizeof(buf), b.p, b.n, ioMode & 31, (ioMode & IoPrefix) != 0); if (len == 0) { *pb = false; return; diff --git a/include/mcl/op.hpp b/include/mcl/op.hpp index ffc679d..dc116e1 100644 --- a/include/mcl/op.hpp +++ b/include/mcl/op.hpp @@ -329,12 +329,6 @@ private: */ size_t strToArray(bool *pIsMinus, Unit *x, size_t xN, const char *buf, size_t bufSize, int ioMode); -/* - return retavl is written size if success else 0 - REMARK : the top of string is buf + bufSize - retval -*/ -size_t arrayToStr(char *buf, size_t bufSize, const Unit *x, size_t n, int ioMode); - inline const char* getIoSeparator(int ioMode) { return (ioMode & (IoArray | IoArrayRaw | IoSerialize)) ? "" : " "; diff --git a/sample/bench.cpp b/sample/bench.cpp index 4e43020..0f865b1 100644 --- a/sample/bench.cpp +++ b/sample/bench.cpp @@ -2,7 +2,7 @@ #include #include #include -#include "../src/conversion.hpp" +#include #include typedef mcl::FpT<> Fp; diff --git a/src/fp.cpp b/src/fp.cpp index e305281..c9a0a47 100644 --- a/src/fp.cpp +++ b/src/fp.cpp @@ -6,7 +6,7 @@ #include #endif #include -#include "conversion.hpp" +#include #ifdef MCL_USE_XBYAK #include "fp_generator.hpp" #endif @@ -575,23 +575,6 @@ bool Op::init(const char *str, size_t maxBitSize, Mode mode, size_t mclMaxBitSiz return true; } -size_t arrayToStr(char *buf, size_t bufSize, const Unit *x, size_t n, int ioMode) -{ - int base = ioMode & ~IoPrefix; - bool withPrefix = (ioMode & IoPrefix) != 0; - switch (base) { - case 0: - case 10: - return mcl::fp::arrayToDec(buf, bufSize, x, n); - case 16: - return mcl::fp::arrayToHex(buf, bufSize, x, n, withPrefix); - case 2: - return mcl::fp::arrayToBin(buf, bufSize, x, n, withPrefix); - default: - return 0; - } -} - void copyUnitToByteAsLE(uint8_t *dst, const Unit *src, size_t byteSize) { while (byteSize >= sizeof(Unit)) { diff --git a/test/base_test.cpp b/test/base_test.cpp index 29a39ee..2733d17 100644 --- a/test/base_test.cpp +++ b/test/base_test.cpp @@ -5,7 +5,7 @@ #include #include #include -#include "../src/conversion.hpp" +#include #include #include "../src/fp_generator.hpp" diff --git a/test/conversion_test.cpp b/test/conversion_test.cpp index 41b4203..0f39907 100644 --- a/test/conversion_test.cpp +++ b/test/conversion_test.cpp @@ -1,5 +1,5 @@ #include -#include "../src/conversion.hpp" +#include CYBOZU_TEST_AUTO(arrayToDec) { diff --git a/test/fp_util_test.cpp b/test/fp_util_test.cpp index c516274..3e77b3b 100644 --- a/test/fp_util_test.cpp +++ b/test/fp_util_test.cpp @@ -1,6 +1,6 @@ #define PUT(x) std::cout << #x "=" << (x) << std::endl -#include "../src/conversion.hpp" #include +#include #include #include