Home > database >  How to use [[(un)likely]] at do while loop in C 20?
How to use [[(un)likely]] at do while loop in C 20?

Time:12-01

do [[unlikely]]
{...}
while(a == 0);

This code can be compiled.

But is this the correct way to tell compiler that a is usually non-zero.

CodePudding user response:

Structurally, this is a correct way to say what you're trying to say. The attribute is placed in a location that tags the path of execution that is likely/unlikely to be executed. Applying it to the block statement of the do/while loop works adequately. It would also work within the block.

That having been said, it's unclear what good this would do practically. It might prevent some unrolling of the loop or inhibit prefetching. But it can't really change the structure of the compiled code, since the block has to be executed at least once and the conditional branch has to come after the block.

  • Related