SH4ZAM! 0.9.1
Fast math library for the Sega Dreamcast's SH4 CPU
Loading...
Searching...
No Matches
shz_scalar.h File Reference
#include <math.h>
#include <stdbool.h>
#include "shz_cdefs.h"
#include "inline/shz_scalar.inl.h"
Include dependency graph for shz_scalar.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define SHZ_FLT_EPSILON
 

Functions

Comparisons

Routines for comparing and classifying floating-point values.

float shz_fminf (float a, float b) noexcept
 
float shz_fmaxf (float a, float b) noexcept
 
bool shz_equalf (float a, float b) noexcept
 
bool shz_equalf_abs (float a, float b) noexcept
 
bool shz_equalf_rel (float a, float b) noexcept
 
Rounding

Routines for rounding and manipulating floats.

float shz_floorf (float x) noexcept
 
float shz_ceilf (float x) noexcept
 
float shz_roundf (float x) noexcept
 
float shz_truncf (float x) noexcept
 
float shz_remainderf (float num, float denom) noexcept
 
float shz_fmodf (float num, float denom) noexcept
 
float shz_remquof (float num, float denom, float *quot) noexcept
 
Mapping

Routines for mapping a number to another range.

float shz_clampf (float value, float min, float max) noexcept
 
float shz_normalizef (float current, float from, float to) noexcept
 
float shz_normalizef_fsrra (float current, float from, float to) noexcept
 
float shz_remapf (float value, float inputStart, float inputEnd, float outputStart, float outputEnd) noexcept
 
float shz_remapf_fsrra (float value, float inputStart, float inputEnd, float outputStart, float outputEnd) noexcept
 
float shz_wrapf (float value, float min, float max) noexcept
 
float shz_wrapf_fsrra (float value, float min, float max) noexcept
 
float shz_fractf (float x) noexcept
 
float shz_signf (float x) noexcept
 
float shz_saturatef (float x) noexcept
 
Miscellaneous

Assorted routines implementing other fp operations.

float shz_fabsf (float x) noexcept
 
float shz_copysignf (float x, float y) noexcept
 
float shz_fmaf (float a, float b, float c) noexcept
 
float shz_fdimf (float x, float y) noexcept
 
float shz_hypotf (float x, float y) noexcept
 
float shz_cbrtf (float x) noexcept
 
float shz_lerpf (float a, float b, float t) noexcept
 
float shz_barycentric_lerpf (float a, float b, float c, float u, float v) noexcept
 
bool shz_quadratic_roots (float a, float b, float c, float *root1, float *root2) noexcept
 
float shz_randf (int *seed) noexcept
 
float shz_randf_range (int *seed, float min, float max) noexcept
 
float shz_stepf (float x, float edge) noexcept
 
float shz_smoothstepf (float x, float edge0, float edge1) noexcept
 
float shz_smoothstepf_safe (float x, float edge0, float edge1) noexcept
 
FSRRA

Routines built around fast reciprocal square root instruction.

float shz_inv_sqrtf_fsrra (float x) noexcept
 
float shz_inv_sqrtf (float x) noexcept
 
float shz_sqrtf_fsrra (float x) noexcept
 
float shz_sqrtf (float x) noexcept
 
float shz_invf_fsrra (float x) noexcept
 
float shz_invf (float x) noexcept
 
float shz_divf_fsrra (float num, float denom) noexcept
 
float shz_divf (float num, float denom) noexcept
 
FIPR

Routines built around fast 4D dot product.

Todo
Generalize these with a macro that lets you choose vector FP regs.
float shz_dot6f (float x1, float y1, float z1, float x2, float y2, float z2) noexcept
 
float shz_dot8f (float x1, float y1, float z1, float w1, float x2, float y2, float z2, float w2) noexcept
 
float shz_mag_sqr3f (float x, float y, float z) noexcept
 
float shz_mag_sqr4f (float x, float y, float z, float w) noexcept
 
Transcendental

Fast approximations for non-trig transcendental functions.

float shz_pow2f (float p) noexcept
 
