SH4ZAM! 0.1.0
Fast math library for the Sega Dreamcast's SH4 CPU
Loading...
Searching...
No Matches
shz_scalar.hpp
Go to the documentation of this file.
1/*! \file
2 * \brief C++ scalar math API.
3 * \ingroup scalar
4 *
5 * This file provides a collection of general-purpose math routines for
6 * individual scalar values in C++23.
7 *
8 * \author 2025, 2026 Falco Girgis
9 * \author 2026 Aleios
10 * \copyright MIT License
11 */
12
13#ifndef SHZ_SCALAR_HPP
14#define SHZ_SCALAR_HPP
15
16#include "shz_scalar.h"
17
18namespace shz {
19 constexpr float fipr_max_error = 0.1f; // Not accurate yet. lol.
20
21 /*! \name Comparisons
22 \brief Routines for comparing and classifying floating-point values.
23 @{
24 */
25 //! C++ alias for shz_fminf()
26 constexpr auto fminf = shz_fminf;
27 //! C++ alias for shz_fmaxf()
28 constexpr auto fmaxf = shz_fmaxf;
29 //! C++ alias for shz_equalf()
30 constexpr auto equalf = shz_equalf;
31 //! C++ alias for shz_equalf_abs()
32 constexpr auto equalf_abs = shz_equalf_abs;
33 //! C++ alias for shz_equalf_rel()
34 constexpr auto equalf_rel = shz_equalf_rel;
35 //! @}
36
37 /*! \name Rounding
38 \brief Routines for rounding floats.
39 @{
40 */
41 //! C++ alias for shz_floorf().
42 constexpr auto floorf = shz_floorf;
43 //! C++ alias for shz_ceilf().
44 constexpr auto ceilf = shz_ceilf;
45 //! C++ alias for shz_roundf().
46 constexpr auto roundf = shz_roundf;
47 //! C++ alias for shz_truncf().
48 constexpr auto truncf = shz_truncf;
49 //! C++ alias for shz_remainderf().
50 constexpr auto remainderf = shz_remainderf;
51 //! C++ alias for shz_fmodf().
52 constexpr auto fmodf = shz_fmodf;
53 //! C++ alias for shz_remquof().
54 constexpr auto remquof = shz_remquof;
55 //! @}
56
57 /*! \name Mapping
58 \brief Routines for mapping a number to another range.
59 @{
60 */
61 //! C++ alias for shz_clampf().
62 constexpr auto clampf = shz_clampf;
63 //! C++ alias for shz_normalizef().
64 constexpr auto normalizef = shz_normalizef;
65 //! C++ alias for shz_normalizef_fsrra().
67 //! C++ alias for shz_remapf().
68 constexpr auto remapf = shz_remapf;
69 //! C++ alias for shz_remapf_fsrra().
71 //! C++ alias for shz_wrapf().
72 constexpr auto wrapf = shz_wrapf;
73 //! C++ alias for shz_wrapf_fsrra().
74 constexpr auto wrapf_fsrra = shz_wrapf_fsrra;
75 //! C++ alias for shz_fractf().
76 constexpr auto fractf = shz_fractf;
77 //! C++ alias for shz_signf().
78 constexpr auto signf = shz_signf;
79 //! C++ alias for shz_saturatef().
80 constexpr auto saturatef = shz_saturatef;
81 //! @}
82
83 /*! \name Miscellaneous
84 \brief Fast versions of miscellaneous FP routines.
85 @{
86 */
87 //! C++ alias for shz_fabsf().
88 constexpr auto fabsf = shz_fabsf;
89 //! C++ alias for shz_copysignf().
90 constexpr auto copysignf = shz_copysignf;
91 //! C++ alias for shz_fmacf().
92 constexpr auto fmaf = shz_fmaf;
93 //! C++ alias for shz_fdimf().
94 constexpr auto fdimf = shz_fdimf;
95 //! C++ alias for shz_hypotf().
96 constexpr auto hypotf = shz_hypotf;
97 //! C++ wrapper for shz_cbrtf().
98 SHZ_FORCE_INLINE float cbrtf(float x) noexcept { return shz_cbrtf(x); }
99 //! C++ alias for shz_lerpf().
100 constexpr auto lerpf = shz_lerpf;
101 //! C++ alias for shz_barycentric_lerpf().
103 //! C++ alias for shz_quadratic_roots().
105 //! C++ alias for shz_randf().
106 constexpr auto randf = shz_randf;
107 //! C++ alias for shz_randf_range()
108 constexpr auto randf_range = shz_randf_range;
109 //! C++ alias for shz_stepf()
110 constexpr auto stepf = shz_stepf;
111 //! C++ alias for shz_smoothstepf()
112 constexpr auto smoothstepf = shz_smoothstepf;
113 //! C++ alias for shz_smoothstepf_safe()
115
116 //! @}
117
118 /*! \name FSRRA
119 \brief Routines built around fast reciprocal square root.
120 @{
121 */
122 //! C++ alias for shz_inv_sqrtf_fsrra().
124 //! C++ alias for shz_inv_sqrtf().
125 constexpr auto inv_sqrtf = shz_inv_sqrtf;
126 //! C++ alias for shz_sqrtf_fsrra().
127 constexpr auto sqrtf_fsrra = shz_sqrtf_fsrra;
128 //! C++ alias for shz_sqrtf().
129 constexpr auto sqrtf = shz_sqrtf;
130 //! C++ alias for shz_invf_fsrra().
131 constexpr auto invf_fsrra = shz_invf_fsrra;
132 //! C++ alias for shz_invf().
133 constexpr auto invf = shz_invf;
134 //! C++ alias for shz_divf_fsrra().
135 constexpr auto divf_fsrra = shz_divf_fsrra;
136 //! C++ alias for shz_divf().
137 constexpr auto divf = shz_divf;
138 //! @}
139
140 /*! \name FIPR
141 \brief Routines built around fast 4D dot product.
142 @{
143 */
144 //! C++ alias for shz_dot6f().
145 constexpr auto dot6f = shz_dot6f;
146 //! C++ alias for shz_dot8f().
147 constexpr auto dot8f = shz_dot8f;
148 //! C++ alias for shz_mag_sqr3f().
149 constexpr auto mag_sqr3f = shz_mag_sqr3f;
150 //! C++ alias for shz_mag_sqr4f().
151 constexpr auto mag_sqr4f = shz_mag_sqr4f;
152 //! @}
153
154 /*! \name Transcendental
155 \brief Routines for accelerating transcendental functions.
156 @{
157 */
158 //! C++ alias for shz_pow2f().
159 constexpr auto pow2f = shz_pow2f;
160 //! C++ alias for shz_powf().
161 constexpr auto powf = shz_powf;
162 //! C++ alias for shz_pow10f().
163 constexpr auto pow10f = shz_pow10f;
164 //! C++ alias for shz_log2f().
165 constexpr auto log2f = shz_log2f;
166 //! C++ alias for shz_logf().
167 constexpr auto logf = shz_logf;
168 //! C++ alias for shz_log10f().
169 constexpr auto log10f = shz_log10f;
170 // C++ alias for shz_expf()
171 constexpr auto expf = shz_expf;
172 //! @}
173}
174
175#endif
Namespace enclosing the SH4ZAM C++ API.
Definition shz_cdefs.hpp:21
constexpr auto remapf
C++ alias for shz_remapf().
constexpr auto fdimf
C++ alias for shz_fdimf().
constexpr auto fmaf
C++ alias for shz_fmacf().
constexpr auto lerpf
C++ alias for shz_lerpf().
constexpr auto saturatef
C++ alias for shz_saturatef().
constexpr auto truncf
C++ alias for shz_truncf().
constexpr auto fmodf
C++ alias for shz_fmodf().
constexpr auto signf
C++ alias for shz_signf().
constexpr auto smoothstepf_safe
C++ alias for shz_smoothstepf_safe()
constexpr auto invf_fsrra
C++ alias for shz_invf_fsrra().
constexpr auto normalizef_fsrra
C++ alias for shz_normalizef_fsrra().
constexpr auto fabsf
C++ alias for shz_fabsf().
constexpr auto stepf
C++ alias for shz_stepf()
constexpr auto copysignf
C++ alias for shz_copysignf().
constexpr auto remainderf
C++ alias for shz_remainderf().
constexpr auto wrapf_fsrra
C++ alias for shz_wrapf_fsrra().
constexpr auto powf
C++ alias for shz_powf().
constexpr auto roundf
C++ alias for shz_roundf().
constexpr auto fminf
C++ alias for shz_fminf()
constexpr auto fmaxf
C++ alias for shz_fmaxf()
constexpr auto fractf
C++ alias for shz_fractf().
constexpr auto quadratic_roots
C++ alias for shz_quadratic_roots().
constexpr auto clampf
C++ alias for shz_clampf().
constexpr auto ceilf
C++ alias for shz_ceilf().
constexpr auto log2f
C++ alias for shz_log2f().
constexpr auto pow2f
C++ alias for shz_pow2f().
constexpr auto dot8f
C++ alias for shz_dot8f().
constexpr auto randf
C++ alias for shz_randf().
constexpr auto pow10f
C++ alias for shz_pow10f().
constexpr auto equalf_abs
C++ alias for shz_equalf_abs()
constexpr auto log10f
C++ alias for shz_log10f().
constexpr auto divf_fsrra
C++ alias for shz_divf_fsrra().
constexpr auto remapf_fsrra
C++ alias for shz_remapf_fsrra().
float cbrtf(float x) noexcept
C++ wrapper for shz_cbrtf().
constexpr auto sqrtf
C++ alias for shz_sqrtf().
constexpr auto floorf
C++ alias for shz_floorf().
constexpr auto remquof
C++ alias for shz_remquof().
constexpr auto sqrtf_fsrra
C++ alias for shz_sqrtf_fsrra().
constexpr auto smoothstepf
C++ alias for shz_smoothstepf()
constexpr auto randf_range
C++ alias for shz_randf_range()
constexpr auto invf
C++ alias for shz_invf().
constexpr auto dot6f
C++ alias for shz_dot6f().
constexpr auto divf
C++ alias for shz_divf().
constexpr auto logf
C++ alias for shz_logf().
constexpr auto hypotf
C++ alias for shz_hypotf().
constexpr auto equalf
C++ alias for shz_equalf()
constexpr auto equalf_rel
C++ alias for shz_equalf_rel()
constexpr auto normalizef
C++ alias for shz_normalizef().
constexpr auto inv_sqrtf_fsrra
C++ alias for shz_inv_sqrtf_fsrra().
constexpr auto inv_sqrtf
C++ alias for shz_inv_sqrtf().
constexpr auto barycentric_lerpf
C++ alias for shz_barycentric_lerpf().
constexpr auto mag_sqr3f
C++ alias for shz_mag_sqr3f().
constexpr auto wrapf
C++ alias for shz_wrapf().
constexpr auto mag_sqr4f
C++ alias for shz_mag_sqr4f().
float shz_mag_sqr3f(float x, float y, float z) SHZ_NOEXCEPT
Takes a 3D vector as 3 floats and calculates its squared magnitude using a fast approximation.
float shz_powf(float x, float p) SHZ_NOEXCEPT
Fast approximation for C's powf().
bool shz_equalf_abs(float a, float b) SHZ_NOEXCEPT
Checks for equality based on the absolute tolerance using SHZ_FLT_EPSILON.
float shz_remquof(float num, float denom, float *quot) SHZ_NOEXCEPT
(Sorta) Replacement for the <math.h> routine, remquof().
float shz_expf(float p) SHZ_NOEXCEPT
Fast approximation for C's expf().
float shz_normalizef_fsrra(float current, float from, float to) SHZ_NOEXCEPT
Maps a value within the given range from to to, to be within the range of 0.0f + 1....
float shz_randf(int *seed) SHZ_NOEXCEPT
Returns a random floating-point number between 0.0f and 1.0f, using and updating the given seed.
float shz_log10f(float x) SHZ_NOEXCEPT
Fast approximation for C's log10f().
float shz_floorf(float x) SHZ_NOEXCEPT
Replacement for the <math.h> routine, floorf().
float shz_wrapf_fsrra(float value, float min, float max) SHZ_NOEXCEPT
Wraps the given value back to be within the range of min to max more quickly, provided max - min is a...
float shz_normalizef(float current, float from, float to) SHZ_NOEXCEPT
Maps a value within the given range from to to, to be within the range of 0.0f += 1....
float shz_log2f(float x) SHZ_NOEXCEPT
Fast approximation for C's log2f().
float shz_mag_sqr4f(float x, float y, float z, float w) SHZ_NOEXCEPT
Takes a 4D vector as 4 floats and calculates its squared magnitude using a fast approximation.
float shz_smoothstepf(float x, float edge0, float edge1) SHZ_NOEXCEPT
Returns 0.0f at/below edge0, 1.0f at/above edge1, smoothly varying in-between. edge0 must be less tha...
float shz_dot8f(float x1, float y1, float z1, float w1, float x2, float y2, float z2, float w2) SHZ_NOEXCEPT
Takes two sets of 4D vectors as 4 floats and calculates their dot product using an approximation.
float shz_fmaxf(float a, float b) SHZ_NOEXCEPT
Returns the maximum value of two given floats.
float shz_signf(float x) SHZ_NOEXCEPT
Returns -1.0f if x < 0, 0.0f if x == 0, or 1.0f if x > 0.
float shz_fdimf(float x, float y) SHZ_NOEXCEPT
Replacement for the <math.h> routine, fdimf(),.
float shz_sqrtf_fsrra(float x) SHZ_NOEXCEPT
Returns the fast approximate square root of the given value, x.
float shz_pow10f(float x) SHZ_NOEXCEPT
Fast approximation of POSIX's pow10f().
bool shz_quadratic_roots(float a, float b, float c, float *root1, float *root2) SHZ_NOEXCEPT
Uses the quadratic formula with the given coefficients to solve for the two roots,...
float shz_dot6f(float x1, float y1, float z1, float x2, float y2, float z2) SHZ_NOEXCEPT
Takes two sets of 3D vectors as 3 floats and calculates their dot product using an approximation.
float shz_ceilf(float x) SHZ_NOEXCEPT
Replacement for the <math.h> routine, ceilf().
float shz_remapf_fsrra(float value, float inputStart, float inputEnd, float outputStart, float outputEnd) SHZ_NOEXCEPT
Maps a value within the given range inputStart to inputEnd, to be within the range of outputStart to ...
float shz_hypotf(float x, float y) SHZ_NOEXCEPT
Replacement for the <math.h> routine, hypotf().
float shz_copysignf(float x, float y) SHZ_NOEXCEPT
Replacement for the <math.h> routine, copysignf().
float shz_invf(float x) SHZ_NOEXCEPT
Takes the inverse of x using a slighty faster approximation than doing a full division,...
float shz_wrapf(float value, float min, float max) SHZ_NOEXCEPT
Wraps the given value back to be within the range of min to max.
float shz_saturatef(float x) SHZ_NOEXCEPT
Clamps x to the range [0.0f, 1.0f].
float shz_truncf(float x) SHZ_NOEXCEPT
Replacement for the <math.h> routine, truncf().
float shz_invf_fsrra(float x) SHZ_NOEXCEPT
Takes the inverse of x using a very fast approximation, returning a positive result.
float shz_sqrtf(float x) SHZ_NOEXCEPT
Returns the fast approximate square root of the given value, x, safely returning 0....
float shz_fmaf(float a, float b, float c) SHZ_NOEXCEPT
Replacement for the <math.h> routine, fmaf().
float shz_barycentric_lerpf(float a, float b, float c, float u, float v) SHZ_NOEXCEPT
Returns a value that is barycentrically interpolated between a, b, and c using the given barycentric ...
bool shz_equalf(float a, float b) SHZ_NOEXCEPT
Checks for equality based on EITHER the absolute tolerance or relative tolerance, using SHZ_FLT_EPSIL...
float shz_smoothstepf_safe(float x, float edge0, float edge1) SHZ_NOEXCEPT
Returns 0.0f at/below edge0, 1.0f at/above edge1, smoothly varying in-between. Accepts inverse edges.
float shz_inv_sqrtf_fsrra(float x) SHZ_NOEXCEPT
Calculates 1.0f/sqrtf( x), using a fast approximation.
float shz_cbrtf(float x) SHZ_NOEXCEPT
Replacement for the <math.h> routine, cbrtf().
bool shz_equalf_rel(float a, float b) SHZ_NOEXCEPT
Checks for equality based on the relative tolerance using SHZ_FLT_EPSILON.
float shz_fractf(float x) SHZ_NOEXCEPT
Returns the fractional part of x, equivalent to GLSL fract().
float shz_fmodf(float num, float denom) SHZ_NOEXCEPT
Replacement for the <math.h> routine, fmodf().
float shz_lerpf(float a, float b, float t) SHZ_NOEXCEPT
Returns a value that is linearly interpolated between a and b by the given ratio, t.
float shz_remainderf(float num, float denom) SHZ_NOEXCEPT
Replacement for the <math.h> routine, remainderf().
float shz_divf_fsrra(float num, float denom) SHZ_NOEXCEPT
Divides num by denom using a very fast approximation, which requires denom be a positive value.
float shz_divf(float num, float denom) SHZ_NOEXCEPT
Divides num by denom using a slightly faster approximation, allowing denom to be negative.
float shz_fminf(float a, float b) SHZ_NOEXCEPT
Returns the minimum value of two given floats.
float shz_inv_sqrtf(float x) SHZ_NOEXCEPT
Calculates 1.0f/sqrtf( x ), using a fast approximation, while safely protecting against division-by-z...
float shz_randf_range(int *seed, float min, float max) SHZ_NOEXCEPT
Returns a random floating-point number between min and max, using and updating the given seed.
float shz_clampf(float value, float min, float max) SHZ_NOEXCEPT
Clamps a floating-point value by the given min and max values.
float shz_roundf(float x) SHZ_NOEXCEPT
Replacement for the <math.h> routine, roundf().
float shz_fabsf(float x) SHZ_NOEXCEPT
Replacement for the <math.h> routine, fabsf().
float shz_logf(float x) SHZ_NOEXCEPT
Fast approximation for C's logf().
float shz_remapf(float value, float inputStart, float inputEnd, float outputStart, float outputEnd) SHZ_NOEXCEPT
Maps a value within the given range inputStart to inputEnd, to be within the range of outputStart to ...
float shz_stepf(float x, float edge) SHZ_NOEXCEPT
returns 0.0f if x < edge, otherwise 1.0f
float shz_pow2f(float p) SHZ_NOEXCEPT
Fast approximation for raising 2 to a floating-point power.