30 template<std::
integral T>
45 template<
class T,
class D>
46 concept kdivisible = std::integral<T> && std::integral<D> && (std::is_signed_v<T> == std::is_signed_v<D>);
58#if defined(IGROS_ARCH_i386)
60 if (!std::is_constant_evaluated() && (0_u64 != divisor) && (0_u64 == (divisor >> 32))) {
64 const auto quotientHigh {high / divisor32};
65 auto remainder {high % divisor32};
70 :
"=a"(quotientLow),
"+d"(remainder)
71 :
"a"(low), [divisor]
"rm"(divisor32)
75 .quotient = (
static_cast<igros_quad_t>(quotientHigh) << 32) | quotientLow,
76 .remainder = remainder,
82 if ((0_u64 == divisor) || (divisor > dividend)) {
87 const auto shift {
static_cast<igros_dword_t>(std::countl_zero(divisor) - std::countl_zero(dividend))};
88 auto aligned {divisor << shift};
91 for (
auto bit {0_u32}; bit <= shift; ++bit) {
93 if (res.remainder >= aligned) {
94 res.remainder -= aligned;
95 res.quotient |= 1_u64;
112 template<std::
integral T, std::
integral D>
113 requires kdivisible<T, D>
120 static_cast<T
>(dividend / divisor),
121 static_cast<T
>(dividend % divisor),
125 }
else if constexpr (std::is_unsigned_v<T>) {
126 const auto res {
kudivmod64(dividend, divisor)};
128 static_cast<T
>(res.quotient),
129 static_cast<T
>(res.remainder),
134 const auto absDividend {(dividend < 0) ? (0_u64 -
static_cast<igros_quad_t>(dividend)) :
static_cast<igros_quad_t>(dividend)};
135 const auto absDivisor {(divisor < 0) ? (0_u64 -
static_cast<igros_quad_t>(divisor)) :
static_cast<igros_quad_t>(divisor)};
136 const auto res {
kudivmod64(absDividend, absDivisor)};
138 const auto quotient {((dividend < 0) != (divisor < 0)) ? (0_u64 - res.quotient) : res.quotient};
139 const auto remainder {(dividend < 0) ? (0_u64 - res.remainder) : res.remainder};
141 static_cast<T
>(quotient),
142 static_cast<T
>(remainder),
Integers of the same signedness (mixed ones convert signed to unsigned silently).
std::size_t igros_usize_t
Unsigned size.
std::uint32_t igros_dword_t
32-bit unsigned integer
std::uint64_t igros_quad_t
64-bit unsigned integer
Freestanding kernel library: printing, math, alignment, flags, strings.
constexpr auto kdivmod(const T dividend, const D divisor) noexcept -> divmod_t< T >
Divide integer by integer of the same signedness.
constexpr auto kudivmod64(const igros_quad_t dividend, const igros_quad_t divisor) noexcept -> divmod_t< igros_quad_t >
64-bit unsigned division for 32-bit CPU (no 64-bit "/" and "%" without libgcc __udivdi3/__umoddi3)
Combined division & modulo result.
T remainder
Remainder (sign of dividend).
constexpr auto operator==(const divmod_t &other) const noexcept -> bool=default
Comparison.
T quotient
Quotient (truncated towards zero).
Common C++ types redefinition.