I’m currently working on a project that uses pow in its DSP maths, and I want to make sure it’s fast. I’ve found dsp::FastMathApproximations, but it doesn’t include pow.
https://docs.juce.com/master/structdsp_1_1FastMathApproximations.html
I’ve also found some fast approximation equations online at https://martin.ankerl.com/2012/01/25/optimized-approximative-pow-in-c-and-cpp/.
The equations are
// 4.2 times as fast as normal pow
inline double fastPow(double a, double b) {
union {
double d;
int x[2];
} u = { a };
u.x[1] = (int)(b * (u.x[1] - 1072632447) + 1072632447);
u.x[0] = 0;
return u.d;
}
~and~
// 3.3 times as fast as normal pow
// should be much more precise with large b
inline double fastPrecisePow(double a, double b) {
// calculate approximation with fraction of the exponent
int e = (int) b;
union {
double d;
int x[2];
} u = { a };
u.x[1] = (int)((b - e) * (u.x[1] - 1072632447) + 1072632447);
u.x[0] = 0;
// exponentiation by squaring with the exponent's integer part
// double r = u.d makes everything much slower, not sure why
double r = 1.0;
while (e) {
if (e & 1) {
r *= a;
}
a *= a;
e >>= 1;
}
return r * u.d;
}
Does anyone here have any experience with fast pow? What are your thoughts?