std.math performance (SSE vs. real)

H. S. Teoh via Digitalmars-d digitalmars-d at puremagic.com
Mon Jun 30 10:48:13 PDT 2014


On Mon, Jun 30, 2014 at 05:35:34PM +0000, Tofu Ninja via Digitalmars-d wrote:
> On Monday, 30 June 2014 at 16:29:08 UTC, H. S. Teoh via Digitalmars-d wrote:
> >What's the status of repainting unions in CTFE? Is there a PR for
> >that yet, or do we need to implement one?
> >
> >
> >T
> 
> What exactly does "repainting a union" mean? I am not familiar with
> that phrase, google has not helped me either.

I may have misused the term, perhaps the proper term is "painting"?

Anyway, it means to write data of one type to the union, and read out a
different type. E.g., write a double into a union of double and
ubyte[double.sizeof], and then read out the latter, in order to get at
the bit representation of the double without using pointer casting:

	ubyte[double.sizeof] getRepresentation(double d) {
		union U {
			double d;
			ubyte[double.sizeof] rep;
		}
		U u;
		u.d = d;
		return u.rep;
	}

(This is a silly example, of course, since in assembly it amounts to a
function that just returns its argument. Usually you do something
non-trivial with u.rep, such as extract the sign bit or mantissa, and so
forth.)

Right now, this doesn't work in CTFE (it implicitly type-tags all
unions, and raises an error when you try to read out a different type
than you wrote in). But this kind of type-punning is needed to implement
some of the fundamental floating-point operations.


T

-- 
Question authority. Don't ask why, just do it.


More information about the Digitalmars-d mailing list