-
Notifications
You must be signed in to change notification settings - Fork 0
/
03d_branchless_predicted.cpp
38 lines (33 loc) · 1.01 KB
/
03d_branchless_predicted.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include "benchmark/benchmark.h"
void BM_branchless(benchmark::State& state) {
srand(1);
const unsigned int N = state.range(0);
std::vector<unsigned long> v1(N), v2(N);
std::vector<int> c1(N);
for (size_t i = 0; i < N; ++i) {
v1[i] = rand();
v2[i] = rand();
c1[i] = rand() > 0;
}
unsigned long* p1 = v1.data();
unsigned long* p2 = v2.data();
int* b1 = c1.data();
for (auto _ : state) {
unsigned long a1 = 0, a2 = 0;
for (size_t i = 0; i < N; ++i) {
unsigned long s1[2] = { 0, p1[i] - p2[i] };
unsigned long s2[2] = { p1[i] * p2[i], 0 };
a1 += s1[bool(b1[i])];
a2 += s2[bool(b1[i])];
}
benchmark::DoNotOptimize(a1);
benchmark::DoNotOptimize(a2);
benchmark::ClobberMemory();
}
state.SetItemsProcessed(N*state.iterations());
}
BENCHMARK(BM_branchless)->Arg(1<<22);
BENCHMARK_MAIN();