fast approximate float function fmath
fast exp(x) and log(x) of float x
What's this?
fmath.hpp provides a fast approximate exp(x)/log(x) of float x.
The function is is 2~10 times faster than std::ofVC10/gcc on Core2Duo.
fmath.hpp is available on 32-bit/64-bit Visual Studio 2008/2010 and 32-bit/64-bit gcc 4.1 or later.
Download
Install
Copy
fmath.hpp into your favorite directory.
If you install
Xbyak and define FMATH_USE_XBYAK before including fmath.hpp,
then fmath::exp() and fmath::exp_ps() (uses SSE4.1 if available) will be 10~20% faster.
Remark: Xbyak requires -fno-operator-names for gcc.
How to use
The following functions are available.
prototype
name | description |
float fmath::exp(float x) | exp |
double fmath::expd(double x) | exp for double [New] |
void fmath::expd_v(double *px, int n) | exp for array of double [New] |
float fmath::log(float x) | log |
__m128 fmath::exp_ps(__m128 x) | SIMD version of exp |
__m128 fmath::log_ps(__m128 x) | SIMD version of log |
You can also use fmath::log2(x) (=log(x) / log(2)) and fmath::exp2(x) (=pow(2, x)).
fmath::PowGenerator is a class to generate a function to compute pow(x, y)
of x >= 0 for a given fixed y > 0.
// example
fmath::PowGenerator f(1.234);
f.get(x) returns pow(x, 1.234);
Benchmark of exp and log
Measure the time of the following code:
float sum = 0;
for (float x = 0; x <= 2; x += 1e-6f) {
sum += fmath::exp(x);
}
Core2duo 2.6GHz unit:cpu clock
| VC10@32-bit Xp | VC10@64-bit Xp | gcc 4.4.3@64-bit Linux |
std::expf | 150.1 | 33.0 | 591.5 |
fmath::exp | 20.3 | 14.1 | 14.6 |
std::expf x 4 | 585.5 | 133.3 | 2608.5 |
fmath::exp_ps | 54.3 | 46.7 | 73.9 |
std::logf | 77.0 | 31.8 | 91.7 |
fmath::log | 21.1 | 13.3 | 13.9 |
std::logf x 4 | 286.1 | 136.1 | 356.3 |
fmath::log_ps | 51.2 | 40.1 | 55.8 |
compile option:
- cl /Ox /Ob2 /arch:SSE2 /fp:fast
- gcc -O3 -fomit-frame-pointer -mfpmath=sse -mssse2 -ffast-math -march=core2
The exp of gcc seems very slow.
See fastexp.cpp for the benchmark of fmath::expd.
Benchmark of pow(x, 1.234)
Core2duo 1.8GHz unit:cpu clock
| VC10@32-bit Xp | gcc 4.3.2@32-bit cygwin |
std::pow(x, 1.234) | 182.806 | 228.132 |
fmath::PowGenerator(1.234) | 9.996 | 11.636 |
License
History
2010/Aug/27 add fmath::expd, fmath::expd_v for double precision
2010/Feb/16 add fmath::exp_ps, log_ps and optimize functions
2010/Jan/10 add fmath::PowGenerator
2009/Dec/28 add fmath::log()
2009/Dec/09 support cygwin
2009/Dec/08 first version
1st:2009/12/08, last update:2016/08/29
mailto:MITSUNARI Shigeo<herumi@nifty.com>