#ifndef TIMEVALOPS_H
#define TIMEVALOPS_H
#include <sys/types.h>
#include <stdio.h>
#include "ntp.h"
#include "timetoa.h"
#define MICROSECONDS 1000000
#ifndef HAVE_U_INT64
# define USE_TSF_USEC_TABLES
#endif
#ifdef USE_TSF_USEC_TABLES
extern const u_int32 ustotslo[];
extern const u_int32 ustotsmid[];
extern const u_int32 ustotshi[];
# define TVUTOTSF(tvu, tsf) \
((tsf) = ustotslo[(tvu) & 0xff] \
+ ustotsmid[((tvu) >> 8) & 0xff] \
+ ustotshi[((tvu) >> 16) & 0xf])
#else
# define TVUTOTSF(tvu, tsf) \
((tsf) = (u_int32) \
((((u_int64)(tvu) << 32) + MICROSECONDS / 2) / \
MICROSECONDS))
#endif
#ifdef USE_TSF_USEC_TABLES
extern const u_int32 tstouslo[256];
extern const u_int32 tstousmid[256];
extern const u_int32 tstoushi[128];
#define TV_SHIFT 3
#define TV_ROUNDBIT 0x4
# define TSFTOTVU(tsf, tvu) \
((tvu) = (tstoushi[((tsf) >> 24) & 0xff] \
+ tstousmid[((tsf) >> 16) & 0xff] \
+ tstouslo[((tsf) >> 9) & 0x7f] \
+ TV_ROUNDBIT) >> TV_SHIFT)
#else
# define TSFTOTVU(tsf, tvu) \
((tvu) = (int32) \
(((u_int64)(tsf) * MICROSECONDS + 0x80000000) >> 32))
#endif
#define TVTOTS(tv, ts) \
do { \
(ts)->l_ui = (u_long)(tv)->tv_sec; \
TVUTOTSF((tv)->tv_usec, (ts)->l_uf); \
} while (FALSE)
#define sTVTOTS(tv, ts) \
do { \
int isneg = 0; \
long usec; \
(ts)->l_ui = (tv)->tv_sec; \
usec = (tv)->tv_usec; \
if (((tv)->tv_sec < 0) || ((tv)->tv_usec < 0)) { \
usec = -usec; \
(ts)->l_ui = -(ts)->l_ui; \
isneg = 1; \
} \
TVUTOTSF(usec, (ts)->l_uf); \
if (isneg) { \
L_NEG((ts)); \
} \
} while (FALSE)
#define TSTOTV(ts, tv) \
do { \
(tv)->tv_sec = (ts)->l_ui; \
TSFTOTVU((ts)->l_uf, (tv)->tv_usec); \
if ((tv)->tv_usec == 1000000) { \
(tv)->tv_sec++; \
(tv)->tv_usec = 0; \
} \
} while (FALSE)
#define timeval_isnormal(x) \
((x)->tv_usec >= 0 && (x)->tv_usec < MICROSECONDS)
#define MSUTOTSF(msu, tsf) TVUTOTSF((msu) * 1000, tsf)
#define timeval_isdenormal(x) (!timeval_isnormal(x))
static inline struct timeval
normalize_tval(
struct timeval x
)
{
long z;
if (x.tv_usec < -3l * MICROSECONDS ||
x.tv_usec > 3l * MICROSECONDS ) {
z = x.tv_usec / MICROSECONDS;
x.tv_usec -= z * MICROSECONDS;
x.tv_sec += z;
}
if (x.tv_usec < 0)
do {
x.tv_usec += MICROSECONDS;
x.tv_sec--;
} while (x.tv_usec < 0);
else if (x.tv_usec >= MICROSECONDS)
do {
x.tv_usec -= MICROSECONDS;
x.tv_sec++;
} while (x.tv_usec >= MICROSECONDS);
return x;
}
static inline struct timeval
add_tval(
struct timeval a,
struct timeval b
)
{
struct timeval x;
x = a;
x.tv_sec += b.tv_sec;
x.tv_usec += b.tv_usec;
return normalize_tval(x);
}
static inline struct timeval
add_tval_us(
struct timeval a,
long b
)
{
struct timeval x;
x = a;
x.tv_usec += b;
return normalize_tval(x);
}
static inline struct timeval
sub_tval(
struct timeval a,
struct timeval b
)
{
struct timeval x;
x = a;
x.tv_sec -= b.tv_sec;
x.tv_usec -= b.tv_usec;
return normalize_tval(x);
}
static inline struct timeval
sub_tval_us(
struct timeval a,
long b
)
{
struct timeval x;
x = a;
x.tv_usec -= b;
return normalize_tval(x);
}
static inline struct timeval
neg_tval(
struct timeval a
)
{
struct timeval x;
x.tv_sec = -a.tv_sec;
x.tv_usec = -a.tv_usec;
return normalize_tval(x);
}
static inline struct timeval
abs_tval(
struct timeval a
)
{
struct timeval c;
c = normalize_tval(a);
if (c.tv_sec < 0) {
if (c.tv_usec != 0) {
c.tv_sec = -c.tv_sec - 1;
c.tv_usec = MICROSECONDS - c.tv_usec;
} else {
c.tv_sec = -c.tv_sec;
}
}
return c;
}
static inline int
cmp_tval(
struct timeval a,
struct timeval b
)
{
int r;
r = (a.tv_sec > b.tv_sec) - (a.tv_sec < b.tv_sec);
if (0 == r)
r = (a.tv_usec > b.tv_usec) -
(a.tv_usec < b.tv_usec);
return r;
}
static inline int
cmp_tval_denorm(
struct timeval a,
struct timeval b
)
{
return cmp_tval(normalize_tval(a), normalize_tval(b));
}
static inline int
test_tval(
struct timeval a
)
{
int r;
r = (a.tv_sec > 0) - (a.tv_sec < 0);
if (r == 0)
r = (a.tv_usec > 0);
return r;
}
static inline int
test_tval_denorm(
struct timeval a
)
{
return test_tval(normalize_tval(a));
}
static inline const char *
tvaltoa(
struct timeval x
)
{
return format_time_fraction(x.tv_sec, x.tv_usec, 6);
}
static inline l_fp
tval_intv_to_lfp(
struct timeval x
)
{
struct timeval v;
l_fp y;
v = normalize_tval(x);
TVUTOTSF(v.tv_usec, y.l_uf);
y.l_i = (int32)v.tv_sec;
return y;
}
static inline l_fp
tval_stamp_to_lfp(
struct timeval x
)
{
l_fp y;
y = tval_intv_to_lfp(x);
y.l_ui += JAN_1970;
return y;
}
static inline struct timeval
lfp_intv_to_tval(
l_fp x
)
{
struct timeval out;
l_fp absx;
int neg;
neg = L_ISNEG(&x);
absx = x;
if (neg) {
L_NEG(&absx);
}
TSFTOTVU(absx.l_uf, out.tv_usec);
out.tv_sec = absx.l_i;
if (neg) {
out.tv_sec = -out.tv_sec;
out.tv_usec = -out.tv_usec;
out = normalize_tval(out);
}
return out;
}
static inline struct timeval
lfp_uintv_to_tval(
l_fp x
)
{
struct timeval out;
TSFTOTVU(x.l_uf, out.tv_usec);
out.tv_sec = x.l_ui;
return out;
}
static inline struct timeval
lfp_stamp_to_tval(
l_fp x,
const time_t * p
)
{
struct timeval out;
vint64 sec;
sec = ntpcal_ntp_to_time(x.l_ui, p);
TSFTOTVU(x.l_uf, out.tv_usec);
#if SIZEOF_TIME_T <= 4
out.tv_sec = (time_t)sec.d_s.lo;
#elif defined(HAVE_INT64)
out.tv_sec = (time_t)sec.q_s;
#else
out.tv_sec = ((time_t)sec.d_s.hi << 32) | sec.d_s.lo;
#endif
out = normalize_tval(out);
return out;
}
#endif