-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathsystime.c
44 lines (34 loc) · 1.06 KB
/
systime.c
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
/**
* \file
* \brief System time, constants and convertors
*
*/
/*
* Copyright (c) 2016, ETH Zurich.
* All rights reserved.
*
* This file is distributed under the terms in the attached LICENSE file.
* If you do not find this file, copies can be found by writing to:
* ETH Zurich D-INFK, Universitaetstrasse 6, CH-8092 Zurich. Attn: Systems Group.
*/
#include <systime.h>
/// Number of system ticks per one millisecond
systime_t systime_frequency = 1;
/// Convert nanoseconds to system ticks
systime_t ns_to_systime(uint64_t nanoseconds)
{
uint64_t q, r;
q = nanoseconds / 1000000000;
r = nanoseconds % 1000000000;
// Adding half a tick to round properly
return q * systime_frequency + (r * systime_frequency + 500000000) / 1000000000;
}
/// Convert system ticks to nanoseconds
uint64_t systime_to_ns(systime_t time)
{
systime_t q, r;
q = time / systime_frequency;
r = time % systime_frequency;
// Adding half a nanosecond to round properly
return q * 1000000000 + (r * 1000000000 + systime_frequency / 2) / systime_frequency;
}