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
Commit pushed to master at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/234fa182b11aaf7e9c87d5b4ad85a1121691e61a Fix issue 9170 CTFE: Allow reinterpret casts float <-> int Only allows the special case *(cast(U*))&e, where e is an expression of type T, T.sizeof == U.sizeof, one type is integral (int, uint, long, ulong) and the other is floating point (float, double, ifloat, idouble). Purpose: allow implementation of HalfFloat.
Comment #2 by bugzilla — 2012-12-18T12:40:09Z
Comment #3 by github-bugzilla — 2013-04-18T11:23:02Z
Commit pushed to dmd-1.x at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/41bc0a847e8390a5c8ae61f013a1b8dc7926a863 Fix issue 9170 CTFE: Allow reinterpret casts float <-> int Only allows the special case *(cast(U*))&e, where e is an expression of type T, T.sizeof == U.sizeof, one type is integral (int, uint, long, ulong) and the other is floating point (float, double, ifloat, idouble). Purpose: allow implementation of HalfFloat.