libdragon
Files
Fast math routines

Fast math routines, optimized for 3D graphics calculations. More...

Files

file  fmath.c
 Fast math routines, optimized for 3D graphics calculations.
 
file  fmath.h
 Fast math routines, optimized for 3D graphics calculations.
 

Detailed Description

Fast math routines, optimized for 3D graphics calculations.

This module collects mathematical functions operating a single-precision floating point numbers (float) that are useful in the context of 3D graphics algorithm. The provided algorithms have prototypes that are identical to the C standard ones (provided by libm), but their implementation has been optimized in a way that is normally useful in the context of graphics programming in games. In particular, compared to the C standard:

The first four compromises above are similar and in-line with those that are usually accepted by programmers that compile their floating point code using -ffast-math.

As for the numerical error, there is no single good trade-off that can be generally taken when deciding how much we want to approximate an inverse square root or a trigonometric function. Using the general understanding that most 3D games on N64 are fill-rate limited rather than CPU or RSP limited, this library stays on the side of spending more CPU cycles more than the most basic version, while still offering a couple of orders of magnitudes of speed improvement over the standard C versions (that are fully accurate for all inputs).

All the functions defined by this library prefixed with "fm_" (eg: fm_sinf). It is possible to define the preprocess macro LIBDRAGON_FAST_MATH to additionally define macros that override the standard library functions, so that calling sinf(x) will actually invoke fm_sinf(x).

The following C99 functions have been tested and the default implementation is already very good (eg: they are intrinsified):