xref: /netbsd-src/external/bsd/ntp/dist/libntp/timespecops.c (revision cdfa2a7ef92791ba9db70a584a1d904730e6fb46)
1*cdfa2a7eSchristos /*	$NetBSD: timespecops.c,v 1.2 2020/05/25 20:47:24 christos Exp $	*/
2067f5680Schristos 
3067f5680Schristos /*
4067f5680Schristos  * timespecops.c -- calculations on 'struct timespec' values
5067f5680Schristos  *
6067f5680Schristos  * Written by Juergen Perlinger (perlinger@ntp.org) for the NTP project.
7067f5680Schristos  * The contents of 'html/copyright.html' apply.
8067f5680Schristos  *
9067f5680Schristos  */
10067f5680Schristos 
11067f5680Schristos #include "config.h"
12067f5680Schristos 
13067f5680Schristos #include <sys/types.h>
14067f5680Schristos #include <stdio.h>
15067f5680Schristos #include <math.h>
16067f5680Schristos 
17067f5680Schristos #include "ntp.h"
18067f5680Schristos #include "timetoa.h"
19067f5680Schristos #include "timespecops.h"
20067f5680Schristos 
21067f5680Schristos 
22067f5680Schristos /* nanoseconds per second */
23067f5680Schristos #define NANOSECONDS 1000000000
24067f5680Schristos 
25067f5680Schristos /* conversion between l_fp fractions and nanoseconds */
26067f5680Schristos #ifdef HAVE_U_INT64
27067f5680Schristos # define FTOTVN(tsf)						\
28067f5680Schristos 	((int32)						\
29067f5680Schristos 	 (((u_int64)(tsf) * NANOSECONDS + 0x80000000) >> 32))
30067f5680Schristos # define TVNTOF(tvu)						\
31067f5680Schristos 	((u_int32)						\
32067f5680Schristos 	 ((((u_int64)(tvu) << 32) + NANOSECONDS / 2) /		\
33067f5680Schristos 	  NANOSECONDS))
34067f5680Schristos #else
35067f5680Schristos # define NSECFRAC	(FRAC / NANOSECONDS)
36067f5680Schristos # define FTOTVN(tsf)						\
37067f5680Schristos 	((int32)((tsf) / NSECFRAC + 0.5))
38067f5680Schristos # define TVNTOF(tvu)						\
39067f5680Schristos 	((u_int32)((tvu) * NSECFRAC + 0.5))
40067f5680Schristos #endif
41067f5680Schristos 
42067f5680Schristos 
43067f5680Schristos 
44067f5680Schristos /* make sure nanoseconds are in nominal range */
45067f5680Schristos struct timespec
normalize_tspec(struct timespec x)46067f5680Schristos normalize_tspec(
47067f5680Schristos 	struct timespec x
48067f5680Schristos 	)
49067f5680Schristos {
50067f5680Schristos #if SIZEOF_LONG > 4
51067f5680Schristos 	long	z;
52067f5680Schristos 
53067f5680Schristos 	/*
54067f5680Schristos 	 * tv_nsec is of type 'long', and on a 64-bit machine using only
55067f5680Schristos 	 * loops becomes prohibitive once the upper 32 bits get
56067f5680Schristos 	 * involved. On the other hand, division by constant should be
57067f5680Schristos 	 * fast enough; so we do a division of the nanoseconds in that
58067f5680Schristos 	 * case. The floor adjustment step follows with the standard
59067f5680Schristos 	 * normalisation loops. And labs() is intentionally not used
60067f5680Schristos 	 * here: it has implementation-defined behaviour when applied
61067f5680Schristos 	 * to LONG_MIN.
62067f5680Schristos 	 */
63067f5680Schristos 	if (x.tv_nsec < -3l * NANOSECONDS ||
64067f5680Schristos 	    x.tv_nsec > 3l * NANOSECONDS) {
65067f5680Schristos 		z = x.tv_nsec / NANOSECONDS;
66067f5680Schristos 		x.tv_nsec -= z * NANOSECONDS;
67067f5680Schristos 		x.tv_sec += z;
68067f5680Schristos 	}
69067f5680Schristos #endif
70067f5680Schristos 	/* since 10**9 is close to 2**32, we don't divide but do a
71067f5680Schristos 	 * normalisation in a loop; this takes 3 steps max, and should
72067f5680Schristos 	 * outperform a division even if the mul-by-inverse trick is
73067f5680Schristos 	 * employed. */
74067f5680Schristos 	if (x.tv_nsec < 0)
75067f5680Schristos 		do {
76067f5680Schristos 			x.tv_nsec += NANOSECONDS;
77067f5680Schristos 			x.tv_sec--;
78067f5680Schristos 		} while (x.tv_nsec < 0);
79067f5680Schristos 	else if (x.tv_nsec >= NANOSECONDS)
80067f5680Schristos 		do {
81067f5680Schristos 			x.tv_nsec -= NANOSECONDS;
82067f5680Schristos 			x.tv_sec++;
83067f5680Schristos 		} while (x.tv_nsec >= NANOSECONDS);
84067f5680Schristos 
85067f5680Schristos 	return x;
86067f5680Schristos }
87067f5680Schristos 
88067f5680Schristos /* x = abs(a) */
89067f5680Schristos struct timespec
abs_tspec(struct timespec a)90067f5680Schristos abs_tspec(
91067f5680Schristos 	struct timespec	a
92067f5680Schristos 	)
93067f5680Schristos {
94067f5680Schristos 	struct timespec	c;
95067f5680Schristos 
96067f5680Schristos 	c = normalize_tspec(a);
97067f5680Schristos 	if (c.tv_sec < 0) {
98067f5680Schristos 		if (c.tv_nsec != 0) {
99067f5680Schristos 			c.tv_sec = -c.tv_sec - 1;
100067f5680Schristos 			c.tv_nsec = NANOSECONDS - c.tv_nsec;
101067f5680Schristos 		} else {
102067f5680Schristos 			c.tv_sec = -c.tv_sec;
103067f5680Schristos 		}
104067f5680Schristos 	}
105067f5680Schristos 
106067f5680Schristos 	return c;
107067f5680Schristos }
108067f5680Schristos 
109067f5680Schristos /*
110067f5680Schristos  * compare previously-normalised a and b
111067f5680Schristos  * return 1 / 0 / -1 if a < / == / > b
112067f5680Schristos  */
113067f5680Schristos int
cmp_tspec(struct timespec a,struct timespec b)114067f5680Schristos cmp_tspec(
115067f5680Schristos 	struct timespec a,
116067f5680Schristos 	struct timespec b
117067f5680Schristos 	)
118067f5680Schristos {
119067f5680Schristos 	int r;
120067f5680Schristos 
121067f5680Schristos 	r = (a.tv_sec > b.tv_sec) - (a.tv_sec < b.tv_sec);
122067f5680Schristos 	if (0 == r)
123067f5680Schristos 		r = (a.tv_nsec > b.tv_nsec) -
124067f5680Schristos 		    (a.tv_nsec < b.tv_nsec);
125067f5680Schristos 
126067f5680Schristos 	return r;
127067f5680Schristos }
128067f5680Schristos 
129067f5680Schristos /*
130067f5680Schristos  * test previously-normalised a
131067f5680Schristos  * return 1 / 0 / -1 if a < / == / > 0
132067f5680Schristos  */
133067f5680Schristos int
test_tspec(struct timespec a)134067f5680Schristos test_tspec(
135067f5680Schristos 	struct timespec	a
136067f5680Schristos 	)
137067f5680Schristos {
138067f5680Schristos 	int		r;
139067f5680Schristos 
140067f5680Schristos 	r = (a.tv_sec > 0) - (a.tv_sec < 0);
141067f5680Schristos 	if (r == 0)
142067f5680Schristos 		r = (a.tv_nsec > 0);
143067f5680Schristos 
144067f5680Schristos 	return r;
145067f5680Schristos }
146067f5680Schristos 
147067f5680Schristos /*
148067f5680Schristos  *  convert to l_fp type, relative and absolute
149067f5680Schristos  */
150067f5680Schristos 
151067f5680Schristos /* convert from timespec duration to l_fp duration */
152067f5680Schristos l_fp
tspec_intv_to_lfp(struct timespec x)153067f5680Schristos tspec_intv_to_lfp(
154067f5680Schristos 	struct timespec	x
155067f5680Schristos 	)
156067f5680Schristos {
157067f5680Schristos 	struct timespec	v;
158067f5680Schristos 	l_fp		y;
159067f5680Schristos 
160067f5680Schristos 	v = normalize_tspec(x);
161067f5680Schristos 	y.l_uf = TVNTOF(v.tv_nsec);
162067f5680Schristos 	y.l_i = (int32)v.tv_sec;
163067f5680Schristos 
164067f5680Schristos 	return y;
165067f5680Schristos }
166067f5680Schristos 
167067f5680Schristos /* convert from l_fp type, relative signed/unsigned and absolute */
168067f5680Schristos struct timespec
lfp_intv_to_tspec(l_fp x)169067f5680Schristos lfp_intv_to_tspec(
170067f5680Schristos 	l_fp		x
171067f5680Schristos 	)
172067f5680Schristos {
173067f5680Schristos 	struct timespec out;
174067f5680Schristos 	l_fp		absx;
175067f5680Schristos 	int		neg;
176067f5680Schristos 
177067f5680Schristos 	neg = L_ISNEG(&x);
178067f5680Schristos 	absx = x;
179067f5680Schristos 	if (neg) {
180067f5680Schristos 		L_NEG(&absx);
181067f5680Schristos 	}
182067f5680Schristos 	out.tv_nsec = FTOTVN(absx.l_uf);
183067f5680Schristos 	out.tv_sec = absx.l_i;
184067f5680Schristos 	if (neg) {
185067f5680Schristos 		out.tv_sec = -out.tv_sec;
186067f5680Schristos 		out.tv_nsec = -out.tv_nsec;
187067f5680Schristos 		out = normalize_tspec(out);
188067f5680Schristos 	}
189067f5680Schristos 
190067f5680Schristos 	return out;
191067f5680Schristos }
192067f5680Schristos 
193067f5680Schristos struct timespec
lfp_uintv_to_tspec(l_fp x)194067f5680Schristos lfp_uintv_to_tspec(
195067f5680Schristos 	l_fp		x
196067f5680Schristos 	)
197067f5680Schristos {
198067f5680Schristos 	struct timespec	out;
199067f5680Schristos 
200067f5680Schristos 	out.tv_nsec = FTOTVN(x.l_uf);
201067f5680Schristos 	out.tv_sec = x.l_ui;
202067f5680Schristos 
203067f5680Schristos 	return out;
204067f5680Schristos }
205067f5680Schristos 
206067f5680Schristos /*
207067f5680Schristos  * absolute (timestamp) conversion. Input is time in NTP epoch, output
208067f5680Schristos  * is in UN*X epoch. The NTP time stamp will be expanded around the
209067f5680Schristos  * pivot time *p or the current time, if p is NULL.
210067f5680Schristos  */
211067f5680Schristos struct timespec
lfp_stamp_to_tspec(l_fp x,const time_t * p)212067f5680Schristos lfp_stamp_to_tspec(
213067f5680Schristos 	l_fp		x,
214067f5680Schristos 	const time_t *	p
215067f5680Schristos 	)
216067f5680Schristos {
217067f5680Schristos 	struct timespec	out;
218067f5680Schristos 	vint64		sec;
219067f5680Schristos 
220067f5680Schristos 	sec = ntpcal_ntp_to_time(x.l_ui, p);
221067f5680Schristos 	out.tv_nsec = FTOTVN(x.l_uf);
222067f5680Schristos 
223067f5680Schristos 	/* copying a vint64 to a time_t needs some care... */
224067f5680Schristos #if SIZEOF_TIME_T <= 4
225067f5680Schristos 	out.tv_sec = (time_t)sec.d_s.lo;
226067f5680Schristos #elif defined(HAVE_INT64)
227067f5680Schristos 	out.tv_sec = (time_t)sec.q_s;
228067f5680Schristos #else
229067f5680Schristos 	out.tv_sec = ((time_t)sec.d_s.hi << 32) | sec.d_s.lo;
230067f5680Schristos #endif
231067f5680Schristos 
232067f5680Schristos 	return out;
233067f5680Schristos }
234067f5680Schristos 
235067f5680Schristos /* -*-EOF-*- */
236