forked from Zephkek/MIDIPlusPlus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer.h
More file actions
42 lines (34 loc) · 1005 Bytes
/
timer.h
File metadata and controls
42 lines (34 loc) · 1005 Bytes
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
#ifndef RDTSC_TIMER_H
#define RDTSC_TIMER_H
#include <windows.h>
#include <intrin.h>
#include <stdint.h>
enum {
RDTSC_TIMER_READY = 0,
RDTSC_TIMER_ERR_CPU_FREQ,
};
static double __cpu_freq;
static unsigned int __timer_status;
static double __timer_calculate_cpu_freq() {
const int sleep_ms = 100;
unsigned long long start = __rdtsc();
Sleep(sleep_ms);
unsigned long long end = __rdtsc();
double elapsed_seconds = (double)sleep_ms / 1000.0;
return (double)(end - start) / elapsed_seconds;
}
static void __timer_init() {
__cpu_freq = __timer_calculate_cpu_freq();
if (__cpu_freq == 0.0) {
__timer_status = RDTSC_TIMER_ERR_CPU_FREQ;
} else {
__timer_status = RDTSC_TIMER_READY;
}
}
static inline unsigned int rdtsc_timer_status() {
return __timer_status;
}
static inline double rdtsc_timer_precision() {
return 1.0 / (__cpu_freq / 1000000000.0);
}
#endif /* RDTSC_TIMER_H */