Here's the reduced version of this bug. Larger than I'd like still, but is proving hard to reduce further.
module bug;
extern(C) int printf(const char *, ...);
struct FormatSpec
{
char spec = 's';
bool flHash = false;
// unused function, but if removed hides the bug
void writeUpToNextSpec(OutputRange)(OutputRange writer) {}
}
void formatValue(long val, ref FormatSpec f)
{
char[] w;
long arg = val;
// always true, but removing conditional hides the bug
uint base = f.spec == 's' ? 10 : 0;
// if moved above base line, or merged with the declaration, hides the bug
arg = -arg;
char buffer[64]; // 64 bits in base 2 at most
uint i = buffer.length;
auto n = cast(ulong) arg;
do
{
--i;
buffer[i] = cast(char) (n % base);
n /= base;
if (buffer[i] < 10)
buffer[i] += '0';
else
buffer[i] += 'A' - 10;
}
while (n);
// unused, but if removed hides the bug;
sizediff_t spacesToPrint = (base == 16 && f.flHash && arg);
// always true, but needs the conditional to show the bug
if (arg)
w = buffer[i .. $];
// the code to append the '-' to w was removable, so '999'
// really is the expected results
printf("w = %.*s\n", w.length, w.ptr);
assert(w == "999");
}
int main()
{
auto spec = FormatSpec();
formatValue(-999L, spec);
return 0;
}
Comment #2 by braddr — 2011-03-20T03:35:07Z
For that reduction, it needs both -m64 and -O to fail.
Also, this likely isn't the only 64 bit bug related to std.format. I'm not sure why, but the reduction in comment 1 isn't the same as the line number in comment 0 points to. The one at line 3651 didn't fail for me when I started reducing the bug but is right now.
Comment #3 by braddr — 2012-01-01T21:02:22Z
The bug in comment 1 has been fixed. The next bug related to creal passing in vararg's. The repro case:
module bugformat;
import core.vararg;
void bar(TypeInfo[] arguments, va_list argptr)
{
creal values = va_arg!(cdouble)(argptr);
assert(values == 1.2 + 3.4i, "value didn't make it through intact");
}
void foo(...)
{
bar(_arguments, _argptr);
}
int main()
{
foo(1.2 + 3.4i);
return 0;
}
Comment #4 by yebblies — 2012-02-26T08:36:47Z
I think I've got this. The complex xmm codegen is pretty awful.
Comment #5 by github-bugzilla — 2012-12-08T19:42:09Z