Skip to main content
← PSoXide SDK guides and examples · SDK 80d1c329e977 · All Cargo features enabled; check feature gates before use.

Module wide_div

Module wide_div 

Source
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

operationdivisorresult
WideU::div_rem_u32u32full 64-bit quotient and the remainder
WideU::quotientWideUfloor(n / d), clamped to u32
Wide::div_i32_wrapi32quotient (a chosen Rounding) as the low 32 bits
Wide::div_i32_sati32the same, clamped to the i32 range
Wide::div_wide_satWidethe same for a wide divisor
WideU::mul_fullWideUthe exact 128-bit product, as two words
WideU::cmp_ratioorder of a / b against c / d, no divide
WideU::scale_by_ratioWideUfloor(a * t / den) for t <= den
Wide::div_pow22^shiftquotient 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.