News for hackers. Stuff that matters.
Re:Why does changing 0.1f to 0 slow down performance by 10x? by pyre on Sunday March 11, 2012 @12:57PM (#3690647)
I'm curious why no one has a good answer for the comments questioning why the compiler doesn't optimize out the +0/-0 instead of converting it to a floating-point, which triggers this issue.
Re:Why does changing 0.1f to 0 slow down performance by 10x? by jedbrown on Sunday March 11, 2012 @01:27PM (#3690721)
The conversion is optimized out by gcc-4.6 at all positive optimization levels, the addition is optimized at -O2 and above. But the slowness is caused by the algorithms being different, with one producing denormals while the other stays reasonable.
Re:Why does changing 0.1f to 0 slow down performance by 10x? by chrisaycock on Sunday March 11, 2012 @01:15PM (#3690676)
@Mystical answers in the comment section:

Now that I look at the assembly, not even + 0.0f gets optimized out. If I had to guess, it could be that + 0.0f would have side-effects if y[i] happened to be a signalling NaN or something.

Re:Why does changing 0.1f to 0 slow down performance by 10x? by onedognight on Sunday March 11, 2012 @01:30PM (#3690730)
Consider this:

  x += 0.0;
If x = -0.0 (yes, zero has a sign) then adding 0.0 gives 0.0, so it can have an effect and optimizing it away is wrong and generally only allowed with -ffast-math.
Re:Why does changing 0.1f to 0 slow down performance by 10x? by jimmybot on Sunday March 11, 2012 @03:17PM (#3691006)
What's an example of 0 having a sign being useful?
Re:Why does changing 0.1f to 0 slow down performance by 10x? by stephencanon on Sunday March 11, 2012 @03:30PM (#3691035)
If you're seriously interested, read Kahan's "Branch Cuts for Complex Elementary Functions (or: Much Ado About Nothing's Sign Bit)".

TLDR: there are some classes of problems for which the sign bit of zero preserves enough important information to get an accurate solution to a problem that would not be possible if you only had an unsigned zero. These happen to turn up in certain types of conformal mappings that are useful for solving certain PDEs.

Re:Why does changing 0.1f to 0 slow down performance by 10x? by lelf on Sunday March 11, 2012 @05:21PM (#3691325)
1/(0-0) = -∞, 1/(0+0) = ∞
Re:Why does changing 0.1f to 0 slow down performance by 10x? by stephencanon on Sunday March 11, 2012 @05:34PM (#3691355)
Actually, (0-0) evaluates to +0 in the default rounding mode, so both of those expressions will typically return +∞.

1/-0 will give you -∞, of course.

Re:Why does changing 0.1f to 0 slow down performance by 10x? by lelf on Sunday March 11, 2012 @08:21PM (#3691787)
Well, I meant it as in math notation, not in C.

(Strictly speaking, 1/0 in C is kaboom! (undefined behaviour))

Re:Why does changing 0.1f to 0 slow down performance by 10x? by FigBug on Sunday March 11, 2012 @01:13PM (#3690674)
It's not the +0/-0 that causes the issue, it's the repeated dividing.