xref: /freebsd/contrib/ntp/include/ntp_fp.h (revision 2b15cb3d0922bd70ea592f0da9b4a5b167f4d53f)
1c0b746e5SOllivier Robert /*
2c0b746e5SOllivier Robert  * ntp_fp.h - definitions for NTP fixed/floating-point arithmetic
3c0b746e5SOllivier Robert  */
4c0b746e5SOllivier Robert 
5c0b746e5SOllivier Robert #ifndef NTP_FP_H
6c0b746e5SOllivier Robert #define NTP_FP_H
7c0b746e5SOllivier Robert 
8c0b746e5SOllivier Robert #include "ntp_types.h"
9c0b746e5SOllivier Robert 
10c0b746e5SOllivier Robert /*
11c0b746e5SOllivier Robert  * NTP uses two fixed point formats.  The first (l_fp) is the "long"
12c0b746e5SOllivier Robert  * format and is 64 bits long with the decimal between bits 31 and 32.
13c0b746e5SOllivier Robert  * This is used for time stamps in the NTP packet header (in network
14c0b746e5SOllivier Robert  * byte order) and for internal computations of offsets (in local host
15c0b746e5SOllivier Robert  * byte order). We use the same structure for both signed and unsigned
16c0b746e5SOllivier Robert  * values, which is a big hack but saves rewriting all the operators
17c0b746e5SOllivier Robert  * twice. Just to confuse this, we also sometimes just carry the
18c0b746e5SOllivier Robert  * fractional part in calculations, in both signed and unsigned forms.
19c0b746e5SOllivier Robert  * Anyway, an l_fp looks like:
20c0b746e5SOllivier Robert  *
21c0b746e5SOllivier Robert  *    0			  1		      2			  3
22c0b746e5SOllivier Robert  *    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
23c0b746e5SOllivier Robert  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
24c0b746e5SOllivier Robert  *   |			       Integral Part			     |
25c0b746e5SOllivier Robert  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
26c0b746e5SOllivier Robert  *   |			       Fractional Part			     |
27c0b746e5SOllivier Robert  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
28c0b746e5SOllivier Robert  *
29c0b746e5SOllivier Robert  */
30c0b746e5SOllivier Robert typedef struct {
31c0b746e5SOllivier Robert 	union {
32c0b746e5SOllivier Robert 		u_int32 Xl_ui;
33c0b746e5SOllivier Robert 		int32 Xl_i;
34c0b746e5SOllivier Robert 	} Ul_i;
35*2b15cb3dSCy Schubert 	u_int32	l_uf;
36c0b746e5SOllivier Robert } l_fp;
37c0b746e5SOllivier Robert 
38c0b746e5SOllivier Robert #define l_ui	Ul_i.Xl_ui		/* unsigned integral part */
39c0b746e5SOllivier Robert #define	l_i	Ul_i.Xl_i		/* signed integral part */
40c0b746e5SOllivier Robert 
41c0b746e5SOllivier Robert /*
42c0b746e5SOllivier Robert  * Fractional precision (of an l_fp) is actually the number of
43c0b746e5SOllivier Robert  * bits in a long.
44c0b746e5SOllivier Robert  */
45c0b746e5SOllivier Robert #define	FRACTION_PREC	(32)
46c0b746e5SOllivier Robert 
47c0b746e5SOllivier Robert 
48c0b746e5SOllivier Robert /*
49c0b746e5SOllivier Robert  * The second fixed point format is 32 bits, with the decimal between
50c0b746e5SOllivier Robert  * bits 15 and 16.  There is a signed version (s_fp) and an unsigned
51c0b746e5SOllivier Robert  * version (u_fp).  This is used to represent synchronizing distance
52c0b746e5SOllivier Robert  * and synchronizing dispersion in the NTP packet header (again, in
53c0b746e5SOllivier Robert  * network byte order) and internally to hold both distance and
54c0b746e5SOllivier Robert  * dispersion values (in local byte order).  In network byte order
55c0b746e5SOllivier Robert  * it looks like:
56c0b746e5SOllivier Robert  *
57c0b746e5SOllivier Robert  *    0			  1		      2			  3
58c0b746e5SOllivier Robert  *    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
59c0b746e5SOllivier Robert  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
60c0b746e5SOllivier Robert  *   |		  Integer Part	     |	   Fraction Part	     |
61c0b746e5SOllivier Robert  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
62c0b746e5SOllivier Robert  *
63c0b746e5SOllivier Robert  */
64c0b746e5SOllivier Robert typedef int32 s_fp;
65c0b746e5SOllivier Robert typedef u_int32 u_fp;
66c0b746e5SOllivier Robert 
67c0b746e5SOllivier Robert /*
68c0b746e5SOllivier Robert  * A unit second in fp format.	Actually 2**(half_the_bits_in_a_long)
69c0b746e5SOllivier Robert  */
70c0b746e5SOllivier Robert #define	FP_SECOND	(0x10000)
71c0b746e5SOllivier Robert 
72c0b746e5SOllivier Robert /*
73c0b746e5SOllivier Robert  * Byte order conversions
74c0b746e5SOllivier Robert  */
75c0b746e5SOllivier Robert #define	HTONS_FP(x)	(htonl(x))
76c0b746e5SOllivier Robert #define	NTOHS_FP(x)	(ntohl(x))
77c0b746e5SOllivier Robert 
78*2b15cb3dSCy Schubert #define	NTOHL_MFP(ni, nf, hi, hf)				\
79*2b15cb3dSCy Schubert 	do {							\
80*2b15cb3dSCy Schubert 		(hi) = ntohl(ni);				\
81*2b15cb3dSCy Schubert 		(hf) = ntohl(nf);				\
82*2b15cb3dSCy Schubert 	} while (FALSE)
83*2b15cb3dSCy Schubert 
84*2b15cb3dSCy Schubert #define	HTONL_MFP(hi, hf, ni, nf)				\
85*2b15cb3dSCy Schubert 	do {							\
86*2b15cb3dSCy Schubert 		(ni) = htonl(hi);				\
87*2b15cb3dSCy Schubert 		(nf) = htonl(hf);				\
88*2b15cb3dSCy Schubert 	} while (FALSE)
89*2b15cb3dSCy Schubert 
90*2b15cb3dSCy Schubert #define HTONL_FP(h, n)						\
91*2b15cb3dSCy Schubert 	HTONL_MFP((h)->l_ui, (h)->l_uf, (n)->l_ui, (n)->l_uf)
92*2b15cb3dSCy Schubert 
93*2b15cb3dSCy Schubert #define NTOHL_FP(n, h)						\
94*2b15cb3dSCy Schubert 	NTOHL_MFP((n)->l_ui, (n)->l_uf, (h)->l_ui, (h)->l_uf)
95*2b15cb3dSCy Schubert 
96*2b15cb3dSCy Schubert /* Convert unsigned ts fraction to net order ts */
97c0b746e5SOllivier Robert #define	HTONL_UF(uf, nts)					\
98*2b15cb3dSCy Schubert 	do {							\
99*2b15cb3dSCy Schubert 		(nts)->l_ui = 0;				\
100*2b15cb3dSCy Schubert 		(nts)->l_uf = htonl(uf);			\
101*2b15cb3dSCy Schubert 	} while (FALSE)
102c0b746e5SOllivier Robert 
103c0b746e5SOllivier Robert /*
104c0b746e5SOllivier Robert  * Conversions between the two fixed point types
105c0b746e5SOllivier Robert  */
106c0b746e5SOllivier Robert #define	MFPTOFP(x_i, x_f)	(((x_i) >= 0x00010000) ? 0x7fffffff : \
107c0b746e5SOllivier Robert 				(((x_i) <= -0x00010000) ? 0x80000000 : \
108c0b746e5SOllivier Robert 				(((x_i)<<16) | (((x_f)>>16)&0xffff))))
109*2b15cb3dSCy Schubert #define	LFPTOFP(v)		MFPTOFP((v)->l_i, (v)->l_uf)
110c0b746e5SOllivier Robert 
111c0b746e5SOllivier Robert #define UFPTOLFP(x, v) ((v)->l_ui = (u_fp)(x)>>16, (v)->l_uf = (x)<<16)
112c0b746e5SOllivier Robert #define FPTOLFP(x, v)  (UFPTOLFP((x), (v)), (x) < 0 ? (v)->l_ui -= 0x10000 : 0)
113c0b746e5SOllivier Robert 
114*2b15cb3dSCy Schubert #define MAXLFP(v) ((v)->l_ui = 0x7fffffffu, (v)->l_uf = 0xffffffffu)
115*2b15cb3dSCy Schubert #define MINLFP(v) ((v)->l_ui = 0x80000000u, (v)->l_uf = 0u)
116c0b746e5SOllivier Robert 
117c0b746e5SOllivier Robert /*
118c0b746e5SOllivier Robert  * Primitive operations on long fixed point values.  If these are
119c0b746e5SOllivier Robert  * reminiscent of assembler op codes it's only because some may
120c0b746e5SOllivier Robert  * be replaced by inline assembler for particular machines someday.
121c0b746e5SOllivier Robert  * These are the (kind of inefficient) run-anywhere versions.
122c0b746e5SOllivier Robert  */
123c0b746e5SOllivier Robert #define	M_NEG(v_i, v_f)		/* v = -v */ \
124c0b746e5SOllivier Robert 	do { \
125*2b15cb3dSCy Schubert 		(v_f) = ~(v_f) + 1u; \
126*2b15cb3dSCy Schubert 		(v_i) = ~(v_i) + ((v_f) == 0); \
127*2b15cb3dSCy Schubert 	} while (FALSE)
128c0b746e5SOllivier Robert 
129c0b746e5SOllivier Robert #define	M_NEGM(r_i, r_f, a_i, a_f)	/* r = -a */ \
130c0b746e5SOllivier Robert 	do { \
131*2b15cb3dSCy Schubert 		(r_f) = ~(a_f) + 1u; \
132*2b15cb3dSCy Schubert 		(r_i) = ~(a_i) + ((r_f) == 0); \
133*2b15cb3dSCy Schubert 	} while (FALSE)
134c0b746e5SOllivier Robert 
135c0b746e5SOllivier Robert #define M_ADD(r_i, r_f, a_i, a_f)	/* r += a */ \
136c0b746e5SOllivier Robert 	do { \
137*2b15cb3dSCy Schubert 		u_int32 add_t = (r_f); \
138*2b15cb3dSCy Schubert 		(r_f) += (a_f); \
139*2b15cb3dSCy Schubert 		(r_i) += (a_i) + ((u_int32)(r_f) < add_t); \
140*2b15cb3dSCy Schubert 	} while (FALSE)
141c0b746e5SOllivier Robert 
142*2b15cb3dSCy Schubert #define M_ADD3(r_o, r_i, r_f, a_o, a_i, a_f) /* r += a, three word */ \
143c0b746e5SOllivier Robert 	do { \
144*2b15cb3dSCy Schubert 		u_int32 add_t, add_c; \
145*2b15cb3dSCy Schubert 		add_t  = (r_f); \
146*2b15cb3dSCy Schubert 		(r_f) += (a_f); \
147*2b15cb3dSCy Schubert 		add_c  = ((u_int32)(r_f) < add_t); \
148*2b15cb3dSCy Schubert 		(r_i) += add_c; \
149*2b15cb3dSCy Schubert 		add_c  = ((u_int32)(r_i) < add_c); \
150*2b15cb3dSCy Schubert 		add_t  = (r_i); \
151*2b15cb3dSCy Schubert 		(r_i) += (a_i); \
152*2b15cb3dSCy Schubert 		add_c |= ((u_int32)(r_i) < add_t); \
153*2b15cb3dSCy Schubert 		(r_o) += (a_o) + add_c; \
154*2b15cb3dSCy Schubert 	} while (FALSE)
155c0b746e5SOllivier Robert 
156c0b746e5SOllivier Robert #define M_SUB(r_i, r_f, a_i, a_f)	/* r -= a */ \
157c0b746e5SOllivier Robert 	do { \
158*2b15cb3dSCy Schubert 		u_int32 sub_t = (r_f); \
159*2b15cb3dSCy Schubert 		(r_f) -= (a_f); \
160*2b15cb3dSCy Schubert 		(r_i) -= (a_i) + ((u_int32)(r_f) > sub_t); \
161*2b15cb3dSCy Schubert 	} while (FALSE)
162c0b746e5SOllivier Robert 
163c0b746e5SOllivier Robert #define	M_RSHIFTU(v_i, v_f)		/* v >>= 1, v is unsigned */ \
164c0b746e5SOllivier Robert 	do { \
165*2b15cb3dSCy Schubert 		(v_f) = ((u_int32)(v_f) >> 1) | ((u_int32)(v_i) << 31);	\
166*2b15cb3dSCy Schubert 		(v_i) = ((u_int32)(v_i) >> 1); \
167*2b15cb3dSCy Schubert 	} while (FALSE)
168c0b746e5SOllivier Robert 
169c0b746e5SOllivier Robert #define	M_RSHIFT(v_i, v_f)		/* v >>= 1, v is signed */ \
170c0b746e5SOllivier Robert 	do { \
171*2b15cb3dSCy Schubert 		(v_f) = ((u_int32)(v_f) >> 1) | ((u_int32)(v_i) << 31);	\
172*2b15cb3dSCy Schubert 		(v_i) = ((u_int32)(v_i) >> 1) | ((u_int32)(v_i) & 0x80000000);	\
173*2b15cb3dSCy Schubert 	} while (FALSE)
174c0b746e5SOllivier Robert 
175c0b746e5SOllivier Robert #define	M_LSHIFT(v_i, v_f)		/* v <<= 1 */ \
176c0b746e5SOllivier Robert 	do { \
177*2b15cb3dSCy Schubert 		(v_i) = ((u_int32)(v_i) << 1) | ((u_int32)(v_f) >> 31);	\
178*2b15cb3dSCy Schubert 		(v_f) = ((u_int32)(v_f) << 1); \
179*2b15cb3dSCy Schubert 	} while (FALSE)
180c0b746e5SOllivier Robert 
181*2b15cb3dSCy Schubert #define	M_LSHIFT3(v_o, v_i, v_f)	/* v <<= 1, with overflow */ \
182c0b746e5SOllivier Robert 	do { \
183*2b15cb3dSCy Schubert 		(v_o) = ((u_int32)(v_o) << 1) | ((u_int32)(v_i) >> 31);	\
184*2b15cb3dSCy Schubert 		(v_i) = ((u_int32)(v_i) << 1) | ((u_int32)(v_f) >> 31);	\
185*2b15cb3dSCy Schubert 		(v_f) = ((u_int32)(v_f) << 1); \
186*2b15cb3dSCy Schubert 	} while (FALSE)
187c0b746e5SOllivier Robert 
188c0b746e5SOllivier Robert #define	M_ADDUF(r_i, r_f, uf)		/* r += uf, uf is u_int32 fraction */ \
189c0b746e5SOllivier Robert 	M_ADD((r_i), (r_f), 0, (uf))	/* let optimizer worry about it */
190c0b746e5SOllivier Robert 
191c0b746e5SOllivier Robert #define	M_SUBUF(r_i, r_f, uf)		/* r -= uf, uf is u_int32 fraction */ \
192c0b746e5SOllivier Robert 	M_SUB((r_i), (r_f), 0, (uf))	/* let optimizer worry about it */
193c0b746e5SOllivier Robert 
194c0b746e5SOllivier Robert #define	M_ADDF(r_i, r_f, f)		/* r += f, f is a int32 fraction */ \
195c0b746e5SOllivier Robert 	do { \
196*2b15cb3dSCy Schubert 		int32 add_f = (int32)(f); \
197*2b15cb3dSCy Schubert 		if (add_f >= 0) \
198*2b15cb3dSCy Schubert 			M_ADD((r_i), (r_f), 0, (uint32)( add_f)); \
199*2b15cb3dSCy Schubert 		else \
200*2b15cb3dSCy Schubert 			M_SUB((r_i), (r_f), 0, (uint32)(-add_f)); \
201c0b746e5SOllivier Robert 	} while(0)
202c0b746e5SOllivier Robert 
203*2b15cb3dSCy Schubert #define	M_ISNEG(v_i)			/* v < 0 */ \
204c0b746e5SOllivier Robert 	(((v_i) & 0x80000000) != 0)
205c0b746e5SOllivier Robert 
206*2b15cb3dSCy Schubert #define	M_ISGT(a_i, a_f, b_i, b_f)	/* a > b signed */ \
207*2b15cb3dSCy Schubert 	(((u_int32)((a_i) ^ 0x80000000) > (u_int32)((b_i) ^ 0x80000000)) || \
208*2b15cb3dSCy Schubert 	  ((a_i) == (b_i) && ((u_int32)(a_f)) > ((u_int32)(b_f))))
209*2b15cb3dSCy Schubert 
210*2b15cb3dSCy Schubert #define	M_ISGTU(a_i, a_f, b_i, b_f)	/* a > b unsigned */ \
211*2b15cb3dSCy Schubert 	(((u_int32)(a_i)) > ((u_int32)(b_i)) || \
212*2b15cb3dSCy Schubert 	  ((a_i) == (b_i) && ((u_int32)(a_f)) > ((u_int32)(b_f))))
213*2b15cb3dSCy Schubert 
214c0b746e5SOllivier Robert #define	M_ISHIS(a_i, a_f, b_i, b_f)	/* a >= b unsigned */ \
215c0b746e5SOllivier Robert 	(((u_int32)(a_i)) > ((u_int32)(b_i)) || \
216c0b746e5SOllivier Robert 	  ((a_i) == (b_i) && ((u_int32)(a_f)) >= ((u_int32)(b_f))))
217c0b746e5SOllivier Robert 
218c0b746e5SOllivier Robert #define	M_ISGEQ(a_i, a_f, b_i, b_f)	/* a >= b signed */ \
219*2b15cb3dSCy Schubert 	(((u_int32)((a_i) ^ 0x80000000) > (u_int32)((b_i) ^ 0x80000000)) || \
220*2b15cb3dSCy Schubert 	  ((a_i) == (b_i) && (u_int32)(a_f) >= (u_int32)(b_f)))
221c0b746e5SOllivier Robert 
222c0b746e5SOllivier Robert #define	M_ISEQU(a_i, a_f, b_i, b_f)	/* a == b unsigned */ \
223*2b15cb3dSCy Schubert 	((u_int32)(a_i) == (u_int32)(b_i) && (u_int32)(a_f) == (u_int32)(b_f))
224c0b746e5SOllivier Robert 
225c0b746e5SOllivier Robert /*
226c0b746e5SOllivier Robert  * Operations on the long fp format
227c0b746e5SOllivier Robert  */
228c0b746e5SOllivier Robert #define	L_ADD(r, a)	M_ADD((r)->l_ui, (r)->l_uf, (a)->l_ui, (a)->l_uf)
229c0b746e5SOllivier Robert #define	L_SUB(r, a)	M_SUB((r)->l_ui, (r)->l_uf, (a)->l_ui, (a)->l_uf)
230c0b746e5SOllivier Robert #define	L_NEG(v)	M_NEG((v)->l_ui, (v)->l_uf)
231c0b746e5SOllivier Robert #define L_ADDUF(r, uf)	M_ADDUF((r)->l_ui, (r)->l_uf, (uf))
232c0b746e5SOllivier Robert #define L_SUBUF(r, uf)	M_SUBUF((r)->l_ui, (r)->l_uf, (uf))
233c0b746e5SOllivier Robert #define	L_ADDF(r, f)	M_ADDF((r)->l_ui, (r)->l_uf, (f))
234c0b746e5SOllivier Robert #define	L_RSHIFT(v)	M_RSHIFT((v)->l_i, (v)->l_uf)
235ea906c41SOllivier Robert #define	L_RSHIFTU(v)	M_RSHIFTU((v)->l_ui, (v)->l_uf)
236c0b746e5SOllivier Robert #define	L_LSHIFT(v)	M_LSHIFT((v)->l_ui, (v)->l_uf)
237c0b746e5SOllivier Robert #define	L_CLR(v)	((v)->l_ui = (v)->l_uf = 0)
238c0b746e5SOllivier Robert 
239*2b15cb3dSCy Schubert #define	L_ISNEG(v)	M_ISNEG((v)->l_ui)
240*2b15cb3dSCy Schubert #define L_ISZERO(v)	(((v)->l_ui | (v)->l_uf) == 0)
241*2b15cb3dSCy Schubert #define	L_ISGT(a, b)	M_ISGT((a)->l_i, (a)->l_uf, (b)->l_i, (b)->l_uf)
242*2b15cb3dSCy Schubert #define	L_ISGTU(a, b)	M_ISGTU((a)->l_ui, (a)->l_uf, (b)->l_ui, (b)->l_uf)
243*2b15cb3dSCy Schubert #define	L_ISHIS(a, b)	M_ISHIS((a)->l_ui, (a)->l_uf, (b)->l_ui, (b)->l_uf)
244*2b15cb3dSCy Schubert #define	L_ISGEQ(a, b)	M_ISGEQ((a)->l_ui, (a)->l_uf, (b)->l_ui, (b)->l_uf)
245c0b746e5SOllivier Robert #define	L_ISEQU(a, b)	M_ISEQU((a)->l_ui, (a)->l_uf, (b)->l_ui, (b)->l_uf)
246c0b746e5SOllivier Robert 
247c0b746e5SOllivier Robert /*
248c0b746e5SOllivier Robert  * s_fp/double and u_fp/double conversions
249c0b746e5SOllivier Robert  */
250*2b15cb3dSCy Schubert #define FRIC		65536.0			/* 2^16 as a double */
251c0b746e5SOllivier Robert #define DTOFP(r)	((s_fp)((r) * FRIC))
252c0b746e5SOllivier Robert #define DTOUFP(r)	((u_fp)((r) * FRIC))
253c0b746e5SOllivier Robert #define FPTOD(r)	((double)(r) / FRIC)
254c0b746e5SOllivier Robert 
255c0b746e5SOllivier Robert /*
256c0b746e5SOllivier Robert  * l_fp/double conversions
257c0b746e5SOllivier Robert  */
258*2b15cb3dSCy Schubert #define FRAC		4294967296.0 		/* 2^32 as a double */
259*2b15cb3dSCy Schubert 
260*2b15cb3dSCy Schubert /*
261*2b15cb3dSCy Schubert  * Use 64 bit integers if available.  Solaris on SPARC has a problem
262*2b15cb3dSCy Schubert  * compiling parsesolaris.c if ntp_fp.h includes math.h, due to
263*2b15cb3dSCy Schubert  * archaic gets() and printf() prototypes used in Solaris kernel
264*2b15cb3dSCy Schubert  * headers.  So far the problem has only been seen with gcc, but it
265*2b15cb3dSCy Schubert  * may also affect Sun compilers, in which case the defined(__GNUC__)
266*2b15cb3dSCy Schubert  * term should be removed.
267*2b15cb3dSCy Schubert  */
268*2b15cb3dSCy Schubert #if defined(HAVE_U_INT64) && \
269*2b15cb3dSCy Schubert     !(defined(__SVR4) && defined(__sun) && \
270*2b15cb3dSCy Schubert       defined(sparc) && defined(__GNUC__))
271*2b15cb3dSCy Schubert 
272*2b15cb3dSCy Schubert #include <math.h>	/* ldexp() */
273*2b15cb3dSCy Schubert 
274*2b15cb3dSCy Schubert #define M_DTOLFP(d, r_ui, r_uf)		/* double to l_fp */	\
275c0b746e5SOllivier Robert 	do {							\
276*2b15cb3dSCy Schubert 		double	d_tmp;					\
277*2b15cb3dSCy Schubert 		u_int64	q_tmp;					\
278*2b15cb3dSCy Schubert 		int	M_isneg;					\
279c0b746e5SOllivier Robert 								\
280c0b746e5SOllivier Robert 		d_tmp = (d);					\
281*2b15cb3dSCy Schubert 		M_isneg = (d_tmp < 0.);				\
282*2b15cb3dSCy Schubert 		if (M_isneg) {					\
283c0b746e5SOllivier Robert 			d_tmp = -d_tmp;				\
284c0b746e5SOllivier Robert 		}						\
285*2b15cb3dSCy Schubert 		q_tmp = (u_int64)ldexp(d_tmp, 32);		\
286*2b15cb3dSCy Schubert 		if (M_isneg) {					\
287*2b15cb3dSCy Schubert 			q_tmp = ~q_tmp + 1;			\
288*2b15cb3dSCy Schubert 		}						\
289*2b15cb3dSCy Schubert 		(r_uf) = (u_int32)q_tmp;			\
290*2b15cb3dSCy Schubert 		(r_ui) = (u_int32)(q_tmp >> 32);		\
291*2b15cb3dSCy Schubert 	} while (FALSE)
292*2b15cb3dSCy Schubert 
293*2b15cb3dSCy Schubert #define M_LFPTOD(r_ui, r_uf, d) 	/* l_fp to double */	\
294c0b746e5SOllivier Robert 	do {							\
295*2b15cb3dSCy Schubert 		double	d_tmp;					\
296*2b15cb3dSCy Schubert 		u_int64	q_tmp;					\
297*2b15cb3dSCy Schubert 		int	M_isneg;				\
298c0b746e5SOllivier Robert 								\
299*2b15cb3dSCy Schubert 		q_tmp = ((u_int64)(r_ui) << 32) + (r_uf);	\
300*2b15cb3dSCy Schubert 		M_isneg = M_ISNEG(r_ui);			\
301*2b15cb3dSCy Schubert 		if (M_isneg) {					\
302*2b15cb3dSCy Schubert 			q_tmp = ~q_tmp + 1;			\
303*2b15cb3dSCy Schubert 		}						\
304*2b15cb3dSCy Schubert 		d_tmp = ldexp((double)q_tmp, -32);		\
305*2b15cb3dSCy Schubert 		if (M_isneg) {					\
306*2b15cb3dSCy Schubert 			d_tmp = -d_tmp;				\
307*2b15cb3dSCy Schubert 		}						\
308*2b15cb3dSCy Schubert 		(d) = d_tmp;					\
309*2b15cb3dSCy Schubert 	} while (FALSE)
310*2b15cb3dSCy Schubert 
311*2b15cb3dSCy Schubert #else /* use only 32 bit unsigned values */
312*2b15cb3dSCy Schubert 
313*2b15cb3dSCy Schubert #define M_DTOLFP(d, r_ui, r_uf) 		/* double to l_fp */ \
314*2b15cb3dSCy Schubert 	do { \
315*2b15cb3dSCy Schubert 		double d_tmp; \
316*2b15cb3dSCy Schubert 		if ((d_tmp = (d)) < 0) { \
317*2b15cb3dSCy Schubert 			(r_ui) = (u_int32)(-d_tmp); \
318*2b15cb3dSCy Schubert 			(r_uf) = (u_int32)(-(d_tmp + (double)(r_ui)) * FRAC); \
319*2b15cb3dSCy Schubert 			M_NEG((r_ui), (r_uf)); \
320c0b746e5SOllivier Robert 		} else { \
321*2b15cb3dSCy Schubert 			(r_ui) = (u_int32)d_tmp; \
322*2b15cb3dSCy Schubert 			(r_uf) = (u_int32)((d_tmp - (double)(r_ui)) * FRAC); \
323c0b746e5SOllivier Robert 		} \
324c0b746e5SOllivier Robert 	} while (0)
325*2b15cb3dSCy Schubert #define M_LFPTOD(r_ui, r_uf, d) 		/* l_fp to double */ \
326*2b15cb3dSCy Schubert 	do { \
327*2b15cb3dSCy Schubert 		u_int32 l_thi, l_tlo; \
328*2b15cb3dSCy Schubert 		l_thi = (r_ui); l_tlo = (r_uf); \
329*2b15cb3dSCy Schubert 		if (M_ISNEG(l_thi)) { \
330*2b15cb3dSCy Schubert 			M_NEG(l_thi, l_tlo); \
331*2b15cb3dSCy Schubert 			(d) = -((double)l_thi + (double)l_tlo / FRAC); \
332*2b15cb3dSCy Schubert 		} else { \
333*2b15cb3dSCy Schubert 			(d) = (double)l_thi + (double)l_tlo / FRAC; \
334*2b15cb3dSCy Schubert 		} \
335*2b15cb3dSCy Schubert 	} while (0)
336*2b15cb3dSCy Schubert #endif
337*2b15cb3dSCy Schubert 
338c0b746e5SOllivier Robert #define DTOLFP(d, v) 	M_DTOLFP((d), (v)->l_ui, (v)->l_uf)
339c0b746e5SOllivier Robert #define LFPTOD(v, d) 	M_LFPTOD((v)->l_ui, (v)->l_uf, (d))
340c0b746e5SOllivier Robert 
341c0b746e5SOllivier Robert /*
342c0b746e5SOllivier Robert  * Prototypes
343c0b746e5SOllivier Robert  */
344*2b15cb3dSCy Schubert extern	char *	dofptoa		(u_fp, int, short, int);
345*2b15cb3dSCy Schubert extern	char *	dolfptoa	(u_int32, u_int32, int, short, int);
346c0b746e5SOllivier Robert 
347*2b15cb3dSCy Schubert extern	int	atolfp		(const char *, l_fp *);
348*2b15cb3dSCy Schubert extern	int	buftvtots	(const char *, l_fp *);
349*2b15cb3dSCy Schubert extern	char *	fptoa		(s_fp, short);
350*2b15cb3dSCy Schubert extern	char *	fptoms		(s_fp, short);
351*2b15cb3dSCy Schubert extern	int	hextolfp	(const char *, l_fp *);
352*2b15cb3dSCy Schubert extern  void	gpstolfp	(int, int, unsigned long, l_fp *);
353*2b15cb3dSCy Schubert extern	int	mstolfp		(const char *, l_fp *);
354*2b15cb3dSCy Schubert extern	char *	prettydate	(l_fp *);
355*2b15cb3dSCy Schubert extern	char *	gmprettydate	(l_fp *);
356*2b15cb3dSCy Schubert extern	char *	uglydate	(l_fp *);
357*2b15cb3dSCy Schubert extern  void	mfp_mul		(int32 *, u_int32 *, int32, u_int32, int32, u_int32);
358c0b746e5SOllivier Robert 
359*2b15cb3dSCy Schubert extern	void	set_sys_fuzz	(double);
360*2b15cb3dSCy Schubert extern	void	init_systime	(void);
361*2b15cb3dSCy Schubert extern	void	get_systime	(l_fp *);
362*2b15cb3dSCy Schubert extern	int	step_systime	(double);
363*2b15cb3dSCy Schubert extern	int	adj_systime	(double);
364c0b746e5SOllivier Robert 
365*2b15cb3dSCy Schubert extern	struct tm * ntp2unix_tm (u_int32 ntp, int local);
366ea906c41SOllivier Robert 
367*2b15cb3dSCy Schubert #define	lfptoa(fpv, ndec)	mfptoa((fpv)->l_ui, (fpv)->l_uf, (ndec))
368*2b15cb3dSCy Schubert #define	lfptoms(fpv, ndec)	mfptoms((fpv)->l_ui, (fpv)->l_uf, (ndec))
369c0b746e5SOllivier Robert 
370*2b15cb3dSCy Schubert #define stoa(addr)		socktoa(addr)
371*2b15cb3dSCy Schubert #define	ntoa(addr)		stoa(addr)
372*2b15cb3dSCy Schubert #define sptoa(addr)		sockporttoa(addr)
373*2b15cb3dSCy Schubert #define stohost(addr)		socktohost(addr)
3749c2daa00SOllivier Robert 
375*2b15cb3dSCy Schubert #define	ufptoa(fpv, ndec)	dofptoa((fpv), 0, (ndec), 0)
376*2b15cb3dSCy Schubert #define	ufptoms(fpv, ndec)	dofptoa((fpv), 0, (ndec), 1)
377*2b15cb3dSCy Schubert #define	ulfptoa(fpv, ndec)	dolfptoa((fpv)->l_ui, (fpv)->l_uf, 0, (ndec), 0)
378*2b15cb3dSCy Schubert #define	ulfptoms(fpv, ndec)	dolfptoa((fpv)->l_ui, (fpv)->l_uf, 0, (ndec), 1)
379*2b15cb3dSCy Schubert #define	umfptoa(fpi, fpf, ndec) dolfptoa((fpi), (fpf), 0, (ndec), 0)
380c0b746e5SOllivier Robert 
381*2b15cb3dSCy Schubert /*
382*2b15cb3dSCy Schubert  * Optional callback from libntp step_systime() to ntpd.  Optional
383*2b15cb3dSCy Schubert *  because other libntp clients like ntpdate don't use it.
384*2b15cb3dSCy Schubert  */
385*2b15cb3dSCy Schubert typedef void (*time_stepped_callback)(void);
386*2b15cb3dSCy Schubert extern time_stepped_callback	step_callback;
387*2b15cb3dSCy Schubert 
388*2b15cb3dSCy Schubert /*
389*2b15cb3dSCy Schubert  * Multi-thread locking for get_systime()
390*2b15cb3dSCy Schubert  *
391*2b15cb3dSCy Schubert  * On most systems, get_systime() is used solely by the main ntpd
392*2b15cb3dSCy Schubert  * thread, but on Windows it's also used by the dedicated I/O thread.
393*2b15cb3dSCy Schubert  * The [Bug 2037] changes to get_systime() have it keep state between
394*2b15cb3dSCy Schubert  * calls to ensure time moves in only one direction, which means its
395*2b15cb3dSCy Schubert  * use on Windows needs to be protected against simultaneous execution
396*2b15cb3dSCy Schubert  * to avoid falsely detecting Lamport violations by ensuring only one
397*2b15cb3dSCy Schubert  * thread at a time is in get_systime().
398*2b15cb3dSCy Schubert  */
399*2b15cb3dSCy Schubert #ifdef SYS_WINNT
400*2b15cb3dSCy Schubert extern CRITICAL_SECTION get_systime_cs;
401*2b15cb3dSCy Schubert # define INIT_GET_SYSTIME_CRITSEC()				\
402*2b15cb3dSCy Schubert 		InitializeCriticalSection(&get_systime_cs)
403*2b15cb3dSCy Schubert # define ENTER_GET_SYSTIME_CRITSEC()				\
404*2b15cb3dSCy Schubert 		EnterCriticalSection(&get_systime_cs)
405*2b15cb3dSCy Schubert # define LEAVE_GET_SYSTIME_CRITSEC()				\
406*2b15cb3dSCy Schubert 		LeaveCriticalSection(&get_systime_cs)
407*2b15cb3dSCy Schubert # define INIT_WIN_PRECISE_TIME()				\
408*2b15cb3dSCy Schubert 		init_win_precise_time()
409*2b15cb3dSCy Schubert #else	/* !SYS_WINNT follows */
410*2b15cb3dSCy Schubert # define INIT_GET_SYSTIME_CRITSEC()			\
411*2b15cb3dSCy Schubert 		do {} while (FALSE)
412*2b15cb3dSCy Schubert # define ENTER_GET_SYSTIME_CRITSEC()			\
413*2b15cb3dSCy Schubert 		do {} while (FALSE)
414*2b15cb3dSCy Schubert # define LEAVE_GET_SYSTIME_CRITSEC()			\
415*2b15cb3dSCy Schubert 		do {} while (FALSE)
416*2b15cb3dSCy Schubert # define INIT_WIN_PRECISE_TIME()			\
417*2b15cb3dSCy Schubert 		do {} while (FALSE)
418*2b15cb3dSCy Schubert #endif
419c0b746e5SOllivier Robert 
420c0b746e5SOllivier Robert #endif /* NTP_FP_H */
421