Getting action on your favorite D issues

Patrick Schluter Patrick.Schluter at bbox.fr
Sat Jun 6 10:09:07 UTC 2020


On Friday, 5 June 2020 at 23:53:28 UTC, H. S. Teoh wrote:
> On Fri, Jun 05, 2020 at 11:57:39PM +0200, ag0aep6g via 
> Digitalmars-d wrote: [...]
>> 2) "bool can be both true and false" 
>> https://issues.dlang.org/show_bug.cgi?id=20148
>> 
>> This one is just silly.
> [...]
>
> Nah, it's not silly, it's just that DMD's backend produces code 
> that exhibits quantum uncertainty:
>
> -----------------
> bool schrodingersCat() @safe {
> 	union Box { bool b; int spin; }
> 	Box u;
> 	u.spin = 2;
> 	return u.b;
> }
> void main() @safe {
> 	import std.stdio;
> 	bool b = schrodingersCat();
> 	if (b) writeln("alive");
> 	if (!b) writeln("dead");
> }
> -----------------
>
> Output:
> -----------------
> alive

TEST AL,AL gives "not zero"

> dead

XOR AL,1 gives "not zero"

xor al,1 is faster than
NOT AL  => stupid Intel prefered to not change flags
TEST AL,AL


> -----------------
>
> :-D
>
> Unfortunately, LDC's backend seems a lot more classical, as the 
> above code produces this output with absolute certainty:

optimizing LDC is so smart that it compiled main down to

writeln("dead");

non optimizing it inserts a (b&1) before testing which explains 
while the optimizer completely remove the test.


GDC does a cmp,0 for the first test and then a xor 1+test 
unoptimized but when optimizing it only does first condition with 
a TEST AL,AL but then prints always the second string.

>
> Output:
> -----------------
> dead
> -----------------
>
> Truly, DMD's backend is in the bright future of strange and 
> wonderful quantum effects, whereas LDC is clearly still stuck 
> in the antiquated classical past. ;-)
>
Sorry, but undefined behaviour is undefined behaviour. The dmd 
code generator is not at fault here.




More information about the Digitalmars-d mailing list