4 * ntp_types.h - defines how int32 and u_int32 are treated.
5 * For 64 bit systems like the DEC Alpha, they have to be defined
7 * For 32 bit systems, define them as long and u_long
12 * Set up for prototyping
15 #if defined(__STDC__) || defined(HAVE_PROTOTYPES)
17 #else /* not __STDC__ and not HAVE_PROTOTYPES */
19 #endif /* not __STDC__ and HAVE_PROTOTYPES */
23 * VMS DECC (v4.1), {u_char,u_short,u_long} are only in SOCKET.H,
24 * and u_int isn't defined anywhere
28 typedef unsigned int u_int
;
30 * Note: VMS DECC has long == int (even on __alpha),
31 * so the distinction below doesn't matter
40 # define u_int32 unsigned int
42 #else /* not sizeof(int) == 4 */
43 # if (SIZEOF_LONG == 4)
44 # else /* not sizeof(long) == 4 */
49 # define u_int32 unsigned long
51 # endif /* not sizeof(long) == 4 */
52 # include "Bletch: what's 32 bits on this machine?"
53 #endif /* not sizeof(int) == 4 */
55 typedef unsigned short associd_t
; /* association ID */
56 typedef u_int32 keyid_t
; /* cryptographic key ID */
57 typedef u_int32 tstamp_t
; /* NTP seconds timestamp */
60 * NTP uses two fixed point formats. The first (l_fp) is the "long"
61 * format and is 64 bits long with the decimal between bits 31 and 32.
62 * This is used for time stamps in the NTP packet header (in network
63 * byte order) and for internal computations of offsets (in local host
64 * byte order). We use the same structure for both signed and unsigned
65 * values, which is a big hack but saves rewriting all the operators
66 * twice. Just to confuse this, we also sometimes just carry the
67 * fractional part in calculations, in both signed and unsigned forms.
68 * Anyway, an l_fp looks like:
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 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
74 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
76 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
90 #define l_ui Ul_i.Xl_ui /* unsigned integral part */
91 #define l_i Ul_i.Xl_i /* signed integral part */
92 #define l_uf Ul_f.Xl_uf /* unsigned fractional part */
93 #define l_f Ul_f.Xl_f /* signed fractional part */
96 * Fractional precision (of an l_fp) is actually the number of
99 #define FRACTION_PREC (32)
103 * The second fixed point format is 32 bits, with the decimal between
104 * bits 15 and 16. There is a signed version (s_fp) and an unsigned
105 * version (u_fp). This is used to represent synchronizing distance
106 * and synchronizing dispersion in the NTP packet header (again, in
107 * network byte order) and internally to hold both distance and
108 * dispersion values (in local byte order). In network byte order
112 * 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
113 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
114 * | Integer Part | Fraction Part |
115 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
119 typedef u_int32 u_fp
;
122 * A unit second in fp format. Actually 2**(half_the_bits_in_a_long)
124 #define FP_SECOND (0x10000)
127 * Byte order conversions
129 #define HTONS_FP(x) (htonl(x))
130 #define HTONL_FP(h, n) do { (n)->l_ui = htonl((h)->l_ui); \
131 (n)->l_uf = htonl((h)->l_uf); } while (0)
132 #define NTOHS_FP(x) (ntohl(x))
133 #define NTOHL_FP(n, h) do { (h)->l_ui = ntohl((n)->l_ui); \
134 (h)->l_uf = ntohl((n)->l_uf); } while (0)
135 #define NTOHL_MFP(ni, nf, hi, hf) \
136 do { (hi) = ntohl(ni); (hf) = ntohl(nf); } while (0)
137 #define HTONL_MFP(hi, hf, ni, nf) \
138 do { (ni) = ntohl(hi); (nf) = ntohl(hf); } while (0)
140 /* funny ones. Converts ts fractions to net order ts */
141 #define HTONL_UF(uf, nts) \
142 do { (nts)->l_ui = 0; (nts)->l_uf = htonl(uf); } while (0)
143 #define HTONL_F(f, nts) do { (nts)->l_uf = htonl(f); \
144 if ((f) & 0x80000000) \
151 * Conversions between the two fixed point types
153 #define MFPTOFP(x_i, x_f) (((x_i) >= 0x00010000) ? 0x7fffffff : \
154 (((x_i) <= -0x00010000) ? 0x80000000 : \
155 (((x_i)<<16) | (((x_f)>>16)&0xffff))))
156 #define LFPTOFP(v) MFPTOFP((v)->l_i, (v)->l_f)
158 #define UFPTOLFP(x, v) ((v)->l_ui = (u_fp)(x)>>16, (v)->l_uf = (x)<<16)
159 #define FPTOLFP(x, v) (UFPTOLFP((x), (v)), (x) < 0 ? (v)->l_ui -= 0x10000 : 0)
161 #define MAXLFP(v) ((v)->l_ui = 0x7fffffff, (v)->l_uf = 0xffffffff)
162 #define MINLFP(v) ((v)->l_ui = 0x80000000, (v)->l_uf = 0)
165 * Primitive operations on long fixed point values. If these are
166 * reminiscent of assembler op codes it's only because some may
167 * be replaced by inline assembler for particular machines someday.
168 * These are the (kind of inefficient) run-anywhere versions.
170 #define M_NEG(v_i, v_f) /* v = -v */ \
173 (v_i) = -((s_fp)(v_i)); \
175 (v_f) = -((s_fp)(v_f)); \
180 #define M_NEGM(r_i, r_f, a_i, a_f) /* r = -a */ \
191 #define M_ADD(r_i, r_f, a_i, a_f) /* r += a */ \
193 register u_int32 lo_tmp; \
194 register u_int32 hi_tmp; \
196 lo_tmp = ((r_f) & 0xffff) + ((a_f) & 0xffff); \
197 hi_tmp = (((r_f) >> 16) & 0xffff) + (((a_f) >> 16) & 0xffff); \
198 if (lo_tmp & 0x10000) \
200 (r_f) = ((hi_tmp & 0xffff) << 16) | (lo_tmp & 0xffff); \
203 if (hi_tmp & 0x10000) \
207 #define M_ADD3(r_ovr, r_i, r_f, a_ovr, a_i, a_f) /* r += a, three word */ \
209 register u_int32 lo_tmp; \
210 register u_int32 hi_tmp; \
212 lo_tmp = ((r_f) & 0xffff) + ((a_f) & 0xffff); \
213 hi_tmp = (((r_f) >> 16) & 0xffff) + (((a_f) >> 16) & 0xffff); \
214 if (lo_tmp & 0x10000) \
216 (r_f) = ((hi_tmp & 0xffff) << 16) | (lo_tmp & 0xffff); \
218 lo_tmp = ((r_i) & 0xffff) + ((a_i) & 0xffff); \
219 if (hi_tmp & 0x10000) \
221 hi_tmp = (((r_i) >> 16) & 0xffff) + (((a_i) >> 16) & 0xffff); \
222 if (lo_tmp & 0x10000) \
224 (r_i) = ((hi_tmp & 0xffff) << 16) | (lo_tmp & 0xffff); \
226 (r_ovr) += (a_ovr); \
227 if (hi_tmp & 0x10000) \
231 #define M_SUB(r_i, r_f, a_i, a_f) /* r -= a */ \
233 register u_int32 lo_tmp; \
234 register u_int32 hi_tmp; \
239 lo_tmp = ((r_f) & 0xffff) + ((-((s_fp)(a_f))) & 0xffff); \
240 hi_tmp = (((r_f) >> 16) & 0xffff) \
241 + (((-((s_fp)(a_f))) >> 16) & 0xffff); \
242 if (lo_tmp & 0x10000) \
244 (r_f) = ((hi_tmp & 0xffff) << 16) | (lo_tmp & 0xffff); \
247 if (hi_tmp & 0x10000) \
252 #define M_RSHIFTU(v_i, v_f) /* v >>= 1, v is unsigned */ \
254 (v_f) = (u_int32)(v_f) >> 1; \
256 (v_f) |= 0x80000000; \
257 (v_i) = (u_int32)(v_i) >> 1; \
260 #define M_RSHIFT(v_i, v_f) /* v >>= 1, v is signed */ \
262 (v_f) = (u_int32)(v_f) >> 1; \
264 (v_f) |= 0x80000000; \
265 if ((v_i) & 0x80000000) \
266 (v_i) = ((v_i) >> 1) | 0x80000000; \
268 (v_i) = (v_i) >> 1; \
271 #define M_LSHIFT(v_i, v_f) /* v <<= 1 */ \
274 if ((v_f) & 0x80000000) \
279 #define M_LSHIFT3(v_ovr, v_i, v_f) /* v <<= 1, with overflow */ \
282 if ((v_i) & 0x80000000) \
285 if ((v_f) & 0x80000000) \
290 #define M_ADDUF(r_i, r_f, uf) /* r += uf, uf is u_int32 fraction */ \
291 M_ADD((r_i), (r_f), 0, (uf)) /* let optimizer worry about it */
293 #define M_SUBUF(r_i, r_f, uf) /* r -= uf, uf is u_int32 fraction */ \
294 M_SUB((r_i), (r_f), 0, (uf)) /* let optimizer worry about it */
296 #define M_ADDF(r_i, r_f, f) /* r += f, f is a int32 fraction */ \
299 M_ADD((r_i), (r_f), 0, (f)); \
301 M_ADD((r_i), (r_f), (-1), (f));\
304 #define M_ISNEG(v_i, v_f) /* v < 0 */ \
305 (((v_i) & 0x80000000) != 0)
307 #define M_ISHIS(a_i, a_f, b_i, b_f) /* a >= b unsigned */ \
308 (((u_int32)(a_i)) > ((u_int32)(b_i)) || \
309 ((a_i) == (b_i) && ((u_int32)(a_f)) >= ((u_int32)(b_f))))
311 #define M_ISGEQ(a_i, a_f, b_i, b_f) /* a >= b signed */ \
312 (((int32)(a_i)) > ((int32)(b_i)) || \
313 ((a_i) == (b_i) && ((u_int32)(a_f)) >= ((u_int32)(b_f))))
315 #define M_ISEQU(a_i, a_f, b_i, b_f) /* a == b unsigned */ \
316 ((a_i) == (b_i) && (a_f) == (b_f))
319 * Operations on the long fp format
321 #define L_ADD(r, a) M_ADD((r)->l_ui, (r)->l_uf, (a)->l_ui, (a)->l_uf)
322 #define L_SUB(r, a) M_SUB((r)->l_ui, (r)->l_uf, (a)->l_ui, (a)->l_uf)
323 #define L_NEG(v) M_NEG((v)->l_ui, (v)->l_uf)
324 #define L_ADDUF(r, uf) M_ADDUF((r)->l_ui, (r)->l_uf, (uf))
325 #define L_SUBUF(r, uf) M_SUBUF((r)->l_ui, (r)->l_uf, (uf))
326 #define L_ADDF(r, f) M_ADDF((r)->l_ui, (r)->l_uf, (f))
327 #define L_RSHIFT(v) M_RSHIFT((v)->l_i, (v)->l_uf)
328 #define L_RSHIFTU(v) M_RSHIFT((v)->l_ui, (v)->l_uf)
329 #define L_LSHIFT(v) M_LSHIFT((v)->l_ui, (v)->l_uf)
330 #define L_CLR(v) ((v)->l_ui = (v)->l_uf = 0)
332 #define L_ISNEG(v) (((v)->l_ui & 0x80000000) != 0)
333 #define L_ISZERO(v) ((v)->l_ui == 0 && (v)->l_uf == 0)
334 #define L_ISHIS(a, b) ((a)->l_ui > (b)->l_ui || \
335 ((a)->l_ui == (b)->l_ui && (a)->l_uf >= (b)->l_uf))
336 #define L_ISGEQ(a, b) ((a)->l_i > (b)->l_i || \
337 ((a)->l_i == (b)->l_i && (a)->l_uf >= (b)->l_uf))
338 #define L_ISEQU(a, b) M_ISEQU((a)->l_ui, (a)->l_uf, (b)->l_ui, (b)->l_uf)
341 * s_fp/double and u_fp/double conversions
343 #define FRIC 65536. /* 2^16 as a double */
344 #define DTOFP(r) ((s_fp)((r) * FRIC))
345 #define DTOUFP(r) ((u_fp)((r) * FRIC))
346 #define FPTOD(r) ((double)(r) / FRIC)
349 * l_fp/double conversions
351 #define FRAC 4294967296. /* 2^32 as a double */
352 #define M_DTOLFP(d, r_i, r_uf) /* double to l_fp */ \
354 register double d_tmp; \
359 (r_i) = (int32)(d_tmp); \
360 (r_uf) = (u_int32)(((d_tmp) - (double)(r_i)) * FRAC); \
361 M_NEG((r_i), (r_uf)); \
363 (r_i) = (int32)(d_tmp); \
364 (r_uf) = (u_int32)(((d_tmp) - (double)(r_i)) * FRAC); \
367 #define M_LFPTOD(r_i, r_uf, d) /* l_fp to double */ \
369 register l_fp l_tmp; \
372 l_tmp.l_f = (r_uf); \
373 if (l_tmp.l_i < 0) { \
374 M_NEG(l_tmp.l_i, l_tmp.l_uf); \
375 (d) = -((double)l_tmp.l_i + ((double)l_tmp.l_uf) / FRAC); \
377 (d) = (double)l_tmp.l_i + ((double)l_tmp.l_uf) / FRAC; \
380 #define DTOLFP(d, v) M_DTOLFP((d), (v)->l_ui, (v)->l_uf)
381 #define LFPTOD(v, d) M_LFPTOD((v)->l_ui, (v)->l_uf, (d))
387 extern char * dofptoa
P((u_fp
, int, short, int));
388 extern char * dolfptoa
P((u_long
, u_long
, int, short, int));
391 extern int atolfp
P((const char *, l_fp
*));
392 extern int buftvtots
P((const char *, l_fp
*));
393 extern char * fptoa
P((s_fp
, short));
394 extern char * fptoms
P((s_fp
, short));
395 extern int hextolfp
P((const char *, l_fp
*));
396 extern void gpstolfp
P((int, int, unsigned long, l_fp
*));
397 extern int mstolfp
P((const char *, l_fp
*));
398 extern char * prettydate
P((l_fp
*));
399 extern char * gmprettydate
P((l_fp
*));
400 extern char * uglydate
P((l_fp
*));
401 extern void mfp_mul
P((int32
*, u_int32
*, int32
, u_int32
, int32
, u_int32
));
403 extern void get_systime
P((l_fp
*));
404 extern int step_systime
P((double));
405 extern int adj_systime
P((double));
407 #define lfptoa(_fpv, _ndec) mfptoa((_fpv)->l_ui, (_fpv)->l_uf, (_ndec))
408 #define lfptoms(_fpv, _ndec) mfptoms((_fpv)->l_ui, (_fpv)->l_uf, (_ndec))
410 #define ufptoa(_fpv, _ndec) dofptoa((_fpv), 0, (_ndec), 0)
411 #define ufptoms(_fpv, _ndec) dofptoa((_fpv), 0, (_ndec), 1)
412 #define ulfptoa(_fpv, _ndec) dolfptoa((_fpv)->l_ui, (_fpv)->l_uf, 0, (_ndec), 0)
413 #define ulfptoms(_fpv, _ndec) dolfptoa((_fpv)->l_ui, (_fpv)->l_uf, 0, (_ndec), 1)
414 #define umfptoa(_fpi, _fpf, _ndec) dolfptoa((_fpi), (_fpf), 0, (_ndec), 0)