From 215106e9e6365fbc3ccd98ea89fc583510843885 Mon Sep 17 00:00:00 2001 From: MITSUNARI Shigeo Date: Fri, 5 Feb 2021 17:00:47 +0900 Subject: [PATCH] replace bool to unit32_t --- misc/low_test.cpp | 14 ++++++++------ src/low_funct.hpp | 40 ++++++++++++++++++++-------------------- 2 files changed, 28 insertions(+), 26 deletions(-) diff --git a/misc/low_test.cpp b/misc/low_test.cpp index 91af412..06418f9 100644 --- a/misc/low_test.cpp +++ b/misc/low_test.cpp @@ -21,6 +21,8 @@ void dump(const char *msg, const uint32_t *x, size_t n) #include #include +const int C = 10000; + template void setRand(uint32_t *x, size_t n, RG& rg) { @@ -61,8 +63,8 @@ void mulTest() mcl::karatsubaT(z, x, y); CYBOZU_TEST_EQUAL_ARRAY(z, vx.getUnit(), N * 2); } - CYBOZU_BENCH_C("mulT", 10000, mcl::mulT, z, x, y); - CYBOZU_BENCH_C("kara", 10000, mcl::karatsubaT, z, x, y); + CYBOZU_BENCH_C("mulT", C, mcl::mulT, z, x, y); + CYBOZU_BENCH_C("kara", C, mcl::karatsubaT, z, x, y); } CYBOZU_TEST_AUTO(mulT) @@ -88,7 +90,7 @@ void sqrTest() mcl::sqrT(y, x); CYBOZU_TEST_EQUAL_ARRAY(y, vx.getUnit(), N * 2); } - CYBOZU_BENCH_C("sqrT", 10000, mcl::sqrT, y, x); + CYBOZU_BENCH_C("sqrT", C, mcl::sqrT, y, x); } CYBOZU_TEST_AUTO(sqrT) @@ -185,8 +187,8 @@ void mulMontTest(const char *pStr) mcl::sqrMontT(z, x, p); CYBOZU_TEST_EQUAL_ARRAY(z, vz.getUnit(), N); } - CYBOZU_BENCH_C("mulMontT", 10000, mcl::mulMontT, x, x, y, p); - CYBOZU_BENCH_C("sqrMontT", 10000, mcl::sqrMontT, x, x, p); + CYBOZU_BENCH_C("mulMontT", C, mcl::mulMontT, x, x, y, p); + CYBOZU_BENCH_C("sqrMontT", C, mcl::sqrMontT, x, x, p); } template @@ -214,7 +216,7 @@ void modTest(const char *pStr) mcl::modT(z, xy, p); CYBOZU_TEST_EQUAL_ARRAY(z, vz.getUnit(), N); } - CYBOZU_BENCH_C("modT", 10000, mcl::modT, z, xy, p); + CYBOZU_BENCH_C("modT", C, mcl::modT, z, xy, p); } CYBOZU_TEST_AUTO(mont) diff --git a/src/low_funct.hpp b/src/low_funct.hpp index 082c2df..885b16a 100644 --- a/src/low_funct.hpp +++ b/src/low_funct.hpp @@ -41,40 +41,40 @@ uint32_t shlT(uint32_t y[N], const uint32_t x[N], size_t bit) // [return:y[N]] += x template -inline bool addUnitT(uint32_t y[N], uint32_t x) +inline uint32_t addUnitT(uint32_t y[N], uint32_t x) { uint64_t v = uint64_t(y[0]) + x; y[0] = uint32_t(v); - bool c = (v >> 32) != 0; - if (!c) return false; + uint32_t c = v >> 32; + if (c == 0) return 0; for (size_t i = 1; i < N; i++) { v = uint64_t(y[i]) + 1; y[i] = uint32_t(v); - if ((v >> 32) == 0) return false; + if ((v >> 32) == 0) return 0; } - return true; + return 1; } template -bool addT(uint32_t z[N], const uint32_t x[N], const uint32_t y[N]) +uint32_t addT(uint32_t z[N], const uint32_t x[N], const uint32_t y[N]) { - bool c = false; + uint32_t c = 0; for (size_t i = 0; i < N; i++) { uint64_t v = uint64_t(x[i]) + y[i] + c; z[i] = uint32_t(v); - c = (v >> 32) != 0; + c = uint32_t(v >> 32); } return c; } template -bool subT(uint32_t z[N], const uint32_t x[N], const uint32_t y[N]) +uint32_t subT(uint32_t z[N], const uint32_t x[N], const uint32_t y[N]) { - bool c = false; + uint32_t c = 0; for (size_t i = 0; i < N; i++) { uint64_t v = uint64_t(x[i]) - y[i] - c; z[i] = uint32_t(v); - c = (v >> 32) != 0; + c = uint32_t(v >> 63); } return c; } @@ -187,8 +187,8 @@ void karatsubaT(uint32_t z[N * 2], const uint32_t x[N], const uint32_t y[N]) const size_t H = N / 2; uint32_t a_b[H]; uint32_t c_d[H]; - bool c1 = addT(a_b, x, x + H); // a + b - bool c2 = addT(c_d, y, y + H); // c + d + uint32_t c1 = addT(a_b, x, x + H); // a + b + uint32_t c2 = addT(c_d, y, y + H); // c + d uint32_t tmp[N]; mulT(tmp, a_b, c_d); if (c1) { @@ -220,11 +220,10 @@ void sqrT(uint32_t y[N * 2], const uint32_t x[N]) assert((x[N - 1] & 0x80000000) == 0); const size_t H = N / 2; uint32_t a_b[H]; - bool c = addT(a_b, x, x + H); // a + b + uint32_t c = addT(a_b, x, x + H); // a + b uint32_t tmp[N]; mulT(tmp, a_b, a_b); if (c) { -// addT(a_b, a_b, a_b); shlT(a_b, a_b, 1); addT(tmp + H, tmp + H, a_b); } @@ -244,7 +243,7 @@ void addModT(uint32_t z[N], const uint32_t x[N], const uint32_t y[N], const uint { uint32_t t[N]; addT(z, x, y); - bool c = subT(t, z, p); + uint32_t c = subT(t, z, p); if (!c) { copyT(z, t); } @@ -253,7 +252,7 @@ void addModT(uint32_t z[N], const uint32_t x[N], const uint32_t y[N], const uint template void subModT(uint32_t z[N], const uint32_t x[N], const uint32_t y[N], const uint32_t p[N]) { - bool c = subT(z, x, y); + uint32_t c = subT(z, x, y); if (c) { addT(z, z, p); } @@ -284,7 +283,7 @@ void mulMontT(uint32_t z[N], const uint32_t x[N], const uint32_t y[N], const uin // [return:z[N+1]] = z[N+1] + x[N] * y + (cc << (N * 32)) template -bool addMulUnit2T(uint32_t z[N + 1], const uint32_t x[N], uint32_t y, const bool *cc = 0) +uint32_t addMulUnit2T(uint32_t z[N + 1], const uint32_t x[N], uint32_t y, const uint32_t *cc = 0) { uint32_t H = 0; for (size_t i = 0; i < N; i++) { @@ -298,7 +297,7 @@ bool addMulUnit2T(uint32_t z[N + 1], const uint32_t x[N], uint32_t y, const bool uint64_t v = uint64_t(z[N]); v += H; z[N] = uint32_t(v); - return (v >> 32) != 0; + return uint32_t(v >> 32); } /* @@ -312,7 +311,7 @@ void modT(uint32_t y[N], const uint32_t xy[N * 2], const uint32_t p[N]) assert((p[N - 1] & 0x80000000) == 0); uint32_t buf[N * 2]; copyT(buf, xy); - bool c = 0; + uint32_t c = 0; for (size_t i = 0; i < N; i++) { uint32_t q = buf[i] * rp; c = addMulUnit2T(buf + i, p, q, &c); @@ -332,6 +331,7 @@ void sqrMontT(uint32_t y[N], const uint32_t x[N], const uint32_t p[N]) #if 1 mulMontT(y, x, x, p); #else + // slower uint32_t xx[N * 2]; sqrT(xx, x); modT(y, xx, p);