xref: /netbsd-src/external/bsd/ntp/dist/include/timespecops.h (revision 946379e7b37692fc43f68eb0d1c10daa0a7f3b6c)
1 /*	$NetBSD: timespecops.h,v 1.4 2016/01/08 21:35:35 christos Exp $	*/
2 
3 /*
4  * timespecops.h -- calculations on 'struct timespec' values
5  *
6  * Written by Juergen Perlinger (perlinger@ntp.org) for the NTP project.
7  * The contents of 'html/copyright.html' apply.
8  *
9  * Rationale
10  * ---------
11  *
12  * Doing basic arithmetic on a 'struct timespec' is not exceedingly
13  * hard, but it requires tedious and repetitive code to keep the result
14  * normalised. We consider a timespec normalised when the nanosecond
15  * fraction is in the interval [0 .. 10^9[ ; there are multiple value
16  * pairs of seconds and nanoseconds that denote the same time interval,
17  * but the normalised representation is unique. No two different
18  * intervals can have the same normalised representation.
19  *
20  * Another topic is the representation of negative time intervals.
21  * There's more than one way to this, since both the seconds and the
22  * nanoseconds of a timespec are signed values. IMHO, the easiest way is
23  * to use a complement representation where the nanoseconds are still
24  * normalised, no matter what the sign of the seconds value. This makes
25  * normalisation easier, since the sign of the integer part is
26  * irrelevant, and it removes several sign decision cases during the
27  * calculations.
28  *
29  * As long as no signed integer overflow can occur with the nanosecond
30  * part of the operands, all operations work as expected and produce a
31  * normalised result.
32  *
33  * The exception to this are functions fix a '_fast' suffix, which do no
34  * normalisation on input data and therefore expect the input data to be
35  * normalised.
36  *
37  * Input and output operands may overlap; all input is consumed before
38  * the output is written to.
39  */
40 #ifndef TIMESPECOPS_H
41 #define TIMESPECOPS_H
42 
43 #include <sys/types.h>
44 #include <stdio.h>
45 #include <math.h>
46 
47 #include "ntp.h"
48 #include "timetoa.h"
49 
50 
51 /* nanoseconds per second */
52 #define NANOSECONDS 1000000000
53 
54 /* predicate: returns TRUE if the nanoseconds are in nominal range */
55 #define timespec_isnormal(x) \
56 	((x)->tv_nsec >= 0 && (x)->tv_nsec < NANOSECONDS)
57 
58 /* predicate: returns TRUE if the nanoseconds are out-of-bounds */
59 #define timespec_isdenormal(x)	(!timespec_isnormal(x))
60 
61 /* conversion between l_fp fractions and nanoseconds */
62 #ifdef HAVE_U_INT64
63 # define FTOTVN(tsf)						\
64 	((int32)						\
65 	 (((u_int64)(tsf) * NANOSECONDS + 0x80000000) >> 32))
66 # define TVNTOF(tvu)						\
67 	((u_int32)						\
68 	 ((((u_int64)(tvu) << 32) + NANOSECONDS / 2) /		\
69 	  NANOSECONDS))
70 #else
71 # define NSECFRAC	(FRAC / NANOSECONDS)
72 # define FTOTVN(tsf)						\
73 	((int32)((tsf) / NSECFRAC + 0.5))
74 # define TVNTOF(tvu)						\
75 	((u_int32)((tvu) * NSECFRAC + 0.5))
76 #endif
77 
78 
79 
80 /* make sure nanoseconds are in nominal range */
81 static inline struct timespec
82 normalize_tspec(
83 	struct timespec x
84 	)
85 {
86 #if SIZEOF_LONG > 4
87 	long	z;
88 
89 	/*
90 	 * tv_nsec is of type 'long', and on a 64-bit machine using only
91 	 * loops becomes prohibitive once the upper 32 bits get
92 	 * involved. On the other hand, division by constant should be
93 	 * fast enough; so we do a division of the nanoseconds in that
94 	 * case. The floor adjustment step follows with the standard
95 	 * normalisation loops. And labs() is intentionally not used
96 	 * here: it has implementation-defined behaviour when applied
97 	 * to LONG_MIN.
98 	 */
99 	if (x.tv_nsec < -3l * NANOSECONDS ||
100 	    x.tv_nsec > 3l * NANOSECONDS) {
101 		z = x.tv_nsec / NANOSECONDS;
102 		x.tv_nsec -= z * NANOSECONDS;
103 		x.tv_sec += z;
104 	}
105 #endif
106 	/* since 10**9 is close to 2**32, we don't divide but do a
107 	 * normalisation in a loop; this takes 3 steps max, and should
108 	 * outperform a division even if the mul-by-inverse trick is
109 	 * employed. */
110 	if (x.tv_nsec < 0)
111 		do {
112 			x.tv_nsec += NANOSECONDS;
113 			x.tv_sec--;
114 		} while (x.tv_nsec < 0);
115 	else if (x.tv_nsec >= NANOSECONDS)
116 		do {
117 			x.tv_nsec -= NANOSECONDS;
118 			x.tv_sec++;
119 		} while (x.tv_nsec >= NANOSECONDS);
120 
121 	return x;
122 }
123 
124 /* x = a + b */
125 static inline struct timespec
126 add_tspec(
127 	struct timespec	a,
128 	struct timespec	b
129 	)
130 {
131 	struct timespec	x;
132 
133 	x = a;
134 	x.tv_sec += b.tv_sec;
135 	x.tv_nsec += b.tv_nsec;
136 
137 	return normalize_tspec(x);
138 }
139 
140 /* x = a + b, b is fraction only */
141 static inline struct timespec
142 add_tspec_ns(
143 	struct timespec	a,
144 	long		b
145 	)
146 {
147 	struct timespec x;
148 
149 	x = a;
150 	x.tv_nsec += b;
151 
152 	return normalize_tspec(x);
153 }
154 
155 /* x = a - b */
156 static inline struct timespec
157 sub_tspec(
158 	struct timespec	a,
159 	struct timespec	b
160 	)
161 {
162 	struct timespec x;
163 
164 	x = a;
165 	x.tv_sec -= b.tv_sec;
166 	x.tv_nsec -= b.tv_nsec;
167 
168 	return normalize_tspec(x);
169 }
170 
171 /* x = a - b, b is fraction only */
172 static inline struct timespec
173 sub_tspec_ns(
174 	struct timespec	a,
175 	long		b
176 	)
177 {
178 	struct timespec	x;
179 
180 	x = a;
181 	x.tv_nsec -= b;
182 
183 	return normalize_tspec(x);
184 }
185 
186 /* x = -a */
187 static inline struct timespec
188 neg_tspec(
189 	struct timespec	a
190 	)
191 {
192 	struct timespec	x;
193 
194 	x.tv_sec = -a.tv_sec;
195 	x.tv_nsec = -a.tv_nsec;
196 
197 	return normalize_tspec(x);
198 }
199 
200 /* x = abs(a) */
201 static inline struct timespec
202 abs_tspec(
203 	struct timespec	a
204 	)
205 {
206 	struct timespec	c;
207 
208 	c = normalize_tspec(a);
209 	if (c.tv_sec < 0) {
210 		if (c.tv_nsec != 0) {
211 			c.tv_sec = -c.tv_sec - 1;
212 			c.tv_nsec = NANOSECONDS - c.tv_nsec;
213 		} else {
214 			c.tv_sec = -c.tv_sec;
215 		}
216 	}
217 
218 	return c;
219 }
220 
221 /*
222  * compare previously-normalised a and b
223  * return 1 / 0 / -1 if a < / == / > b
224  */
225 static inline int
226 cmp_tspec(
227 	struct timespec a,
228 	struct timespec b
229 	)
230 {
231 	int r;
232 
233 	r = (a.tv_sec > b.tv_sec) - (a.tv_sec < b.tv_sec);
234 	if (0 == r)
235 		r = (a.tv_nsec > b.tv_nsec) -
236 		    (a.tv_nsec < b.tv_nsec);
237 
238 	return r;
239 }
240 
241 /*
242  * compare possibly-denormal a and b
243  * return 1 / 0 / -1 if a < / == / > b
244  */
245 static inline int
246 cmp_tspec_denorm(
247 	struct timespec	a,
248 	struct timespec	b
249 	)
250 {
251 	return cmp_tspec(normalize_tspec(a), normalize_tspec(b));
252 }
253 
254 /*
255  * test previously-normalised a
256  * return 1 / 0 / -1 if a < / == / > 0
257  */
258 static inline int
259 test_tspec(
260 	struct timespec	a
261 	)
262 {
263 	int		r;
264 
265 	r = (a.tv_sec > 0) - (a.tv_sec < 0);
266 	if (r == 0)
267 		r = (a.tv_nsec > 0);
268 
269 	return r;
270 }
271 
272 /*
273  * test possibly-denormal a
274  * return 1 / 0 / -1 if a < / == / > 0
275  */
276 static inline int
277 test_tspec_denorm(
278 	struct timespec	a
279 	)
280 {
281 	return test_tspec(normalize_tspec(a));
282 }
283 
284 /* return LIB buffer ptr to string rep */
285 static inline const char *
286 tspectoa(
287 	struct timespec	x
288 	)
289 {
290 	return format_time_fraction(x.tv_sec, x.tv_nsec, 9);
291 }
292 
293 /*
294  *  convert to l_fp type, relative and absolute
295  */
296 
297 /* convert from timespec duration to l_fp duration */
298 static inline l_fp
299 tspec_intv_to_lfp(
300 	struct timespec	x
301 	)
302 {
303 	struct timespec	v;
304 	l_fp		y;
305 
306 	v = normalize_tspec(x);
307 	y.l_uf = TVNTOF(v.tv_nsec);
308 	y.l_i = (int32)v.tv_sec;
309 
310 	return y;
311 }
312 
313 /* x must be UN*X epoch, output will be in NTP epoch */
314 static inline l_fp
315 tspec_stamp_to_lfp(
316 	struct timespec	x
317 	)
318 {
319 	l_fp		y;
320 
321 	y = tspec_intv_to_lfp(x);
322 	y.l_ui += JAN_1970;
323 
324 	return y;
325 }
326 
327 /* convert from l_fp type, relative signed/unsigned and absolute */
328 static inline struct timespec
329 lfp_intv_to_tspec(
330 	l_fp		x
331 	)
332 {
333 	struct timespec out;
334 	l_fp		absx;
335 	int		neg;
336 
337 	neg = L_ISNEG(&x);
338 	absx = x;
339 	if (neg) {
340 		L_NEG(&absx);
341 	}
342 	out.tv_nsec = FTOTVN(absx.l_uf);
343 	out.tv_sec = absx.l_i;
344 	if (neg) {
345 		out.tv_sec = -out.tv_sec;
346 		out.tv_nsec = -out.tv_nsec;
347 		out = normalize_tspec(out);
348 	}
349 
350 	return out;
351 }
352 
353 static inline struct timespec
354 lfp_uintv_to_tspec(
355 	l_fp		x
356 	)
357 {
358 	struct timespec	out;
359 
360 	out.tv_nsec = FTOTVN(x.l_uf);
361 	out.tv_sec = x.l_ui;
362 
363 	return out;
364 }
365 
366 /*
367  * absolute (timestamp) conversion. Input is time in NTP epoch, output
368  * is in UN*X epoch. The NTP time stamp will be expanded around the
369  * pivot time *p or the current time, if p is NULL.
370  */
371 static inline struct timespec
372 lfp_stamp_to_tspec(
373 	l_fp		x,
374 	const time_t *	p
375 	)
376 {
377 	struct timespec	out;
378 	vint64		sec;
379 
380 	sec = ntpcal_ntp_to_time(x.l_ui, p);
381 	out.tv_nsec = FTOTVN(x.l_uf);
382 
383 	/* copying a vint64 to a time_t needs some care... */
384 #if SIZEOF_TIME_T <= 4
385 	out.tv_sec = (time_t)sec.d_s.lo;
386 #elif defined(HAVE_INT64)
387 	out.tv_sec = (time_t)sec.q_s;
388 #else
389 	out.tv_sec = ((time_t)sec.d_s.hi << 32) | sec.d_s.lo;
390 #endif
391 
392 	return out;
393 }
394 
395 #endif	/* TIMESPECOPS_H */
396