AMReX-Astro / Microphysics

common astrophysical microphysics routines with interfaces for the different AMReX codes
https://amrex-astro.github.io/Microphysics
Other
35 stars 34 forks source link

Fast log and pow implementation #1591

Closed zhichen3 closed 3 months ago

zhichen3 commented 3 months ago

Fast log implementation by inverting fast_exp, roughly 10 times faster than std::log, but it doesn't check bounds like inf and 0. With additional if-statements, it would be slower, but still ~5 times faster than std::log

This is discussed in: https://martin.ankerl.com/2007/10/04/optimized-pow-approximation-for-java-and-c-c/

Also adds fast_pow implementation by combining fast_exp and fast_log, this is the vfast_pow function. It is roughly 30 times faster than std::pow but poor accuracy when exponent is >> 1.

If we combine the method of exponentiation by squaring for solving the integer part and only use approximation for the fraction part of the exponent, then we get overall ~3% relative error for all exponents, but its a lot slower than vfast_pow, but still ~5 times faster than std::pow.