Comment #0 by safety0ff.bugz — 2013-03-30T20:01:23Z
This program:
import std.stdio;
void main() {
int a = -1;
long b = a;
long c = -1;
writeln("a = ",a);
writeln("b = ",b);
writeln("c = ",c);
assert(a==b&&b==c&&a==c);
}
Gives the following erroneous output:
a = -1
b = -1
c = 4294967295
core.exception.AssertError@buggy(9): Assertion failure
Using 64bit output from DMD 2.062 (also DMD git 6202b02f0e from DPaste.)
The following compilers/options give the expected output:
DMD 2.062 -m32
LDC 2.060 (64 bit on DPaste)
GDC 2.060 (64 bit on DPaste)
Comment #1 by safety0ff.bugz — 2013-03-31T01:58:24Z
Also, adding the explicit "L" integer suffix does not help.
Comment #2 by safety0ff.bugz — 2013-03-31T02:30:52Z
When passing the -O (optimize) flag, the issue does not manifest itself.
Comment #3 by john.loughran.colvin — 2013-04-03T12:05:58Z
A simplified test:
import std.stdio;
void foo() {
int a = -1;
long b = -1;
writeln(a); // -1
writeln(b); // (2^32)-1
}
The mistake only happens when a and b are initialised to the same negative value.
In light of that and after some perusing of the asm generated, it appears that dmd goes "-1 == -1" and uses the same value for a as for b, except of course that doesn't work because of the different sign bit positions for int and long.
e.g.
mov eax,0xffffffff //rax is now 0x00000000ffffffff
mov DWORD PTR [rbp-0x10],eax
mov QWORD PTR [rbp-0x8],rax
I don't get any bug with ldc, so it's probably either a recent regression or a backend problem.
Comment #4 by safety0ff.bugz — 2013-04-03T22:10:50Z
(In reply to comment #3)
> The mistake only happens when a and b are initialised to the same negative
> value.
>
> In light of that and after some perusing of the asm generated, it appears that
> dmd goes "-1 == -1" and uses the same value for a as for b, except of course
> that doesn't work because of the different sign bit positions for int and long.
Yes, you're right, as demonstrated in this program:
import std.stdio;
void main() {
int a = -2;
long b = -1;
long c = -2;
writeln(a); // -2
writeln(b); // -1
writeln(c); // 2^32-2
}
I suppose I should now find a better title for this issue.
Comment #5 by safety0ff.bugz — 2013-04-04T13:54:00Z
Created attachment 1206
this kludge makes things work for me
This kludge makes things work for me.
Comment #6 by vlad.z.4096 — 2013-04-04T20:34:29Z
The recent version of LDC (based on DMD v2.062 and LLVM 3.3svn) does not suffer from this issue.