![]() |
SH4ZAM! 0.1.0
Fast math library for the Sega Dreamcast's SH4 CPU
|
Users Guide
The following guide will walk you through installing SH4ZAM, pulling it into your project, and leveraging it to make gainz.
To quickly get started using SH4ZAM within your preexisting KallistiOS environment, simply use the git pull command from within the folder containing the kos-ports git repository, which is typically installed to /opt/toolchains/dc/kos-ports.
Next, cd into the sh4zam folder and run make install to build and install the latest version of SH4ZAM as a statically linked library. Once this succeeds, your KOS application can now easily begin leveraging SH4ZAM to make big performance gainz.
At this point it is advised that you check out and attempt to run the examples provided with sh4zam, which get installed to the examples/sh4zam directory within kos-ports. You should be able to simply type make to build any example. You can quickly verify the integrity of your SH4ZAM install by playing with Bruce's Balls.
In order to use SH4ZAM within your KOS-based Dreamcast project, you must do two things:
#include <sh4zam/shz_sh4zam.h> globally includes the whole SH4ZAM C API.#include <sh4zam/shz_sh4zam.hpp> globally includes the whole SH4ZAM C++ API.libsh4zam.a within your build system.Unless you link to SH4ZAM, your project will compile correctly, yet fail to link.
To link to SH4ZAM using a standard, KallistiOS-style Makefile, simply pass the -lsh4zam flag to the compiler along with any other libraries you are using, which typically looks something like this:
kos-cc -o $(TARGET) $(OBJS) -lsh4zam
To link to SH4ZAM using cmake as your build system, you must add the following line to your CMakeLists.txt:
target_link_libraries(MyProject PUBLIC -lsh4zam)
One of the most common and important use-cases of SH4ZAM is for it to be pulled into an existing codebase, with its own vector math types and abstractions, with the goal of using SH4ZAM to accelerate the back-end implementation without breaking the API and needing to change client code.
SH4ZAM goes to great lengths to not only prioritize making such interoperability as seamless, ergonomic, and noninvasive as possible, but it also aims to add such niceties with a zero-overhead rule by rigorously validating that any conversions or adapting of SH4ZAM's types to existing types will get 100% cleanly optimized away.
SH4ZAMification of an existing codebase usually involves taking the following steps in order to leverage the gainz it has to offer without breaking an existing codebase or math interface:
The following provides an example of adapting SH4ZAM to accelerate the math behind an existing, idiomatic C API with support for multiple targets, such as CGLM or raymath:
The following provides an example of adapting SH4ZAM to accelerate the math behind an existing, idiomatic C++ API with support for multiple targets, such as GLM or the Simulant engine:
Some of the largest, easiest-to-exploit gainz for a project lie within its matrix multiplication and transformation code. It is important to understand how to SH4ZAmify such code optimally in order to achieve the highest gainz.
The following code snippet was taken from a real-world application which was using the CGLM library to create a model-view matrix:
The most straightforward way to accelerate such a routine is to simply do a direct, 1:1 translation between the CGLM and SH4ZAM APIs:
While this will work, it's still leaving a MASSIVE amount of gainz on the table. The following will perform far better:
We are now leveraging the following: