From 189da7cee3f5a93612ebf89f85b4631b21f5e84b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Bylica?= Date: Wed, 17 Jun 2020 16:25:35 +0200 Subject: [PATCH 1/3] Move normalize() into intx::internal namespace --- lib/intx/div.cpp | 2 +- lib/intx/div.hpp | 3 +++ test/benchmarks/bench_div.cpp | 4 ++-- test/unittests/test_div.cpp | 8 ++++---- 4 files changed, 10 insertions(+), 7 deletions(-) diff --git a/lib/intx/div.cpp b/lib/intx/div.cpp index 6dddfec9..b45fac9d 100644 --- a/lib/intx/div.cpp +++ b/lib/intx/div.cpp @@ -134,7 +134,7 @@ void udivrem_knuth(uint64_t q[], uint64_t u[], int ulen, const uint64_t d[], int template div_result> udivrem(const uint& u, const uint& v) noexcept { - auto na = normalize(u, v); + auto na = internal::normalize(u, v); if (na.num_numerator_words <= na.num_divisor_words) return {0, u}; diff --git a/lib/intx/div.hpp b/lib/intx/div.hpp index 2cdb2291..b82e19e6 100644 --- a/lib/intx/div.hpp +++ b/lib/intx/div.hpp @@ -8,6 +8,8 @@ namespace intx { +namespace internal +{ template struct normalized_div_args { @@ -67,4 +69,5 @@ template return na; } +} // namespace internal } // namespace intx diff --git a/test/benchmarks/bench_div.cpp b/test/benchmarks/bench_div.cpp index f00bb0fa..e8715906 100644 --- a/test/benchmarks/bench_div.cpp +++ b/test/benchmarks/bench_div.cpp @@ -27,7 +27,7 @@ inline uint64_t udiv_by_reciprocal(uint64_t uu, uint64_t du) noexcept } -template ) NormalizeFn> +template ) NormalizeFn> static void div_normalize(benchmark::State& state) { auto u = uint512{{48882153453, 100324254353}, {4343242153453, 1324254353}}; @@ -40,7 +40,7 @@ static void div_normalize(benchmark::State& state) benchmark::DoNotOptimize(x); } } -BENCHMARK_TEMPLATE(div_normalize, normalize); +BENCHMARK_TEMPLATE(div_normalize, internal::normalize); constexpr uint64_t neg(uint64_t x) noexcept { diff --git a/test/unittests/test_div.cpp b/test/unittests/test_div.cpp index 88933d92..c3d1337e 100644 --- a/test/unittests/test_div.cpp +++ b/test/unittests/test_div.cpp @@ -13,7 +13,7 @@ TEST(div, normalize) { uint512 u; uint512 v = 1; - auto na = normalize(u, v); + auto na = internal::normalize(u, v); EXPECT_EQ(na.shift, 63u); EXPECT_EQ(na.num_divisor_words, 1); EXPECT_EQ(na.num_numerator_words, 0); @@ -23,7 +23,7 @@ TEST(div, normalize) u = uint512{1414, 1313}; v = uint512{12, 1212}; - na = normalize(u, v); + na = internal::normalize(u, v); EXPECT_EQ(na.shift, 60u); EXPECT_EQ(na.num_divisor_words, 5); EXPECT_EQ(na.num_numerator_words, 6); @@ -33,7 +33,7 @@ TEST(div, normalize) u = uint512{3} << 510; v = uint256{0xffffffffffffffff, 1}; - na = normalize(u, v); + na = internal::normalize(u, v); EXPECT_EQ(na.shift, 0u); EXPECT_EQ(na.num_divisor_words, 3); EXPECT_EQ(na.num_numerator_words, 8); @@ -43,7 +43,7 @@ TEST(div, normalize) u = uint512{7} << 509; v = uint256{0x3fffffffffffffff, 1}; - na = normalize(u, v); + na = internal::normalize(u, v); EXPECT_EQ(na.shift, 2u); EXPECT_EQ(na.num_divisor_words, 3); EXPECT_EQ(na.num_numerator_words, 9); From b0faa729bdcbf67904bbf568142821987ad143cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Bylica?= Date: Wed, 17 Jun 2020 22:08:23 +0200 Subject: [PATCH 2/3] Move division impl. into intx::internal namespace --- lib/intx/div.cpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/intx/div.cpp b/lib/intx/div.cpp index b45fac9d..c864a9e2 100644 --- a/lib/intx/div.cpp +++ b/lib/intx/div.cpp @@ -7,7 +7,7 @@ namespace intx { -namespace +namespace internal { /// Divides arbitrary long unsigned integer by 64-bit unsigned integer (1 word). /// @param u The array of a normalized numerator words. It will contain @@ -129,7 +129,7 @@ void udivrem_knuth(uint64_t q[], uint64_t u[], int ulen, const uint64_t d[], int } } -} // namespace +} // namespace internal template div_result> udivrem(const uint& u, const uint& v) noexcept @@ -141,23 +141,23 @@ div_result> udivrem(const uint& u, const uint& v) noexcept if (na.num_divisor_words == 1) { - auto r = - udivrem_by1(as_words(na.numerator), na.num_numerator_words, as_words(na.divisor)[0]); + const auto r = internal::udivrem_by1( + as_words(na.numerator), na.num_numerator_words, as_words(na.divisor)[0]); return {na.numerator, r >> na.shift}; } if (na.num_divisor_words == 2) { - auto d = as_words(na.divisor); - auto r = udivrem_by2(as_words(na.numerator), na.num_numerator_words, {d[1], d[0]}); + const auto d = as_words(na.divisor); + const auto r = + internal::udivrem_by2(as_words(na.numerator), na.num_numerator_words, {d[1], d[0]}); return {na.numerator, r >> na.shift}; } auto un = as_words(na.numerator); // Will be modified. uint q; - - udivrem_knuth( + internal::udivrem_knuth( as_words(q), &un[0], na.num_numerator_words, as_words(na.divisor), na.num_divisor_words); uint r; From d8210d3a4fe2a1f4b0f266ace45c87038c3426d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Bylica?= Date: Wed, 17 Jun 2020 22:09:35 +0200 Subject: [PATCH 3/3] Make udivrem_knuth() inline --- lib/intx/div.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/intx/div.cpp b/lib/intx/div.cpp index c864a9e2..be892e3e 100644 --- a/lib/intx/div.cpp +++ b/lib/intx/div.cpp @@ -88,7 +88,8 @@ inline uint64_t submul( return borrow; } -void udivrem_knuth(uint64_t q[], uint64_t u[], int ulen, const uint64_t d[], int dlen) noexcept +inline void udivrem_knuth( + uint64_t q[], uint64_t u[], int ulen, const uint64_t d[], int dlen) noexcept { INTX_REQUIRE(dlen >= 3); INTX_REQUIRE(ulen >= dlen);