float shz_powf (float x, float p) noexcept
 
float shz_pow10f (float x) noexcept
 
float shz_log2f (float x) noexcept
 
float shz_logf (float x) noexcept
 
float shz_log10f (float x) noexcept
 
float shz_expf (float p) noexcept
 

Detailed Description

General-purpose scalar math routines.

This file provides a collection of general-purpose math routines for operating on floating-point scalar values.

Todo
  • Use FP rounding modes for rounding functionality.
Author
2025, 2026 Falco Girgis
2025, 2026 Paul Cercueil
2026 Aleios
2026 jnmartin64

Definition in file shz_scalar.h.

Macro Definition Documentation

◆ SHZ_FLT_EPSILON

#define SHZ_FLT_EPSILON

Floating-point epsilon used with inexact FP-based comparisons.

Definition at line 40 of file shz_scalar.h.

Function Documentation

◆ shz_fminf()

float shz_fminf ( float a,
float b )
noexcept

Returns the minimum value of two given floats.

◆ shz_fmaxf()

float shz_fmaxf ( float a,
float b )
noexcept

Returns the maximum value of two given floats.

◆ shz_equalf()

bool shz_equalf ( float a,
float b )
noexcept

Checks for equality based on EITHER the absolute tolerance or relative tolerance, using SHZ_FLT_EPSILON.

◆ shz_equalf_abs()

bool shz_equalf_abs ( float a,
float b )
noexcept

Checks for equality based on the absolute tolerance using SHZ_FLT_EPSILON.

◆ shz_equalf_rel()

bool shz_equalf_rel ( float a,
float b )
noexcept

Checks for equality based on the relative tolerance using SHZ_FLT_EPSILON.

◆ shz_floorf()

float shz_floorf ( float x)
noexcept

Replacement for the <math.h> routine, floorf().

Returns the closest integral value to x, rounded down, as a float.

Warning
This routine is only valid for for the range INT32_MIN <= x <= INT32_MAX.
See also
ceilf()

◆ shz_ceilf()

float shz_ceilf ( float x)
noexcept

Replacement for the <math.h> routine, ceilf().

Returns the closest integral value to x, rounded up, as a float.

Warning
This routine only returns valid values for the input range INT32_MIN <= x <= INT32_MAX.
See also
floorf()

◆ shz_roundf()

float shz_roundf ( float x)
noexcept

Replacement for the <math.h> routine, roundf().

Returns the value of x rounded to the nearest integer, as a float.

Warning
This routine only returns valid values for the input range -UINT32_MAX <= x <= UINT32_MAX.

◆ shz_truncf()

float shz_truncf ( float x)
noexcept

Replacement for the <math.h> routine, truncf().

Returns the value of with its fractional component discarded.

Warning
This routine only returns valid values for the input range INT32_MIN <= x <= INT32_MAX.

◆ shz_remainderf()

float shz_remainderf ( float num,
float denom )
noexcept

Replacement for the <math.h> routine, remainderf().

Returns the floating-point remainder of num divided by denom, rounded to the nearest integer (as a float).

Warning
This routine does not gracefully handle dividing by zero, and it is only valid for the input range INT32_MIN <= num / denom <= INT32_MAX.
See also
shz_fmodf(), shz_remquof(), shz_truncf().

◆ shz_fmodf()

float shz_fmodf ( float num,
float denom )
noexcept

Replacement for the <math.h> routine, fmodf().

Returns the floating-point remainder of num divided by denom, rounded towards zero.

Warning
This routine does not gracefully handle dividing by zero.
See also
shz_remainderf()

◆ shz_remquof()

float shz_remquof ( float num,
float denom,
float * quot )
noexcept

(Sorta) Replacement for the <math.h> routine, remquof().

Returns the floating-point remainder of num divided by denom, rounded to the nearest integer (as a float). quot is set equal to the quotient which is used as part of the calculation.

Note
quot is returning as a float rather than an int as with standard C. Simply cast to an int manually afterwards if that is the desired behavior.
Warning
This routine does not gracefully handle dividing by zero.
See also
shz_remainderf()

