-
-
Notifications
You must be signed in to change notification settings - Fork 608
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve CFTE support for float/double/real painting #4674
Conversation
|
I don't really like the ctfe float-cast ability. If we added ctfe-able intrinsics to extract the bit pattern on the mantissa/exponent/sign bit would that be sufficient for implementing these math functions? |
|
That would work, yes. |
|
Actually we would need the ability to set them too: |
|
Setting them is easy too. I would guess they'd be the same speed as C bitfields, so they would be slower than raw pointer manipulation. Is it much of a burden to have a slow ctfe version? It should be easy to write one portable one: ulong doSomething(real f)
{
if (ctfe)
return extractMantissa(f) + extractExponent(f) + extractSign(f);
static if (f.sizeof == 10)
...
else static if (f.sizeof == 16)
...
} |
I'm not a huge fan of having to specifically write an explicit ctfe version though - it's much better if "normal" (runtime) code can just work at compile time. This PR was a stab at "just having normal code work". Else it means different versions to maintain, more sources of bugs and possible discrepancies between runtime and compile time (note that the current solution is not much better since it's /still/ different code - but all in one place, in the compiler). |
|
Tests will only work on x86. |
|
Also, FYI I did this some time ago: https://github.com/ibuclaw/dmd/commits/ctfepaint Never raise a PR because my intention is to eventually make it so that float bits are accessible via unions. With CTFE-able unions, we can start removing the error-prone |
Taking I mean really, here is the 100% portable, completely ctfe-compatible and return x != x;Adding more pointer casts and unions to ctfe is a step in the wrong direction. |
|
@yebblies like. |
|
|
@yebblies Very good point, that is much better than going the painting/union way (why is it not the only impl btw ?). However we still need a solution for other instances like ieeeMean.
@ibuclaw I disagree with that one. I should be able to use isNaN (and other std.math functions) at compile time. Even if this PR is not the right way to fix it it should be fixed. |
You are misreading me. |
@ibuclaw OK got it ! |
I'd guess the optimizer fails to reduce the floating point operation to an integer op. |
|
@legrosbuffle Will you be updating this for DDMD? |
|
@Biotronic Nope; It feels like there was no consensus on this approach. |
This extends the special case for float/int painting:
float f;
uint i = cast(uint)&f;
To code like:
float f;
ushort s1 = cast(ushort)&f;
ushort s2 = (cast(ushort*)&f)[1];
for any integer type smaller than float (resp. double and real).
This is required to make isNaN and isInfinite work at compile time in phobos (dlang/phobos#3307).