Why is std.math slower than the C baseline?

jmh530 john.michael.hall at gmail.com
Thu Jun 4 17:25:51 UTC 2020


On Thursday, 4 June 2020 at 17:04:54 UTC, H. S. Teoh wrote:
> On Thu, Jun 04, 2020 at 04:49:38PM +0000, kinke via 
> Digitalmars-d wrote:
>> On Thursday, 4 June 2020 at 14:14:22 UTC, Andrei Alexandrescu 
>> wrote:
>> > D should just use the C functions when they offer better 
>> > speed.
>> 
>> They don't, or at least, not always, and can be beaten (e.g., 
>> by inlinable D implementations). The culprit is Phobos, 
>> apparently originating from the pre-SSE world and hence mostly 
>> focused on reals, many times not even offering single and 
>> double precision overloads (and if offered, many times casting 
>> and using the real version).
>
> Yes, std.math seriously needs to offer single/double precision 
> overloads that do NOT cast to real. This is a significant 
> performance degrader in compute intensive software, and makes D 
> look antiquated.  Leave the real overload there for when people 
> are obsessed with the extra precision, but if the caller passes 
> in a double, please oh please don't just forward it to the real 
> overload!
>

mir.math.common [1] calls ldc/gcc intrinsics on LDC/GDC. On DMD, 
std.math function are primarily called. There are some DMD 
std.math functions that don't have overloads for float/double. In 
those cases, it calls std.math for CTFE, for types larger than 
double, or for impure C functions. Otherwise it calls the C 
function.

>
> [...]
>> The latest LDC beta also comes with a significantly improved 
>> core.math.ldexp, easily beating the C version on my system 
>> (inlinable, no errno handling...).
>
> Would LDC consider providing float/double overloads for 
> std.math functions?  Ideally this would be merged upstream, but 
> sometimes if there's no other way to get things done...
>
>
> T

LDC has intrinsics that I mentioned above that mir.math.common 
uses. However, I'm not sure how they are implemented...

[1] 
https://github.com/libmir/mir-core/blob/7b896e5b1a2ce1e78d672889bb8bbb649f1d748c/source/mir/math/common.d#L293


More information about the Digitalmars-d mailing list