Sound Byte Libs 0.5.1-340-gab5b79a
C++ firmware library for audio applications on 32-bit ARM Cortex-M processors
Loading...
Searching...
No Matches
fast_math.hpp
Go to the documentation of this file.
1/**
2 * @file fast_math.hpp
3 * @brief Fast analytical approximations of sin, tan, 2^x and tanh
4 *
5 * Pure arithmetic, with no lookup tables, for hot audio paths where libm
6 * calls cost too much. Each function states its domain and accuracy.
7 *
8 * @code
9 * float g = sbl::dsp::math::fast_tan_pif(fc / fs); // TPT integrator gain
10 *
11 * // Exponential modulation (LFO -> filter cutoff):
12 * float mod_cutoff = cutoff * sbl::dsp::math::fast_exp2f(lfo * depth_oct);
13 * @endcode
14 */
15
16#ifndef SBL_DSP_MATH_FAST_MATH_HPP_
17#define SBL_DSP_MATH_FAST_MATH_HPP_
18
19#include <cstring>
20
22
23namespace sbl::dsp::math {
24
25inline constexpr float FAST_TAN_PIF_MAX_F = 0.497f; /**< fast_tan_pif is valid below this normalized frequency */
26inline constexpr float EXP2_MAX_ARG = 16.0f; /**< fast_exp2f clamps |x| here */
27inline constexpr int32_t FLOAT_MANTISSA_BITS = 23; /**< Mantissa width of an IEEE 754 single */
28inline constexpr float TANH_SATURATE_X = 3.0f; /**< fast_tanhf returns +/-1 beyond this */
29
30/**
31 * @brief Fast sine approximation for x in [0, pi/2]
32 *
33 * Uses the 5th-order Taylor series x - x^3/6 + x^5/120.
34 *
35 * Absolute error, which grows with x:
36 * @code
37 * x = 0.0 -> error 0
38 * x = 0.589 -> error 4.9e-6
39 * x = pi/2 -> error 0.0045 (the maximum in the domain)
40 * @endcode
41 *
42 * @param x Input angle in radians, in [0, pi/2] (not checked)
43 * @return Approximation of sin(x)
44 */
45inline float fast_sinf(float x) {
46 // Taylor: x - x^3/6 + x^5/120
47 float x2 = x * x;
48 return x * (1.0f - x2 * (1.0f / 6.0f - x2 * (1.0f / 120.0f)));
49}
50
51/**
52 * @brief Fast tan(pi f) for normalized frequency f in [0, 0.497]
53 *
54 * [5,4] Pade approximant of tan(x) evaluated at x = pi f:
55 * @code
56 * tan(x) ~= x (945 - 105 x^2 + x^4) / (945 - 420 x^2 + 15 x^4)
57 * @endcode
58 *
59 * Unlike a polynomial, the rational function models the pole at f = 0.5
60 * (Nyquist): its denominator goes to zero near x = pi/2.
61 *
62 * Relative error against tan(pi f), which grows with f:
63 * @code
64 * f = 0.10 (4.8 kHz at 48 kHz) -> error 1.1e-5%
65 * f = 0.35 (16.8 kHz) -> error 6.8e-5%
66 * f = 0.45 (21.6 kHz) -> error 0.0025%
67 * f = 0.497 (23.9 kHz) -> error 0.115%
68 * @endcode
69 *
70 * @param f Normalized frequency (freq_hz / sample_rate), in [0, 0.497] (not checked)
71 * @return Approximation of tan(pi f)
72 */
73inline float fast_tan_pif(float f) {
74 constexpr float PI2 = sbl::math::PI * sbl::math::PI;
75 constexpr float PI4 = PI2 * PI2;
76 float f2 = f * f;
77 float f4 = f2 * f2;
78 // Pade for tan(x), x = pi f: x (945 - 105 x^2 + x^4) / (945 - 420 x^2 + 15 x^4)
79 float num = 945.0f - 105.0f * PI2 * f2 + PI4 * f4;
80 float den = 945.0f - 420.0f * PI2 * f2 + 15.0f * PI4 * f4;
81 return sbl::math::PI * f * num / den;
82}
83
84/**
85 * @brief Fast 2^x approximation for exponential modulation
86 *
87 * Decomposes x into integer and fractional parts. The integer part is
88 * applied by shifting the IEEE 754 exponent field (exact). The fractional
89 * part uses a 4th-order polynomial.
90 *
91 * Accuracy against std::exp2f:
92 * @code
93 * |x| <= 16 -> max relative error 0.092%, just below each whole x
94 * x > 16 -> clamped to 16 (returns 65536)
95 * x < -16 -> returns 0
96 * @endcode
97 *
98 * @param x Exponent, e.g. +/-2.0 for +/-2 octaves of modulation
99 * @return Approximation of 2^x
100 */
101inline float fast_exp2f(float x) {
102 // Clamp to prevent overflow/underflow
103 if (x < -EXP2_MAX_ARG) {
104 return 0.0f;
105 }
106 if (x > EXP2_MAX_ARG) {
107 x = EXP2_MAX_ARG;
108 }
109
110 // Decompose into integer and fractional parts
111 // Use truncation toward negative infinity
112 int i = static_cast<int>(x);
113 float f = x - static_cast<float>(i);
114 if (f < 0.0f) {
115 f += 1.0f;
116 --i;
117 }
118
119 // 4th-order polynomial for 2^f, f in [0, 1): the Taylor series with C3 and C4 adjusted
120 constexpr float C1 = 0.6931472f; // ln(2)
121 constexpr float C2 = 0.2402265f; // ln(2)^2 / 2
122 constexpr float C3 = 0.0558011f; // near ln(2)^3 / 6 = 0.0555041
123 constexpr float C4 = 0.00898f; // near ln(2)^4 / 24 = 0.0096181
124 float p = 1.0f + f * (C1 + f * (C2 + f * (C3 + f * C4)));
125
126 // Apply integer exponent via IEEE 754 bit manipulation. i is in [-16, 16], so the product fits; a left shift of
127 // a negative i would be undefined.
128 int32_t bits = 0;
129 std::memcpy(&bits, &p, sizeof(bits));
130 bits += i * (int32_t{1} << FLOAT_MANTISSA_BITS);
131 std::memcpy(&p, &bits, sizeof(p));
132 return p;
133}
134
135/**
136 * @brief Fast tanh approximation using a [3,2] Pade approximant
137 *
138 * tanh(x) ~= x (27 + x^2) / (27 + 9 x^2) for |x| <= 3. Inputs outside
139 * +/-3 return +/-1, and a NaN returns 0. Max relative error 2.6%, near
140 * |x| = 1.45. SoftLimiter bends its excess over a knee with it.
141 *
142 * @param x Input value, any range
143 * @return Approximation of tanh(x), in [-1, 1]
144 */
145inline float fast_tanhf(float x) {
146 if (x < -TANH_SATURATE_X) {
147 return -1.0f;
148 }
149 if (x > TANH_SATURATE_X) {
150 return 1.0f;
151 }
152 if (x != x) {
153 return 0.0f; // NaN -> 0
154 }
155 // [3,2] Pade: coefficients 27 = 3^3 and 9 = 3^2
156 float x2 = x * x;
157 return x * (27.0f + x2) / (27.0f + 9.0f * x2);
158}
159
160/**
161 * @brief 1 - e^(-x) by inverse Taylor, with no expf
162 *
163 * Gives a one-pole coefficient from a time constant or a cutoff.
164 *
165 * @param x Argument, at or above 0
166 * @return 1 - 1 / (1 + x + x^2/2 + x^3/6), in [0, 1)
167 */
168inline constexpr float one_minus_exp_neg(float x) {
169 const float denom = 1.0f + x * (1.0f + x * (0.5f + x * (1.0f / 6.0f)));
170 return 1.0f - 1.0f / denom;
171}
172
173} // namespace sbl::dsp::math
174
175#endif // SBL_DSP_MATH_FAST_MATH_HPP_
The numbers every layer reaches for.
Math for the audio stack.
Definition crossfade.hpp:18
constexpr int32_t FLOAT_MANTISSA_BITS
Definition fast_math.hpp:27
float fast_sinf(float x)
Fast sine approximation for x in [0, pi/2].
Definition fast_math.hpp:45
float fast_exp2f(float x)
Fast 2^x approximation for exponential modulation.
constexpr float EXP2_MAX_ARG
Definition fast_math.hpp:26
float fast_tan_pif(float f)
Fast tan(pi f) for normalized frequency f in [0, 0.497].
Definition fast_math.hpp:73
constexpr float one_minus_exp_neg(float x)
1 - e^(-x) by inverse Taylor, with no expf
constexpr float FAST_TAN_PIF_MAX_F
Definition fast_math.hpp:25
constexpr float TANH_SATURATE_X
Definition fast_math.hpp:28
float fast_tanhf(float x)
Fast tanh approximation using a [3,2] Pade approximant.
constexpr float PI
Definition constants.hpp:17