-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtime.c
More file actions
55 lines (42 loc) · 1.01 KB
/
time.c
File metadata and controls
55 lines (42 loc) · 1.01 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <aria/time.h>
struct time invariant_tsc_read(struct timer *timer)
{
struct time ret = { .sec = 0, .nsec = 0 };
if (timer == NULL)
return ret;
uint64_t tsc = ({
uint64_t rax, rdx;
__asm__ volatile("rdtsc" : "=a"(rax), "=d"(rdx));
(uint64_t)rax | ((uint64_t)rdx << 32);
});
uint64_t ns = (tsc * NANO_PER_SECOND) / timer->freq;
ret = (struct time){ .nsec = ns % NANO_PER_SECOND,
.sec = ns / NANO_PER_SECOND };
return ret;
}
struct time time_add(struct time a, struct time b)
{
struct time ret = { .sec = a.sec + b.sec, .nsec = a.nsec + b.nsec };
if (ret.nsec > NANO_PER_SECOND) {
ret.nsec -= NANO_PER_SECOND;
ret.sec++;
}
return ret;
}
struct time time_sub(struct time a, struct time b)
{
struct time ret = { .nsec = a.nsec - b.nsec, .sec = a.sec - b.sec };
if (ret.nsec < 0) {
ret.nsec = NANO_PER_SECOND - b.nsec - a.nsec;
ret.sec--;
}
if (ret.sec < 0) {
ret.nsec = 0;
ret.sec = 0;
}
return ret;
}
time_t time_to_ns(struct time a)
{
return S_TO_NS(a.sec) + a.nsec;
}