◆ shz_clampf()

float shz_clampf ( float value,
float min,
float max )
noexcept

Clamps a floating-point value by the given min and max values.

◆ shz_normalizef()

float shz_normalizef ( float current,
float from,
float to )
noexcept

Maps a value within the given range from to to, to be within the range of 0.0f += 1.0f.

◆ shz_normalizef_fsrra()

float shz_normalizef_fsrra ( float current,
float from,
float to )
noexcept

Maps a value within the given range from to to, to be within the range of 0.0f + 1.0f more quickly, provided to - from is a positive difference.

◆ shz_remapf()

float shz_remapf ( float value,
float inputStart,
float inputEnd,
float outputStart,
float outputEnd )
noexcept

Maps a value within the given range inputStart to inputEnd, to be within the range of outputStart to outputEnd.

◆ shz_remapf_fsrra()

float shz_remapf_fsrra ( float value,
float inputStart,
float inputEnd,
float outputStart,
float outputEnd )
noexcept

Maps a value within the given range inputStart to inputEnd, to be within the range of outputStart to outputEnd more quickly, provided the outputEnd - outputStart is a positive difference.

◆ shz_wrapf()

float shz_wrapf ( float value,
float min,
float max )
noexcept

Wraps the given value back to be within the range of min to max.

◆ shz_wrapf_fsrra()

float shz_wrapf_fsrra ( float value,
float min,
float max )
noexcept

Wraps the given value back to be within the range of min to max more quickly, provided max - min is a positive difference.

◆ shz_fractf()

float shz_fractf ( float x)
noexcept

Returns the fractional part of x, equivalent to GLSL fract().

◆ shz_signf()

float shz_signf ( float x)
noexcept

Returns -1.0f if x < 0, 0.0f if x == 0, or 1.0f if x > 0.

◆ shz_saturatef()

float shz_saturatef ( float x)
noexcept

Clamps x to the range [0.0f, 1.0f].

◆ shz_fabsf()

float shz_fabsf ( float x)
noexcept

Replacement for the <math.h> routine, fabsf().

Returns the absolute value of x.

◆ shz_copysignf()

float shz_copysignf ( float x,
float y )
noexcept

Replacement for the <math.h> routine, copysignf().

Returns the value of x with the sign of y.

Warning
When y == -0.0f, a positive value is returned.

◆ shz_fmaf()

float shz_fmaf ( float a,
float b,
float c )
noexcept

Replacement for the <math.h> routine, fmaf().

Returns a * b + c, performing an FP multiply + accumulate operation.

◆ shz_fdimf()

float shz_fdimf ( float x,
float y )
noexcept

Replacement for the <math.h> routine, fdimf(),.

Returns the positive difference between x and y or zero if y >= x.

Warning
Unlike fdimf(), this routine does not handle INF and NAN values.

◆ shz_hypotf()

float shz_hypotf ( float x,
float y )
noexcept

Replacement for the <math.h> routine, hypotf().

Returns the hypoteneuse of the right triangle with the given legs.

Warning
Unlike hypotf(), this routine has no error or overflow handling.

◆ shz_cbrtf()

float shz_cbrtf ( float x)
noexcept

Replacement for the <math.h> routine, cbrtf().

Returns the cubed root of x, using a fast approximation.

◆ shz_lerpf()

float shz_lerpf ( float a,
float b,
float t )
noexcept

Returns a value that is linearly interpolated between a and b by the given ratio, t.

◆ shz_barycentric_lerpf()

float shz_barycentric_lerpf ( float a,
float b,
float c,
float u,
float v )
noexcept

Returns a value that is barycentrically interpolated between a, b, and c using the given barycentric coordinates, u and v.

◆ shz_quadratic_roots()

bool shz_quadratic_roots ( float a,
float b,
float c,
float * root1,
float * root2 )
noexcept

Uses the quadratic formula with the given coefficients to solve for the two roots, returning true if any real roots exist, and false if the roots are only imaginary.

◆ shz_randf()

