Does iteration of the for loop match like branches?

When I program on a normal day, I’m sure that all branches will most likely not be accepted.

int retval = do_somting();
if(!retval) { /* Less-than-likely event*/ }

These are predictions of optimistic branches, as a result of which the processor predictor bit is set to “not accept”. However, if the predictor bit (s) is forcibly returned to "accept" after the for?

// prediction = "likely take"
if(false) { }

// prediction = "probably take"
if(false) { }

// prediction = "probably not take"
if(false) { }

// prediction = "likely not take"
if(false) { }

/* ... thousands of other if(false) that are speedy-fast */

for(int i = 0; i < 5; i++) { }
// prediction = "likely take"?

I know this is an unrealistic and tiny optimization, but hey, the more you know.

EDIT: Assume that GCC does not destroy all of this code above, and also allows talking only about the amd64 architecture. Since I did not understand how low this question is.

+4
source share
2 answers

, CPU.

, . . , if for. .

, . , .

... , , .

+3

( AMD64) / / , / / . , , , . do-while , for while, - ; , .

gcc -O3 __builtin_expect(). , , , , . . : -O3 gcc , .

, , , , . , , , , , . "" , - (, 4 ) , .

+1

Source: https://habr.com/ru/post/1667897/


All Articles