Bug 19712 – std.math.sin and cos cast float to real before evaluating.

Status
RESOLVED
Resolution
DUPLICATE
Severity
minor
Priority
P1
Component
phobos
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2019-03-01T12:33:09Z
Last change time
2021-01-09T22:34:14Z
Assigned to
No Owner
Creator
Alex

Comments

Comment #0 by alex — 2019-03-01T12:33:09Z
import std.stdio; import std.math; import std.random; import core.stdc.math; import std.datetime; void main() { auto r = benchmark!( ()=> std.math.sin(uniform(0.0f,1.0f)) , ()=> core.stdc.math.sinf(uniform(0.0f,1.0f)) )(1000000); writefln("%s\n%s",r[0].usecs,r[1].usecs); } With gdc-8, this shows around a 4 times difference in speed between std.math.sin and core.stdc.math.sinf. Around 1.5 times difference with dmd via dlang.org web interface This is because std.math.sin(float) casts the argument to real and then evaluates it. Apparently ldc does this better.
Comment #1 by greensunny12 — 2019-03-01T12:52:32Z
> With gdc-8, this shows around a 4 times difference in speed between std.math.sin and core.stdc.math.sinf. FYI: GDC has a separate bug tracker https://bugzilla.gdcproject.org/describecomponents.cgi > Apparently ldc does this better. Not apparently, it's has the same speed as the C runtime: https://run.dlang.io/is/4AweFj
Comment #2 by alex — 2019-03-01T13:16:40Z
I put the bug here because it is caused by phobos. Ldc has added a version(ldc) section that removes the casts to real and implements sin and cos differently depending on precision. if I can get around it by calling a different sin function in phobos, then I think it is a phobos bug, but there is some interaction with the compilers
Comment #3 by pro.mathias.lang — 2021-01-09T22:34:14Z
Duplicate of 18559 *** This issue has been marked as a duplicate of issue 18559 ***