float shz_randf ( int * seed)
noexcept

Returns a random floating-point number between 0.0f and 1.0f, using and updating the given seed.

◆ shz_randf_range()

float shz_randf_range ( int * seed,
float min,
float max )
noexcept

Returns a random floating-point number between min and max, using and updating the given seed.

◆ shz_stepf()

float shz_stepf ( float x,
float edge )
noexcept

returns 0.0f if x < edge, otherwise 1.0f

◆ shz_smoothstepf()

float shz_smoothstepf ( float x,
float edge0,
float edge1 )
noexcept

Returns 0.0f at/below edge0, 1.0f at/above edge1, smoothly varying in-between. edge0 must be less than edge1 or result is undefined.

◆ shz_smoothstepf_safe()

float shz_smoothstepf_safe ( float x,
float edge0,
float edge1 )
noexcept

Returns 0.0f at/below edge0, 1.0f at/above edge1, smoothly varying in-between. Accepts inverse edges.

◆ shz_inv_sqrtf_fsrra()

float shz_inv_sqrtf_fsrra ( float x)
noexcept

Calculates 1.0f/sqrtf( x), using a fast approximation.

◆ shz_inv_sqrtf()

float shz_inv_sqrtf ( float x)
noexcept

Calculates 1.0f/sqrtf( x ), using a fast approximation, while safely protecting against division-by-zero.

◆ shz_sqrtf_fsrra()

float shz_sqrtf_fsrra ( float x)
noexcept

Returns the fast approximate square root of the given value, x.

◆ shz_sqrtf()

float shz_sqrtf ( float x)
noexcept

Returns the fast approximate square root of the given value, x, safely returning 0.0f is x == 0.0f.

◆ shz_invf_fsrra()

float shz_invf_fsrra ( float x)
noexcept

Takes the inverse of x using a very fast approximation, returning a positive result.

◆ shz_invf()

float shz_invf ( float x)
noexcept

Takes the inverse of x using a slighty faster approximation than doing a full division, safely handling negative values.

◆ shz_divf_fsrra()

float shz_divf_fsrra ( float num,
float denom )
noexcept

Divides num by denom using a very fast approximation, which requires denom be a positive value.

◆ shz_divf()

float shz_divf ( float num,
float denom )
noexcept

Divides num by denom using a slightly faster approximation, allowing denom to be negative.

◆ shz_dot6f()

float shz_dot6f ( float x1,
float y1,
float z1,
float x2,
float y2,
float z2 )
noexcept

Takes two sets of 3D vectors as 3 floats and calculates their dot product using an approximation.

◆ shz_dot8f()

float shz_dot8f ( float x1,
float y1,
float z1,
float w1,
float x2,
float y2,
float z2,
float w2 )
noexcept

Takes two sets of 4D vectors as 4 floats and calculates their dot product using an approximation.

◆ shz_mag_sqr3f()

float shz_mag_sqr3f ( float x,
float y,
float z )
noexcept

Takes a 3D vector as 3 floats and calculates its squared magnitude using a fast approximation.

◆ shz_mag_sqr4f()

float shz_mag_sqr4f ( float x,
float y,
float z,
float w )
noexcept

Takes a 4D vector as 4 floats and calculates its squared magnitude using a fast approximation.

◆ shz_pow2f()

float shz_pow2f ( float p)
noexcept

Fast approximation for raising 2 to a floating-point power.

◆ shz_powf()

float shz_powf ( float x,
float p )
noexcept

Fast approximation for C's powf().

◆ shz_pow10f()

float shz_pow10f ( float x)
noexcept

Fast approximation of POSIX's pow10f().

◆ shz_log2f()

float shz_log2f ( float x)
noexcept

Fast approximation for C's log2f().

◆ shz_logf()

float shz_logf ( float x)
noexcept

Fast approximation for C's logf().

◆ shz_log10f()

float shz_log10f ( float x)
noexcept

Fast approximation for C's log10f().

◆ shz_expf()

float shz_expf ( float p)
noexcept

Fast approximation for C's expf().