4 * ntp_fp.h - definitions for NTP fixed/floating-point arithmetic
10 #include "ntp_types.h"
13 * NTP uses two fixed point formats. The first (l_fp) is the "long"
14 * format and is 64 bits long with the decimal between bits 31 and 32.
15 * This is used for time stamps in the NTP packet header (in network
16 * byte order) and for internal computations of offsets (in local host
17 * byte order). We use the same structure for both signed and unsigned
18 * values, which is a big hack but saves rewriting all the operators
19 * twice. Just to confuse this, we also sometimes just carry the
20 * fractional part in calculations, in both signed and unsigned forms.
21 * Anyway, an l_fp looks like:
24 * 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
25 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
27 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
29 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
43 #define l_ui Ul_i.Xl_ui /* unsigned integral part */
44 #define l_i Ul_i.Xl_i /* signed integral part */
45 #define l_uf Ul_f.Xl_uf /* unsigned fractional part */
46 #define l_f Ul_f.Xl_f /* signed fractional part */
49 * Fractional precision (of an l_fp) is actually the number of
52 #define FRACTION_PREC (32)
56 * The second fixed point format is 32 bits, with the decimal between
57 * bits 15 and 16. There is a signed version (s_fp) and an unsigned
58 * version (u_fp). This is used to represent synchronizing distance
59 * and synchronizing dispersion in the NTP packet header (again, in
60 * network byte order) and internally to hold both distance and
61 * dispersion values (in local byte order). In network byte order
65 * 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
66 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
67 * | Integer Part | Fraction Part |
68 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
75 * A unit second in fp format. Actually 2**(half_the_bits_in_a_long)
77 #define FP_SECOND (0x10000)
80 * Byte order conversions
82 #define HTONS_FP(x) (htonl(x))
83 #define HTONL_FP(h, n) do { (n)->l_ui = htonl((h)->l_ui); \
84 (n)->l_uf = htonl((h)->l_uf); } while (0)
85 #define NTOHS_FP(x) (ntohl(x))
86 #define NTOHL_FP(n, h) do { (h)->l_ui = ntohl((n)->l_ui); \
87 (h)->l_uf = ntohl((n)->l_uf); } while (0)
88 #define NTOHL_MFP(ni, nf, hi, hf) \
89 do { (hi) = ntohl(ni); (hf) = ntohl(nf); } while (0)
90 #define HTONL_MFP(hi, hf, ni, nf) \
91 do { (ni) = ntohl(hi); (nf) = ntohl(hf); } while (0)
93 /* funny ones. Converts ts fractions to net order ts */
94 #define HTONL_UF(uf, nts) \
95 do { (nts)->l_ui = 0; (nts)->l_uf = htonl(uf); } while (0)
96 #define HTONL_F(f, nts) do { (nts)->l_uf = htonl(f); \
97 if ((f) & 0x80000000) \
104 * Conversions between the two fixed point types
106 #define MFPTOFP(x_i, x_f) (((x_i) >= 0x00010000) ? 0x7fffffff : \
107 (((x_i) <= -0x00010000) ? 0x80000000 : \
108 (((x_i)<<16) | (((x_f)>>16)&0xffff))))
109 #define LFPTOFP(v) MFPTOFP((v)->l_i, (v)->l_f)
111 #define UFPTOLFP(x, v) ((v)->l_ui = (u_fp)(x)>>16, (v)->l_uf = (x)<<16)
112 #define FPTOLFP(x, v) (UFPTOLFP((x), (v)), (x) < 0 ? (v)->l_ui -= 0x10000 : 0)
114 #define MAXLFP(v) ((v)->l_ui = 0x7fffffff, (v)->l_uf = 0xffffffff)
115 #define MINLFP(v) ((v)->l_ui = 0x80000000, (v)->l_uf = 0)
118 * Primitive operations on long fixed point values. If these are
119 * reminiscent of assembler op codes it's only because some may
120 * be replaced by inline assembler for particular machines someday.
121 * These are the (kind of inefficient) run-anywhere versions.
123 #define M_NEG(v_i, v_f) /* v = -v */ \
126 (v_i) = -((s_fp)(v_i)); \
128 (v_f) = -((s_fp)(v_f)); \
133 #define M_NEGM(r_i, r_f, a_i, a_f) /* r = -a */ \
144 #define M_ADD(r_i, r_f, a_i, a_f) /* r += a */ \
146 register u_int32 lo_tmp; \
147 register u_int32 hi_tmp; \
149 lo_tmp = ((r_f) & 0xffff) + ((a_f) & 0xffff); \
150 hi_tmp = (((r_f) >> 16) & 0xffff) + (((a_f) >> 16) & 0xffff); \
151 if (lo_tmp & 0x10000) \
153 (r_f) = ((hi_tmp & 0xffff) << 16) | (lo_tmp & 0xffff); \
156 if (hi_tmp & 0x10000) \
160 #define M_ADD3(r_ovr, r_i, r_f, a_ovr, a_i, a_f) /* r += a, three word */ \
162 register u_int32 lo_tmp; \
163 register u_int32 hi_tmp; \
165 lo_tmp = ((r_f) & 0xffff) + ((a_f) & 0xffff); \
166 hi_tmp = (((r_f) >> 16) & 0xffff) + (((a_f) >> 16) & 0xffff); \
167 if (lo_tmp & 0x10000) \
169 (r_f) = ((hi_tmp & 0xffff) << 16) | (lo_tmp & 0xffff); \
171 lo_tmp = ((r_i) & 0xffff) + ((a_i) & 0xffff); \
172 if (hi_tmp & 0x10000) \
174 hi_tmp = (((r_i) >> 16) & 0xffff) + (((a_i) >> 16) & 0xffff); \
175 if (lo_tmp & 0x10000) \
177 (r_i) = ((hi_tmp & 0xffff) << 16) | (lo_tmp & 0xffff); \
179 (r_ovr) += (a_ovr); \
180 if (hi_tmp & 0x10000) \
184 #define M_SUB(r_i, r_f, a_i, a_f) /* r -= a */ \
186 register u_int32 lo_tmp; \
187 register u_int32 hi_tmp; \
192 lo_tmp = ((r_f) & 0xffff) + ((-((s_fp)(a_f))) & 0xffff); \
193 hi_tmp = (((r_f) >> 16) & 0xffff) \
194 + (((-((s_fp)(a_f))) >> 16) & 0xffff); \
195 if (lo_tmp & 0x10000) \
197 (r_f) = ((hi_tmp & 0xffff) << 16) | (lo_tmp & 0xffff); \
200 if (hi_tmp & 0x10000) \
205 #define M_RSHIFTU(v_i, v_f) /* v >>= 1, v is unsigned */ \
207 (v_f) = (u_int32)(v_f) >> 1; \
209 (v_f) |= 0x80000000; \
210 (v_i) = (u_int32)(v_i) >> 1; \
213 #define M_RSHIFT(v_i, v_f) /* v >>= 1, v is signed */ \
215 (v_f) = (u_int32)(v_f) >> 1; \
217 (v_f) |= 0x80000000; \
218 if ((v_i) & 0x80000000) \
219 (v_i) = ((v_i) >> 1) | 0x80000000; \
221 (v_i) = (v_i) >> 1; \
224 #define M_LSHIFT(v_i, v_f) /* v <<= 1 */ \
227 if ((v_f) & 0x80000000) \
232 #define M_LSHIFT3(v_ovr, v_i, v_f) /* v <<= 1, with overflow */ \
235 if ((v_i) & 0x80000000) \
238 if ((v_f) & 0x80000000) \
243 #define M_ADDUF(r_i, r_f, uf) /* r += uf, uf is u_int32 fraction */ \
244 M_ADD((r_i), (r_f), 0, (uf)) /* let optimizer worry about it */
246 #define M_SUBUF(r_i, r_f, uf) /* r -= uf, uf is u_int32 fraction */ \
247 M_SUB((r_i), (r_f), 0, (uf)) /* let optimizer worry about it */
249 #define M_ADDF(r_i, r_f, f) /* r += f, f is a int32 fraction */ \
252 M_ADD((r_i), (r_f), 0, (f)); \
254 M_ADD((r_i), (r_f), (-1), (f));\
257 #define M_ISNEG(v_i, v_f) /* v < 0 */ \
258 (((v_i) & 0x80000000) != 0)
260 #define M_ISHIS(a_i, a_f, b_i, b_f) /* a >= b unsigned */ \
261 (((u_int32)(a_i)) > ((u_int32)(b_i)) || \
262 ((a_i) == (b_i) && ((u_int32)(a_f)) >= ((u_int32)(b_f))))
264 #define M_ISGEQ(a_i, a_f, b_i, b_f) /* a >= b signed */ \
265 (((int32)(a_i)) > ((int32)(b_i)) || \
266 ((a_i) == (b_i) && ((u_int32)(a_f)) >= ((u_int32)(b_f))))
268 #define M_ISEQU(a_i, a_f, b_i, b_f) /* a == b unsigned */ \
269 ((a_i) == (b_i) && (a_f) == (b_f))
272 * Operations on the long fp format
274 #define L_ADD(r, a) M_ADD((r)->l_ui, (r)->l_uf, (a)->l_ui, (a)->l_uf)
275 #define L_SUB(r, a) M_SUB((r)->l_ui, (r)->l_uf, (a)->l_ui, (a)->l_uf)
276 #define L_NEG(v) M_NEG((v)->l_ui, (v)->l_uf)
277 #define L_ADDUF(r, uf) M_ADDUF((r)->l_ui, (r)->l_uf, (uf))
278 #define L_SUBUF(r, uf) M_SUBUF((r)->l_ui, (r)->l_uf, (uf))
279 #define L_ADDF(r, f) M_ADDF((r)->l_ui, (r)->l_uf, (f))
280 #define L_RSHIFT(v) M_RSHIFT((v)->l_i, (v)->l_uf)
281 #define L_RSHIFTU(v) M_RSHIFTU((v)->l_ui, (v)->l_uf)
282 #define L_LSHIFT(v) M_LSHIFT((v)->l_ui, (v)->l_uf)
283 #define L_CLR(v) ((v)->l_ui = (v)->l_uf = 0)
285 #define L_ISNEG(v) (((v)->l_ui & 0x80000000) != 0)
286 #define L_ISZERO(v) ((v)->l_ui == 0 && (v)->l_uf == 0)
287 #define L_ISHIS(a, b) ((a)->l_ui > (b)->l_ui || \
288 ((a)->l_ui == (b)->l_ui && (a)->l_uf >= (b)->l_uf))
289 #define L_ISGEQ(a, b) ((a)->l_i > (b)->l_i || \
290 ((a)->l_i == (b)->l_i && (a)->l_uf >= (b)->l_uf))
291 #define L_ISEQU(a, b) M_ISEQU((a)->l_ui, (a)->l_uf, (b)->l_ui, (b)->l_uf)
294 * s_fp/double and u_fp/double conversions
296 #define FRIC 65536. /* 2^16 as a double */
297 #define DTOFP(r) ((s_fp)((r) * FRIC))
298 #define DTOUFP(r) ((u_fp)((r) * FRIC))
299 #define FPTOD(r) ((double)(r) / FRIC)
302 * l_fp/double conversions
304 #define FRAC 4294967296. /* 2^32 as a double */
305 #define M_DTOLFP(d, r_i, r_uf) /* double to l_fp */ \
307 register double d_tmp; \
312 (r_i) = (int32)(d_tmp); \
313 (r_uf) = (u_int32)(((d_tmp) - (double)(r_i)) * FRAC); \
314 M_NEG((r_i), (r_uf)); \
316 (r_i) = (int32)(d_tmp); \
317 (r_uf) = (u_int32)(((d_tmp) - (double)(r_i)) * FRAC); \
320 #define M_LFPTOD(r_i, r_uf, d) /* l_fp to double */ \
322 register l_fp l_tmp; \
325 l_tmp.l_f = (r_uf); \
326 if (l_tmp.l_i < 0) { \
327 M_NEG(l_tmp.l_i, l_tmp.l_uf); \
328 (d) = -((double)l_tmp.l_i + ((double)l_tmp.l_uf) / FRAC); \
330 (d) = (double)l_tmp.l_i + ((double)l_tmp.l_uf) / FRAC; \
333 #define DTOLFP(d, v) M_DTOLFP((d), (v)->l_ui, (v)->l_uf)
334 #define LFPTOD(v, d) M_LFPTOD((v)->l_ui, (v)->l_uf, (d))
339 extern char * dofptoa (u_fp
, int, short, int);
340 extern char * dolfptoa (u_long
, u_long
, int, short, int);
342 extern int atolfp (const char *, l_fp
*);
343 extern int buftvtots (const char *, l_fp
*);
344 extern char * fptoa (s_fp
, short);
345 extern char * fptoms (s_fp
, short);
346 extern int hextolfp (const char *, l_fp
*);
347 extern void gpstolfp (int, int, unsigned long, l_fp
*);
348 extern int mstolfp (const char *, l_fp
*);
349 extern char * prettydate (l_fp
*);
350 extern char * gmprettydate (l_fp
*);
351 extern char * uglydate (l_fp
*);
352 extern void mfp_mul (int32
*, u_int32
*, int32
, u_int32
, int32
, u_int32
);
354 extern void get_systime (l_fp
*);
355 extern int step_systime (double);
356 extern int adj_systime (double);
358 extern struct tm
* ntp2unix_tm (u_long ntp
, int local
);
360 #define lfptoa(fpv, ndec) mfptoa((fpv)->l_ui, (fpv)->l_uf, (ndec))
361 #define lfptoms(fpv, ndec) mfptoms((fpv)->l_ui, (fpv)->l_uf, (ndec))
363 #define stoa(addr) socktoa(addr)
364 #define ntoa(addr) stoa(addr)
365 #define stohost(addr) socktohost(addr)
367 #define ufptoa(fpv, ndec) dofptoa((fpv), 0, (ndec), 0)
368 #define ufptoms(fpv, ndec) dofptoa((fpv), 0, (ndec), 1)
369 #define ulfptoa(fpv, ndec) dolfptoa((fpv)->l_ui, (fpv)->l_uf, 0, (ndec), 0)
370 #define ulfptoms(fpv, ndec) dolfptoa((fpv)->l_ui, (fpv)->l_uf, 0, (ndec), 1)
371 #define umfptoa(fpi, fpf, ndec) dolfptoa((fpi), (fpf), 0, (ndec), 0)
373 #endif /* NTP_FP_H */