/
util.c
96 lines (82 loc) · 2.08 KB
/
util.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include "util.h"
#ifdef __MACH__
#include <mach/clock.h>
#include <mach/mach.h>
#endif
/**
* Starts the high resolution timer
*/
void start_timer(struct hrtimer_t *timer) {
#ifdef __linux__
clock_gettime(CLOCK_REALTIME, &timer->start);
#elif __APPLE__
// OS X does not have clock_gettime, use clock_get_time
clock_serv_t cclock;
mach_timespec_t mts;
host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
clock_get_time(cclock, &mts);
mach_port_deallocate(mach_task_self(), cclock);
timer->start.tv_sec = mts.tv_sec;
timer->start.tv_nsec = mts.tv_nsec;
#else
QueryPerformanceFrequency(&timer->freq);
QueryPerformanceCounter(&timer->start);
#endif
}
/**
* Stops the high resolution timer
*/
void stop_timer(struct hrtimer_t *timer) {
#ifdef __linux__
clock_gettime(CLOCK_REALTIME, &timer->stop);
#elif __APPLE__
// OS X does not have clock_gettime, use clock_get_time
clock_serv_t cclock;
mach_timespec_t mts;
host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
clock_get_time(cclock, &mts);
mach_port_deallocate(mach_task_self(), cclock);
timer->stop.tv_sec = mts.tv_sec;
timer->stop.tv_nsec = mts.tv_nsec;
#else
QueryPerformanceCounter(&timer->stop);
#endif
}
/**
* Reads the high resolution timer in seconds
*/
double get_timer_interval(struct hrtimer_t *timer) {
#ifdef __linux__
long dnsec = timer->stop.tv_nsec - timer->start.tv_nsec;
int dsec = timer->stop.tv_sec - timer->start.tv_sec;
if (dnsec < 0) {
dnsec += 1e9;
dsec -= 1;
}
return ((double)dsec) + dnsec * 1.e-9;
#elif __APPLE__
long dnsec = timer->stop.tv_nsec - timer->start.tv_nsec;
long dsec = timer->stop.tv_sec - timer->start.tv_sec;
if (dnsec < 0) {
dnsec += 1e9;
dsec -= 1;
}
return ((double)dsec) + dnsec * 1.e-9;
#else
return ((double)(timer->stop.QuadPart - timer->start.QuadPart))/timer->freq.QuadPart;
#endif
}
/**
* Finds the ceiling of the log2 of a number iteratively
*/
int cb_log2(int x) {
int res = 0;
int x2 = x;
while (x2 > 1) {
x2 >>= 1;
res += 1;
}
if ((1 << res) == x)
return res;
return res+1;
}