Skip to content

Scalar API

Markdown

This API is designed around performing various scalar operations, providing alternatives for routines typically found within <math.h>. Many are drop-in replacements that beat libm on the SH4 by staying in single-precision float: no libm call and no promotion to double. Unlike the standard C floating-point routines, these routines are often saving cycles by not handling NaN and INF values as well as by not reporting rounding and domain errors back to the user. The _fsrra variants use the SH4 FSRRA instruction (fast approximate reciprocal square root) and the FIPR helpers run raw dot products through the vector FPU. In C++ every routine is also available as an alias in namespace shz with the shz_ prefix dropped, for example shz::floorf.

  • C header: #include <sh4zam/shz_scalar.h>
  • C++ header: #include <sh4zam/shz_scalar.hpp>
  • Symbols: 56

Suffixes used in this module. The full list is in Naming and Suffixes.

  • _fsrra: Uses FSRRA (fast approximate 1/sqrt(x)). The argument or denominator must be positive. Faster, less precise.
  • _safe: Guards degenerate input (zero-length vectors, zero denominators) at a small cost.

Routines for comparing and classifying floating-point values.

SymbolDescription
shz_fminfReturns the minimum value of two given floats.
shz_fmaxfReturns the maximum value of two given floats.
shz_equalfChecks for equality based on EITHER the absolute tolerance or relative tolerance, using SHZ_FLT_EPSILON.
shz_equalf_absChecks for equality based on the absolute tolerance using SHZ_FLT_EPSILON.
shz_equalf_relChecks for equality based on the relative tolerance using SHZ_FLT_EPSILON.

Routines for rounding and manipulating floats.

SymbolDescription
shz_floorfReplacement for the <math.h> routine, floorf().
shz_ceilfReplacement for the <math.h> routine, ceilf().
shz_roundfReplacement for the <math.h> routine, roundf().
shz_truncfReplacement for the <math.h> routine, truncf().
shz_remainderfReplacement for the <math.h> routine, remainderf().
shz_fmodfReplacement for the <math.h> routine, fmodf().
shz_remquof(Sorta) Replacement for the <math.h> routine, remquof().

Routines for mapping a number to another range.

SymbolDescription
shz_clampfClamps a floating-point value by the given min and max values.
shz_normalizefMaps a value within the given range from to to, to be within the range of 0.0f += 1.0f.
shz_normalizef_fsrraMaps 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_remapfMaps a value within the given range inputStart to inputEnd, to be within the range of outputStart to outputEnd.
shz_remapf_fsrraMaps 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_wrapfWraps the given value back to be within the range of min to max.
shz_wrapf_fsrraWraps the given value back to be within the range of min to max more quickly, provided max - min is a positive difference.
shz_fractfReturns the fractional part of x, equivalent to GLSL fract().
shz_signfReturns -1.0f if x < 0, 0.0f if x == 0, or 1.0f if x > 0.
shz_saturatefClamps x to the range [0.0f, 1.0f].

Assorted routines implementing other fp operations.

SymbolDescription
shz_fabsfReplacement for the <math.h> routine, fabsf().
shz_copysignfReplacement for the <math.h> routine, copysignf().
shz_fmafReplacement for the <math.h> routine, fmaf().
shz_fdimfReplacement for the <math.h> routine, fdimf(),.
shz_hypotfReplacement for the <math.h> routine, hypotf().
shz_cbrtfReplacement for the <math.h> routine, cbrtf().
shz_lerpfReturns a value that is linearly interpolated between a and b by the given ratio, t.
shz_barycentric_lerpfReturns a value that is barycentrically interpolated between a, b, and c using the given barycentric coordinates, u and v.
shz_quadratic_rootsUses 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_randfReturns a random floating-point number between 0.0f and 1.0f, using and updating the given seed.
shz_randf_rangeReturns a random floating-point number between min and max, using and updating the given seed.
shz_stepfreturns 0.0f if x < edge, otherwise 1.0f
shz_smoothstepfReturns 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_safeReturns 0.0f at/below edge0, 1.0f at/above edge1, smoothly varying in-between. Accepts inverse edges.

Routines built around fast reciprocal square root instruction.

SymbolDescription
shz_inv_sqrtf_fsrraCalculates 1.0f/sqrtf( x), using a fast approximation.
shz_inv_sqrtfCalculates 1.0f/sqrtf( x ), using a fast approximation, while safely protecting against division-by-zero.
shz_sqrtf_fsrraReturns the fast approximate square root of the given value, x.
shz_sqrtfReturns the fast approximate square root of the given value, x, safely returning 0.0f is x == 0.0f.
shz_invf_fsrraTakes the inverse of x using a very fast approximation, returning a positive result.
shz_invfTakes the inverse of x using a slighty faster approximation than doing a full division, safely handling negative values.
shz_divf_fsrraDivides num by denom using a very fast approximation, which requires denom be a positive value.
shz_divfDivides num by denom using a slightly faster approximation, allowing denom to be negative.

Routines built around fast 4D dot product.

SymbolDescription
shz_dot6fTakes two sets of 3D vectors as 3 floats and calculates their dot product using an approximation.
shz_dot8fTakes two sets of 4D vectors as 4 floats and calculates their dot product using an approximation.
shz_mag_sqr3fTakes a 3D vector as 3 floats and calculates its squared magnitude using a fast approximation.
shz_mag_sqr4fTakes a 4D vector as 4 floats and calculates its squared magnitude using a fast approximation.

Fast approximations for non-trig transcendental functions.

SymbolDescription
shz_pow2fFast approximation for raising 2 to a floating-point power.
shz_powfFast approximation for C's powf().
shz_pow10fFast approximation of POSIX's pow10f().
shz_log2fFast approximation for C's log2f().
shz_logfFast approximation for C's logf().
shz_log10fFast approximation for C's log10f().
shz_expfFast approximation for C's expf().
SymbolDescription
SHZ_FLT_EPSILONFloating-point epsilon used with inexact FP-based comparisons.