forked from richardcochran/linuxptp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tmv.h
109 lines (91 loc) · 2.51 KB
/
tmv.h
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/**
* @file tmv.h
* @brief Implements an abstract time value type.
* @note Copyright (C) 2011 Richard Cochran <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifndef HAVE_TMV_H
#define HAVE_TMV_H
#include <time.h>
#include "ddt.h"
#include "pdt.h"
#define NS_PER_SEC 1000000000LL
/**
* We implement the time value as a 64 bit signed integer containing
* nanoseconds. Using this representation, we could really spare the
* arithmetic functions such as @ref tmv_add() and the like, and just
* use plain old math operators in the code.
*
* However, we are going to be a bit pedantic here and enforce the
* use of the these functions, so that we can easily upgrade the code
* to a finer representation later on. In that way, we can make use of
* the fractional nanosecond parts of the correction fields, if and
* when people start asking for them.
*/
typedef int64_t tmv_t;
static inline tmv_t tmv_add(tmv_t a, tmv_t b)
{
return a + b;
}
static inline tmv_t tmv_div(tmv_t a, int divisor)
{
return a / divisor;
}
static inline int tmv_eq(tmv_t a, tmv_t b)
{
return a == b ? 1 : 0;
}
static inline int tmv_is_zero(tmv_t x)
{
return x == ((tmv_t) 0) ? 1 : 0;
}
static inline tmv_t tmv_sub(tmv_t a, tmv_t b)
{
return a - b;
}
static inline tmv_t tmv_zero(void)
{
return (tmv_t) 0;
}
static inline tmv_t correction_to_tmv(Integer64 c)
{
return c >> 16;
}
static inline double tmv_dbl(tmv_t x)
{
return (double) x;
}
static inline tmv_t dbl_tmv(double x)
{
return (tmv_t) x;
}
static inline int64_t tmv_to_nanoseconds(tmv_t x)
{
return x;
}
static inline TimeInterval tmv_to_TimeInterval(tmv_t x)
{
return x << 16;
}
static inline tmv_t timespec_to_tmv(struct timespec ts)
{
return ts.tv_sec * NS_PER_SEC + ts.tv_nsec;
}
static inline tmv_t timestamp_to_tmv(struct timestamp ts)
{
return ts.sec * NS_PER_SEC + ts.nsec;
}
#endif