According to C++
branch-aware prediction, I prepared a test to see how much effective it is.
So, in a control sample, I write:
int count=0;
for (auto _ : state) {
if(count%13==0) {
count+=2;
}
else
count++;
benchmark::DoNotOptimize(count);
}
In a C++11
branch-prediction, I write:
#define LIKELY(condition) __builtin_expect(static_cast<bool>(condition), 1)
#define UNLIKELY(condition) __builtin_expect(static_cast<bool>(condition), 0)
int count=0;
for (auto _ : state) {
if(UNLIKELY(count%13==0)) {
count+=2;
}
else
count++;
benchmark::DoNotOptimize(count);
}
In a C++20
,
int count=0;
for (auto _ : state) {
if(count%13==0)[[unlikely]]{
count+=2;
}
else
count++;
benchmark::DoNotOptimize(count);
}
which unfortunately is not supported under quick-bench
. But anyway, I leave it there.
Now, getting the benchmark under gcc and clang shows no effectiveness for such a basic example.
Am I doing anything wrong?