2 * timespecops.c -- calculations on 'struct timespec' values
4 * Written by Juergen Perlinger (perlinger@ntp.org) for the NTP project.
5 * The contents of 'html/copyright.html' apply.
11 #include <sys/types.h>
17 #include "timespecops.h"
20 /* nanoseconds per second */
21 #define NANOSECONDS 1000000000
23 /* conversion between l_fp fractions and nanoseconds */
25 # define FTOTVN(tsf) \
27 (((u_int64)(tsf) * NANOSECONDS + 0x80000000) >> 32))
28 # define TVNTOF(tvu) \
30 ((((u_int64)(tvu) << 32) + NANOSECONDS / 2) / \
33 # define NSECFRAC (FRAC / NANOSECONDS)
34 # define FTOTVN(tsf) \
35 ((int32)((tsf) / NSECFRAC + 0.5))
36 # define TVNTOF(tvu) \
37 ((u_int32)((tvu) * NSECFRAC + 0.5))
42 /* make sure nanoseconds are in nominal range */
52 * tv_nsec is of type 'long', and on a 64-bit machine using only
53 * loops becomes prohibitive once the upper 32 bits get
54 * involved. On the other hand, division by constant should be
55 * fast enough; so we do a division of the nanoseconds in that
56 * case. The floor adjustment step follows with the standard
57 * normalisation loops. And labs() is intentionally not used
58 * here: it has implementation-defined behaviour when applied
61 if (x.tv_nsec < -3l * NANOSECONDS ||
62 x.tv_nsec > 3l * NANOSECONDS) {
63 z = x.tv_nsec / NANOSECONDS;
64 x.tv_nsec -= z * NANOSECONDS;
68 /* since 10**9 is close to 2**32, we don't divide but do a
69 * normalisation in a loop; this takes 3 steps max, and should
70 * outperform a division even if the mul-by-inverse trick is
74 x.tv_nsec += NANOSECONDS;
76 } while (x.tv_nsec < 0);
77 else if (x.tv_nsec >= NANOSECONDS)
79 x.tv_nsec -= NANOSECONDS;
81 } while (x.tv_nsec >= NANOSECONDS);
94 c = normalize_tspec(a);
97 c.tv_sec = -c.tv_sec - 1;
98 c.tv_nsec = NANOSECONDS - c.tv_nsec;
100 c.tv_sec = -c.tv_sec;
108 * compare previously-normalised a and b
109 * return 1 / 0 / -1 if a < / == / > b
119 r = (a.tv_sec > b.tv_sec) - (a.tv_sec < b.tv_sec);
121 r = (a.tv_nsec > b.tv_nsec) -
122 (a.tv_nsec < b.tv_nsec);
128 * test previously-normalised a
129 * return 1 / 0 / -1 if a < / == / > 0
138 r = (a.tv_sec > 0) - (a.tv_sec < 0);
146 * convert to l_fp type, relative and absolute
149 /* convert from timespec duration to l_fp duration */
158 v = normalize_tspec(x);
159 y.l_uf = TVNTOF(v.tv_nsec);
160 y.l_i = (int32)v.tv_sec;
165 /* convert from l_fp type, relative signed/unsigned and absolute */
180 out.tv_nsec = FTOTVN(absx.l_uf);
181 out.tv_sec = absx.l_i;
183 out.tv_sec = -out.tv_sec;
184 out.tv_nsec = -out.tv_nsec;
185 out = normalize_tspec(out);
198 out.tv_nsec = FTOTVN(x.l_uf);
205 * absolute (timestamp) conversion. Input is time in NTP epoch, output
206 * is in UN*X epoch. The NTP time stamp will be expanded around the
207 * pivot time *p or the current time, if p is NULL.
218 sec = ntpcal_ntp_to_time(x.l_ui, p);
219 out.tv_nsec = FTOTVN(x.l_uf);
221 /* copying a vint64 to a time_t needs some care... */
222 #if SIZEOF_TIME_T <= 4
223 out.tv_sec = (time_t)sec.d_s.lo;
224 #elif defined(HAVE_INT64)
225 out.tv_sec = (time_t)sec.q_s;
227 out.tv_sec = ((time_t)sec.d_s.hi << 32) | sec.d_s.lo;