From 6c5fda3e623b97d856f6316f1e89448cf5ad614a Mon Sep 17 00:00:00 2001 From: MITSUNARI Shigeo Date: Mon, 3 May 2021 15:13:21 +0900 Subject: [PATCH] refactor convertArrayAsLE --- include/mcl/conversion.hpp | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/include/mcl/conversion.hpp b/include/mcl/conversion.hpp index 99f5100..6e5091c 100644 --- a/include/mcl/conversion.hpp +++ b/include/mcl/conversion.hpp @@ -30,22 +30,25 @@ bool convertArrayAsLE(D *dst, size_t dstN, const S *src, size_t srcN) (void)assert_S_is_unsigned; if (sizeof(D) * dstN < sizeof(S) * srcN) return false; size_t pos = 0; - size_t i = 0; - while (i < dstN) { - if (sizeof(D) < sizeof(S)) { - S s = (pos < srcN) ? src[pos++] : 0; + if (sizeof(D) < sizeof(S)) { + for (size_t i = 0; i < srcN; i++) { + S s = src[i]; for (size_t j = 0; j < sizeof(S); j += sizeof(D)) { - assert(i < dstN); - dst[i++] = D(s); + dst[pos++] = D(s); s >>= sizeof(D) * 8; } - } else { + } + for (; pos < dstN; pos++) { + dst[pos] = 0; + } + } else { + for (size_t i = 0; i < dstN; i++) { D u = 0; for (size_t j = 0; j < sizeof(D); j += sizeof(S)) { S s = (pos < srcN) ? src[pos++] : 0; u |= D(s) << (j * 8); } - dst[i++] = u; + dst[i] = u; } } return true;