Not a compiler bug.
FeepingCreature
feepingcreature at gmail.com
Thu Aug 13 07:51:06 UTC 2020
On Thursday, 13 August 2020 at 07:22:18 UTC, mw wrote:
> I post here because I think this bug's impact maybe pretty
> wide, it's a div bug on Linux x86_64, (both dmd & ldc2). I'm
> interested in knowing what caused this bug.
>
> On Windows, I only tested dmd.exe, it correctly outputs -2500.
>
>
> size_t: because I was taking array length, maybe many people do
> that too.
>
>
> https://issues.dlang.org/show_bug.cgi?id=21151
>
> import std.stdio;
>
> void main() {
> long a = -5000;
> size_t b = 2;
> long c = a / b;
> writeln(c);
> }
>
>
> $ dmd divbug.d
> $ ./divbug
> 9223372036854773308
>
>
> $ ldc2 divbug.d
> $ ./divbug
> 9223372036854773308
>
>
> x86_64 x86_64 x86_64 GNU/Linux
>
> $ dmd --version
> DMD64 D Compiler v2.092.0
>
> $ ldc2 --version
> LDC - the LLVM D compiler (1.21.0):
> based on DMD v2.091.1 and LLVM 10.0.0
> built with LDC - the LLVM D compiler (1.21.0)
> Default target: x86_64-unknown-linux-gnu
> Host CPU: bdver2
"Not a bug." Or rather, same semantics as in C. The division
reinterprets a as unsigned, and the result is within the positive
range of a long, so it stays positive post-division.
#include <stdio.h>
void main() {
long long int a = -5000;
long long unsigned int b = 2;
// the expression is unsigned, and reinterpreted as positive
signed with the assignment
long long int c = a / b;
printf("%lli\n", c); // crazy number
}
Whether that is correct is a whole other question. Personally I
think it's insane.
More information about the Digitalmars-d
mailing list