__ieee754_exp_avx from libm*.so being used intensively in a certain source code, I would like to replace it with a faster exp(x) implementation?
custom exp(x):
inline
double exp2(double x) {
x = 1.0 + x / 1024;
x *= x; x *= x; x *= x; x *= x;
x *= x; x *= x; x *= x; x *= x;
x *= x; x *= x;
return x;
}
What gcc tags should I use to make gcc automatically use a custom exp(x) implementation? If it is not possible with gcc how can I do it then?
https://codingforspeed.com/using-faster-exponential-approximation/
Don't. This function is slower than the native implementation of exp, and is an extremely poor approximation.
First, the speed. My benchmarking indicates that, depending on your compiler and CPU, this implementation of exp2 may be anywhere between 1.5x and 4.5x slower than the native exp. I'm not sure where the web site got their figures -- "360 times faster than the traditional exp" seems absurd, and is completely inconsistent with my tests.
Second, the accuracy. exp2(x) is reasonably close to exp(x) for x ≤ 1, but fails badly for larger values. For instance:
exp(1) = 2.7182818
exp2(1) = 2.7169557 (0.05% too low)
exp(2) = 7.3890561
exp2(2) = 7.3746572 (0.20% too low)
exp(5) = 148.41316
exp2(5) = 146.61829 (1.21% too low)
exp(10) = 22026.466
exp2(10) = 20983.411 (4.74% too low)
exp(20) = 4.851652e+08
exp2(20) = 4.0008755e+08 (17.5% too low)
While the web site you got this function from claims that there is "very good agreement for input smaller than 5", this is simply not true. A 1.21% difference (for x=5) is huge, and is likely to cause significant errors in any calculations using this approximation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With