Using -O with DMD seems to produce non-random values.

Michael via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu May 19 08:09:10 PDT 2016


I'm not entirely sure what optimisations are made when supplying 
the -O flag to rdmd, but this may be related to an earlier issue 
I found for similar code here:
https://issues.dlang.org/show_bug.cgi?id=16027

The code is:
void main()
{
	auto seed = 128;
	auto rand = Random(seed);
	double[] values;

	values = generateValues(rand, 10);

	writeln(values);
}

double[] generateValues(ref Random rand, int l)
{
	auto values = new double[](l);
	foreach (ref val; values)
	{
		auto value = 1.0;
		if (uniform(0, 2, rand))
		{
			value = value * -1;
		}
		val = value;
	}

	return values;
}

Which returns different values depending on whether -O is passed:
>$rdmd testing_optimiser.d
>[1, -1, -1, 1, -1, 1, 1, 1, -1, -1]
>$rdmd -O testing_optimiser.d
>[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]

Any idea what causes this to occur when optimising? I wanted to 
try and speed up a simulation I'm running but it just produces 
too many unexpected consequences.


More information about the Digitalmars-d-learn mailing list