2011-11-12 20:58:28 +08:00
|
|
|
/**
|
|
|
|
* @file tmv.h
|
|
|
|
* @brief Implements an abstract time value type.
|
|
|
|
* @note Copyright (C) 2011 Richard Cochran <richardcochran@gmail.com>
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
|
2011-11-27 21:19:29 +08:00
|
|
|
#include <time.h>
|
|
|
|
|
2011-11-12 20:58:28 +08:00
|
|
|
#include "ddt.h"
|
|
|
|
#include "pdt.h"
|
|
|
|
|
|
|
|
#define NS_PER_SEC 1000000000LL
|
2018-11-13 20:44:40 +08:00
|
|
|
#define MIN_TMV_TO_TIMEINTERVAL 0xFFFF800000000000ll
|
|
|
|
#define MAX_TMV_TO_TIMEINTERVAL 0x00007FFFFFFFFFFFll
|
2011-11-12 20:58:28 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
2012-09-12 23:21:07 +08:00
|
|
|
* However, we are going to be a bit pedantic here and enforce the
|
2011-11-12 20:58:28 +08:00
|
|
|
* 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.
|
|
|
|
*/
|
2018-03-02 02:12:28 +08:00
|
|
|
typedef struct {
|
|
|
|
int64_t ns;
|
|
|
|
} tmv_t;
|
2011-11-12 20:58:28 +08:00
|
|
|
|
|
|
|
static inline tmv_t tmv_add(tmv_t a, tmv_t b)
|
|
|
|
{
|
2018-03-02 02:12:28 +08:00
|
|
|
tmv_t t;
|
|
|
|
t.ns = a.ns + b.ns;
|
|
|
|
return t;
|
2011-11-12 20:58:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline tmv_t tmv_div(tmv_t a, int divisor)
|
|
|
|
{
|
2018-03-02 02:12:28 +08:00
|
|
|
tmv_t t;
|
|
|
|
t.ns = a.ns / divisor;
|
|
|
|
return t;
|
2011-11-12 20:58:28 +08:00
|
|
|
}
|
|
|
|
|
2018-03-02 02:12:24 +08:00
|
|
|
static inline int tmv_cmp(tmv_t a, tmv_t b)
|
2012-09-04 13:51:59 +08:00
|
|
|
{
|
2018-03-02 02:12:28 +08:00
|
|
|
return a.ns == b.ns ? 0 : a.ns > b.ns ? +1 : -1;
|
2012-09-04 13:51:59 +08:00
|
|
|
}
|
|
|
|
|
2018-03-02 02:12:25 +08:00
|
|
|
static inline int tmv_sign(tmv_t x)
|
|
|
|
{
|
2018-03-02 02:12:28 +08:00
|
|
|
return x.ns == 0 ? 0 : x.ns > 0 ? +1 : -1;
|
2018-03-02 02:12:25 +08:00
|
|
|
}
|
|
|
|
|
2012-03-11 17:46:45 +08:00
|
|
|
static inline int tmv_is_zero(tmv_t x)
|
|
|
|
{
|
2018-03-02 02:12:28 +08:00
|
|
|
return x.ns == 0 ? 1 : 0;
|
2012-03-11 17:46:45 +08:00
|
|
|
}
|
|
|
|
|
2011-11-12 20:58:28 +08:00
|
|
|
static inline tmv_t tmv_sub(tmv_t a, tmv_t b)
|
|
|
|
{
|
2018-03-02 02:12:28 +08:00
|
|
|
tmv_t t;
|
|
|
|
t.ns = a.ns - b.ns;
|
|
|
|
return t;
|
2011-11-12 20:58:28 +08:00
|
|
|
}
|
|
|
|
|
2012-03-11 17:46:45 +08:00
|
|
|
static inline tmv_t tmv_zero(void)
|
|
|
|
{
|
2018-03-02 02:12:28 +08:00
|
|
|
tmv_t t = { 0 };
|
|
|
|
return t;
|
2012-03-11 17:46:45 +08:00
|
|
|
}
|
|
|
|
|
2011-11-12 20:58:28 +08:00
|
|
|
static inline tmv_t correction_to_tmv(Integer64 c)
|
|
|
|
{
|
2018-03-02 02:12:28 +08:00
|
|
|
tmv_t t;
|
|
|
|
t.ns = (c >> 16);
|
|
|
|
return t;
|
2011-11-12 20:58:28 +08:00
|
|
|
}
|
|
|
|
|
2012-08-31 00:16:41 +08:00
|
|
|
static inline double tmv_dbl(tmv_t x)
|
|
|
|
{
|
2018-03-02 02:12:28 +08:00
|
|
|
return (double) x.ns;
|
2012-08-31 00:16:41 +08:00
|
|
|
}
|
|
|
|
|
2014-02-21 17:40:54 +08:00
|
|
|
static inline tmv_t dbl_tmv(double x)
|
|
|
|
{
|
2018-03-02 02:12:28 +08:00
|
|
|
tmv_t t;
|
|
|
|
t.ns = x;
|
|
|
|
return t;
|
2014-02-21 17:40:54 +08:00
|
|
|
}
|
|
|
|
|
2012-09-12 23:22:27 +08:00
|
|
|
static inline int64_t tmv_to_nanoseconds(tmv_t x)
|
|
|
|
{
|
2018-03-02 02:12:28 +08:00
|
|
|
return x.ns;
|
2012-09-12 23:22:27 +08:00
|
|
|
}
|
|
|
|
|
2012-08-22 13:25:46 +08:00
|
|
|
static inline TimeInterval tmv_to_TimeInterval(tmv_t x)
|
|
|
|
{
|
2018-11-13 20:44:40 +08:00
|
|
|
if (x.ns < (int64_t)MIN_TMV_TO_TIMEINTERVAL) {
|
|
|
|
return MIN_TMV_TO_TIMEINTERVAL << 16;
|
|
|
|
} else if (x.ns > (int64_t)MAX_TMV_TO_TIMEINTERVAL) {
|
|
|
|
return MAX_TMV_TO_TIMEINTERVAL << 16;
|
|
|
|
}
|
2018-03-02 02:12:28 +08:00
|
|
|
return x.ns << 16;
|
2012-08-22 13:25:46 +08:00
|
|
|
}
|
|
|
|
|
2017-11-27 10:40:51 +08:00
|
|
|
static inline struct Timestamp tmv_to_Timestamp(tmv_t x)
|
|
|
|
{
|
|
|
|
struct Timestamp result;
|
|
|
|
uint64_t sec, nsec;
|
|
|
|
|
|
|
|
sec = x.ns / 1000000000ULL;
|
|
|
|
nsec = x.ns % 1000000000ULL;
|
|
|
|
|
|
|
|
result.seconds_lsb = sec & 0xFFFFFFFF;
|
|
|
|
result.seconds_msb = (sec >> 32) & 0xFFFF;
|
|
|
|
result.nanoseconds = nsec;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2011-11-12 20:58:28 +08:00
|
|
|
static inline tmv_t timespec_to_tmv(struct timespec ts)
|
|
|
|
{
|
2018-03-02 02:12:28 +08:00
|
|
|
tmv_t t;
|
|
|
|
t.ns = ts.tv_sec * NS_PER_SEC + ts.tv_nsec;
|
|
|
|
return t;
|
2011-11-12 20:58:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline tmv_t timestamp_to_tmv(struct timestamp ts)
|
|
|
|
{
|
2018-03-02 02:12:28 +08:00
|
|
|
tmv_t t;
|
|
|
|
t.ns = ts.sec * NS_PER_SEC + ts.nsec;
|
|
|
|
return t;
|
2011-11-12 20:58:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|