www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 9844] New: DMD (-m64) int->long conversion bug

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9844

           Summary: DMD (-m64) int->long conversion bug
           Product: D
           Version: D2
          Platform: x86_64
        OS/Version: Linux
            Status: NEW
          Severity: blocker
          Priority: P5
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: safety0ff.bugz gmail.com



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)

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Mar 30 2013
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9844




Also, adding the explicit "L" integer suffix does not help.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Mar 31 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9844




When passing the -O (optimize) flag, the issue does not manifest itself.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Mar 31 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9844


safety0ff.bugz gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Priority|P5                          |P1


-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Apr 01 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9844


John Colvin <john.loughran.colvin gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |john.loughran.colvin gmail.
                   |                            |com



20:05:58 BST ---
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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Apr 03 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9844





 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. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Apr 03 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9844




Created an attachment (id=1206)
this kludge makes things work for me

This kludge makes things work for me.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Apr 04 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9844


Index Int <vlad.z.4096 gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |vlad.z.4096 gmail.com



---
The recent version of LDC (based on DMD v2.062 and LLVM 3.3svn) does not suffer
from this issue.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Apr 04 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9844


Walter Bright <bugzilla digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bugzilla digitalmars.com
            Version|D2                          |D1 & D2
         OS/Version|Linux                       |All



01:15:49 PDT ---
https://github.com/D-Programming-Language/dmd/pull/1846

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Apr 05 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9844




Commits pushed to master at https://github.com/D-Programming-Language/dmd

https://github.com/D-Programming-Language/dmd/commit/34706176b888442c11d426fa98852a13c3a6b342
fix Issue 9844 - DMD (-m64) int long initialisation bug

https://github.com/D-Programming-Language/dmd/commit/2e1547d1bcea7cfae6f9489343bc15e6a193cba6


fix Issue 9844 - DMD (-m64) int long initialisation bug

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Apr 07 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9844


Martin Nowak <code dawg.eu> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |code dawg.eu
         Resolution|                            |FIXED


-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Apr 07 2013
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9844


safety0ff.bugz <safety0ff.bugz gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |g.sayol yahoo.es



20:48:36 PDT ---
*** Issue 9331 has been marked as a duplicate of this issue. ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Oct 04 2013