PageRenderTime 28ms CodeModel.GetById 13ms app.highlight 10ms RepoModel.GetById 2ms app.codeStats 0ms

/contrib/ntp/util/jitter.h

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