tmv: Convert tmv_t to a non-scalar type

Enforce the use of the tmv_t wrapper functions by converting tmv_t
from an int64_t to a struct containing an int64_t.

Inspection of the disassembly shows that this change has essentially
no impact on the resulting object code, at least for x86_64 with
compiler optimisations enabled.

RC:
  - Fixed up after dropping one previous patch in the series.
  - Removed the new function, tmv_to_Timestamp, as that will
    be introduced later on.

Signed-off-by: Michael Brown <mbrown@fensystems.co.uk>
Signed-off-by: Richard Cochran <richardcochran@gmail.com>
master
Michael Brown 2018-03-01 18:12:28 +00:00 committed by Richard Cochran
parent ffc5b93f95
commit d432cdc52a
1 changed files with 32 additions and 15 deletions

47
tmv.h
View File

@ -39,76 +39,93 @@
* the fractional nanosecond parts of the correction fields, if and
* when people start asking for them.
*/
typedef int64_t tmv_t;
typedef struct {
int64_t ns;
} tmv_t;
static inline tmv_t tmv_add(tmv_t a, tmv_t b)
{
return a + b;
tmv_t t;
t.ns = a.ns + b.ns;
return t;
}
static inline tmv_t tmv_div(tmv_t a, int divisor)
{
return a / divisor;
tmv_t t;
t.ns = a.ns / divisor;
return t;
}
static inline int tmv_cmp(tmv_t a, tmv_t b)
{
return a == b ? 0 : a > b ? +1 : -1;
return a.ns == b.ns ? 0 : a.ns > b.ns ? +1 : -1;
}
static inline int tmv_sign(tmv_t x)
{
return x == 0 ? 0 : x > 0 ? +1 : -1;
return x.ns == 0 ? 0 : x.ns > 0 ? +1 : -1;
}
static inline int tmv_is_zero(tmv_t x)
{
return x == ((tmv_t) 0) ? 1 : 0;
return x.ns == 0 ? 1 : 0;
}
static inline tmv_t tmv_sub(tmv_t a, tmv_t b)
{
return a - b;
tmv_t t;
t.ns = a.ns - b.ns;
return t;
}
static inline tmv_t tmv_zero(void)
{
return (tmv_t) 0;
tmv_t t = { 0 };
return t;
}
static inline tmv_t correction_to_tmv(Integer64 c)
{
return c >> 16;
tmv_t t;
t.ns = (c >> 16);
return t;
}
static inline double tmv_dbl(tmv_t x)
{
return (double) x;
return (double) x.ns;
}
static inline tmv_t dbl_tmv(double x)
{
return (tmv_t) x;
tmv_t t;
t.ns = x;
return t;
}
static inline int64_t tmv_to_nanoseconds(tmv_t x)
{
return x;
return x.ns;
}
static inline TimeInterval tmv_to_TimeInterval(tmv_t x)
{
return x << 16;
return x.ns << 16;
}
static inline tmv_t timespec_to_tmv(struct timespec ts)
{
return ts.tv_sec * NS_PER_SEC + ts.tv_nsec;
tmv_t t;
t.ns = ts.tv_sec * NS_PER_SEC + ts.tv_nsec;
return t;
}
static inline tmv_t timestamp_to_tmv(struct timestamp ts)
{
return ts.sec * NS_PER_SEC + ts.nsec;
tmv_t t;
t.ns = ts.sec * NS_PER_SEC + ts.nsec;
return t;
}
#endif