From 4f67fd2b93eaebb48a191d78a1751bd7a0ff605a Mon Sep 17 00:00:00 2001 From: Aleksey Selihov <1+faumaray@noreply@void.com> Date: Thu, 3 Dec 2026 06:53:17 +0000 Subject: [PATCH 1/2] feat(Math): checked Euclidean division for signed grid coordinates (#11) --- include/Void/Math/Numeric.hpp | 41 +++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/include/Void/Math/Numeric.hpp b/include/Void/Math/Numeric.hpp index 0c67480..bb03e60 100644 --- a/include/Void/Math/Numeric.hpp +++ b/include/Void/Math/Numeric.hpp @@ -298,6 +298,47 @@ template return a / b; } +/// Euclidean quotient and remainder for a strictly positive divisor: +/// `a == quotient * divisor + remainder` and `0 <= remainder < divisor`. +template struct EuclideanDivision { + T quotient{}; + T remainder{}; + + [[nodiscard]] constexpr bool + operator==(const EuclideanDivision&) const noexcept = default; +}; + +/// Checked Euclidean division. Unlike C++ integer division (which truncates +/// toward zero), this floors toward negative infinity so the remainder is +/// always nonnegative, e.g. (-1, 32) -> (-1, 31). +/// +/// `divisor == 0` reports `DivisionByZero`; a negative divisor reports +/// `InvalidInput`. The signed minimum is handled without negating it, so no +/// intermediate overflows. +template +[[nodiscard]] constexpr std::expected, Error> + checked_euclidean_div(T a, T divisor) noexcept +{ + if (divisor == T{0}) { + return std::unexpected{Error::DivisionByZero}; + } + if constexpr (std::is_signed_v) { + if (divisor < T{0}) { + return std::unexpected{Error::InvalidInput}; + } + } + T quotient = static_cast(a / divisor); + T remainder = static_cast(a % divisor); + if constexpr (std::is_signed_v) { + if (remainder < T{0}) { + // Truncated division rounded toward zero: step one page down. + quotient = static_cast(quotient - T{1}); + remainder = static_cast(remainder + divisor); + } + } + return EuclideanDivision{.quotient = quotient, .remainder = remainder}; +} + template [[nodiscard]] constexpr std::expected checked_neg(T a) noexcept From f100b4c89ea2b76687707012012b0f15356457cc Mon Sep 17 00:00:00 2001 From: Aleksey Selihov <1+faumaray@noreply@void.com> Date: Thu, 3 Dec 2026 06:53:21 +0000 Subject: [PATCH 2/2] feat(Math): checked Euclidean division for signed grid coordinates (#11) --- tests/standalone/Math/Numeric.cpp | 116 ++++++++++++++++++++++++++++++ 1 file changed, 116 insertions(+) diff --git a/tests/standalone/Math/Numeric.cpp b/tests/standalone/Math/Numeric.cpp index 2b3fefa..d8c6efb 100644 --- a/tests/standalone/Math/Numeric.cpp +++ b/tests/standalone/Math/Numeric.cpp @@ -505,3 +505,119 @@ Void::Testing::Test portable_overflow_helpers_boundary_cases() Void::Testing::expect(portable_mul_overflow( std::numeric_limits::max(), 2ULL, &u64)); } + +// ── checked_euclidean_div ──────────────────────────────────────────────────── + +Void::Testing::Test checked_euclidean_div_examples_width_32() +{ + using Void::Numeric::checked_euclidean_div; + const auto check = [](std::int32_t a, std::int32_t q, std::int32_t r) { + const auto result = checked_euclidean_div(a, std::int32_t{32}); + Void::Testing::require(result.has_value()); + Void::Testing::expect_eq(result->quotient, q); + Void::Testing::expect_eq(result->remainder, r); + }; + + check(-33, -2, 31); + check(-32, -1, 0); + check(-1, -1, 31); + check(0, 0, 0); + check(31, 0, 31); + check(32, 1, 0); + check(33, 1, 1); + + // The existing truncating contract is intentionally unchanged. + Void::Testing::expect_eq(*Void::Numeric::checked_div(-1, 32), 0); +} + +Void::Testing::Test checked_euclidean_div_is_constexpr() +{ + using Void::Numeric::checked_euclidean_div; + static_assert(checked_euclidean_div(-1, 32).value().quotient == -1); + static_assert(checked_euclidean_div(-1, 32).value().remainder == 31); + static_assert(checked_euclidean_div(-33, 32).value().quotient == -2); + static_assert(checked_euclidean_div(31, 32).value().quotient == 0); + static_assert(checked_euclidean_div(32, 32).value().remainder == 0); + Void::Testing::expect(true); +} + +Void::Testing::Test checked_euclidean_div_exhaustive_int8() +{ + bool ok = true; + for (int a = -128; a <= 127 && ok; ++a) { + for (int d = 1; d <= 127 && ok; ++d) { + const auto result = Void::Numeric::checked_euclidean_div( + static_cast(a), static_cast(d)); + ok = result.has_value(); + if (ok) { + const int q = result->quotient; + const int r = result->remainder; + ok = r >= 0 && r < d && q * d + r == a; + } + } + } + Void::Testing::expect(ok); +} + +Void::Testing::Test checked_euclidean_div_extremes() +{ + using Void::Numeric::checked_euclidean_div; + const std::int32_t divisors[] = {1, 3, 32}; + bool ok = true; + for (const auto d : divisors) { + const std::int32_t values[] = { + std::numeric_limits::min(), + std::numeric_limits::max()}; + for (const auto a : values) { + const auto result = checked_euclidean_div(a, d); + ok = ok && result.has_value(); + if (ok) { + const std::int64_t q = result->quotient; + const std::int64_t r = result->remainder; + ok = r >= 0 && r < d && q * d + r == a; + } + } + } + Void::Testing::expect(ok); + + const auto min32 = checked_euclidean_div( + std::numeric_limits::min(), std::int64_t{32}); + Void::Testing::require(min32.has_value()); + Void::Testing::expect_eq(min32->quotient, + std::int64_t{-288230376151711744}); + Void::Testing::expect_eq(min32->remainder, std::int64_t{0}); + + const auto min3 = checked_euclidean_div( + std::numeric_limits::min(), std::int64_t{3}); + Void::Testing::require(min3.has_value()); + Void::Testing::expect_eq(min3->quotient, + std::int64_t{-3074457345618258603}); + Void::Testing::expect_eq(min3->remainder, std::int64_t{1}); + + const auto max32 = checked_euclidean_div( + std::numeric_limits::max(), std::int64_t{32}); + Void::Testing::require(max32.has_value()); + Void::Testing::expect_eq(max32->quotient, + std::int64_t{288230376151711743}); + Void::Testing::expect_eq(max32->remainder, std::int64_t{31}); + + const auto max3 = checked_euclidean_div( + std::numeric_limits::max(), std::int64_t{3}); + Void::Testing::require(max3.has_value()); + Void::Testing::expect_eq(max3->quotient, + std::int64_t{3074457345618258602}); + Void::Testing::expect_eq(max3->remainder, std::int64_t{1}); +} + +Void::Testing::Test checked_euclidean_div_invalid_width() +{ + using Void::Numeric::checked_euclidean_div; + using Void::Numeric::Error; + const auto zero = checked_euclidean_div(5, 0); + Void::Testing::require(!zero.has_value()); + Void::Testing::expect(zero.error() == Error::DivisionByZero); + + const auto negative = checked_euclidean_div(5, -1); + Void::Testing::require(!negative.has_value()); + Void::Testing::expect(negative.error() == Error::InvalidInput); +}