Branch Prediction on a Function Pointer

I have a loop that works over and over. The logic inside this cycle depends on the mode in which the program is located. To improve performance, I thought that an array of function pointers could be initialized, functionPtr [], so it would just call the FunctionPtrmode function, which would work with the correct logic. The cycle will remain in the same mode for many cycles (the number is not known in advance, but many thousands). The program works only on intel x64 computer and does not need portability.

I was hoping the processor would use branch prediction, but since my branch is not conditional (at the assembly level), but the location of the branch depends on the variable (functionPtr + mode). Will the processor try to calculate the functionPtr + mode and start pulling these instructions at runtime?

+4
source share
3 answers

The reflective prediction is for actual branches where we do not know until a branch has been evaluated that tells which command should be executed next. But since the following command is known in your code depending on what mode we are in, there is no need for any prediction, and they will not wait in the pipeline.

, , - .

+2

, ( , - ) .

Pentium ( Intel ) Pentium IV, , , (BTB). , "" , ( ) - , .

Pentium M/Prescott ( Pentium IV) Intel . (.. , , ), BTB . , () , , . , BTB , ( , ).

+2

From Intel, AMD, and VIA Processor Microarchitecture Optimization Guide for Assembly Programmers and Compiler Developers

http://www.agner.org/optimize/microarchitecture.pdf

section 3.7 (other processors are in different sections for Sandy Bridge) Pattern recognition for indirect jumps and calls Indirect jumps and indirect calls (but not return) are predicted using the same two-level predictor as the branch instructions.

A function pointer is an indirect call.

+1
source

All Articles