Float values are wrong in union

Cauterite via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Aug 21 21:52:40 PDT 2016


On Monday, 22 August 2016 at 04:37:50 UTC, stunaep wrote:
> I made a union to convert between int bits and floats, but the 
> values are coming out wrong sometimes.

I can already tell what this is going to be...
The problem is almost certainly nothing to do with your union, 
it's this line:
> float t2 = t.f;
This will load 0x7fb00000 into ST(0), which instantly converts it 
to 7FF00000 because it's a signalling NaN, then store ST(0) in 
your float `t2`.

Signalling NaNs are an ongoing problem in D's codegen. See Don's 
remarks at this page: 
https://issues.dlang.org/show_bug.cgi?id=16105#c2

The reason it works in other languages is because they don't 
place floats in the floating point registers for non-arithmetic 
operations. I've been trying to patch DMD's backend to behave 
this way too, but it's much harder than I expected (difficult 
codebase to understand/navigate).


More information about the Digitalmars-d-learn mailing list