Expand description
Exact division of a Wide / WideU value, in 32-bit instructions only.
crate::wide leaves division out on purpose: i64 / i64 links the
compiler’s __divdi3 / u64_div_rem, which the guest symbol gate refuses.
A game that really has a wide numerator (a dot product over a Q12 length, a
cross product against a cross product, a tick count over a rate) divides it
here instead. Nothing below names a 64-bit integer type: every operation is
built from WideU::mul (multu), 64-bit add / subtract / compare / shift
and 32-bit divu, so it links no helper. The operations are exact; none
estimates or drops low bits.
§The operations
| operation | divisor | result |
|---|---|---|
WideU::div_rem_u32 | u32 | full 64-bit quotient and the remainder |
WideU::quotient | WideU | floor(n / d), clamped to u32 |
Wide::div_i32_wrap | i32 | quotient (a chosen Rounding) as the low 32 bits |
Wide::div_i32_sat | i32 | the same, clamped to the i32 range |
Wide::div_wide_sat | Wide | the same for a wide divisor |
WideU::mul_full | WideU | the exact 128-bit product, as two words |
WideU::cmp_ratio | order of a / b against c / d, no divide | |
WideU::scale_by_ratio | WideU | floor(a * t / den) for t <= den |
Wide::div_pow2 | 2^shift | quotient truncated toward zero, still wide |
A divisor of zero panics, like /.
§Cost
Measured on the emulator by sdk/examples/hello-wide-div (cycles per call,
random operands of every magnitude, call and return included). The table is
in that example’s header and in docs; the shape is: a numerator that fits
32 bits is one native divu; otherwise a high-word divu plus the two-digit
crate::int32::div_u64_by_u32; a divisor wider than 32 bits adds one
div_u64_by_u32 for the estimate and a 96-bit product and compare to settle
it. The division bodies are #[inline(never)]: one shared copy per image,
because an inlined copy is several hundred bytes.
Enums§
- Rounding
- How an inexact quotient is rounded to an integer.