www.digitalmars.com         C & C++   DMDScript  

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

reply Michael <michael toohuman.io> writes:
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.
May 19 2016
next sibling parent reply Michael <michael toohuman.io> writes:
Could it be that the code is optimised to the same as that in the 
original issue and so the current compiler still produces the 
incorrect result? Obviously the original issue has since been 
fixed but I won't be able to test this until the next version of 
DMD is released.
May 19 2016
parent Kagamin <spam here.lot> writes:
how this works?

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

	return values;
}
May 19 2016
prev sibling parent reply ag0aep6g <anonymous example.com> writes:
On 05/19/2016 05:09 PM, Michael wrote:
 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.
I suspect that you're seeing issue 16027 [1], a bad bug in 2.071.0. The upcoming 2.071.1 point release should have it fixed. [1] https://issues.dlang.org/show_bug.cgi?id=16027
May 19 2016
parent reply Michael <michael toohuman.io> writes:
On Thursday, 19 May 2016 at 15:49:17 UTC, ag0aep6g wrote:
 On 05/19/2016 05:09 PM, Michael wrote:
 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.
I suspect that you're seeing issue 16027 [1], a bad bug in 2.071.0. The upcoming 2.071.1 point release should have it fixed. [1] https://issues.dlang.org/show_bug.cgi?id=16027
Yeah that's what I was thinking but I don't know much about the optimisation process so I wanted to mention it anyway.
May 19 2016
parent ag0aep6g <anonymous example.com> writes:
On Thursday, 19 May 2016 at 17:07:27 UTC, Michael wrote:
 On Thursday, 19 May 2016 at 15:49:17 UTC, ag0aep6g wrote:
[...]
 [1] https://issues.dlang.org/show_bug.cgi?id=16027
Yeah that's what I was thinking but I don't know much about the optimisation process so I wanted to mention it anyway.
And now I see that you linked there yourself in the OP. No idea how I missed that.
May 19 2016