API Design

/* shachi/sh4math.h — SH4 math for Shachi games */

/*
 * This file re-exports DreamHAL sh4_math.h (public domain, Moopthehedgehog 2020)
 * and adds Shachi-specific convenience functions on top.
 *
 * DreamHAL provides:
 *   - FTRV (4x4 matrix × vector): sh4_mat_ftrv(...)
 *   - FIPR (dot product): sh4_vec_fipr(...)
 *   - FSRRA (1/sqrt): sh4_math_fsrra(x)
 *   - FSCA (sin/cos): sh4_math_fsca_sincos(angle)
 *   - Full matrix load/store to XMTRX
 *   - Cross product, normalize, length via intrinsics
 *
 * KOS provides (dc/fmath.h, dc/matrix.h):
 *   - frsqrt(x) — maps to FSRRA
 *   - fsin/fcos — maps to FSCA
 *   - mat_load, mat_store, mat_apply — XMTRX manipulation
 *   - mat_transform — bulk vertex transform via FTRV
 *   - vec3f_dot, vec3f_length — convenience
 */

#include <dc/fmath.h>
#include <dc/matrix.h>
#include "vendor/sh4_math.h"  /* DreamHAL, public domain */


/* --- Shachi Additions --- */

/* 4x4 matrix type (column-major, matches XMTRX layout) */
typedef float sh_mat4_t[4][4] __attribute__((aligned(8)));

/* 4D vector (for FTRV input/output) */
typedef float sh_vec4_t[4] __attribute__((aligned(8)));

/* Transform N vertices through current XMTRX. Uses FTRV in tight loop.
   src and dst may alias if in-place transform is desired. */
void sh_transform_points(const sh_vec4_t *src, sh_vec4_t *dst, int count);

/* Perspective divide after transform (W divide for screen projection). */
void sh_perspective_divide(sh_vec4_t *verts, int count);

/* Batch normalize N vec3s. Uses FIPR + FSRRA. */
void sh_normalize_batch(sh_vec4_t *verts, int count);

/* SH4-optimized lerp. Uses register bank switching for dual-vector ops. */
void sh_vec4_lerp(const sh_vec4_t a, const sh_vec4_t b, float t, sh_vec4_t out);