#ifndef TIMESPECOPS_H
#define TIMESPECOPS_H
#include <sys/types.h>
#include <stdio.h>
#include <math.h>
#include "ntp.h"
#include "timetoa.h"
#define NANOSECONDS 1000000000
#define timespec_isnormal(x) \
((x)->tv_nsec >= 0 && (x)->tv_nsec < NANOSECONDS)
#define timespec_isdenormal(x) (!timespec_isnormal(x))
#ifdef HAVE_U_INT64
# define FTOTVN(tsf) \
((int32) \
(((u_int64)(tsf) * NANOSECONDS + 0x80000000) >> 32))
# define TVNTOF(tvu) \
((u_int32) \
((((u_int64)(tvu) << 32) + NANOSECONDS / 2) / \
NANOSECONDS))
#else
# define NSECFRAC (FRAC / NANOSECONDS)
# define FTOTVN(tsf) \
((int32)((tsf) / NSECFRAC + 0.5))
# define TVNTOF(tvu) \
((u_int32)((tvu) * NSECFRAC + 0.5))
#endif
static inline struct timespec
normalize_tspec(
struct timespec x
)
{
#if SIZEOF_LONG > 4
long z;
if (x.tv_nsec < -3l * NANOSECONDS ||
x.tv_nsec > 3l * NANOSECONDS) {
z = x.tv_nsec / NANOSECONDS;
x.tv_nsec -= z * NANOSECONDS;
x.tv_sec += z;
}
#endif
if (x.tv_nsec < 0)
do {
x.tv_nsec += NANOSECONDS;
x.tv_sec--;
} while (x.tv_nsec < 0);
else if (x.tv_nsec >= NANOSECONDS)
do {
x.tv_nsec -= NANOSECONDS;
x.tv_sec++;
} while (x.tv_nsec >= NANOSECONDS);
return x;
}
static inline struct timespec
add_tspec(
struct timespec a,
struct timespec b
)
{
struct timespec x;
x = a;
x.tv_sec += b.tv_sec;
x.tv_nsec += b.tv_nsec;
return normalize_tspec(x);
}
static inline struct timespec
add_tspec_ns(
struct timespec a,
long b
)
{
struct timespec x;
x = a;
x.tv_nsec += b;
return normalize_tspec(x);
}
static inline struct timespec
sub_tspec(
struct timespec a,
struct timespec b
)
{
struct timespec x;
x = a;
x.tv_sec -= b.tv_sec;
x.tv_nsec -= b.tv_nsec;
return normalize_tspec(x);
}
static inline struct timespec
sub_tspec_ns(
struct timespec a,
long b
)
{
struct timespec x;
x = a;
x.tv_nsec -= b;
return normalize_tspec(x);
}
static inline struct timespec
neg_tspec(
struct timespec a
)
{
struct timespec x;
x.tv_sec = -a.tv_sec;
x.tv_nsec = -a.tv_nsec;
return normalize_tspec(x);
}
static inline struct timespec
abs_tspec(
struct timespec a
)
{
struct timespec c;
c = normalize_tspec(a);
if (c.tv_sec < 0) {
if (c.tv_nsec != 0) {
c.tv_sec = -c.tv_sec - 1;
c.tv_nsec = NANOSECONDS - c.tv_nsec;
} else {
c.tv_sec = -c.tv_sec;
}
}
return c;
}
static inline int
cmp_tspec(
struct timespec a,
struct timespec b
)
{
int r;
r = (a.tv_sec > b.tv_sec) - (a.tv_sec < b.tv_sec);
if (0 == r)
r = (a.tv_nsec > b.tv_nsec) -
(a.tv_nsec < b.tv_nsec);
return r;
}
static inline int
cmp_tspec_denorm(
struct timespec a,
struct timespec b
)
{
return cmp_tspec(normalize_tspec(a), normalize_tspec(b));
}
static inline int
test_tspec(
struct timespec a
)
{
int r;
r = (a.tv_sec > 0) - (a.tv_sec < 0);
if (r == 0)
r = (a.tv_nsec > 0);
return r;
}
static inline int
test_tspec_denorm(
struct timespec a
)
{
return test_tspec(normalize_tspec(a));
}
static inline const char *
tspectoa(
struct timespec x
)
{
return format_time_fraction(x.tv_sec, x.tv_nsec, 9);
}
static inline l_fp
tspec_intv_to_lfp(
struct timespec x
)
{
struct timespec v;
l_fp y;
v = normalize_tspec(x);
y.l_uf = TVNTOF(v.tv_nsec);
y.l_i = (int32)v.tv_sec;
return y;
}
static inline l_fp
tspec_stamp_to_lfp(
struct timespec x
)
{
l_fp y;
y = tspec_intv_to_lfp(x);
y.l_ui += JAN_1970;
return y;
}
static inline struct timespec
lfp_intv_to_tspec(
l_fp x
)
{
struct timespec out;
l_fp absx;
int neg;
neg = L_ISNEG(&x);
absx = x;
if (neg) {
L_NEG(&absx);
}
out.tv_nsec = FTOTVN(absx.l_uf);
out.tv_sec = absx.l_i;
if (neg) {
out.tv_sec = -out.tv_sec;
out.tv_nsec = -out.tv_nsec;
out = normalize_tspec(out);
}
return out;
}
static inline struct timespec
lfp_uintv_to_tspec(
l_fp x
)
{
struct timespec out;
out.tv_nsec = FTOTVN(x.l_uf);
out.tv_sec = x.l_ui;
return out;
}
static inline struct timespec
lfp_stamp_to_tspec(
l_fp x,
const time_t * p
)
{
struct timespec out;
vint64 sec;
sec = ntpcal_ntp_to_time(x.l_ui, p);
out.tv_nsec = FTOTVN(x.l_uf);
#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
return out;
}
#endif