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