xref: /haiku/src/system/libroot/os/arch/x86_64/system_time.cpp (revision 410ed2fbba58819ac21e27d3676739728416761d)
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 #include <x86intrin.h>
10 
11 
12 static uint64_t cv_factor;
13 static uint64_t cv_factor_nsec;
14 
15 
16 extern "C" void
17 __x86_setup_system_time(uint64_t cv, uint64_t cv_nsec)
18 {
19 	cv_factor = cv;
20 	cv_factor_nsec = cv_nsec;
21 }
22 
23 
24 extern "C" [[gnu::optimize("omit-frame-pointer")]] int64_t
25 system_time()
26 {
27 	__uint128_t time = static_cast<__uint128_t>(__rdtsc()) * cv_factor;
28 	return time >> 64;
29 }
30 
31 
32 extern "C" [[gnu::optimize("omit-frame-pointer")]] int64_t
33 system_time_nsecs()
34 {
35 	__uint128_t t = static_cast<__uint128_t>(__rdtsc()) * cv_factor_nsec;
36 	return t >> 32;
37 }
38 
39