-
Notifications
You must be signed in to change notification settings - Fork 12
/
func_pointer.c
77 lines (62 loc) · 1.85 KB
/
func_pointer.c
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#define LOOP_ITERATION 100000000
uint64_t
trace_cpu_time_now(void)
{
uint32_t a, d;
asm volatile ("rdtsc":"=a" (a), "=d" (d));
return (uint64_t)a + ((uint64_t)d << (uint64_t)32);
}
typedef uint32_t (*func_pointer_1)(void*);
typedef void (*func_pointer_2)(void*);
typedef void (*func_pointer_3)(void*);
struct abstraction_layer {
func_pointer_1 fp_1;
func_pointer_2 fp_2;
func_pointer_3 fp_3;
};
uint32_t func_point_1_1(void *arg)
{
uint32_t *i = (uint32_t *)arg;
(*i)++;
return *i;
}
uint32_t func_point_1_2(void *arg)
{
uint32_t *i = (uint32_t *)arg;
(*i) += 2;
return *i;
}
int main()
{
struct abstraction_layer al_1 = {.fp_1 = func_point_1_1,
.fp_2 = NULL,
.fp_3 = NULL };
struct abstraction_layer al_2 = {.fp_1 = func_point_1_2,
.fp_2 = NULL,
.fp_3 = NULL };
asm volatile("" ::: "memory");
uint64_t start = trace_cpu_time_now();
int i = 0;
uint32_t arg = 0;
uint32_t ret = 0;
for(; i < LOOP_ITERATION; i++) {
ret = al_1.fp_1((void *)&arg);
}
asm volatile("" ::: "memory");
uint64_t end = trace_cpu_time_now();
printf("ret: %d, CPU cycles: %ld\n", ret, end - start);
asm volatile("" ::: "memory");
start = trace_cpu_time_now();
arg = 0;
for(i = 0; i < LOOP_ITERATION; i++) {
if (al_2.fp_1 == func_point_1_2) {
ret = al_2.fp_1((void *)&arg);
}
}
asm volatile("" ::: "memory");
end = trace_cpu_time_now();
printf("ret: %d, CPU cycles: %ld\n", ret, end - start);
}