|
7 | 7 | ******************************************************************************
|
8 | 8 | */
|
9 | 9 |
|
10 |
| -#include "xenia/base/clock.h" |
11 |
| - |
12 | 10 | #include <sys/time.h>
|
13 |
| -#include <time.h> |
| 11 | + |
| 12 | +#include "xenia/base/assert.h" |
| 13 | +#include "xenia/base/clock.h" |
14 | 14 |
|
15 | 15 | namespace xe {
|
16 | 16 |
|
17 | 17 | uint64_t Clock::host_tick_frequency_platform() {
|
18 | 18 | 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); |
20 | 22 |
|
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; |
22 | 25 | }
|
23 | 26 |
|
24 | 27 | 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); |
27 | 31 |
|
28 |
| - return uint64_t(res.tv_sec) + uint64_t(res.tv_nsec) * 1000000000ull; |
| 32 | + return tp.tv_nsec + tp.tv_sec * 1000000000ull; |
29 | 33 | }
|
30 | 34 |
|
31 | 35 | 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; |
40 | 48 | }
|
41 | 49 |
|
42 | 50 | uint64_t Clock::QueryHostUptimeMillis() {
|
|
0 commit comments