SH4ZAM! 0.1.0
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) SHZ_NOEXCEPT
 
float shz_fmaxf (float a, float b) SHZ_NOEXCEPT
 
bool shz_equalf (float a, float b) SHZ_NOEXCEPT
 
bool shz_equalf_abs (float a, float b) SHZ_NOEXCEPT
 
bool shz_equalf_rel (float a, float b) SHZ_NOEXCEPT
 
Rounding

Routines for rounding and manipulating floats.

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

Routines for mapping a number to another range.

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

Assorted routines implementing other fp operations.

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

Routines built around fast reciprocal square root instruction.

float shz_inv_sqrtf_fsrra (float x) SHZ_NOEXCEPT
 
float shz_inv_sqrtf (float x) SHZ_NOEXCEPT
 
float shz_sqrtf_fsrra (float x) SHZ_NOEXCEPT
 
float shz_sqrtf (float x) SHZ_NOEXCEPT
 
float shz_invf_fsrra (float x) SHZ_NOEXCEPT
 
float shz_invf (float x) SHZ_NOEXCEPT
 
float shz_divf_fsrra (float num, float denom) SHZ_NOEXCEPT
 
float shz_divf (float num, float denom) SHZ_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) SHZ_NOEXCEPT
 
float shz_dot8f (float x1, float y1, float z1, float w1, float x2, float y2, float z2, float w2) SHZ_NOEXCEPT
 
float shz_mag_sqr3f (float x, float y, float z) SHZ_NOEXCEPT
 
float shz_mag_sqr4f (float x, float y, float z, float w) SHZ_NOEXCEPT
 
Transcendental

Fast approximations for non-trig transcendental functions.

float shz_pow2f (float p) SHZ_NOEXCEPT
 
float shz_powf (float x, float p) SHZ_NOEXCEPT
 
float shz_pow10f (float x) SHZ_NOEXCEPT
 
float shz_log2f (float x) SHZ_NOEXCEPT
 
float shz_logf (float x) SHZ_NOEXCEPT
 
float shz_log10f (float x) SHZ_NOEXCEPT
 
float shz_expf (float p) SHZ_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 )

Returns the minimum value of two given floats.

◆ shz_fmaxf()

float shz_fmaxf ( float a,
float b )

Returns the maximum value of two given floats.

◆ shz_equalf()

bool shz_equalf ( float a,
float b )

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 )

Checks for equality based on the absolute tolerance using SHZ_FLT_EPSILON.

◆ shz_equalf_rel()

bool shz_equalf_rel ( float a,
float b )

Checks for equality based on the relative tolerance using SHZ_FLT_EPSILON.

◆ shz_floorf()

float shz_floorf ( float x)

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)

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)

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)

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 )

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 )

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 )

(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 )

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

◆ shz_normalizef()

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

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 )

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 )

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 )

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 )

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 )

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)

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

◆ shz_signf()

float shz_signf ( float x)

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

◆ shz_saturatef()

float shz_saturatef ( float x)

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

◆ shz_fabsf()

float shz_fabsf ( float x)

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

Returns the absolute value of x.

◆ shz_copysignf()

float shz_copysignf ( float x,
float y )

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

Returns the value of x with the sign of y.

◆ shz_fmaf()

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

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 )

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 )

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)

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 )

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 )

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 )

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)

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 )

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 )

returns 0.0f if x < edge, otherwise 1.0f

◆ shz_smoothstepf()

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

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 )

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)

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

◆ shz_inv_sqrtf()

float shz_inv_sqrtf ( float x)

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)

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

◆ shz_sqrtf()

float shz_sqrtf ( float x)

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)

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

◆ shz_invf()

float shz_invf ( float x)

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 )

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 )

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 )

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 )

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 )

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 )

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

◆ shz_pow2f()

float shz_pow2f ( float p)

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

◆ shz_powf()

float shz_powf ( float x,
float p )

Fast approximation for C's powf().

◆ shz_pow10f()

float shz_pow10f ( float x)

Fast approximation of POSIX's pow10f().

◆ shz_log2f()

float shz_log2f ( float x)

Fast approximation for C's log2f().

◆ shz_logf()

float shz_logf ( float x)

Fast approximation for C's logf().

◆ shz_log10f()

float shz_log10f ( float x)

Fast approximation for C's log10f().

◆ shz_expf()

float shz_expf ( float p)

Fast approximation for C's expf().