

What is Branch Prediction? - ralphchurch
http://stackoverflow.com/a/11227902/127880

======
ralphchurch
The author of the answer has set multiple world records for calculating digits
of pi. I highly recommend browsing through his answer history. You can learn
more from his answers than from entire computer science classes.

<http://stackoverflow.com/users/922184/mysticial>

~~~
nirvanatikku
For those interested in a Stack-Ed perspective of Mysticial's l337ness (top 5
tags):

C++: <http://www.stack-ed.com/#tag/c%2B%2B/user/922184/Mysticial>

Performance: [http://www.stack-
ed.com/#tag/performance/user/922184/Mystici...](http://www.stack-
ed.com/#tag/performance/user/922184/Mysticial)

Language-Agnostic: [http://www.stack-ed.com/#tag/language-
agnostic/user/922184/M...](http://www.stack-ed.com/#tag/language-
agnostic/user/922184/Mysticial)

Optimization: [http://www.stack-
ed.com/#tag/optimization/user/922184/Mystic...](http://www.stack-
ed.com/#tag/optimization/user/922184/Mysticial)

Branch-Prediction: [http://www.stack-ed.com/#tag/branch-
prediction/user/922184/M...](http://www.stack-ed.com/#tag/branch-
prediction/user/922184/Mysticial)

------
mattbillenstein
Why do people keep posting this every month???

~~~
alexkus
Karma whoring.

~~~
stephengillie
I notice ralphchurch's name is green. That means recently registered, right?

~~~
jere
I believe so. And they have posted 5 stackoverflow questions in 11 days.

IIRC, this is called Power Leveling.

~~~
speeder
the funny thing is I tried this YESTERDAY even this same post, I wonder how he
managed to post a duplicate...

and I did it to test if it would work, and it did not worked, and now it
worked.. :/

now I guess that spamming SO does work after all.

------
infogulch
Question: can a processor have two pipelines? When there's a branch it would
be able to start loading both branches and just switch the pipeline when the
final branch is determined.

It might still need to flush the pipelines when there are several branches in
succession but you'd still be able to use branch prediction to predict the two
most likely branches, which would help tremendously.

~~~
vault_
This is basically what computers do, it's called speculative execution. Modern
computers actually have several pipelines, each for a different function. This
is what makes a computer "superscalar". Basically, the CPU tries to keep the
pipelines of all of the execution units filled at all times. If it's able to,
it will schedule both sides of a branch to be executed, and flush out
whichever half wasn't used.

~~~
wmf
Generally speculation only executes one side of the branch. Speculative
threading has been proposed using SMT to execute both sides, but usually
you're better off trusting the branch predictor and using threads as non-
speculative state.

~~~
onan_barbarian
Speculation down both sides is independent of SMT and if I remember correct
there were proposals to do this - especially where the branch predictor "knew"
it wasn't doing a good job on a branch (no point going down both sides of a
99.9% predicted branch).

The problem is that you rapidly run out of execution resources (what if there
are more branches to follow, for example) and you will burn lots of energy
executing things that don't happen (not to mention speculatively loading
things that aren't needed, etc). I remember seeing some papers on the limits
of ILP and I think this was one of the 'extreme' ideas (speculation down
multiple paths).

~~~
wmf
To execute both sides, the fetch unit needs to have multiple program counters
which is essentially a form of SMT.

------
JacksonGariety
How many time must this get re-posted before people get that it's a good Stack
Overflow answer? Does HN really churn that many users each month?

------
randomletters
A beautiful example that shows how useful genuine understanding of computers
can be, even to high-level developers.

