-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpredictable.c
More file actions
53 lines (46 loc) · 1.23 KB
/
predictable.c
File metadata and controls
53 lines (46 loc) · 1.23 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <stdlib.h>
void nop(void*, int);
#define N 20000
#define ITERS 10000
static int *m_s1, *m_s2, *m_s3, *m_dst;
/* This loop body contains a condition which is predicted well because it's
* usually false. This case demonstrates why static heuristics do not
* aggressively speculate in order to remove control flow: the computation of z
* would be almost never be used, and performance would decrease. PMU feedback
* in this case will confirm that the branch is well-predicted, and no special
* attempts at conditional move conversion will be made.
*/
void predictable(int *dst, int *s1, int *s2, int *s3) {
#pragma novector
#pragma nounroll
for (int i = 0; i < N; i++) {
int *p;
if(s1[i] % 2) {
p = &s2[i] ;
int z = i * i * i * i * i * i * i;
nop(p, z);
} else {
p = &s3[i];
nop(p, 3);
}
dst[i] = *p;
}
}
void init(void) {
m_s1 = malloc(sizeof(int)*N);
m_s2 = malloc(sizeof(int)*N);
m_s3 = malloc(sizeof(int)*N);
m_dst = malloc(sizeof(int)*N);
for (int i = 0; i < N; i++) {
m_s1[i] = (i*i*i*i*i*i*i) % N;
m_s2[i] = 0;
m_s3[i] = 1;
}
}
int main(void) {
init();
for(int i=0; i<ITERS; ++i)
#pragma noinline
predictable(m_dst, m_s1, m_s2, m_s3);
return 0;
}