I have the following code in a loop:
while(true)
{
float i1, i2;
if(y==0)
{
i1 = 0;
}
else
{
//if y==108, this gives 74.821136 (note the two last digits)
i1 = ((values[y]+values[y+1])-values[1])*0.5f;
}
if(y+2==values.size())
{
i2 = values[y+1];
}
else
{
//if y==107, this gives 74.821129 (note the two last digits)
i2 = ((values[y+1]+values[y+2])-values[1])*0.5f;
}
if(i1<=t && t<i2) {
break;
}
else if(t<i1) {
y--;
}
else {
y++;
}
}
This loop gets evaluated for y=107, t=74.821133
And for y=108:
As you can see, i2 when y=107 is slightly different from i1 when y=108, while the lines for calculating these two values are identical.
I understand that funsafe-math-optimizations reorganizes math formulas using algebra rules which may lead to numerical errors due finite precision. But here, two equivalent formulas seem optimized differently. Which in this example, lead to an infinite loop (as this function looks, for a given float t, the y value for which i1 <= t < i2 )
Is this a faulty gcc 4.8.0 behavior?
If I create a function:
float getDifValue(y) const { (values[y]+values[y+1])-values[1])*0.5f; }
And then use it in the loop:
if(y==0)
{
i1 = 0;
}
else
{
i1 = getDifValue(y);
}
if(y+2==values.size())
{
i2 = values[y+1];
}
else
{
i2 = getDifValue(y+1);
}
Am I ensured that i2 for y=107 and i1 for y=108 will produce the same result? Or can the compiler inline getDifValue and optimize it differently on both places?
Thanks
After looking at disassembly, it seems that funsafe-optimization does change
float i1 = ((values[y]+values[y+1])-values[1])*0.5f;
float i2 = ((values[y+1]+values[y+2])-values[1])*0.5f;
into:
float i1 = ((values[y+1]-values[1])+values[y])*0.5f;
float i2 = ((values[y+1]-values[1])+values[y+2])*0.5f;
As it may then compute (values[y+1]-values[1]) only once.
Then, i2 for y==127 and i1 for y==128 are now computed slightly differently and fpu rounding make the result different.
Writing the calculation as a separate function of y solves the issue. But the question about the problem potentially reappearing if the compiler decides to inline and optimize is still open.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With