xref: /freebsd/contrib/ntp/libntp/timespecops.c (revision 2d4e511c)
1*2d4e511cSCy Schubert /*
2*2d4e511cSCy Schubert  * timespecops.c -- calculations on 'struct timespec' values
3*2d4e511cSCy Schubert  *
4*2d4e511cSCy Schubert  * Written by Juergen Perlinger (perlinger@ntp.org) for the NTP project.
5*2d4e511cSCy Schubert  * The contents of 'html/copyright.html' apply.
6*2d4e511cSCy Schubert  *
7*2d4e511cSCy Schubert  */
8*2d4e511cSCy Schubert 
9*2d4e511cSCy Schubert #include "config.h"
10*2d4e511cSCy Schubert 
11*2d4e511cSCy Schubert #include <sys/types.h>
12*2d4e511cSCy Schubert #include <stdio.h>
13*2d4e511cSCy Schubert #include <math.h>
14*2d4e511cSCy Schubert 
15*2d4e511cSCy Schubert #include "ntp.h"
16*2d4e511cSCy Schubert #include "timetoa.h"
17*2d4e511cSCy Schubert #include "timespecops.h"
18*2d4e511cSCy Schubert 
19*2d4e511cSCy Schubert 
20*2d4e511cSCy Schubert /* nanoseconds per second */
21*2d4e511cSCy Schubert #define NANOSECONDS 1000000000
22*2d4e511cSCy Schubert 
23*2d4e511cSCy Schubert /* conversion between l_fp fractions and nanoseconds */
24*2d4e511cSCy Schubert #ifdef HAVE_U_INT64
25*2d4e511cSCy Schubert # define FTOTVN(tsf)						\
26*2d4e511cSCy Schubert 	((int32)						\
27*2d4e511cSCy Schubert 	 (((u_int64)(tsf) * NANOSECONDS + 0x80000000) >> 32))
28*2d4e511cSCy Schubert # define TVNTOF(tvu)						\
29*2d4e511cSCy Schubert 	((u_int32)						\
30*2d4e511cSCy Schubert 	 ((((u_int64)(tvu) << 32) + NANOSECONDS / 2) /		\
31*2d4e511cSCy Schubert 	  NANOSECONDS))
32*2d4e511cSCy Schubert #else
33*2d4e511cSCy Schubert # define NSECFRAC	(FRAC / NANOSECONDS)
34*2d4e511cSCy Schubert # define FTOTVN(tsf)						\
35*2d4e511cSCy Schubert 	((int32)((tsf) / NSECFRAC + 0.5))
36*2d4e511cSCy Schubert # define TVNTOF(tvu)						\
37*2d4e511cSCy Schubert 	((u_int32)((tvu) * NSECFRAC + 0.5))
38*2d4e511cSCy Schubert #endif
39*2d4e511cSCy Schubert 
40*2d4e511cSCy Schubert 
41*2d4e511cSCy Schubert 
42*2d4e511cSCy Schubert /* make sure nanoseconds are in nominal range */
43*2d4e511cSCy Schubert struct timespec
normalize_tspec(struct timespec x)44*2d4e511cSCy Schubert normalize_tspec(
45*2d4e511cSCy Schubert 	struct timespec x
46*2d4e511cSCy Schubert 	)
47*2d4e511cSCy Schubert {
48*2d4e511cSCy Schubert #if SIZEOF_LONG > 4
49*2d4e511cSCy Schubert 	long	z;
50*2d4e511cSCy Schubert 
51*2d4e511cSCy Schubert 	/*
52*2d4e511cSCy Schubert 	 * tv_nsec is of type 'long', and on a 64-bit machine using only
53*2d4e511cSCy Schubert 	 * loops becomes prohibitive once the upper 32 bits get
54*2d4e511cSCy Schubert 	 * involved. On the other hand, division by constant should be
55*2d4e511cSCy Schubert 	 * fast enough; so we do a division of the nanoseconds in that
56*2d4e511cSCy Schubert 	 * case. The floor adjustment step follows with the standard
57*2d4e511cSCy Schubert 	 * normalisation loops. And labs() is intentionally not used
58*2d4e511cSCy Schubert 	 * here: it has implementation-defined behaviour when applied
59*2d4e511cSCy Schubert 	 * to LONG_MIN.
60*2d4e511cSCy Schubert 	 */
61*2d4e511cSCy Schubert 	if (x.tv_nsec < -3l * NANOSECONDS ||
62*2d4e511cSCy Schubert 	    x.tv_nsec > 3l * NANOSECONDS) {
63*2d4e511cSCy Schubert 		z = x.tv_nsec / NANOSECONDS;
64*2d4e511cSCy Schubert 		x.tv_nsec -= z * NANOSECONDS;
65*2d4e511cSCy Schubert 		x.tv_sec += z;
66*2d4e511cSCy Schubert 	}
67*2d4e511cSCy Schubert #endif
68*2d4e511cSCy Schubert 	/* since 10**9 is close to 2**32, we don't divide but do a
69*2d4e511cSCy Schubert 	 * normalisation in a loop; this takes 3 steps max, and should
70*2d4e511cSCy Schubert 	 * outperform a division even if the mul-by-inverse trick is
71*2d4e511cSCy Schubert 	 * employed. */
72*2d4e511cSCy Schubert 	if (x.tv_nsec < 0)
73*2d4e511cSCy Schubert 		do {
74*2d4e511cSCy Schubert 			x.tv_nsec += NANOSECONDS;
75*2d4e511cSCy Schubert 			x.tv_sec--;
76*2d4e511cSCy Schubert 		} while (x.tv_nsec < 0);
77*2d4e511cSCy Schubert 	else if (x.tv_nsec >= NANOSECONDS)
78*2d4e511cSCy Schubert 		do {
79*2d4e511cSCy Schubert 			x.tv_nsec -= NANOSECONDS;
80*2d4e511cSCy Schubert 			x.tv_sec++;
81*2d4e511cSCy Schubert 		} while (x.tv_nsec >= NANOSECONDS);
82*2d4e511cSCy Schubert 
83*2d4e511cSCy Schubert 	return x;
84*2d4e511cSCy Schubert }
85*2d4e511cSCy Schubert 
86*2d4e511cSCy Schubert /* x = abs(a) */
87*2d4e511cSCy Schubert struct timespec
abs_tspec(struct timespec a)88*2d4e511cSCy Schubert abs_tspec(
89*2d4e511cSCy Schubert 	struct timespec	a
90*2d4e511cSCy Schubert 	)
91*2d4e511cSCy Schubert {
92*2d4e511cSCy Schubert 	struct timespec	c;
93*2d4e511cSCy Schubert 
94*2d4e511cSCy Schubert 	c = normalize_tspec(a);
95*2d4e511cSCy Schubert 	if (c.tv_sec < 0) {
96*2d4e511cSCy Schubert 		if (c.tv_nsec != 0) {
97*2d4e511cSCy Schubert 			c.tv_sec = -c.tv_sec - 1;
98*2d4e511cSCy Schubert 			c.tv_nsec = NANOSECONDS - c.tv_nsec;
99*2d4e511cSCy Schubert 		} else {
100*2d4e511cSCy Schubert 			c.tv_sec = -c.tv_sec;
101*2d4e511cSCy Schubert 		}
102*2d4e511cSCy Schubert 	}
103*2d4e511cSCy Schubert 
104*2d4e511cSCy Schubert 	return c;
105*2d4e511cSCy Schubert }
106*2d4e511cSCy Schubert 
107*2d4e511cSCy Schubert /*
108*2d4e511cSCy Schubert  * compare previously-normalised a and b
109*2d4e511cSCy Schubert  * return 1 / 0 / -1 if a < / == / > b
110*2d4e511cSCy Schubert  */
111*2d4e511cSCy Schubert int
cmp_tspec(struct timespec a,struct timespec b)112*2d4e511cSCy Schubert cmp_tspec(
113*2d4e511cSCy Schubert 	struct timespec a,
114*2d4e511cSCy Schubert 	struct timespec b
115*2d4e511cSCy Schubert 	)
116*2d4e511cSCy Schubert {
117*2d4e511cSCy Schubert 	int r;
118*2d4e511cSCy Schubert 
119*2d4e511cSCy Schubert 	r = (a.tv_sec > b.tv_sec) - (a.tv_sec < b.tv_sec);
120*2d4e511cSCy Schubert 	if (0 == r)
121*2d4e511cSCy Schubert 		r = (a.tv_nsec > b.tv_nsec) -
122*2d4e511cSCy Schubert 		    (a.tv_nsec < b.tv_nsec);
123*2d4e511cSCy Schubert 
124*2d4e511cSCy Schubert 	return r;
125*2d4e511cSCy Schubert }
126*2d4e511cSCy Schubert 
127*2d4e511cSCy Schubert /*
128*2d4e511cSCy Schubert  * test previously-normalised a
129*2d4e511cSCy Schubert  * return 1 / 0 / -1 if a < / == / > 0
130*2d4e511cSCy Schubert  */
131*2d4e511cSCy Schubert int
test_tspec(struct timespec a)132*2d4e511cSCy Schubert test_tspec(
133*2d4e511cSCy Schubert 	struct timespec	a
134*2d4e511cSCy Schubert 	)
135*2d4e511cSCy Schubert {
136*2d4e511cSCy Schubert 	int		r;
137*2d4e511cSCy Schubert 
138*2d4e511cSCy Schubert 	r = (a.tv_sec > 0) - (a.tv_sec < 0);
139*2d4e511cSCy Schubert 	if (r == 0)
140*2d4e511cSCy Schubert 		r = (a.tv_nsec > 0);
141*2d4e511cSCy Schubert 
142*2d4e511cSCy Schubert 	return r;
143*2d4e511cSCy Schubert }
144*2d4e511cSCy Schubert 
145*2d4e511cSCy Schubert /*
146*2d4e511cSCy Schubert  *  convert to l_fp type, relative and absolute
147*2d4e511cSCy Schubert  */
148*2d4e511cSCy Schubert 
149*2d4e511cSCy Schubert /* convert from timespec duration to l_fp duration */
150*2d4e511cSCy Schubert l_fp
tspec_intv_to_lfp(struct timespec x)151*2d4e511cSCy Schubert tspec_intv_to_lfp(
152*2d4e511cSCy Schubert 	struct timespec	x
153*2d4e511cSCy Schubert 	)
154*2d4e511cSCy Schubert {
155*2d4e511cSCy Schubert 	struct timespec	v;
156*2d4e511cSCy Schubert 	l_fp		y;
157*2d4e511cSCy Schubert 
158*2d4e511cSCy Schubert 	v = normalize_tspec(x);
159*2d4e511cSCy Schubert 	y.l_uf = TVNTOF(v.tv_nsec);
160*2d4e511cSCy Schubert 	y.l_i = (int32)v.tv_sec;
161*2d4e511cSCy Schubert 
162*2d4e511cSCy Schubert 	return y;
163*2d4e511cSCy Schubert }
164*2d4e511cSCy Schubert 
165*2d4e511cSCy Schubert /* convert from l_fp type, relative signed/unsigned and absolute */
166*2d4e511cSCy Schubert struct timespec
lfp_intv_to_tspec(l_fp x)167*2d4e511cSCy Schubert lfp_intv_to_tspec(
168*2d4e511cSCy Schubert 	l_fp		x
169*2d4e511cSCy Schubert 	)
170*2d4e511cSCy Schubert {
171*2d4e511cSCy Schubert 	struct timespec out;
172*2d4e511cSCy Schubert 	l_fp		absx;
173*2d4e511cSCy Schubert 	int		neg;
174*2d4e511cSCy Schubert 
175*2d4e511cSCy Schubert 	neg = L_ISNEG(&x);
176*2d4e511cSCy Schubert 	absx = x;
177*2d4e511cSCy Schubert 	if (neg) {
178*2d4e511cSCy Schubert 		L_NEG(&absx);
179*2d4e511cSCy Schubert 	}
180*2d4e511cSCy Schubert 	out.tv_nsec = FTOTVN(absx.l_uf);
181*2d4e511cSCy Schubert 	out.tv_sec = absx.l_i;
182*2d4e511cSCy Schubert 	if (neg) {
183*2d4e511cSCy Schubert 		out.tv_sec = -out.tv_sec;
184*2d4e511cSCy Schubert 		out.tv_nsec = -out.tv_nsec;
185*2d4e511cSCy Schubert 		out = normalize_tspec(out);
186*2d4e511cSCy Schubert 	}
187*2d4e511cSCy Schubert 
188*2d4e511cSCy Schubert 	return out;
189*2d4e511cSCy Schubert }
190*2d4e511cSCy Schubert 
191*2d4e511cSCy Schubert struct timespec
lfp_uintv_to_tspec(l_fp x)192*2d4e511cSCy Schubert lfp_uintv_to_tspec(
193*2d4e511cSCy Schubert 	l_fp		x
194*2d4e511cSCy Schubert 	)
195*2d4e511cSCy Schubert {
196*2d4e511cSCy Schubert 	struct timespec	out;
197*2d4e511cSCy Schubert 
198*2d4e511cSCy Schubert 	out.tv_nsec = FTOTVN(x.l_uf);
199*2d4e511cSCy Schubert 	out.tv_sec = x.l_ui;
200*2d4e511cSCy Schubert 
201*2d4e511cSCy Schubert 	return out;
202*2d4e511cSCy Schubert }
203*2d4e511cSCy Schubert 
204*2d4e511cSCy Schubert /*
205*2d4e511cSCy Schubert  * absolute (timestamp) conversion. Input is time in NTP epoch, output
206*2d4e511cSCy Schubert  * is in UN*X epoch. The NTP time stamp will be expanded around the
207*2d4e511cSCy Schubert  * pivot time *p or the current time, if p is NULL.
208*2d4e511cSCy Schubert  */
209*2d4e511cSCy Schubert struct timespec
lfp_stamp_to_tspec(l_fp x,const time_t * p)210*2d4e511cSCy Schubert lfp_stamp_to_tspec(
211*2d4e511cSCy Schubert 	l_fp		x,
212*2d4e511cSCy Schubert 	const time_t *	p
213*2d4e511cSCy Schubert 	)
214*2d4e511cSCy Schubert {
215*2d4e511cSCy Schubert 	struct timespec	out;
216*2d4e511cSCy Schubert 	vint64		sec;
217*2d4e511cSCy Schubert 
218*2d4e511cSCy Schubert 	sec = ntpcal_ntp_to_time(x.l_ui, p);
219*2d4e511cSCy Schubert 	out.tv_nsec = FTOTVN(x.l_uf);
220*2d4e511cSCy Schubert 
221*2d4e511cSCy Schubert 	/* copying a vint64 to a time_t needs some care... */
222*2d4e511cSCy Schubert #if SIZEOF_TIME_T <= 4
223*2d4e511cSCy Schubert 	out.tv_sec = (time_t)sec.d_s.lo;
224*2d4e511cSCy Schubert #elif defined(HAVE_INT64)
225*2d4e511cSCy Schubert 	out.tv_sec = (time_t)sec.q_s;
226*2d4e511cSCy Schubert #else
227*2d4e511cSCy Schubert 	out.tv_sec = ((time_t)sec.d_s.hi << 32) | sec.d_s.lo;
228*2d4e511cSCy Schubert #endif
229*2d4e511cSCy Schubert 
230*2d4e511cSCy Schubert 	return out;
231*2d4e511cSCy Schubert }
232*2d4e511cSCy Schubert 
233*2d4e511cSCy Schubert /* -*-EOF-*- */
234