Skip to content

Commit 0c57001

Browse files
JoelLinnbwrsandman
authored andcommitted
[Base] Fix Clock platform functions on Linux.
1 parent f3647e0 commit 0c57001

File tree

1 file changed

+24
-16
lines changed

1 file changed

+24
-16
lines changed

src/xenia/base/clock_posix.cc

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,36 +7,44 @@
77
******************************************************************************
88
*/
99

10-
#include "xenia/base/clock.h"
11-
1210
#include <sys/time.h>
13-
#include <time.h>
11+
12+
#include "xenia/base/assert.h"
13+
#include "xenia/base/clock.h"
1414

1515
namespace xe {
1616

1717
uint64_t Clock::host_tick_frequency_platform() {
1818
timespec res;
19-
clock_getres(CLOCK_MONOTONIC_RAW, &res);
19+
auto error = clock_getres(CLOCK_MONOTONIC_RAW, &res);
20+
assert(!error);
21+
assert(!res.tv_sec);
2022

21-
return uint64_t(res.tv_sec) + uint64_t(res.tv_nsec) * 1000000000ull;
23+
// Convert nano seconds to hertz. Resolution is usually 1ns on most systems.
24+
return 1000000000ull / res.tv_nsec;
2225
}
2326

2427
uint64_t Clock::host_tick_count_platform() {
25-
timespec res;
26-
clock_gettime(CLOCK_MONOTONIC_RAW, &res);
28+
timespec tp;
29+
auto error = clock_gettime(CLOCK_MONOTONIC_RAW, &tp);
30+
assert(!error);
2731

28-
return uint64_t(res.tv_sec) + uint64_t(res.tv_nsec) * 1000000000ull;
32+
return tp.tv_nsec + tp.tv_sec * 1000000000ull;
2933
}
3034

3135
uint64_t Clock::QueryHostSystemTime() {
32-
struct timeval tv;
33-
gettimeofday(&tv, NULL);
34-
35-
uint64_t ret = tv.tv_usec;
36-
ret /= 1000; // usec -> msec
37-
38-
ret += (tv.tv_sec * 1000); // sec -> msec
39-
return ret;
36+
constexpr uint64_t seconds_per_day = 3600 * 24;
37+
// Don't forget the 89 leap days.
38+
constexpr uint64_t seconds_1601_to_1970 =
39+
((369 * 365 + 89) * seconds_per_day);
40+
41+
timeval now;
42+
auto error = gettimeofday(&now, nullptr);
43+
assert(!error);
44+
assert(sizeof(now.tv_sec) == 8);
45+
46+
// NT systems use 100ns intervals.
47+
return (now.tv_sec + seconds_1601_to_1970) * 10000000ull + now.tv_usec * 10;
4048
}
4149

4250
uint64_t Clock::QueryHostUptimeMillis() {

0 commit comments

Comments
 (0)