Googles appar
Huvudmeny

Post a Comment On: cbloom rants

"05-27-10 - Loop Branch Inversion"

4 Comments -

1 – 4 of 4
Blogger won3d said...

http://en.wikipedia.org/wiki/Loop_unswitching

I guess MSVC doesn't do this, or maybe the switch bodies are too big for it to want to duplicate. The whole "switch in a loop" pattern is common enough (e.g. indirect threaded interpreters) that it is usually pretty optimized. Like, when the dispatch predicate is NOT constant, some compilers (like GCC) will actually replicate the predicate code and jump directly to a case, rather than separately going through the loop and branch. Among other things, this improves branch prediction of such a loop.

But yeah, MSVC is bad at unrolling loops. Sad you have to manually unroll, or is it because the body of the loop is a function call?

May 28, 2010 at 8:26 AM

Blogger cbloom said...

"But yeah, MSVC is bad at unrolling loops. Sad you have to manually unroll, or is it because the body of the loop is a function call?"

Usually when I'm doing this it's very complicated code and many function calls.

It's also often complicated enough that I don't want to expose the whole thing out to the world, or the template/functor method would work. The only real disadvantages to the functor method are that you have publish your whole routine in a header, and that functors are a pain in the ass to make (fixed in C++0x).

May 28, 2010 at 10:18 AM

Blogger Brian said...

What type of performance wins do you actually see by doing the unrolling? I wonder if the processor branch prediction does well enough that this doesn't really matter.

May 28, 2010 at 12:42 PM

Anonymous Anonymous said...

Brian: PPC don't have branch prediction and are very bad at it. This include X360's Xenon and PS3's PPU and SPU.

May 28, 2010 at 12:46 PM

You can use some HTML tags, such as <b>, <i>, <a>

This blog does not allow anonymous comments.

Comment moderation has been enabled. All comments must be approved by the blog author.

You will be asked to sign in after submitting your comment.