SH4ZAM! 0.1.0
Fast math library for the Sega Dreamcast's SH4 CPU
Loading...
Searching...
No Matches
SH4ZAM!

SH4ZAM

SH4ZAM!

Fast math library for the Sega Dreamcast's SH4 CPU
Explore the docs ยป

Overview

SH4ZAM is a hand-optimized, general-purpose math and linear algebra library for harnessing the floating-point power of the SH4 processor in the Sega Dreamcast. It originally began as a collection of inline assembly routines developed specifically to accelerate the math in the Grand Theft Auto 3 and Vice City DC ports. These routines, along with many others from various contributors, have been separated and combined into a single standalone library, which can be leveraged to bring gainz to the Dreamcast and SuperH communities at large.

Features

  • Dual, modern, interoperable C17 and C++20 APIs
  • Extra API features supported for C23 and C++23
  • Ergonomic, overhead-free composition from either C or C++
  • Clean header files, separating interface from implementation
  • Constexpr evaluation handled for both C and C++
  • Zero-cost conversion operators for interop with other APIs
  • Meticulously optimized for latest SH-GCC version 15.x
  • Rigorously unit tested and validated on physical hardware
  • Heavily documented header files and external Doxygen site

APIs

  • Preprocessor directives controlling optimizations
  • Intrinsics for SH4 instructions not emitted by GCC
  • Memory routines (memcpy(), memset(), memmove(), etc)
  • Floating-point Environment control
  • Scalar math operations, including faster <math.h> replacements
  • Trigonometry functions, inverse trig functions
  • Vector types: 2D, 3D, 4D
  • Matrix types: 2x2, 3x2, 3x3, 3x4, 4x3, and 4x4
  • Quaternion math operations
  • XMTRX API for manipulating 4x4 back-bank of FP registers
  • Miscellaneous optimized routines

Usage

C23

For C code, include the header files with the .h extensions or sh4zam/shz_sh4zam.h to include everything.

int main(int argc, const char *argv[]) {
shz_vec4_t vec1 = shz_vec4_init(2.0f, 3.0f, 4.0f, 1.0f);
shz_mat4x4_t mat = {};
shz_xmtrx_init_diagonal(vec2.x, vec2.y, vec2.z, vec2.w);
shz_xmtrx_apply_translation(vec1.x, vec1.y, vec1.z);
return 0;
}
Aggregate include file for C API.
#define SHZ_F_PI
Single-precision floating-point PI approximation (do not use M_PI!)
Definition shz_trig.h:27
float shz_sinf(float radians) SHZ_NOEXCEPT
One-off routine for returning only sinf() from an angle in radians.
shz_vec4_t shz_vec4_init(float x, float y, float z, float w) SHZ_NOEXCEPT
Returns a 4D vector with the given x, y, z, and w coordinates.
float shz_vec4_dot(shz_vec4_t vec1, shz_vec4_t vec2) SHZ_NOEXCEPT
Returns the dot product between the two given 4D vectors.
shz_vec4_t shz_vec4_scale(shz_vec4_t vec, float factor) SHZ_NOEXCEPT
Returns a 4D vector whose component values are those of the given vector multiplied by a factor.
shz_vec4_t shz_vec4_normalize(shz_vec4_t vec) SHZ_NOEXCEPT
Returns a normalized unit vector from the given 4D vector.
void shz_xmtrx_store_4x4(shz_mat4x4_t *matrix) SHZ_NOEXCEPT
Stores the current values held within XMTRX into the given 4x4 matrix.
void shz_xmtrx_apply_translation(float x, float y, float z) SHZ_NOEXCEPT
Adds the values of the given 3 components to the 3D translation components of XMTRX.
void shz_xmtrx_init_diagonal(float x, float y, float z, float w) SHZ_NOEXCEPT
Initializes XMTRX to be a diagonal matrix with the given diagonal values.
shz_vec4_t shz_xmtrx_transform_vec4(shz_vec4_t vec) SHZ_NOEXCEPT
Returns the 4D vector that is the result of transforming vec by XMTRX.
void shz_xmtrx_apply_rotation_x(float x) SHZ_NOEXCEPT
Transforms the values of the inner 3x3 matrix by a rotation matrix of x radians about the X axis.
Structure representing a 4x4 column-major matrix.
Definition shz_matrix.h:73
4D Vector type
Definition shz_vector.h:82
float z
Z coordinate.
Definition shz_vector.h:90
float w
W coordinate.
Definition shz_vector.h:94
float x
X coordinate.
Definition shz_vector.h:88
float y
Y coordinate.
Definition shz_vector.h:89

