Replacing std.math raw pointer arithmetic with a union type

H. S. Teoh via Digitalmars-d digitalmars-d at puremagic.com
Tue May 16 21:25:30 PDT 2017


On Wed, May 17, 2017 at 03:02:02AM +0000, tsbockman via Digitalmars-d wrote:
> std.math contains a lot of raw pointer arithmetic for accessing the
> various bit fields of floating-point values (see
> https://en.wikipedia.org/wiki/IEEE_754-1985). Much of this code has
> several nearly-identical copies, manually specialized for each
> supported floating-point format.
> 
> Such code is verbose, hard to read, and, judging by the bugs and
> missing specializations I've found, hard to write correctly. It is
> also not compatible with CTFE.
> 
> A while ago I created Phobos #4336
> (https://github.com/dlang/phobos/pull/4336), which begins the process
> of replacing all the pointer arithmetic in std.math, and the
> supporting floatTraits template, using a fast union type: RealRep.
> 
> Ian Buclaw recently approved my work, but I believe that a pull
> request of this size and importance should be review by at least one
> other qualified person.
> 
> Would any of our other floating-point experts be willing to take a
> look?

Nowhere near a floating-point "expert" here, but I think one of the big
blockers for CTFE right now is that none of the ways of accessing the
underlying bits in the floating-point representation are supported:
neither pointer arithmetic nor unions are supported. Well, the current
CTFE engine does support unions, but you can only read values of the
same type that you write into it, so you can't use it to get at the bit
representation of floating-point values. And AFAIK, Stefan's new CTFE
engine won't be supporting unions until the last stage (floating-point
support is scheduled to be implemented last because of anticipated
complexities), so it will be a while before we have any hope of std.math
working in CTFE.

Having said that, though, prepping Phobos to be more maintainable is
always a good thing, and avoiding pointer arithmetic means it's more
likely to be supported by the new CTFE engine eventually, so in
principle I like the idea, even though I don't think I'm qualified to
officially approve it.


T

-- 
Tell me and I forget. Teach me and I remember. Involve me and I understand. -- Benjamin Franklin


More information about the Digitalmars-d mailing list