Order of expressions to minimize branch mis-predictions?

Jeffrey Walton noloader@gmail.com
Wed Sep 28 19:15:00 GMT 2016


On Wed, Sep 28, 2016 at 11:51 AM, Tim Prince <n8tm@aol.com> wrote:
> Most CPU s are designed to favor forward conditional branches not taken, as
> far as static prediction is concerned. This should correspond with the
> priorities you quoted. If you wish to change the code generation to favor a
> given training set (without sorting source code), you may try the profile
> guided feature of your compiler.
> In some cases where several branches are likely, putting the shortest cases
> first may be helpful.

Thanks Tim.

I audited a class that provides a BigInteger in C++. It performed well
before the profile driven changes. Reordering the statements to take
advantage of the profiling data sped up public key operations. It was
a small change, but it was measurable and repeatable. It also looks
like newer processors benefit the most.

Jeff



More information about the Gcc-help mailing list