gyrovorbis/sh4zam
소개
Fast math library for the Sega Dreamcast's SH4 CPU, and everywhere else too.
개발 도구What you can do
- Dual, interoperable C11 and C++17 APIs
- Extra API features supported for C23, C++20 and C++23
- Ergonomic, overhead-free composition from either C or C++
- Clean header files, separating interface from implementation
측정된 스타 증가
- in 24h
- +1
- in 7 days
- +10
- in 15 days
- +18
- in 1 month
- 기록 수집 중
서로 겹치는 기간의 순증가량이며 일별 합계가 아닙니다. 측정값이 없으면 비워 둡니다. 스타는 관심도를 나타내며 품질을 보장하지 않습니다.
README.md
Original
SH4ZAM!
Fast math library for the Sega Dreamcast's SH4 CPU, and everywhere else too
Explore the docs »
Optimized
·
Ergonomic
·
Multiplatform
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.
Since then, SH4ZAM has grown into a complete, general-purpose math library in the same vein as cglm and raymath, with scalar, trig, vector, matrix, quaternion, complex and memory APIs. The Dreamcast is still its home and its first priority: every routine is optimized for the SH4 and benchmarked on real hardware. But a portable C back-end implements the entire API, so the same code also builds and runs on Linux, Windows, macOS, WebAssembly and more. See Platforms.
Features
- Dual, interoperable C11 and C++17 APIs
- Extra API features supported for C23, C++20 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 and benchmarked with SH-GCC 15, 16 and 17
- Rigorously unit tested and validated on physical hardware
- Heavily documented header files and documentation site
- Software back-end for using SH4ZAM in cross-platform codebases
APIs
- Scalar math operations, including faster
<math.h>replacements - Trigonometry functions, inverse and hyperbolic 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
- Complex and imaginary math API, including accelerated FFT
- Memory routines (memcpy(), memset(), memmove(), etc)
Platforms
SH4ZAM is tuned for the Dreamcast, but it is not Dreamcast-only. It uses the same headers and the same API on every platform, with no configuration necessary. The back-end is chosen automatically at compile time:
| Back-end | Used when | Notes |
|---|---|---|
| SH4 | Building for SH4 in single-precision FPU mode (-m4-single, -m4-single-only) |
Hand-optimized inline assembly, the reason SH4ZAM exists. |
| SW | Every other target, including SH4 in double-precision mode | Portable C implementation of the entire API. |
| SPU | Building for the Cell Broadband Engine's SPU | Experimental and partial. Currently covers part of the vector API; everything else falls back to SW. |
You can call SH4ZAM unconditionally in a cross-platform codebase. There is no need for #ifdef blocks or a separate fallback for non-Dreamcast builds, because the SW back-end already is that fallback. For example, Meese Engine uses it on the GameCube.
Continuous Integration
Every commit is built and the full unit test suite is run on the following platforms:
| Platform | Compiler | Back-end |
|---|---|---|
| Dreamcast | SH-GCC (KallistiOS) | SH4, tested on physical hardware |
| Linux | GCC | SW |
| Linux | Clang | SW |
| Windows | MSVC | SW |
| Windows | MinGW-w64 GCC | SW |
| macOS | Apple Clang | SW |
| WebAssembly | Emscripten, run under Node.js | SW |
Any other platform with a C11 or C++17 compiler should work through the SW back-end. If it doesn't, please open an issue.
Language Standards
| Language | Minimum | Preferred |
|---|---|---|
| C | GNU C11 (-std=gnu11) |
GNU C23 (-std=gnu23) |
| C++ | C++17 (-std=gnu++17) |
C++23 (-std=gnu++23) |
GNU extensions are required for C on GCC and Clang. Newer standards enable extra API features: C++20 adds std::bit_cast-based conversions, and C++23 adds operator[] and operator<=> on the vector, quaternion, matrix and complex types.
Usage
C23
For C code, include the header files with the .h extensions or sh4zam/shz_sh4zam.h to include everything.
#include <sh4zam/shz_sh4zam.h>
int main(int argc, const char *argv[]) {
shz_vec4_t vec1 = shz_vec4_init(2.0f, 3.0f, 4.0f, 1.0f);
shz_vec4_t vec2 = shz_vec4_normalize(shz_vec4_scale(vec1, shz_sinf(SHZ_F_PI)));
shz_mat4x4_t mat = {};
shz_xmtrx_init_diagonal(vec2.x, vec2.y, vec2.z, vec2.w);
shz_xmtrx_rotate_x(shz_vec4_dot(vec1, vec2));
shz_xmtrx_add_translation(vec1.x, vec1.y, vec1.z);
shz_xmtrx_store_4x4(&mat);
shz_vec4_t vec3 = shz_xmtrx_transform_vec4(vec2);
return 0;
}
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.
#include <sh4zam/shz_sh4zam.hpp>
int main(int argc, const char* argv[]) {
shz::vec4 vec1(2.0f, 3.0f, 4.0f, 1.0f);
shz::vec4 vec2 = shz::vec4(vec1 * shz::sinf(shz::pi_f)).direction();
shz::mat4x4 mat {};
shz::xmtrx::init_diagonal(vec2.x, vec2.y, vec2.z, vec2.w);
shz::xmtrx::rotate_x(vec1.dot(vec2));
shz::xmtrx::add_translation(vec1.x, vec1.y, vec1.z);
shz::xmtrx::store(&mat);
shz::vec4 vec3 = shz::xmtrx::transform(vec2);
return 0;
}
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
[!IMPORTANT] Please don't copy and paste SH4ZAM into your codebase, whether by hand or through an AI agent. Copied code falls out of sync and misses every SH4-specific fix, new feature, and optimization that lands upstream. Use the KallistiOS port below, or add this repository as a git submodule and link the
sh4zamCMake target. If you need something SH4ZAM doesn't have, or were able to further optimize something, please open an issue or feature request or submit a pull request! See AGENTS.md for the instructions given to AI agents.
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!
Git Submodule
To pin a specific version of SH4ZAM, or to use it outside of KallistiOS, add it to your project as a git submodule:
git submodule add https://github.com/gyrovorbis/sh4zam.git external/sh4zam
Then add it to your CMake build and link against the sh4zam target, which exports its include directory:
add_subdirectory(external/sh4zam)
target_link_libraries(<your_target> PRIVATE sh4zam)
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.
Showcase
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. |
| FFFFTT | Audio signal visualizations, with SH4ZAM accelerated FFT and more. |
| Sonic Mania DC | Sonic Mania DC port with SH4ZAM acceleration. |
| Simulant Engine | General-purpose cross-platform engine backed by SH4ZAM math. |
| Meese Engine | Open-world voxel game built for the GC, using the SH4ZAM SW back-end. |
| Quake 2 DC | Port of Quake 2 to the DC, with SH4ZAM accelerated matrix math. |
| DCSinge | Singe API + FMV playback engine on DC using SH4ZAM memory routines. |
| Xash3D DC | SH4ZAM'd up DC port of the Half-Life compatible Xash3D engine. |
| Gael Force | Stylized tactical RPG with sprites, 3D worlds, and dynamic lighting effects. |
| DMS-Engine | Bruceleet's "Dreamcast Mesh Shit" Engine, SH4ZAM'd to hell with skinning. |
| The Cave | FreakDave's SH4ZAM-boosted DC tech demo with impressive lighting and effects. |
| Audio Visualizer | Brielle Garcia's CD Audio Visualizer for Dreamcast, using SH4ZAM math routines. |
| DemoTek | Crash Bandicoot-style 3D platformer, with SH4ZAM'd up 3D transform math. |
| QuakeSpasm DC | Sega Dreamcast port of QuakeSpasm, with native PVR renderer + SH4ZAM math. |
| Monkey Ball DC | Reimplementation of Monkey Ball using GameCube assets from Super MB. |
| Elias Daler Work | Renowned PS1 homebrew dev uses SH4ZAM for his beautiful DC experiments. |
| Diddy Kong Racing DC | Rare's renowned N64 kart racer, ported to the DC. SH4ZAMed to all hell for gainz. |
| World of Warcraft DC | WoW DC port by CYPRESS, with SH4ZAM-based math gainz. |
| Resident Evil 4 DC | lamb2k's RE4 port to DC, features a build flag SH4ZAM-ing TnL. |
| Sonic Adventure | Reshaped's full decomp of SADC, can override Ninja math with SH4ZAM. |
| Sonic Adventure 2 | Reshaped's full decompilation of SA2DC, can override Ninja math with SH4ZAM. |
| FrNES | Maslevin's FrNES DC port; uses SH4ZAM XMTRX + FFT APIs for audio processing. |
| SH4DER-PVR | Jilted Valkyrie's DC graphics pipeline library, uses SH4ZAM for math. |
| Balatro | DC port by Espouaisso, using SH4ZAM for math. |
| Scud Race | Christian Fernando Perucchi's DC port uses SH4ZAM + SH4DER-PVR for effects. |
| Final Fantasy X | SpecialK's DC port of the PS2 exclusive, using SH4ZAM for FPS GAINZ. |
| Halo DCE | Jilted Valkyrie's insane Halo: Dreamcast Evolved port is SH4ZAM'd up. |
| Global Rally Overdrive | Freakdave's incredibly ambitious DC racing homebrew, SH4ZAM accelerated. |
| DreamKarts | Mario Kart-style racer with online play, using SH4ZAM XMTRX transforms. |
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!
Acknowledgements
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
- IANN (@meisei4)
- Niels "Mouse's Mistake" Van Son
- TechProGabe
- LobotomyDC
- Haikuno
- Brendan "Cypress" Lawson
- Kyle Mayer
관련 프로젝트 살펴보기
불러오는 중…