[dmd-internals] Pure CTFE-able std.math

Iain Buclaw ibuclaw at ubuntu.com
Mon Jul 15 02:46:29 PDT 2013


Don,

I'm in the middle of doing pure real implementations of the elementary
math functions in std.math in D for the @safe/pure/nothrow routines
that call impure extern(C) mathlib as a fallback if D_InlineAsm is not
defined.

One nice side effect of this is that the functions are now CTFE-able -
well... almost. :o)

The current blocker at the moment where CTFE support would be greatly
appreciated is in the functions that require bit set/testing.  In
particular: isInfinity, floor, and ceil.

With isInfinity, could perhaps do an alternate test:

if (__ctfe)
  return (x / 2) == x;


But for floor and ceil, at least one of the following must be
available in CTFE to allow setting bits.

----
// Cannot convert &real to ushort* at compile time
real y = x;
ushort* sh = cast(ushort*)&y;
----

----
// Cannot convert &real to ushort[8LU]* at compile time
enum RSIZE_SHORT = real.sizeof/ushort.sizeof;
ushort[RSIZE_SHORT] sh = *cast(ushort[RSIZE_SHORT]*)&x;  // _d_arraycopy
/* ... */
// reinterpreting cast from immutable(ushort[8LU]) to real* is not
supported in CTFE
real y = *cast(real*)&sh;
----

----
// Unions with overlapping fields are not yet supported in CTFE
union U
{
    real y;
    ushort[RSIZE_SHORT] sh;
}
U u = { x };
----


Now, the pressing question is, which method do you prefer / would like
to see in CTFE first. :o)


Thanks,
--
Iain Buclaw

*(p < e ? p++ : p) = (c & 0x0f) + '0';


More information about the dmd-internals mailing list