Expand description
Saturating 32-bit integer scalar helpers.
The PS1 has no FPU and the engine keeps all gameplay math in
i32/i16 fixed-point, so the same small scalar helpers (absolute
value that cannot overflow at MIN, a clamp into the GTE’s i16
vertex range, an integer square root for vector lengths) get
re-implemented next to every consumer. This module owns them
once. Every function is branch-light, allocation-free, and safe
on the full input domain:
abs_*saturateMINtoMAXinstead of overflowing.square_i32_saturatingreturnsi32::MAXonce the true square would exceed i32.isqrt_i32is the classic digit-by-digit (binary restoring) integer square root: exact floor(sqrt(n)), no multiplies.mul_q12_i32multiplies an i32 by a Q1.12 factor without a 64-bit intermediate, splitting whole and fractional parts so the products stay inside i32.mul_div_i32,mul_shr_i32andmul_shr_trunc_i32are the signeda * b / cforms games otherwise spell asi64expressions (which link__divdi3): bit-identical results, nativemult/divu.isqrt_u64anddiv_u64_by_u32give full-width roots and 64-by-32 quotients in 32-bit operations.
Structs§
- Invariant
Divisor31 - Exact
n / dfor0 <= n < 2^31by onemultuand a shift, for a divisor that is fixed across many dividends.
Functions§
- abs_i16
- Absolute value of an
i16, saturatingi16::MINtoi16::MAX. - abs_i32
- Absolute value of an
i32, saturatingi32::MINtoi32::MAX. - clamp_
i16 - Clamp an
i32into thei16range (the GTE’s vertex domain). - div_
u64_ by_ u32 floor((hi * 2^32 + lo) / divisor)for a quotient that fitsu32, in 32-bit operations only.- isqrt_
i32 - Integer square root: exact
floor(sqrt(value))for positive inputs,0for zero and negative inputs. - isqrt_
u32 - Exact floor square root across the complete unsigned 32-bit domain.
- isqrt_
u64 - Exact
floor(sqrt(value))across the complete unsigned 64-bit domain, with 32-bit operations and nativemultuonly (no divide). - mul_
div_ i32 a * b / cthrough the full 64-bit product, truncated toward zero: identical to(i64::from(a) * i64::from(b) / i64::from(c)) as i32for every input, with no 64-bit helper call.- mul_
div_ u32 - Exact
floor(a * b / divisor)when the quotient fitsu32. - mul_
q12_ i32 - Multiply an
i32by a Q1.12 factor (4096= 1.0) without a 64-bit intermediate. - mul_
q12_ i32_ wide floor(value * q12 / 4096)through the full 64-bit product.- mul_
shr_ i32 (a * b) >> shiftthrough the full 64-bit product: rounds toward negative infinity like an arithmetic shift, and wraps to the low 32 bits where the shifted value does not fiti32.- mul_
shr_ trunc_ i32 a * b / 2^shift, truncated toward zero, through the full 64-bit product.- scroll_
q8_ wrapped - Signed Q8 texels/second, truncated toward zero and wrapped to a texture.
periodmust be in1..=256; a zero tick rate leaves only the phase. - square_
i32_ saturating value * value, saturating toi32::MAXwhen the square would overflow (|value| > 46340, the largest i32 whose square fits).