Skip to content

Commit

Permalink
Add !!( ) to branch predictor hints to force 1 or 0 result
Browse files Browse the repository at this point in the history
This makes it so we can program as usual and no issues result from
for example:
if (MVM_LIKELY(my_var))
Turns into __builtin_expect(myvar, 1). But in most cases my_var isn't 1
even though it evaluates as true.
Make we instead turn it into __builtin_expect(!!myvar, 1) which will be
correct in all cases where myvar is not zero.

The compiler may have been smart enough to figure this out before or
it may not have been, but this resolves warnings of type issues when
using a pointer in a conditional and we can be more sure that MVM_LIKELY
is operating properly.
  • Loading branch information
samcv committed May 26, 2018
1 parent 9d7a705 commit 3cf9ae3
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions build/setup.pm
Expand Up @@ -287,9 +287,9 @@ our %COMPILERS = (
noreturnspecifier => '',
noreturnattribute => '__attribute__((noreturn))',
formatattribute => '__attribute__((format(X, Y, Z)))',
expect_likely => '__builtin_expect((condition), 1)',
expect_unlikely => '__builtin_expect((condition), 0)',
expect_condition => '__builtin_expect((condition), (expection))'
expect_likely => '__builtin_expect(!!(condition), 1)',
expect_unlikely => '__builtin_expect(!!(condition), 0)',
expect_condition => '__builtin_expect(!!(condition), (expection))'
},

clang => {
Expand All @@ -315,9 +315,9 @@ our %COMPILERS = (
noreturnattribute => '__attribute__((noreturn))',
formatattribute => '__attribute__((format(X, Y, Z)))',
vectorizerspecifier => '_Pragma ("clang loop vectorize(enable)")',
expect_likely => '__builtin_expect((condition), 1)',
expect_unlikely => '__builtin_expect((condition), 0)',
expect_condition => '__builtin_expect((condition), (expection))'
expect_likely => '__builtin_expect(!!(condition), 1)',
expect_unlikely => '__builtin_expect(!!(condition), 0)',
expect_condition => '__builtin_expect(!!(condition), (expection))'
},

cl => {
Expand Down

0 comments on commit 3cf9ae3

Please sign in to comment.