Why is std.math slower than the C baseline?

Guillaume Piolat first.last at gmail.com
Sat Jun 6 11:45:26 UTC 2020


On Thursday, 4 June 2020 at 17:25:51 UTC, jmh530 wrote:
>
> LDC has intrinsics that I mentioned above that mir.math.common 
> uses. However, I'm not sure how they are implemented...
>

Things like llvm_pow and llvm_exp actually defers to libc IIRC.
So the results varies a bit from libc to libc. And is also 
slightly different from phobos. ^^

Complicated stuff if one want to be perfectly backwards 
compatible.

Example:

     // Gives considerable speed improvement over `std.math.exp`.
     // Exhaustive testing for 32-bit `float` shows
     // Relative accuracy is within < 0.0002% of std.math.exp
     // for every possible input.
     // So a -120 dB inaccuracy max, and -140dB the vast majority 
of the time.
     alias fast_exp = llvm_exp;

This is with the Microsoft C runtime, haven't tested others.
So, this one looks relatively safe to use, -140dB is as good as 
you can go with float, but for other transcendental is might be a 
bit more shakey.

The ideal transcendental would be
    - faster than libc
    - same precision on all platforms
    - specialized by float/double size
    - within a given margin of the truth (in ulp)


More information about the Digitalmars-d mailing list