[dmd-internals] math intrinsics

Don Clugston dclugston at googlemail.com
Sat Apr 2 13:35:28 PDT 2011


On 2 April 2011 21:35, Brad Roberts <braddr at puremagic.com> wrote:
> Yes, std.math publicly imports core.math in the pull requests I submitted last night.
>
> Can you point to concrete examples / bug reports for the optlink issue?

No, but I know Tango changed the names of all such modules because of it.

> Intrinsics aren't compiler specific, they tend to be platform specific.  But even when not done as a builtin, they can
> just as easily be written a d code or asm in the runtime.

No, that's not correct. It's particularly untrue for the math
functions. DMD does the trig functions as intrinsics using the x87
instructions. But they are slow, and not accurate in some
circumstances. It's arguably better to use SSE2 (especially on 64
bits). Try sin(2^^65)   --- the intrinsic returns 2^^65, ie absolute
garbage!

If instead, they are implemented with d code or asm in the runtime,
you end up with code duplication. If the functions are not intrinsics,
they will be implemented with std.math.poly.

Note that even the intrinsics for GDC are different to the ones for
DMD (I think exp() is an intrinsic).

The question I have, is, what does this extra complexity and code
duplication buy you?
(If there was a single module, core.intrinsic, which included all
intrinsics which reduce to a single machine instruction, I think it
would be OK -- that is clearly CPU specific; and you could even test
for existence of particular intrinsics using reflection. This would
even allow the use of (for example) the x87 sin() function, even if
std.math implemented sin() in a slower but more accurate way).
But I think putting all math intrinsics in a module is something I'm
sure we'd later regret.


>
> For what it's worth, I created the pull requests for all three packages and I've tested it on both linux32 and win32 and
> everything passes just fine.
>
> Later,
> Brad
>
> On 4/2/2011 8:42 AM, Don Clugston wrote:
>> There have been linking problems (at least with optlink) when two
>> modules have the same filename. I'm a bit wary of the name core.math.
>> Now that I think about it a bit more, I've changed my mind. I don't
>> think it's a good idea. The fact that (say) sin is an intrinsic is
>> DMD-specific. It won't be an intrinsic on many other compilers.
>>
>> On 2 April 2011 16:13, David Simcha <dsimcha at gmail.com> wrote:
>>> As long as core.math would be publicly imported by std.math instead of being
>>> yet another import to remember, I'm on board.
>>>
>>> On Sat, Apr 2, 2011 at 4:24 AM, Don Clugston <dclugston at googlemail.com>
>>> wrote:
>>>>
>>>> Great, if everything still works.
>>>>
>>>> On 2 April 2011 08:06, Brad Roberts <braddr at puremagic.com> wrote:
>>>>> Any objection to moving the math intrinsics from phobos std/math.d to
>>>>> druntime core/math.d similar to the bitops?  I'll
>>>>> be happy to whip up the pull requests.
>>>>> _______________________________________________
>
> _______________________________________________
> dmd-internals mailing list
> dmd-internals at puremagic.com
> http://lists.puremagic.com/mailman/listinfo/dmd-internals
>


More information about the dmd-internals mailing list