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

psx_math/
lib.rs

1// SPDX-License-Identifier: GPL-2.0-or-later
2//! Fixed-point math primitives for the PS1 SDK.
3//!
4//! The PS1 has no FPU. Games that want sine-driven animation,
5//! camera rotation, pseudo-3D, or smoothed HUDs all reach for the
6//! same toolbox: a Q-format fixed-point type, a sin/cos LUT, and
7//! a couple of vector/matrix helpers. This crate owns those
8//! primitives once so every downstream crate (`psx-font`, a
9//! future `psx-gte`-wrapper, a future `psx-ui`, gameplay code)
10//! shares the same representation.
11//!
12//! ## What's here today
13//!
14//! - [`sincos`] -- Q0.12 angle type + 256-entry Q1.12 sine LUT +
15//!   interpolated `sin_q12` / `cos_q12` lookups, and the matching
16//!   octant-linear `atan2_q12`.
17//! - [`int32`] -- saturating 32-bit scalar helpers: `abs_i32` /
18//!   `abs_i16`, `clamp_i16`, `square_i32_saturating`, `isqrt_i32`,
19//!   `mul_q12_i32`, plus the 64-bit-free wide forms `mul_div_i32`,
20//!   `mul_shr_i32`, `mul_shr_trunc_i32`, `mul_div_u32`, `isqrt_u64` and
21//!   `div_u64_by_u32`.
22//! - [`wide`] -- [`Wide`] / [`WideU`], the one sanctioned 64-bit value
23//!   for guest code: `mult`/`multu` products, 64-bit add, subtract,
24//!   compare and shift, and explicit extraction to 32 bits. Nothing that
25//!   needs a compiler helper exists. Also [`Mask64`], a 64-flag bit set in
26//!   two words.
27//! - [`wide_div`] -- exact division of a [`Wide`] / [`WideU`] by a word or a
28//!   wide value, with [`Rounding`] and clamping chosen by the caller, a
29//!   128-bit product and ratio compare, all in 32-bit instructions.
30//! - [`fmt`] -- integer-to-decimal ASCII without `core::fmt`, for
31//!   HUD/score text drawn with `psx-font`.
32//!
33//! ## What's planned (same crate, future modules)
34//!
35//! - `fixed` -- Q3.12 and Q16.16 wrappers with operator overloads
36//!   and saturating / wrapping arithmetic.
37//! - `vec` -- `Vec2` / `Vec3` over integer and fixed-point types
38//!   with dot / cross / length² / normalise.
39//! - `mat` -- 2×2 / 3×3 / GTE-shape matrices, chosen to match how
40//!   the GTE's `RTPS` / `MVMVA` instructions consume data.
41//! - `ease` -- sinusoidal / quadratic / cubic easing helpers for
42//!   timeline animation.
43//! - `lerp` -- generic linear interpolation.
44//!
45//! These modules land as concrete consumer code needs them -- no
46//! premature abstractions. The crate ships empty apart from the
47//! documented scope so reviewers can see where new primitives
48//! should go.
49//!
50//! ## Q-format refresher
51//!
52//! `Qm.n` means `m` integer bits + `n` fractional bits in a
53//! signed integer. Arithmetic rules:
54//!
55//! - Add / sub: same as ordinary integer add / sub.
56//! - Multiply: `(a * b) >> n` -- two Q_._n values produce a Q_._2n
57//!   intermediate that must be shifted back.
58//! - Divide: `(a << n) / b` -- scale up the numerator by `n` bits
59//!   first so the quotient is in Qm.n.
60//!
61//! Conventions the PS1 SDK uses:
62//!
63//! - **Q0.12 angle**: `u16` in `[0, 4096)` maps to `[0°, 360°)`.
64//!   Matches the GTE's angle resolution. `4096` = one full
65//!   revolution. Overflow wraps naturally in u16.
66//! - **Q1.12 trig values**: `i16` in `[-4096, 4096]`. `4096` =
67//!   +1.0, `-4096` = -1.0. Multiplying a Q_._12 input by a Q1.12
68//!   coefficient produces a Q_._24 intermediate; shift right 12
69//!   to land back in the original format.
70//! - **Q3.12 transforms** (future): `i16` with range `±7.999…`.
71//!   Used by `psx-font`'s affine matrices today; planned as a
72//!   proper `Mat2` type here.
73//! - **Q16.16 positions** (future): `i32` in `±32767` with 16
74//!   fractional bits -- standard precision for sub-pixel
75//!   positions, accumulated velocities, etc.
76
77#![no_std]
78#![cfg_attr(target_arch = "mips", feature(asm_experimental_arch))]
79#![deny(unsafe_op_in_unsafe_fn)]
80#![warn(missing_docs)]
81
82#[cfg(all(target_arch = "mips", feature = "asm-kernels"))]
83mod asm_kernels;
84pub mod attributed_clip;
85pub mod clip_lanes;
86pub mod color;
87pub mod fmt;
88pub mod int32;
89pub mod sincos;
90pub mod wide;
91pub mod wide_div;
92
93// Re-export the most common symbols at the crate root so users
94// can `use psx_math::{sin_q12, cos_q12}` without reaching through
95// the submodule -- these are the hot-loop primitives.
96pub use sincos::{atan2_q12, cos_q12, sin_q12, SIN_TABLE};
97pub use wide::{Mask64, Wide, WideU};
98pub use wide_div::Rounding;