Bug 9170 – CTFE: Allow reinterpret casts float <-> int
Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P2
Component
dmd
Product
D
Version
D1 (retired)
Platform
All
OS
All
Creation time
2012-12-17T02:06:00Z
Last change time
2013-04-19T01:02:51Z
Keywords
CTFE
Assigned to
nobody
Creator
clugdbug
Comments
Comment #0 by clugdbug — 2012-12-17T02:06:33Z
Requested by Walter. This is necessary for implementing things such as half-float, and is also necessary for implementing printing of floats.
As a bare minimum, the following should be supported:
long testdouble(double x)
{
return *(cast(long*)&x);
}
int testfloat(float x)
{
return *(cast(int*)&x);
}
static assert(testfloat(1.25) == 0x3FA0_0000);
static assert(testdouble(1.25) == 0x3FF4_0000_0000_0000L);
Likewise, converting int->float and long->double should be supported.
(Supporting 80-bit reals is also ultimately necessary but is problematic since no integral type is large enough to contain the entire value. This means it inevitably exposes endianness, and also involves creating a pointer into part of a floating-point value).
Comment #1 by github-bugzilla — 2012-12-18T10:13:28Z