xref: /haiku/src/system/libroot/os/arch/x86_64/system_time.cpp (revision df3ac004ba00d875be84ec7853864b739a2292bf)
1 /*
2  * Copyright 2014, Paweł Dziepak, pdziepak@quarnos.org.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 
7 #include <stdint.h>
8 
9 
10 static uint64_t cv_factor;
11 static uint64_t cv_factor_nsec;
12 
13 
14 extern "C" void
15 __x86_setup_system_time(uint64_t cv, uint64_t cv_nsec)
16 {
17 	cv_factor = cv;
18 	cv_factor_nsec = cv_nsec;
19 }
20 
21 
22 static inline uint64_t
23 rdtsc()
24 {
25 	uint64_t lo, hi;
26 	__asm__("rdtsc" : "=a" (lo), "=d" (hi));
27 	return lo | (hi << 32);
28 }
29 
30 
31 extern "C" [[gnu::optimize("omit-frame-pointer")]] int64_t
32 system_time()
33 {
34 	__uint128_t time = static_cast<__uint128_t>(rdtsc()) * cv_factor;
35 	return time >> 64;
36 }
37 
38 
39 extern "C" [[gnu::optimize("omit-frame-pointer")]] int64_t
40 system_time_nsecs()
41 {
42 	__uint128_t t = static_cast<__uint128_t>(rdtsc()) * cv_factor_nsec;
43 	return t >> 32;
44 }
45 
46