Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

About reducing the branch miss prediciton

I saw a sentence in a paper "Transforming branches into data dependencies to avoid mispredicted branches." (Page 6)

I wonder how to change the code from branches into data dependencies.

This is the paper: http://www.adms-conf.org/p1-SCHLEGEL.pdf

Update: How to transform branches in the binary search?

like image 839
Fan Zhang Avatar asked Jul 05 '12 17:07

Fan Zhang


People also ask

How can branch prediction be improved?

One thing you can do in a high-level language is to eliminate branches by expressing the problem in terms of lookups or arithmetic. This helps branch prediction work better on the remaining branches, because there's more "history" available.

Why is branch prediction so important?

The purpose of the branch predictor is to improve the flow in the instruction pipeline. Branch predictors play a critical role in achieving high performance in many modern pipelined microprocessor architectures such as x86.

What if branch prediction is wrong?

Once the branch is decided, if the prediction was correct nothing happens but if the prediction was wrong, the pipeline simply switches to processing the correct instruction at the next clock.

What is branch prediction and how can it control hazards?

the idea behind branch prediction is simple: if we can correctly predict whether branches are taken or not, we can reduce the stalls due to control hazards.


1 Answers

The basic idea (I would presume) would be to change something like:

if (a>b)
    return "A is greater than B";
else
    return "A is less than or equal to B";

into:

static char const *strings[] = {
    "A is less than or equal to B", 
    "A is greater than B"
};

return strings[a>b];

For branches in a binary search, let's consider the basic idea of the "normal" binary search, which typically looks (at least vaguely) like this:

while (left < right) {
    middle = (left + right)/2;
    if (search_for < array[middle])
        right = middle;
    else
        left = middle;
}

We could get rid of most of the branching here in pretty much the same way:

size_t positions[] = {left, right};

while (left < right) {
    size_t middle = (left + right)/2;

    positions[search_for >= array[middle]] = middle;
}

[For general purpose code use left + (right-left)/2 instead of (left+right)/2.]

We do still have the branching for the loop itself, of course, but we're generally a lot less concerned about that--that branch is extremely amenable to prediction, so even if we did eliminate it, doing so would gain little as a rule.

like image 73
Jerry Coffin Avatar answered Sep 22 '22 14:09

Jerry Coffin