2 * timevalops.h -- calculations on 'struct timeval' values
4 * Written by Juergen Perlinger (perlinger@ntp.org) for the NTP project.
5 * The contents of 'html/copyright.html' apply.
7 * For a rationale look at 'timespecops.h'; we do the same here, but the
8 * normalisation keeps the microseconds in [0 .. 10^6[, of course.
13 #include <sys/types.h>
20 /* microseconds per second */
21 #define MICROSECONDS 1000000
24 # define USE_TSF_USEC_TABLES
28 * Convert usec to a time stamp fraction.
30 #ifdef USE_TSF_USEC_TABLES
31 extern const u_int32 ustotslo[];
32 extern const u_int32 ustotsmid[];
33 extern const u_int32 ustotshi[];
35 # define TVUTOTSF(tvu, tsf) \
36 ((tsf) = ustotslo[(tvu) & 0xff] \
37 + ustotsmid[((tvu) >> 8) & 0xff] \
38 + ustotshi[((tvu) >> 16) & 0xf])
40 # define TVUTOTSF(tvu, tsf) \
42 ((((u_int64)(tvu) << 32) + MICROSECONDS / 2) / \
47 * Convert a time stamp fraction to microseconds. The time stamp
48 * fraction is assumed to be unsigned.
50 #ifdef USE_TSF_USEC_TABLES
51 extern const u_int32 tstouslo[256];
52 extern const u_int32 tstousmid[256];
53 extern const u_int32 tstoushi[128];
56 * TV_SHIFT is used to turn the table result into a usec value. To
57 * round, add in TV_ROUNDBIT before shifting.
60 #define TV_ROUNDBIT 0x4
62 # define TSFTOTVU(tsf, tvu) \
63 ((tvu) = (tstoushi[((tsf) >> 24) & 0xff] \
64 + tstousmid[((tsf) >> 16) & 0xff] \
65 + tstouslo[((tsf) >> 9) & 0x7f] \
66 + TV_ROUNDBIT) >> TV_SHIFT)
68 # define TSFTOTVU(tsf, tvu) \
70 (((u_int64)(tsf) * MICROSECONDS + 0x80000000) >> 32))
74 * Convert a struct timeval to a time stamp.
76 #define TVTOTS(tv, ts) \
78 (ts)->l_ui = (u_long)(tv)->tv_sec; \
79 TVUTOTSF((tv)->tv_usec, (ts)->l_uf); \
82 #define sTVTOTS(tv, ts) \
86 (ts)->l_ui = (tv)->tv_sec; \
87 usec = (tv)->tv_usec; \
88 if (((tv)->tv_sec < 0) || ((tv)->tv_usec < 0)) { \
90 (ts)->l_ui = -(ts)->l_ui; \
93 TVUTOTSF(usec, (ts)->l_uf); \
100 * Convert a time stamp to a struct timeval. The time stamp
101 * has to be positive.
103 #define TSTOTV(ts, tv) \
105 (tv)->tv_sec = (ts)->l_ui; \
106 TSFTOTVU((ts)->l_uf, (tv)->tv_usec); \
107 if ((tv)->tv_usec == 1000000) { \
115 * predicate: returns TRUE if the microseconds are in nominal range
116 * use like: int timeval_isnormal(const struct timeval *x)
118 #define timeval_isnormal(x) \
119 ((x)->tv_usec >= 0 && (x)->tv_usec < MICROSECONDS)
122 * Convert milliseconds to a time stamp fraction. Unused except for
123 * refclock_leitch.c, so accompanying lookup tables were removed in
124 * favor of reusing the microseconds conversion tables.
126 #define MSUTOTSF(msu, tsf) TVUTOTSF((msu) * 1000, tsf)
129 * predicate: returns TRUE if the microseconds are out-of-bounds
130 * use like: int timeval_isdenormal(const struct timeval *x)
132 #define timeval_isdenormal(x) (!timeval_isnormal(x))
134 /* make sure microseconds are in nominal range */
135 static inline struct timeval
143 * If the fraction becomes excessive denormal, we use division
144 * to do first partial normalisation. The normalisation loops
145 * following will do the remaining cleanup. Since the size of
146 * tv_usec has a peculiar definition by the standard the range
147 * check is coded manually. And labs() is intentionally not used
148 * here: it has implementation-defined behaviour when applied
151 if (x.tv_usec < -3l * MICROSECONDS ||
152 x.tv_usec > 3l * MICROSECONDS ) {
153 z = x.tv_usec / MICROSECONDS;
154 x.tv_usec -= z * MICROSECONDS;
159 * Do any remaining normalisation steps in loops. This takes 3
160 * steps max, and should outperform a division even if the
161 * mul-by-inverse trick is employed. (It also does the floor
162 * division adjustment if the above division was executed.)
166 x.tv_usec += MICROSECONDS;
168 } while (x.tv_usec < 0);
169 else if (x.tv_usec >= MICROSECONDS)
171 x.tv_usec -= MICROSECONDS;
173 } while (x.tv_usec >= MICROSECONDS);
179 static inline struct timeval
188 x.tv_sec += b.tv_sec;
189 x.tv_usec += b.tv_usec;
191 return normalize_tval(x);
194 /* x = a + b, b is fraction only */
195 static inline struct timeval
206 return normalize_tval(x);
210 static inline struct timeval
219 x.tv_sec -= b.tv_sec;
220 x.tv_usec -= b.tv_usec;
222 return normalize_tval(x);
225 /* x = a - b, b is fraction only */
226 static inline struct timeval
237 return normalize_tval(x);
241 static inline struct timeval
248 x.tv_sec = -a.tv_sec;
249 x.tv_usec = -a.tv_usec;
251 return normalize_tval(x);
255 static inline struct timeval
262 c = normalize_tval(a);
264 if (c.tv_usec != 0) {
265 c.tv_sec = -c.tv_sec - 1;
266 c.tv_usec = MICROSECONDS - c.tv_usec;
268 c.tv_sec = -c.tv_sec;
276 * compare previously-normalised a and b
277 * return 1 / 0 / -1 if a < / == / > b
287 r = (a.tv_sec > b.tv_sec) - (a.tv_sec < b.tv_sec);
289 r = (a.tv_usec > b.tv_usec) -
290 (a.tv_usec < b.tv_usec);
296 * compare possibly-denormal a and b
297 * return 1 / 0 / -1 if a < / == / > b
305 return cmp_tval(normalize_tval(a), normalize_tval(b));
309 * test previously-normalised a
310 * return 1 / 0 / -1 if a < / == / > 0
319 r = (a.tv_sec > 0) - (a.tv_sec < 0);
327 * test possibly-denormal a
328 * return 1 / 0 / -1 if a < / == / > 0
335 return test_tval(normalize_tval(a));
338 /* return LIB buffer ptr to string rep */
339 static inline const char *
344 return format_time_fraction(x.tv_sec, x.tv_usec, 6);
347 /* convert from timeval duration to l_fp duration */
356 v = normalize_tval(x);
357 TVUTOTSF(v.tv_usec, y.l_uf);
358 y.l_i = (int32)v.tv_sec;
363 /* x must be UN*X epoch, output *y will be in NTP epoch */
371 y = tval_intv_to_lfp(x);
377 /* convert to l_fp type, relative signed/unsigned and absolute */
378 static inline struct timeval
392 TSFTOTVU(absx.l_uf, out.tv_usec);
393 out.tv_sec = absx.l_i;
395 out.tv_sec = -out.tv_sec;
396 out.tv_usec = -out.tv_usec;
397 out = normalize_tval(out);
403 static inline struct timeval
410 TSFTOTVU(x.l_uf, out.tv_usec);
417 * absolute (timestamp) conversion. Input is time in NTP epoch, output
418 * is in UN*X epoch. The NTP time stamp will be expanded around the
419 * pivot time *p or the current time, if p is NULL.
421 static inline struct timeval
430 sec = ntpcal_ntp_to_time(x.l_ui, p);
431 TSFTOTVU(x.l_uf, out.tv_usec);
433 /* copying a vint64 to a time_t needs some care... */
434 #if SIZEOF_TIME_T <= 4
435 out.tv_sec = (time_t)sec.d_s.lo;
436 #elif defined(HAVE_INT64)
437 out.tv_sec = (time_t)sec.q_s;
439 out.tv_sec = ((time_t)sec.d_s.hi << 32) | sec.d_s.lo;
441 out = normalize_tval(out);
446 #endif /* TIMEVALOPS_H */