Fix division of unlimited ints

This commit is contained in:
tovjemam 2025-12-04 13:36:04 +01:00
parent 8798ee646d
commit 8778898afb
3 changed files with 13 additions and 5 deletions

View File

@ -1,7 +1,5 @@
#pragma once
#include <limits>
#include "storage.hpp"
#include "utils.hpp"
@ -220,7 +218,6 @@ private:
template <size_t MaxBytes>
using Int = BasicInt<LongestElementSuitableType, MaxBytes>;
constexpr size_t UNLIMITED = std::numeric_limits<size_t>::max();
using UnlimitedInt = BasicInt<LongestElementSuitableType, UNLIMITED>;
} // namespace mp

View File

@ -351,13 +351,13 @@ struct Division
const LongerType MASK = BASE - 1;
// Prepare u (normalized dividend) length m + n + 1
BasicInt<ElementType, TLhs::MAX_BYTES + TRhs::MAX_BYTES + sizeof(ElementType)> u;
BasicInt<ElementType, calculate_sum_bytes(calculate_sum_bytes(TLhs::MAX_BYTES, TRhs::MAX_BYTES), sizeof(ElementType))> u;
u.zero();
for (size_t i = 0; i < lhs.size_elems(); ++i)
u.set(i, lhs.get(i));
// Prepare v (normalized divisor) length n
BasicInt<ElementType, TRhs::MAX_BYTES + sizeof(ElementType)> v;
BasicInt<ElementType, calculate_sum_bytes(TRhs::MAX_BYTES, sizeof(ElementType))> v;
v.zero();
for (size_t i = 0; i < n; ++i)
v.set(i, rhs.get(i));

View File

@ -3,6 +3,7 @@
#include <concepts>
#include <cstddef>
#include <cstdint>
#include <limits>
#include <type_traits>
#ifdef __SIZEOF_INT128__
@ -137,4 +138,14 @@ constexpr TElem calculate_last_elem_mask()
return mask;
}
constexpr size_t UNLIMITED = std::numeric_limits<size_t>::max();
constexpr size_t calculate_sum_bytes(size_t bytes_a, size_t bytes_b)
{
if (bytes_a == UNLIMITED || bytes_b == UNLIMITED)
return UNLIMITED;
return bytes_a + bytes_b;
}
} // namespace mp