Live data from Hacker News

What is Branch Prediction?

stackoverflow.com

11–20 of 31 posts

Re: What is Branch Prediction?

#13

Why do people keep posting this every month???

Well, it is a very good answer. Most likely people admire that.

Also, I've been here for three years and I don't recall seeing it before, so it can't be posted that often. You must just happen to catch it a lot. I wouldn't have known it was a dupe if you hadn't pointed it out.

Re: What is Branch Prediction?

#14
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.

Re: What is Branch Prediction?

#15

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.

I think it's possible, but I think it's more efficient to use a completely separate...thread?...if you have a 2nd core. (Or if you have lots of empty space bubbles in your pipeline, you can just try forcing 2 threads in there, aka Hyperthreading)

Re: What is Branch Prediction?

#16

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

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...

Language-Agnostic: http://www.stack-ed.com/#tag/language-agnostic/user/922184/M...

Optimization: http://www.stack-ed.com/#tag/optimization/user/922184/Mystic...

Branch-Prediction: http://www.stack-ed.com/#tag/branch-prediction/user/922184/M...

Re: What is Branch Prediction?

#18

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.

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.

Re: What is Branch Prediction?

#19

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.

I think it's possible, but I think it's more efficient to use a completely separate...thread?...if you have a 2nd core. (Or if you have lots of empty space bubbles in your pipeline, you can just try forcing 2 threads in there, aka Hyperthreading)

That makes sense for most processor architectures. But there are exceptions. The Cell Processor (Sony PS3 Processor) has 8 RISC SPUs. These processors do not have branch prediction logic. This is because they were designed for computationally intensive graphics kernels which are useful for Sony PS3 games. There is a huge performance intensive to avoiding branches on these processors. So many times both routes are taken and then the appropriate value is chosen using a mask instruction. This does not stall the pipeline on the processor.

Re: What is Branch Prediction?

#20

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.

Sure, but it defeats the purpose of branch prediction. If you have a high confidence of which branch will be taken you don't want to waste execution units. The high level of data dependencies between branch points is another obstacle. It isn't as simple as hyperthreading.
Post reply on HN