Thread overview
Compiler bug? Addition/subtraction code
Jun 20, 2017
Era Scarecrow
Jun 20, 2017
Era Scarecrow
Jun 20, 2017
Era Scarecrow
Jun 20, 2017
Era Scarecrow
Jun 20, 2017
Iain Buclaw
Jun 20, 2017
Iain Buclaw
June 20, 2017
Passes in DMD compiler, but breaks in GDC during assert tests.

B4S1L3 suggested it might be an old compiler bug. I need confirmation if this is an old (not yet caught up) or a new one (to which I'll have to write or have a work-around until the bug is fixed).

// http://dpaste.com/0M7M8NK
uint[] sub(uint[] lhs, const (uint)[] rhs) {
    long t;    //temporary, doubles as carry

    foreach(i, ref v; lhs) {
        t += v;
        t -= rhs[i];
        v = cast(uint) t;
        //reset carry
        t >>= uint.sizeof*8;
    }

    return lhs;
}

unittest {
    uint[3] lhs = [99, 201, 300],
            rhs = [-1, 0, 0];

    assert(sub(lhs, rhs) == [100, 200, 300]);
}
June 20, 2017
On Tuesday, 20 June 2017 at 01:58:22 UTC, Era Scarecrow wrote:
>    long t;    //temporary, doubles as carry
>    <snip>
>         t >>= uint.sizeof*8;


 To note, looking in compiler explorer (GDC 5.2.0), the following output is present (for the above line):

        mov     rax, QWORD PTR [rbp-24]
        shr     rax, 32
        mov     QWORD PTR [rbp-24], rax

 'shr' is unsigned right shift, when it should be 'sar' signed right shift. That is my guess where the bug is.
June 20, 2017
On Tuesday, 20 June 2017 at 02:14:06 UTC, Era Scarecrow wrote:
>  To note, looking in compiler explorer (GDC 5.2.0), the following output is present (for the above line):

 Apparently on GDC 6.3 this doesn't fail. So likely an old compiler bug.

 Adding the following after the shift more or less confirms the unsigned shift is the error.

    t >>= uint.sizeof*8;
    version(GNU) {
        if (t)
            t |= 0xffffffff_00000000L;
    }
June 20, 2017
On Tuesday, 20 June 2017 at 03:31:24 UTC, Era Scarecrow wrote:
>  Apparently on GDC 6.3 this doesn't fail. So likely an old compiler bug.

 Got a message not long ago, apparently the assertion still happens on GDC 6.3. So...
June 20, 2017
On Tuesday, 20 June 2017 at 18:49:24 UTC, Era Scarecrow wrote:
> On Tuesday, 20 June 2017 at 03:31:24 UTC, Era Scarecrow wrote:
>>  Apparently on GDC 6.3 this doesn't fail. So likely an old compiler bug.
>
>  Got a message not long ago, apparently the assertion still happens on GDC 6.3. So...

OK. Bugzilla? I'll have a look.

(By the way, you can inspect disassembly here: https://explore.dgnu.org/)

Regards,
Iain.
June 20, 2017
On Tuesday, 20 June 2017 at 19:06:22 UTC, Iain Buclaw wrote:
> On Tuesday, 20 June 2017 at 18:49:24 UTC, Era Scarecrow wrote:
>> On Tuesday, 20 June 2017 at 03:31:24 UTC, Era Scarecrow wrote:
>>>  Apparently on GDC 6.3 this doesn't fail. So likely an old compiler bug.
>>
>>  Got a message not long ago, apparently the assertion still happens on GDC 6.3. So...
>
> OK. Bugzilla? I'll have a look.
>

OK, no need.  It's close enough to a prior bug (171).

https://github.com/D-Programming-GDC/GDC/pull/501

Regards,
Iain.