1 /* $NetBSD: ntp_fp.h,v 1.2 2003/12/04 16:23:36 drochner Exp $ */
4 * ntp_fp.h - definitions for NTP fixed/floating-point arithmetic
10 #include <sys/types.h>
11 #include <sys/socket.h>
12 #include <netinet/in.h>
14 #include "ntp_rfc2553.h"
16 #include "ntp_types.h"
19 * NTP uses two fixed point formats. The first (l_fp) is the "long"
20 * format and is 64 bits long with the decimal between bits 31 and 32.
21 * This is used for time stamps in the NTP packet header (in network
22 * byte order) and for internal computations of offsets (in local host
23 * byte order). We use the same structure for both signed and unsigned
24 * values, which is a big hack but saves rewriting all the operators
25 * twice. Just to confuse this, we also sometimes just carry the
26 * fractional part in calculations, in both signed and unsigned forms.
27 * Anyway, an l_fp looks like:
30 * 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
31 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
33 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
35 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
49 #define l_ui Ul_i.Xl_ui /* unsigned integral part */
50 #define l_i Ul_i.Xl_i /* signed integral part */
51 #define l_uf Ul_f.Xl_uf /* unsigned fractional part */
52 #define l_f Ul_f.Xl_f /* signed fractional part */
55 * Fractional precision (of an l_fp) is actually the number of
58 #define FRACTION_PREC (32)
62 * The second fixed point format is 32 bits, with the decimal between
63 * bits 15 and 16. There is a signed version (s_fp) and an unsigned
64 * version (u_fp). This is used to represent synchronizing distance
65 * and synchronizing dispersion in the NTP packet header (again, in
66 * network byte order) and internally to hold both distance and
67 * dispersion values (in local byte order). In network byte order
71 * 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
72 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
73 * | Integer Part | Fraction Part |
74 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
81 * A unit second in fp format. Actually 2**(half_the_bits_in_a_long)
83 #define FP_SECOND (0x10000)
86 * Byte order conversions
88 #define HTONS_FP(x) (htonl(x))
89 #define HTONL_FP(h, n) do { (n)->l_ui = htonl((h)->l_ui); \
90 (n)->l_uf = htonl((h)->l_uf); } while (0)
91 #define NTOHS_FP(x) (ntohl(x))
92 #define NTOHL_FP(n, h) do { (h)->l_ui = ntohl((n)->l_ui); \
93 (h)->l_uf = ntohl((n)->l_uf); } while (0)
94 #define NTOHL_MFP(ni, nf, hi, hf) \
95 do { (hi) = ntohl(ni); (hf) = ntohl(nf); } while (0)
96 #define HTONL_MFP(hi, hf, ni, nf) \
97 do { (ni) = ntohl(hi); (nf) = ntohl(hf); } while (0)
99 /* funny ones. Converts ts fractions to net order ts */
100 #define HTONL_UF(uf, nts) \
101 do { (nts)->l_ui = 0; (nts)->l_uf = htonl(uf); } while (0)
102 #define HTONL_F(f, nts) do { (nts)->l_uf = htonl(f); \
103 if ((f) & 0x80000000) \
110 * Conversions between the two fixed point types
112 #define MFPTOFP(x_i, x_f) (((x_i) >= 0x00010000) ? 0x7fffffff : \
113 (((x_i) <= -0x00010000) ? 0x80000000 : \
114 (((x_i)<<16) | (((x_f)>>16)&0xffff))))
115 #define LFPTOFP(v) MFPTOFP((v)->l_i, (v)->l_f)
117 #define UFPTOLFP(x, v) ((v)->l_ui = (u_fp)(x)>>16, (v)->l_uf = (x)<<16)
118 #define FPTOLFP(x, v) (UFPTOLFP((x), (v)), (x) < 0 ? (v)->l_ui -= 0x10000 : 0)
120 #define MAXLFP(v) ((v)->l_ui = 0x7fffffff, (v)->l_uf = 0xffffffff)
121 #define MINLFP(v) ((v)->l_ui = 0x80000000, (v)->l_uf = 0)
124 * Primitive operations on long fixed point values. If these are
125 * reminiscent of assembler op codes it's only because some may
126 * be replaced by inline assembler for particular machines someday.
127 * These are the (kind of inefficient) run-anywhere versions.
129 #define M_NEG(v_i, v_f) /* v = -v */ \
132 (v_i) = -((s_fp)(v_i)); \
134 (v_f) = -((s_fp)(v_f)); \
139 #define M_NEGM(r_i, r_f, a_i, a_f) /* r = -a */ \
150 #define M_ADD(r_i, r_f, a_i, a_f) /* r += a */ \
152 register u_int32 lo_tmp; \
153 register u_int32 hi_tmp; \
155 lo_tmp = ((r_f) & 0xffff) + ((a_f) & 0xffff); \
156 hi_tmp = (((r_f) >> 16) & 0xffff) + (((a_f) >> 16) & 0xffff); \
157 if (lo_tmp & 0x10000) \
159 (r_f) = ((hi_tmp & 0xffff) << 16) | (lo_tmp & 0xffff); \
162 if (hi_tmp & 0x10000) \
166 #define M_ADD3(r_ovr, r_i, r_f, a_ovr, a_i, a_f) /* r += a, three word */ \
168 register u_int32 lo_tmp; \
169 register u_int32 hi_tmp; \
171 lo_tmp = ((r_f) & 0xffff) + ((a_f) & 0xffff); \
172 hi_tmp = (((r_f) >> 16) & 0xffff) + (((a_f) >> 16) & 0xffff); \
173 if (lo_tmp & 0x10000) \
175 (r_f) = ((hi_tmp & 0xffff) << 16) | (lo_tmp & 0xffff); \
177 lo_tmp = ((r_i) & 0xffff) + ((a_i) & 0xffff); \
178 if (hi_tmp & 0x10000) \
180 hi_tmp = (((r_i) >> 16) & 0xffff) + (((a_i) >> 16) & 0xffff); \
181 if (lo_tmp & 0x10000) \
183 (r_i) = ((hi_tmp & 0xffff) << 16) | (lo_tmp & 0xffff); \
185 (r_ovr) += (a_ovr); \
186 if (hi_tmp & 0x10000) \
190 #define M_SUB(r_i, r_f, a_i, a_f) /* r -= a */ \
192 register u_int32 lo_tmp; \
193 register u_int32 hi_tmp; \
198 lo_tmp = ((r_f) & 0xffff) + ((-((s_fp)(a_f))) & 0xffff); \
199 hi_tmp = (((r_f) >> 16) & 0xffff) \
200 + (((-((s_fp)(a_f))) >> 16) & 0xffff); \
201 if (lo_tmp & 0x10000) \
203 (r_f) = ((hi_tmp & 0xffff) << 16) | (lo_tmp & 0xffff); \
206 if (hi_tmp & 0x10000) \
211 #define M_RSHIFTU(v_i, v_f) /* v >>= 1, v is unsigned */ \
213 (v_f) = (u_int32)(v_f) >> 1; \
215 (v_f) |= 0x80000000; \
216 (v_i) = (u_int32)(v_i) >> 1; \
219 #define M_RSHIFT(v_i, v_f) /* v >>= 1, v is signed */ \
221 (v_f) = (u_int32)(v_f) >> 1; \
223 (v_f) |= 0x80000000; \
224 if ((v_i) & 0x80000000) \
225 (v_i) = ((v_i) >> 1) | 0x80000000; \
227 (v_i) = (v_i) >> 1; \
230 #define M_LSHIFT(v_i, v_f) /* v <<= 1 */ \
233 if ((v_f) & 0x80000000) \
238 #define M_LSHIFT3(v_ovr, v_i, v_f) /* v <<= 1, with overflow */ \
241 if ((v_i) & 0x80000000) \
244 if ((v_f) & 0x80000000) \
249 #define M_ADDUF(r_i, r_f, uf) /* r += uf, uf is u_int32 fraction */ \
250 M_ADD((r_i), (r_f), 0, (uf)) /* let optimizer worry about it */
252 #define M_SUBUF(r_i, r_f, uf) /* r -= uf, uf is u_int32 fraction */ \
253 M_SUB((r_i), (r_f), 0, (uf)) /* let optimizer worry about it */
255 #define M_ADDF(r_i, r_f, f) /* r += f, f is a int32 fraction */ \
258 M_ADD((r_i), (r_f), 0, (f)); \
260 M_ADD((r_i), (r_f), (-1), (f));\
263 #define M_ISNEG(v_i, v_f) /* v < 0 */ \
264 (((v_i) & 0x80000000) != 0)
266 #define M_ISHIS(a_i, a_f, b_i, b_f) /* a >= b unsigned */ \
267 (((u_int32)(a_i)) > ((u_int32)(b_i)) || \
268 ((a_i) == (b_i) && ((u_int32)(a_f)) >= ((u_int32)(b_f))))
270 #define M_ISGEQ(a_i, a_f, b_i, b_f) /* a >= b signed */ \
271 (((int32)(a_i)) > ((int32)(b_i)) || \
272 ((a_i) == (b_i) && ((u_int32)(a_f)) >= ((u_int32)(b_f))))
274 #define M_ISEQU(a_i, a_f, b_i, b_f) /* a == b unsigned */ \
275 ((a_i) == (b_i) && (a_f) == (b_f))
278 * Operations on the long fp format
280 #define L_ADD(r, a) M_ADD((r)->l_ui, (r)->l_uf, (a)->l_ui, (a)->l_uf)
281 #define L_SUB(r, a) M_SUB((r)->l_ui, (r)->l_uf, (a)->l_ui, (a)->l_uf)
282 #define L_NEG(v) M_NEG((v)->l_ui, (v)->l_uf)
283 #define L_ADDUF(r, uf) M_ADDUF((r)->l_ui, (r)->l_uf, (uf))
284 #define L_SUBUF(r, uf) M_SUBUF((r)->l_ui, (r)->l_uf, (uf))
285 #define L_ADDF(r, f) M_ADDF((r)->l_ui, (r)->l_uf, (f))
286 #define L_RSHIFT(v) M_RSHIFT((v)->l_i, (v)->l_uf)
287 #define L_RSHIFTU(v) M_RSHIFTU((v)->l_ui, (v)->l_uf)
288 #define L_LSHIFT(v) M_LSHIFT((v)->l_ui, (v)->l_uf)
289 #define L_CLR(v) ((v)->l_ui = (v)->l_uf = 0)
291 #define L_ISNEG(v) (((v)->l_ui & 0x80000000) != 0)
292 #define L_ISZERO(v) ((v)->l_ui == 0 && (v)->l_uf == 0)
293 #define L_ISHIS(a, b) ((a)->l_ui > (b)->l_ui || \
294 ((a)->l_ui == (b)->l_ui && (a)->l_uf >= (b)->l_uf))
295 #define L_ISGEQ(a, b) ((a)->l_i > (b)->l_i || \
296 ((a)->l_i == (b)->l_i && (a)->l_uf >= (b)->l_uf))
297 #define L_ISEQU(a, b) M_ISEQU((a)->l_ui, (a)->l_uf, (b)->l_ui, (b)->l_uf)
300 * s_fp/double and u_fp/double conversions
302 #define FRIC 65536. /* 2^16 as a double */
303 #define DTOFP(r) ((s_fp)((r) * FRIC))
304 #define DTOUFP(r) ((u_fp)((r) * FRIC))
305 #define FPTOD(r) ((double)(r) / FRIC)
308 * l_fp/double conversions
310 #define FRAC 4294967296. /* 2^32 as a double */
311 #define M_DTOLFP(d, r_i, r_uf) /* double to l_fp */ \
313 register double d_tmp; \
318 (r_i) = (int32)(d_tmp); \
319 (r_uf) = (u_int32)(((d_tmp) - (double)(r_i)) * FRAC); \
320 M_NEG((r_i), (r_uf)); \
322 (r_i) = (int32)(d_tmp); \
323 (r_uf) = (u_int32)(((d_tmp) - (double)(r_i)) * FRAC); \
326 #define M_LFPTOD(r_i, r_uf, d) /* l_fp to double */ \
328 register l_fp l_tmp; \
331 l_tmp.l_f = (r_uf); \
332 if (l_tmp.l_i < 0) { \
333 M_NEG(l_tmp.l_i, l_tmp.l_uf); \
334 (d) = -((double)l_tmp.l_i + ((double)l_tmp.l_uf) / FRAC); \
336 (d) = (double)l_tmp.l_i + ((double)l_tmp.l_uf) / FRAC; \
339 #define DTOLFP(d, v) M_DTOLFP((d), (v)->l_ui, (v)->l_uf)
340 #define LFPTOD(v, d) M_LFPTOD((v)->l_ui, (v)->l_uf, (d))
345 extern char * dofptoa
P((u_fp
, int, short, int));
346 extern char * dolfptoa
P((u_long
, u_long
, int, short, int));
348 extern int atolfp
P((const char *, l_fp
*));
349 extern int buftvtots
P((const char *, l_fp
*));
350 extern char * fptoa
P((s_fp
, short));
351 extern char * fptoms
P((s_fp
, short));
352 extern int hextolfp
P((const char *, l_fp
*));
353 extern void gpstolfp
P((int, int, unsigned long, l_fp
*));
354 extern int mstolfp
P((const char *, l_fp
*));
355 extern char * prettydate
P((l_fp
*));
356 extern char * gmprettydate
P((l_fp
*));
357 extern char * uglydate
P((l_fp
*));
358 extern void mfp_mul
P((int32
*, u_int32
*, int32
, u_int32
, int32
, u_int32
));
360 extern void get_systime
P((l_fp
*));
361 extern int step_systime
P((double));
362 extern int adj_systime
P((double));
364 extern struct tm
* ntp2unix_tm
P((u_long ntp
, int local
));
366 #define lfptoa(_fpv, _ndec) mfptoa((_fpv)->l_ui, (_fpv)->l_uf, (_ndec))
367 #define lfptoms(_fpv, _ndec) mfptoms((_fpv)->l_ui, (_fpv)->l_uf, (_ndec))
369 #define stoa(_sin) socktoa((_sin))
370 #define stohost(_sin) socktohost((_sin))
372 #define ntoa(_sin) stoa(_sin)
373 #define ntohost(_sin) stohost(_sin)
375 #define ufptoa(_fpv, _ndec) dofptoa((_fpv), 0, (_ndec), 0)
376 #define ufptoms(_fpv, _ndec) dofptoa((_fpv), 0, (_ndec), 1)
377 #define ulfptoa(_fpv, _ndec) dolfptoa((_fpv)->l_ui, (_fpv)->l_uf, 0, (_ndec), 0)
378 #define ulfptoms(_fpv, _ndec) dolfptoa((_fpv)->l_ui, (_fpv)->l_uf, 0, (_ndec), 1)
379 #define umfptoa(_fpi, _fpf, _ndec) dolfptoa((_fpi), (_fpf), 0, (_ndec), 0)
381 #endif /* NTP_FP_H */