NOTE: Where applicable, the C API also includes a variety of type-generic routines which will automatically resolve to calling the proper functions based on argument type. for example, shz_vec_dot() will automatically forward to the proper routine based on the type of vectors passed to it.

C++23

For C++ code, include the header files with the .hpp extensions or sh4zam/shz_sh4zam.hpp to include everything.

int main(int argc, const char* argv[]) {
shz::vec4 vec1(2.0f, 3.0f, 4.0f, 1.0f);
shz::mat4x4 mat {};
shz::xmtrx::init_diagonal(vec2.x, vec2.y, vec2.z, vec2.w);
shz::xmtrx::apply_translation(vec1.x, vec2.y, vec2.z);
return 0;
}
constexpr auto sinf
C++ wrapper around shz_sinf().
Definition shz_trig.hpp:112
constexpr float pi_f
Floating-point constant approximation for Pi.
Definition shz_trig.hpp:22
Aggregate include file for C++ API.
4D Vector type
CppType direction() const noexcept
Returns the direction vector resulting from normalizing the given vector.
static void store(shz_mat4x4_t *mat) noexcept
C++ wrapper around shz_xmtrx_store_4x4().
static void init_diagonal(float x, float y, float z, float w) noexcept
C++ wrapper around shz_xmtrx_init_diagonal().
static void apply_rotation_x(float x) noexcept
C++ wrapper around shz_xmtrx_apply_rotation_x().
static void apply_translation(float x, float y, float z) noexcept
C++ wrapper around shz_xmtrx_apply_translation().
static vec4 transform(shz_vec4_t in) noexcept
C++ wrapper around shz_xmtrx_transform_vec4().

NOTE: C++ can still use the C API by design, and every C++ type is also compatible with its corresponding C types and C API, so you can mix and match.

Installation

KallistiOS

SH4ZAM is now an official part of kos-ports, meaning that it is now directly integrated within the KallistiOS ecosystem and will be automatically set up as part of the regular KOS installation process. If you're following the standard community guide for Setting up KallistiOS, SH4ZAM will be installed as part of the Building KOS Ports step. If you're using DreamSDK to manage your KallistiOS development environment, SH4ZAM may be installed from the KallistiOS Ports tab.

CMake

To quickly build the project with CMake in any environment, independently of KOS, run the following from the repo root:

mkdir build
cd build
cmake ..
make
make install

This will build and install the statically linked library as well as the public headers.

If you would like to also build and run the unit tests, include -DSHZ_ENABLE_TESTS=on within the cmake command. Now a separate binary for the unit test executable should be built as well.

NOTE: For KOS users, use kos-cmake instead of your system cmake command!

Examples

Examples can be found within the example subdirectory. New examples are always welcome to be contributed!

Example Description
Bruce's Balls Pushes up to 4.5 million PPS, rendering Bruce's balls.
PVR DMA Real-world, complex renderer using KOS's PVR DMA API.

NOTE: If you've installed SH4ZAM through KOS-ports, the examples should get installed to kos-ports/examples/sh4zam.

Projects

The following projects have been successfully accelerated with SH4ZAM and make great references:

Project Description
SH4ZAM PVR Advanced PVR DR rendering examples with SH4ZAM acceleration.
Mario Kart 64 DC jnmartin's Mario Kart 64 DC port with SH4ZAM acceleration.
Star Fox 64 DC jnmartin's Star Fox 64 DC port with SH4ZAM acceleration.
Super Mario 64 DC Super Mario 64 DC port with SH4ZAM acceleration.

Conventions

The following conventions are used throughout the API by default, unless otherwise specified:

Convention Description
Coordinates Right-handed for world/view space, Left-handed for screen/clip-space, as with GL.
Matrices Stored in column-major order, as with GL.
Quaternions Stored in <W, X, Y, Z> order.

Community

The #sh4zam channel within the Simulant Discord Server for Dreamcast development is the best place to ask questions, get help, give feedback, or discuss anything relating to SH4ZAM. Also, feel free to open tickets for any issues or feature requests!

Contributors

Special thanks to the following individuals who have either directly or indirectly contributed to SH4ZAM:

  • Falco Girgis
  • Paul Cercueil
  • Oleg Endo
  • jnmartin64
  • Stefanos Kornilios Mitsis Poiitidis
  • MoopTheHedgehog
  • Mikael Kalms
  • Twada
  • TapamN
  • UnknownShadow
  • yomboprime
  • David Reichelt
  • Luke Benstead
  • Ian Michael
  • HailToDodongo
  • Jesse Stojan
  • Jamie Hyland
  • Aleios
  • John Brooks
  • Daniel Fairchild
  • BruceLeet
  • Eric Fradella
  • Donald Haase