xref: /freebsd/contrib/ntp/include/ntp_fp.h (revision 1670a1c2a47d10ecccd001970b859caf93cd3b6e)
1 /*
2  * ntp_fp.h - definitions for NTP fixed/floating-point arithmetic
3  */
4 
5 #ifndef NTP_FP_H
6 #define NTP_FP_H
7 
8 #include <sys/types.h>
9 #include <sys/socket.h>
10 #include <netinet/in.h>
11 
12 #include "ntp_rfc2553.h"
13 
14 #include "ntp_types.h"
15 
16 /*
17  * NTP uses two fixed point formats.  The first (l_fp) is the "long"
18  * format and is 64 bits long with the decimal between bits 31 and 32.
19  * This is used for time stamps in the NTP packet header (in network
20  * byte order) and for internal computations of offsets (in local host
21  * byte order). We use the same structure for both signed and unsigned
22  * values, which is a big hack but saves rewriting all the operators
23  * twice. Just to confuse this, we also sometimes just carry the
24  * fractional part in calculations, in both signed and unsigned forms.
25  * Anyway, an l_fp looks like:
26  *
27  *    0			  1		      2			  3
28  *    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
29  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
30  *   |			       Integral Part			     |
31  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
32  *   |			       Fractional Part			     |
33  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
34  *
35  */
36 typedef struct {
37 	union {
38 		u_int32 Xl_ui;
39 		int32 Xl_i;
40 	} Ul_i;
41 	union {
42 		u_int32 Xl_uf;
43 		int32 Xl_f;
44 	} Ul_f;
45 } l_fp;
46 
47 #define l_ui	Ul_i.Xl_ui		/* unsigned integral part */
48 #define	l_i	Ul_i.Xl_i		/* signed integral part */
49 #define	l_uf	Ul_f.Xl_uf		/* unsigned fractional part */
50 #define	l_f	Ul_f.Xl_f		/* signed fractional part */
51 
52 /*
53  * Fractional precision (of an l_fp) is actually the number of
54  * bits in a long.
55  */
56 #define	FRACTION_PREC	(32)
57 
58 
59 /*
60  * The second fixed point format is 32 bits, with the decimal between
61  * bits 15 and 16.  There is a signed version (s_fp) and an unsigned
62  * version (u_fp).  This is used to represent synchronizing distance
63  * and synchronizing dispersion in the NTP packet header (again, in
64  * network byte order) and internally to hold both distance and
65  * dispersion values (in local byte order).  In network byte order
66  * it looks like:
67  *
68  *    0			  1		      2			  3
69  *    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
70  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
71  *   |		  Integer Part	     |	   Fraction Part	     |
72  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
73  *
74  */
75 typedef int32 s_fp;
76 typedef u_int32 u_fp;
77 
78 /*
79  * A unit second in fp format.  Actually 2**(half_the_bits_in_a_long)
80  */
81 #define	FP_SECOND	(0x10000)
82 
83 /*
84  * Byte order conversions
85  */
86 #define	HTONS_FP(x)	(htonl(x))
87 #define	HTONL_FP(h, n)	do { (n)->l_ui = htonl((h)->l_ui); \
88 			     (n)->l_uf = htonl((h)->l_uf); } while (0)
89 #define	NTOHS_FP(x)	(ntohl(x))
90 #define	NTOHL_FP(n, h)	do { (h)->l_ui = ntohl((n)->l_ui); \
91 			     (h)->l_uf = ntohl((n)->l_uf); } while (0)
92 #define	NTOHL_MFP(ni, nf, hi, hf) \
93 	do { (hi) = ntohl(ni); (hf) = ntohl(nf); } while (0)
94 #define	HTONL_MFP(hi, hf, ni, nf) \
95 	do { (ni) = ntohl(hi); (nf) = ntohl(hf); } while (0)
96 
97 /* funny ones.  Converts ts fractions to net order ts */
98 #define	HTONL_UF(uf, nts) \
99 	do { (nts)->l_ui = 0; (nts)->l_uf = htonl(uf); } while (0)
100 #define	HTONL_F(f, nts) do { (nts)->l_uf = htonl(f); \
101 				if ((f) & 0x80000000) \
102 					(nts)->l_i = -1; \
103 				else \
104 					(nts)->l_i = 0; \
105 			} while (0)
106 
107 /*
108  * Conversions between the two fixed point types
109  */
110 #define	MFPTOFP(x_i, x_f)	(((x_i) >= 0x00010000) ? 0x7fffffff : \
111 				(((x_i) <= -0x00010000) ? 0x80000000 : \
112 				(((x_i)<<16) | (((x_f)>>16)&0xffff))))
113 #define	LFPTOFP(v)		MFPTOFP((v)->l_i, (v)->l_f)
114 
115 #define UFPTOLFP(x, v) ((v)->l_ui = (u_fp)(x)>>16, (v)->l_uf = (x)<<16)
116 #define FPTOLFP(x, v)  (UFPTOLFP((x), (v)), (x) < 0 ? (v)->l_ui -= 0x10000 : 0)
117 
118 #define MAXLFP(v) ((v)->l_ui = 0x7fffffff, (v)->l_uf = 0xffffffff)
119 #define MINLFP(v) ((v)->l_ui = 0x80000000, (v)->l_uf = 0)
120 
121 /*
122  * Primitive operations on long fixed point values.  If these are
123  * reminiscent of assembler op codes it's only because some may
124  * be replaced by inline assembler for particular machines someday.
125  * These are the (kind of inefficient) run-anywhere versions.
126  */
127 #define	M_NEG(v_i, v_f) 	/* v = -v */ \
128 	do { \
129 		if ((v_f) == 0) \
130 			(v_i) = -((s_fp)(v_i)); \
131 		else { \
132 			(v_f) = -((s_fp)(v_f)); \
133 			(v_i) = ~(v_i); \
134 		} \
135 	} while(0)
136 
137 #define	M_NEGM(r_i, r_f, a_i, a_f) 	/* r = -a */ \
138 	do { \
139 		if ((a_f) == 0) { \
140 			(r_f) = 0; \
141 			(r_i) = -(a_i); \
142 		} else { \
143 			(r_f) = -(a_f); \
144 			(r_i) = ~(a_i); \
145 		} \
146 	} while(0)
147 
148 #define M_ADD(r_i, r_f, a_i, a_f) 	/* r += a */ \
149 	do { \
150 		register u_int32 lo_tmp; \
151 		register u_int32 hi_tmp; \
152 		\
153 		lo_tmp = ((r_f) & 0xffff) + ((a_f) & 0xffff); \
154 		hi_tmp = (((r_f) >> 16) & 0xffff) + (((a_f) >> 16) & 0xffff); \
155 		if (lo_tmp & 0x10000) \
156 			hi_tmp++; \
157 		(r_f) = ((hi_tmp & 0xffff) << 16) | (lo_tmp & 0xffff); \
158 		\
159 		(r_i) += (a_i); \
160 		if (hi_tmp & 0x10000) \
161 			(r_i)++; \
162 	} while (0)
163 
164 #define M_ADD3(r_ovr, r_i, r_f, a_ovr, a_i, a_f) /* r += a, three word */ \
165 	do { \
166 		register u_int32 lo_tmp; \
167 		register u_int32 hi_tmp; \
168 		\
169 		lo_tmp = ((r_f) & 0xffff) + ((a_f) & 0xffff); \
170 		hi_tmp = (((r_f) >> 16) & 0xffff) + (((a_f) >> 16) & 0xffff); \
171 		if (lo_tmp & 0x10000) \
172 			hi_tmp++; \
173 		(r_f) = ((hi_tmp & 0xffff) << 16) | (lo_tmp & 0xffff); \
174 		\
175 		lo_tmp = ((r_i) & 0xffff) + ((a_i) & 0xffff); \
176 		if (hi_tmp & 0x10000) \
177 			lo_tmp++; \
178 		hi_tmp = (((r_i) >> 16) & 0xffff) + (((a_i) >> 16) & 0xffff); \
179 		if (lo_tmp & 0x10000) \
180 			hi_tmp++; \
181 		(r_i) = ((hi_tmp & 0xffff) << 16) | (lo_tmp & 0xffff); \
182 		\
183 		(r_ovr) += (a_ovr); \
184 		if (hi_tmp & 0x10000) \
185 			(r_ovr)++; \
186 	} while (0)
187 
188 #define M_SUB(r_i, r_f, a_i, a_f)	/* r -= a */ \
189 	do { \
190 		register u_int32 lo_tmp; \
191 		register u_int32 hi_tmp; \
192 		\
193 		if ((a_f) == 0) { \
194 			(r_i) -= (a_i); \
195 		} else { \
196 			lo_tmp = ((r_f) & 0xffff) + ((-((s_fp)(a_f))) & 0xffff); \
197 			hi_tmp = (((r_f) >> 16) & 0xffff) \
198 			    + (((-((s_fp)(a_f))) >> 16) & 0xffff); \
199 			if (lo_tmp & 0x10000) \
200 				hi_tmp++; \
201 			(r_f) = ((hi_tmp & 0xffff) << 16) | (lo_tmp & 0xffff); \
202 			\
203 			(r_i) += ~(a_i); \
204 			if (hi_tmp & 0x10000) \
205 				(r_i)++; \
206 		} \
207 	} while (0)
208 
209 #define	M_RSHIFTU(v_i, v_f)		/* v >>= 1, v is unsigned */ \
210 	do { \
211 		(v_f) = (u_int32)(v_f) >> 1; \
212 		if ((v_i) & 01) \
213 			(v_f) |= 0x80000000; \
214 		(v_i) = (u_int32)(v_i) >> 1; \
215 	} while (0)
216 
217 #define	M_RSHIFT(v_i, v_f)		/* v >>= 1, v is signed */ \
218 	do { \
219 		(v_f) = (u_int32)(v_f) >> 1; \
220 		if ((v_i) & 01) \
221 			(v_f) |= 0x80000000; \
222 		if ((v_i) & 0x80000000) \
223 			(v_i) = ((v_i) >> 1) | 0x80000000; \
224 		else \
225 			(v_i) = (v_i) >> 1; \
226 	} while (0)
227 
228 #define	M_LSHIFT(v_i, v_f)		/* v <<= 1 */ \
229 	do { \
230 		(v_i) <<= 1; \
231 		if ((v_f) & 0x80000000) \
232 			(v_i) |= 0x1; \
233 		(v_f) <<= 1; \
234 	} while (0)
235 
236 #define	M_LSHIFT3(v_ovr, v_i, v_f)	/* v <<= 1, with overflow */ \
237 	do { \
238 		(v_ovr) <<= 1; \
239 		if ((v_i) & 0x80000000) \
240 			(v_ovr) |= 0x1; \
241 		(v_i) <<= 1; \
242 		if ((v_f) & 0x80000000) \
243 			(v_i) |= 0x1; \
244 		(v_f) <<= 1; \
245 	} while (0)
246 
247 #define	M_ADDUF(r_i, r_f, uf) 		/* r += uf, uf is u_int32 fraction */ \
248 	M_ADD((r_i), (r_f), 0, (uf))	/* let optimizer worry about it */
249 
250 #define	M_SUBUF(r_i, r_f, uf)		/* r -= uf, uf is u_int32 fraction */ \
251 	M_SUB((r_i), (r_f), 0, (uf))	/* let optimizer worry about it */
252 
253 #define	M_ADDF(r_i, r_f, f)		/* r += f, f is a int32 fraction */ \
254 	do { \
255 		if ((f) > 0) \
256 			M_ADD((r_i), (r_f), 0, (f)); \
257 		else if ((f) < 0) \
258 			M_ADD((r_i), (r_f), (-1), (f));\
259 	} while(0)
260 
261 #define	M_ISNEG(v_i, v_f) 		/* v < 0 */ \
262 	(((v_i) & 0x80000000) != 0)
263 
264 #define	M_ISHIS(a_i, a_f, b_i, b_f)	/* a >= b unsigned */ \
265 	(((u_int32)(a_i)) > ((u_int32)(b_i)) || \
266 	  ((a_i) == (b_i) && ((u_int32)(a_f)) >= ((u_int32)(b_f))))
267 
268 #define	M_ISGEQ(a_i, a_f, b_i, b_f)	/* a >= b signed */ \
269 	(((int32)(a_i)) > ((int32)(b_i)) || \
270 	  ((a_i) == (b_i) && ((u_int32)(a_f)) >= ((u_int32)(b_f))))
271 
272 #define	M_ISEQU(a_i, a_f, b_i, b_f)	/* a == b unsigned */ \
273 	((a_i) == (b_i) && (a_f) == (b_f))
274 
275 /*
276  * Operations on the long fp format
277  */
278 #define	L_ADD(r, a)	M_ADD((r)->l_ui, (r)->l_uf, (a)->l_ui, (a)->l_uf)
279 #define	L_SUB(r, a)	M_SUB((r)->l_ui, (r)->l_uf, (a)->l_ui, (a)->l_uf)
280 #define	L_NEG(v)	M_NEG((v)->l_ui, (v)->l_uf)
281 #define L_ADDUF(r, uf)	M_ADDUF((r)->l_ui, (r)->l_uf, (uf))
282 #define L_SUBUF(r, uf)	M_SUBUF((r)->l_ui, (r)->l_uf, (uf))
283 #define	L_ADDF(r, f)	M_ADDF((r)->l_ui, (r)->l_uf, (f))
284 #define	L_RSHIFT(v)	M_RSHIFT((v)->l_i, (v)->l_uf)
285 #define	L_RSHIFTU(v)	M_RSHIFTU((v)->l_ui, (v)->l_uf)
286 #define	L_LSHIFT(v)	M_LSHIFT((v)->l_ui, (v)->l_uf)
287 #define	L_CLR(v)	((v)->l_ui = (v)->l_uf = 0)
288 
289 #define	L_ISNEG(v)	(((v)->l_ui & 0x80000000) != 0)
290 #define L_ISZERO(v)	((v)->l_ui == 0 && (v)->l_uf == 0)
291 #define	L_ISHIS(a, b)	((a)->l_ui > (b)->l_ui || \
292 			  ((a)->l_ui == (b)->l_ui && (a)->l_uf >= (b)->l_uf))
293 #define	L_ISGEQ(a, b)	((a)->l_i > (b)->l_i || \
294 			  ((a)->l_i == (b)->l_i && (a)->l_uf >= (b)->l_uf))
295 #define	L_ISEQU(a, b)	M_ISEQU((a)->l_ui, (a)->l_uf, (b)->l_ui, (b)->l_uf)
296 
297 /*
298  * s_fp/double and u_fp/double conversions
299  */
300 #define FRIC		65536.	 		/* 2^16 as a double */
301 #define DTOFP(r)	((s_fp)((r) * FRIC))
302 #define DTOUFP(r)	((u_fp)((r) * FRIC))
303 #define FPTOD(r)	((double)(r) / FRIC)
304 
305 /*
306  * l_fp/double conversions
307  */
308 #define FRAC		4294967296. 		/* 2^32 as a double */
309 #define M_DTOLFP(d, r_i, r_uf) 			/* double to l_fp */ \
310 	do { \
311 		register double d_tmp; \
312 		\
313 		d_tmp = (d); \
314 		if (d_tmp < 0) { \
315 			d_tmp = -d_tmp; \
316 			(r_i) = (int32)(d_tmp); \
317 			(r_uf) = (u_int32)(((d_tmp) - (double)(r_i)) * FRAC); \
318 			M_NEG((r_i), (r_uf)); \
319 		} else { \
320 			(r_i) = (int32)(d_tmp); \
321 			(r_uf) = (u_int32)(((d_tmp) - (double)(r_i)) * FRAC); \
322 		} \
323 	} while (0)
324 #define M_LFPTOD(r_i, r_uf, d) 			/* l_fp to double */ \
325 	do { \
326 		register l_fp l_tmp; \
327 		\
328 		l_tmp.l_i = (r_i); \
329 		l_tmp.l_f = (r_uf); \
330 		if (l_tmp.l_i < 0) { \
331 			M_NEG(l_tmp.l_i, l_tmp.l_uf); \
332 			(d) = -((double)l_tmp.l_i + ((double)l_tmp.l_uf) / FRAC); \
333 		} else { \
334 			(d) = (double)l_tmp.l_i + ((double)l_tmp.l_uf) / FRAC; \
335 		} \
336 	} while (0)
337 #define DTOLFP(d, v) 	M_DTOLFP((d), (v)->l_ui, (v)->l_uf)
338 #define LFPTOD(v, d) 	M_LFPTOD((v)->l_ui, (v)->l_uf, (d))
339 
340 /*
341  * Prototypes
342  */
343 extern	char *	dofptoa		P((u_fp, int, short, int));
344 extern	char *	dolfptoa	P((u_long, u_long, int, short, int));
345 
346 extern	int	atolfp		P((const char *, l_fp *));
347 extern	int	buftvtots	P((const char *, l_fp *));
348 extern	char *	fptoa		P((s_fp, short));
349 extern	char *	fptoms		P((s_fp, short));
350 extern	int	hextolfp	P((const char *, l_fp *));
351 extern  void    gpstolfp        P((int, int, unsigned long, l_fp *));
352 extern	int	mstolfp		P((const char *, l_fp *));
353 extern	char *	prettydate	P((l_fp *));
354 extern	char *	gmprettydate	P((l_fp *));
355 extern	char *	uglydate	P((l_fp *));
356 extern  void    mfp_mul         P((int32 *, u_int32 *, int32, u_int32, int32, u_int32));
357 
358 extern	void	get_systime	P((l_fp *));
359 extern	int	step_systime	P((double));
360 extern	int	adj_systime	P((double));
361 
362 extern	struct tm * ntp2unix_tm P((u_long ntp, int local));
363 
364 #define	lfptoa(_fpv, _ndec)	mfptoa((_fpv)->l_ui, (_fpv)->l_uf, (_ndec))
365 #define	lfptoms(_fpv, _ndec)	mfptoms((_fpv)->l_ui, (_fpv)->l_uf, (_ndec))
366 
367 #define stoa(_sin)	socktoa((_sin))
368 #define stohost(_sin)	socktohost((_sin))
369 
370 #define	ntoa(_sin)	stoa(_sin)
371 #define	ntohost(_sin)	stohost(_sin)
372 
373 #define	ufptoa(_fpv, _ndec)	dofptoa((_fpv), 0, (_ndec), 0)
374 #define	ufptoms(_fpv, _ndec)	dofptoa((_fpv), 0, (_ndec), 1)
375 #define	ulfptoa(_fpv, _ndec)	dolfptoa((_fpv)->l_ui, (_fpv)->l_uf, 0, (_ndec), 0)
376 #define	ulfptoms(_fpv, _ndec)	dolfptoa((_fpv)->l_ui, (_fpv)->l_uf, 0, (_ndec), 1)
377 #define	umfptoa(_fpi, _fpf, _ndec) dolfptoa((_fpi), (_fpf), 0, (_ndec), 0)
378 
379 #endif /* NTP_FP_H */
380