From 6131a6e51bae7cfa005d2b6ab62ffb372157ffca Mon Sep 17 00:00:00 2001 From: Not_Leonian <75620009+NotLeonian@users.noreply.github.com> Date: Sat, 11 Jul 2026 11:47:30 +0900 Subject: [PATCH 1/2] =?UTF-8?q?=E3=80=8C128=20bit=20=E6=95=B4=E6=95=B0?= =?UTF-8?q?=E5=9E=8B=E3=80=8D=E3=81=AE=E5=86=85=E9=83=A8=E5=AE=9F=E8=A3=85?= =?UTF-8?q?=E3=82=92=E5=A4=89=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/int128.hpp | 24 +++++++++++++++++------- verify/standalone-int128.test.cpp | 30 ++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 7 deletions(-) diff --git a/internal/int128.hpp b/internal/int128.hpp index 52e0a60..f42950c 100644 --- a/internal/int128.hpp +++ b/internal/int128.hpp @@ -133,7 +133,22 @@ class UInt128 { assert(rhs != UInt128{}); UInt128 q, r; - div_mod_32bit_words(lhs, rhs, q, r); + if (rhs.high_ == 0) { + if (lhs.high_ == 0) { + q = UInt128(lhs.low_ / rhs.low_); + r = UInt128(lhs.low_ % rhs.low_); + } else if (rhs.low_ == 1) { + q = lhs; + r = UInt128{}; + } else { + div_mod_32bit_words(lhs, rhs, q, r); + } + } else if (lhs < rhs) { + q = UInt128{}; + r = lhs; + } else { + div_mod_32bit_words(lhs, rhs, q, r); + } quotient = q; remainder = r; } @@ -336,6 +351,7 @@ class UInt128 { } } + // 呼び出し元で lhs >= rhs が保証されていることを仮定する。 static constexpr void div_mod_32bit_words(UInt128 lhs, UInt128 rhs, UInt128 "ient, UInt128 &remainder) { @@ -348,12 +364,6 @@ class UInt128 { const int rhs_length = word_length(rhs_words); assert(rhs_length > 0); - if (lhs_length < rhs_length || lhs < rhs) { - quotient = UInt128{}; - remainder = lhs; - return; - } - if (rhs_length == 1) { std::uint32_t rem = 0; quotient = div_mod_uint32(lhs, rhs_words[0], rem); diff --git a/verify/standalone-int128.test.cpp b/verify/standalone-int128.test.cpp index 2704d03..3ca699d 100644 --- a/verify/standalone-int128.test.cpp +++ b/verify/standalone-int128.test.cpp @@ -115,12 +115,42 @@ void test_signed_division(Int128 lhs, Int128 rhs) { assert(remainder.is_negative() == lhs.is_negative()); } } +constexpr bool test_uint128_div_mod_fast_paths() { + UInt128 quotient; + UInt128 remainder; + + UInt128::div_mod( + UInt128::from_words(0, ~std::uint64_t{}), + UInt128::from_words(0, (std::uint64_t{1} << 63) + 1), quotient, + remainder); + if (quotient != UInt128(1) || + remainder != + UInt128::from_words(0, (std::uint64_t{1} << 63) - 2)) { + return false; + } + + const UInt128 wide = UInt128::from_words( + 0x123456789abcdef0ULL, 0xfedcba9876543210ULL); + UInt128::div_mod(wide, UInt128(1), quotient, remainder); + if (quotient != wide || remainder != UInt128{}) { + return false; + } + + const UInt128 smaller = UInt128::from_words(1, ~std::uint64_t{}); + const UInt128 larger = UInt128::from_words(2, 0); + UInt128::div_mod(smaller, larger, quotient, remainder); + return quotient == UInt128{} && remainder == smaller; +} + } // namespace int main() { using NicheLibrary::Int128; using NicheLibrary::UInt128; + static_assert(test_uint128_div_mod_fast_paths(), + "UInt128 division fast paths must be constexpr-correct."); + static_assert(std::numeric_limits::is_integer, "UInt128 must be integer."); static_assert(!std::numeric_limits::is_signed, From 8ecf4639a759b3b22784aa9c2aed5991e1bae0f7 Mon Sep 17 00:00:00 2001 From: Not_Leonian <75620009+NotLeonian@users.noreply.github.com> Date: Sat, 11 Jul 2026 11:58:48 +0900 Subject: [PATCH 2/2] =?UTF-8?q?=E3=83=95=E3=82=A9=E3=83=BC=E3=83=9E?= =?UTF-8?q?=E3=83=83=E3=83=88=E3=81=A8=E3=80=81STYLE=5FGUIDE.md=20?= =?UTF-8?q?=E3=81=B8=E3=81=AE=E8=BF=BD=E8=A8=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- STYLE_GUIDE.md | 2 +- verify/standalone-int128.test.cpp | 15 ++++++--------- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/STYLE_GUIDE.md b/STYLE_GUIDE.md index 24b6cf1..2bfb14d 100644 --- a/STYLE_GUIDE.md +++ b/STYLE_GUIDE.md @@ -73,7 +73,7 @@ - g++ および clang++ の C++20(`-std=c++20`)でコンパイルできるようにする。 - コンパイラ依存の実装にしない。 - 前提を満たさない入力に対する救済処理を過度に入れない。 - - 必要な前提は `assert` などで確認する。 + - 必要な前提は `assert` などで確認する。ただし、`private` な関数について、全ての呼び出し箇所でその関数に必要な前提が満たされることを保証する場合は、`assert` などによる確認を省略してもよい。 - 前提を満たす入力に対する性能を著しく落とすような救済処理を入れない。 - 公開 API の名前は VS Code 等による補完を使用しやすい名前にする。 - 用途から思い出しにくい名前や、短すぎる名前にしない。 diff --git a/verify/standalone-int128.test.cpp b/verify/standalone-int128.test.cpp index 3ca699d..5564a0e 100644 --- a/verify/standalone-int128.test.cpp +++ b/verify/standalone-int128.test.cpp @@ -119,18 +119,16 @@ constexpr bool test_uint128_div_mod_fast_paths() { UInt128 quotient; UInt128 remainder; - UInt128::div_mod( - UInt128::from_words(0, ~std::uint64_t{}), - UInt128::from_words(0, (std::uint64_t{1} << 63) + 1), quotient, - remainder); + UInt128::div_mod(UInt128::from_words(0, ~std::uint64_t{}), + UInt128::from_words(0, (std::uint64_t{1} << 63) + 1), + quotient, remainder); if (quotient != UInt128(1) || - remainder != - UInt128::from_words(0, (std::uint64_t{1} << 63) - 2)) { + remainder != UInt128::from_words(0, (std::uint64_t{1} << 63) - 2)) { return false; } - const UInt128 wide = UInt128::from_words( - 0x123456789abcdef0ULL, 0xfedcba9876543210ULL); + const UInt128 wide = + UInt128::from_words(0x123456789abcdef0ULL, 0xfedcba9876543210ULL); UInt128::div_mod(wide, UInt128(1), quotient, remainder); if (quotient != wide || remainder != UInt128{}) { return false; @@ -141,7 +139,6 @@ constexpr bool test_uint128_div_mod_fast_paths() { UInt128::div_mod(smaller, larger, quotient, remainder); return quotient == UInt128{} && remainder == smaller; } - } // namespace int main() {