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; 352b15cb3dSCy 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 782b15cb3dSCy Schubert #define NTOHL_MFP(ni, nf, hi, hf) \ 792b15cb3dSCy Schubert do { \ 802b15cb3dSCy Schubert (hi) = ntohl(ni); \ 812b15cb3dSCy Schubert (hf) = ntohl(nf); \ 822b15cb3dSCy Schubert } while (FALSE) 832b15cb3dSCy Schubert 842b15cb3dSCy Schubert #define HTONL_MFP(hi, hf, ni, nf) \ 852b15cb3dSCy Schubert do { \ 862b15cb3dSCy Schubert (ni) = htonl(hi); \ 872b15cb3dSCy Schubert (nf) = htonl(hf); \ 882b15cb3dSCy Schubert } while (FALSE) 892b15cb3dSCy Schubert 902b15cb3dSCy Schubert #define HTONL_FP(h, n) \ 912b15cb3dSCy Schubert HTONL_MFP((h)->l_ui, (h)->l_uf, (n)->l_ui, (n)->l_uf) 922b15cb3dSCy Schubert 932b15cb3dSCy Schubert #define NTOHL_FP(n, h) \ 942b15cb3dSCy Schubert NTOHL_MFP((n)->l_ui, (n)->l_uf, (h)->l_ui, (h)->l_uf) 952b15cb3dSCy Schubert 962b15cb3dSCy Schubert /* Convert unsigned ts fraction to net order ts */ 97c0b746e5SOllivier Robert #define HTONL_UF(uf, nts) \ 982b15cb3dSCy Schubert do { \ 992b15cb3dSCy Schubert (nts)->l_ui = 0; \ 1002b15cb3dSCy Schubert (nts)->l_uf = htonl(uf); \ 1012b15cb3dSCy 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)))) 1092b15cb3dSCy 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 1142b15cb3dSCy Schubert #define MAXLFP(v) ((v)->l_ui = 0x7fffffffu, (v)->l_uf = 0xffffffffu) 1152b15cb3dSCy 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 { \ 1252b15cb3dSCy Schubert (v_f) = ~(v_f) + 1u; \ 1262b15cb3dSCy Schubert (v_i) = ~(v_i) + ((v_f) == 0); \ 1272b15cb3dSCy Schubert } while (FALSE) 128c0b746e5SOllivier Robert 129c0b746e5SOllivier Robert #define M_NEGM(r_i, r_f, a_i, a_f) /* r = -a */ \ 130c0b746e5SOllivier Robert do { \ 1312b15cb3dSCy Schubert (r_f) = ~(a_f) + 1u; \ 1322b15cb3dSCy Schubert (r_i) = ~(a_i) + ((r_f) == 0); \ 1332b15cb3dSCy Schubert } while (FALSE) 134c0b746e5SOllivier Robert 135c0b746e5SOllivier Robert #define M_ADD(r_i, r_f, a_i, a_f) /* r += a */ \ 136c0b746e5SOllivier Robert do { \ 1372b15cb3dSCy Schubert u_int32 add_t = (r_f); \ 1382b15cb3dSCy Schubert (r_f) += (a_f); \ 1392b15cb3dSCy Schubert (r_i) += (a_i) + ((u_int32)(r_f) < add_t); \ 1402b15cb3dSCy Schubert } while (FALSE) 141c0b746e5SOllivier Robert 1422b15cb3dSCy Schubert #define M_ADD3(r_o, r_i, r_f, a_o, a_i, a_f) /* r += a, three word */ \ 143c0b746e5SOllivier Robert do { \ 1442b15cb3dSCy Schubert u_int32 add_t, add_c; \ 1452b15cb3dSCy Schubert add_t = (r_f); \ 1462b15cb3dSCy Schubert (r_f) += (a_f); \ 1472b15cb3dSCy Schubert add_c = ((u_int32)(r_f) < add_t); \ 1482b15cb3dSCy Schubert (r_i) += add_c; \ 1492b15cb3dSCy Schubert add_c = ((u_int32)(r_i) < add_c); \ 1502b15cb3dSCy Schubert add_t = (r_i); \ 1512b15cb3dSCy Schubert (r_i) += (a_i); \ 1522b15cb3dSCy Schubert add_c |= ((u_int32)(r_i) < add_t); \ 1532b15cb3dSCy Schubert (r_o) += (a_o) + add_c; \ 1542b15cb3dSCy Schubert } while (FALSE) 155c0b746e5SOllivier Robert 156c0b746e5SOllivier Robert #define M_SUB(r_i, r_f, a_i, a_f) /* r -= a */ \ 157c0b746e5SOllivier Robert do { \ 1582b15cb3dSCy Schubert u_int32 sub_t = (r_f); \ 1592b15cb3dSCy Schubert (r_f) -= (a_f); \ 1602b15cb3dSCy Schubert (r_i) -= (a_i) + ((u_int32)(r_f) > sub_t); \ 1612b15cb3dSCy Schubert } while (FALSE) 162c0b746e5SOllivier Robert 163c0b746e5SOllivier Robert #define M_RSHIFTU(v_i, v_f) /* v >>= 1, v is unsigned */ \ 164c0b746e5SOllivier Robert do { \ 1652b15cb3dSCy Schubert (v_f) = ((u_int32)(v_f) >> 1) | ((u_int32)(v_i) << 31); \ 1662b15cb3dSCy Schubert (v_i) = ((u_int32)(v_i) >> 1); \ 1672b15cb3dSCy Schubert } while (FALSE) 168c0b746e5SOllivier Robert 169c0b746e5SOllivier Robert #define M_RSHIFT(v_i, v_f) /* v >>= 1, v is signed */ \ 170c0b746e5SOllivier Robert do { \ 1712b15cb3dSCy Schubert (v_f) = ((u_int32)(v_f) >> 1) | ((u_int32)(v_i) << 31); \ 1722b15cb3dSCy Schubert (v_i) = ((u_int32)(v_i) >> 1) | ((u_int32)(v_i) & 0x80000000); \ 1732b15cb3dSCy Schubert } while (FALSE) 174c0b746e5SOllivier Robert 175c0b746e5SOllivier Robert #define M_LSHIFT(v_i, v_f) /* v <<= 1 */ \ 176c0b746e5SOllivier Robert do { \ 1772b15cb3dSCy Schubert (v_i) = ((u_int32)(v_i) << 1) | ((u_int32)(v_f) >> 31); \ 1782b15cb3dSCy Schubert (v_f) = ((u_int32)(v_f) << 1); \ 1792b15cb3dSCy Schubert } while (FALSE) 180c0b746e5SOllivier Robert 1812b15cb3dSCy Schubert #define M_LSHIFT3(v_o, v_i, v_f) /* v <<= 1, with overflow */ \ 182c0b746e5SOllivier Robert do { \ 1832b15cb3dSCy Schubert (v_o) = ((u_int32)(v_o) << 1) | ((u_int32)(v_i) >> 31); \ 1842b15cb3dSCy Schubert (v_i) = ((u_int32)(v_i) << 1) | ((u_int32)(v_f) >> 31); \ 1852b15cb3dSCy Schubert (v_f) = ((u_int32)(v_f) << 1); \ 1862b15cb3dSCy 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 { \ 1962b15cb3dSCy Schubert int32 add_f = (int32)(f); \ 1972b15cb3dSCy Schubert if (add_f >= 0) \ 198*a466cc55SCy Schubert M_ADD((r_i), (r_f), 0, (u_int32)( add_f)); \ 1992b15cb3dSCy Schubert else \ 200*a466cc55SCy Schubert M_SUB((r_i), (r_f), 0, (u_int32)(-add_f)); \ 201c0b746e5SOllivier Robert } while(0) 202c0b746e5SOllivier Robert 2032b15cb3dSCy Schubert #define M_ISNEG(v_i) /* v < 0 */ \ 204c0b746e5SOllivier Robert (((v_i) & 0x80000000) != 0) 205c0b746e5SOllivier Robert 2062b15cb3dSCy Schubert #define M_ISGT(a_i, a_f, b_i, b_f) /* a > b signed */ \ 2072b15cb3dSCy Schubert (((u_int32)((a_i) ^ 0x80000000) > (u_int32)((b_i) ^ 0x80000000)) || \ 2082b15cb3dSCy Schubert ((a_i) == (b_i) && ((u_int32)(a_f)) > ((u_int32)(b_f)))) 2092b15cb3dSCy Schubert 2102b15cb3dSCy Schubert #define M_ISGTU(a_i, a_f, b_i, b_f) /* a > b unsigned */ \ 2112b15cb3dSCy Schubert (((u_int32)(a_i)) > ((u_int32)(b_i)) || \ 2122b15cb3dSCy Schubert ((a_i) == (b_i) && ((u_int32)(a_f)) > ((u_int32)(b_f)))) 2132b15cb3dSCy 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 */ \ 2192b15cb3dSCy Schubert (((u_int32)((a_i) ^ 0x80000000) > (u_int32)((b_i) ^ 0x80000000)) || \ 2202b15cb3dSCy 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 */ \ 2232b15cb3dSCy 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 2392b15cb3dSCy Schubert #define L_ISNEG(v) M_ISNEG((v)->l_ui) 2402b15cb3dSCy Schubert #define L_ISZERO(v) (((v)->l_ui | (v)->l_uf) == 0) 2412b15cb3dSCy Schubert #define L_ISGT(a, b) M_ISGT((a)->l_i, (a)->l_uf, (b)->l_i, (b)->l_uf) 2422b15cb3dSCy Schubert #define L_ISGTU(a, b) M_ISGTU((a)->l_ui, (a)->l_uf, (b)->l_ui, (b)->l_uf) 2432b15cb3dSCy Schubert #define L_ISHIS(a, b) M_ISHIS((a)->l_ui, (a)->l_uf, (b)->l_ui, (b)->l_uf) 2442b15cb3dSCy 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 */ 2502b15cb3dSCy 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 */ 2582b15cb3dSCy Schubert #define FRAC 4294967296.0 /* 2^32 as a double */ 2592b15cb3dSCy Schubert 2602b15cb3dSCy Schubert /* 2612b15cb3dSCy Schubert * Use 64 bit integers if available. Solaris on SPARC has a problem 2622b15cb3dSCy Schubert * compiling parsesolaris.c if ntp_fp.h includes math.h, due to 2632b15cb3dSCy Schubert * archaic gets() and printf() prototypes used in Solaris kernel 2642b15cb3dSCy Schubert * headers. So far the problem has only been seen with gcc, but it 2652b15cb3dSCy Schubert * may also affect Sun compilers, in which case the defined(__GNUC__) 2662b15cb3dSCy Schubert * term should be removed. 267a25439b6SCy Schubert * XSCALE also generates bad code for these, at least with GCC 3.3.5. 268a25439b6SCy Schubert * This is unrelated to math.h, but the same solution applies. 2692b15cb3dSCy Schubert */ 2702b15cb3dSCy Schubert #if defined(HAVE_U_INT64) && \ 2712b15cb3dSCy Schubert !(defined(__SVR4) && defined(__sun) && \ 272a25439b6SCy Schubert defined(sparc) && defined(__GNUC__) || \ 273a25439b6SCy Schubert defined(__arm__) && defined(__XSCALE__) && defined(__GNUC__)) 2742b15cb3dSCy Schubert 2752b15cb3dSCy Schubert #include <math.h> /* ldexp() */ 2762b15cb3dSCy Schubert 2772b15cb3dSCy Schubert #define M_DTOLFP(d, r_ui, r_uf) /* double to l_fp */ \ 278c0b746e5SOllivier Robert do { \ 2792b15cb3dSCy Schubert double d_tmp; \ 2802b15cb3dSCy Schubert u_int64 q_tmp; \ 2812b15cb3dSCy Schubert int M_isneg; \ 282c0b746e5SOllivier Robert \ 283c0b746e5SOllivier Robert d_tmp = (d); \ 2842b15cb3dSCy Schubert M_isneg = (d_tmp < 0.); \ 2852b15cb3dSCy Schubert if (M_isneg) { \ 286c0b746e5SOllivier Robert d_tmp = -d_tmp; \ 287c0b746e5SOllivier Robert } \ 2882b15cb3dSCy Schubert q_tmp = (u_int64)ldexp(d_tmp, 32); \ 2892b15cb3dSCy Schubert if (M_isneg) { \ 2902b15cb3dSCy Schubert q_tmp = ~q_tmp + 1; \ 2912b15cb3dSCy Schubert } \ 2922b15cb3dSCy Schubert (r_uf) = (u_int32)q_tmp; \ 2932b15cb3dSCy Schubert (r_ui) = (u_int32)(q_tmp >> 32); \ 2942b15cb3dSCy Schubert } while (FALSE) 2952b15cb3dSCy Schubert 2962b15cb3dSCy Schubert #define M_LFPTOD(r_ui, r_uf, d) /* l_fp to double */ \ 297c0b746e5SOllivier Robert do { \ 2982b15cb3dSCy Schubert double d_tmp; \ 2992b15cb3dSCy Schubert u_int64 q_tmp; \ 3002b15cb3dSCy Schubert int M_isneg; \ 301c0b746e5SOllivier Robert \ 3022b15cb3dSCy Schubert q_tmp = ((u_int64)(r_ui) << 32) + (r_uf); \ 3032b15cb3dSCy Schubert M_isneg = M_ISNEG(r_ui); \ 3042b15cb3dSCy Schubert if (M_isneg) { \ 3052b15cb3dSCy Schubert q_tmp = ~q_tmp + 1; \ 3062b15cb3dSCy Schubert } \ 3072b15cb3dSCy Schubert d_tmp = ldexp((double)q_tmp, -32); \ 3082b15cb3dSCy Schubert if (M_isneg) { \ 3092b15cb3dSCy Schubert d_tmp = -d_tmp; \ 3102b15cb3dSCy Schubert } \ 3112b15cb3dSCy Schubert (d) = d_tmp; \ 3122b15cb3dSCy Schubert } while (FALSE) 3132b15cb3dSCy Schubert 3142b15cb3dSCy Schubert #else /* use only 32 bit unsigned values */ 3152b15cb3dSCy Schubert 3162b15cb3dSCy Schubert #define M_DTOLFP(d, r_ui, r_uf) /* double to l_fp */ \ 3172b15cb3dSCy Schubert do { \ 3182b15cb3dSCy Schubert double d_tmp; \ 3192b15cb3dSCy Schubert if ((d_tmp = (d)) < 0) { \ 3202b15cb3dSCy Schubert (r_ui) = (u_int32)(-d_tmp); \ 3212b15cb3dSCy Schubert (r_uf) = (u_int32)(-(d_tmp + (double)(r_ui)) * FRAC); \ 3222b15cb3dSCy Schubert M_NEG((r_ui), (r_uf)); \ 323c0b746e5SOllivier Robert } else { \ 3242b15cb3dSCy Schubert (r_ui) = (u_int32)d_tmp; \ 3252b15cb3dSCy Schubert (r_uf) = (u_int32)((d_tmp - (double)(r_ui)) * FRAC); \ 326c0b746e5SOllivier Robert } \ 327c0b746e5SOllivier Robert } while (0) 3282b15cb3dSCy Schubert #define M_LFPTOD(r_ui, r_uf, d) /* l_fp to double */ \ 3292b15cb3dSCy Schubert do { \ 3302b15cb3dSCy Schubert u_int32 l_thi, l_tlo; \ 3312b15cb3dSCy Schubert l_thi = (r_ui); l_tlo = (r_uf); \ 3322b15cb3dSCy Schubert if (M_ISNEG(l_thi)) { \ 3332b15cb3dSCy Schubert M_NEG(l_thi, l_tlo); \ 3342b15cb3dSCy Schubert (d) = -((double)l_thi + (double)l_tlo / FRAC); \ 3352b15cb3dSCy Schubert } else { \ 3362b15cb3dSCy Schubert (d) = (double)l_thi + (double)l_tlo / FRAC; \ 3372b15cb3dSCy Schubert } \ 3382b15cb3dSCy Schubert } while (0) 3392b15cb3dSCy Schubert #endif 3402b15cb3dSCy Schubert 341c0b746e5SOllivier Robert #define DTOLFP(d, v) M_DTOLFP((d), (v)->l_ui, (v)->l_uf) 342c0b746e5SOllivier Robert #define LFPTOD(v, d) M_LFPTOD((v)->l_ui, (v)->l_uf, (d)) 343c0b746e5SOllivier Robert 344c0b746e5SOllivier Robert /* 345c0b746e5SOllivier Robert * Prototypes 346c0b746e5SOllivier Robert */ 3472d4e511cSCy Schubert extern char * dofptoa (u_fp, char, short, int); 3482d4e511cSCy Schubert extern char * dolfptoa (u_int32, u_int32, char, short, int); 349c0b746e5SOllivier Robert 3502b15cb3dSCy Schubert extern int atolfp (const char *, l_fp *); 3512b15cb3dSCy Schubert extern int buftvtots (const char *, l_fp *); 3522b15cb3dSCy Schubert extern char * fptoa (s_fp, short); 3532b15cb3dSCy Schubert extern char * fptoms (s_fp, short); 3542b15cb3dSCy Schubert extern int hextolfp (const char *, l_fp *); 355f0574f5cSXin LI extern void gpstolfp (u_int, u_int, unsigned long, l_fp *); 3562b15cb3dSCy Schubert extern int mstolfp (const char *, l_fp *); 3572b15cb3dSCy Schubert extern char * prettydate (l_fp *); 3582b15cb3dSCy Schubert extern char * gmprettydate (l_fp *); 3592b15cb3dSCy Schubert extern char * uglydate (l_fp *); 3602b15cb3dSCy Schubert extern void mfp_mul (int32 *, u_int32 *, int32, u_int32, int32, u_int32); 361c0b746e5SOllivier Robert 3622b15cb3dSCy Schubert extern void set_sys_fuzz (double); 3632b15cb3dSCy Schubert extern void init_systime (void); 3642b15cb3dSCy Schubert extern void get_systime (l_fp *); 3652b15cb3dSCy Schubert extern int step_systime (double); 3662b15cb3dSCy Schubert extern int adj_systime (double); 36709100258SXin LI extern int clamp_systime (void); 368c0b746e5SOllivier Robert 3692b15cb3dSCy Schubert extern struct tm * ntp2unix_tm (u_int32 ntp, int local); 370ea906c41SOllivier Robert 3712b15cb3dSCy Schubert #define lfptoa(fpv, ndec) mfptoa((fpv)->l_ui, (fpv)->l_uf, (ndec)) 3722b15cb3dSCy Schubert #define lfptoms(fpv, ndec) mfptoms((fpv)->l_ui, (fpv)->l_uf, (ndec)) 373c0b746e5SOllivier Robert 3742b15cb3dSCy Schubert #define stoa(addr) socktoa(addr) 3752b15cb3dSCy Schubert #define ntoa(addr) stoa(addr) 3762b15cb3dSCy Schubert #define sptoa(addr) sockporttoa(addr) 3772b15cb3dSCy Schubert #define stohost(addr) socktohost(addr) 3789c2daa00SOllivier Robert 3792b15cb3dSCy Schubert #define ufptoa(fpv, ndec) dofptoa((fpv), 0, (ndec), 0) 3802b15cb3dSCy Schubert #define ufptoms(fpv, ndec) dofptoa((fpv), 0, (ndec), 1) 3812b15cb3dSCy Schubert #define ulfptoa(fpv, ndec) dolfptoa((fpv)->l_ui, (fpv)->l_uf, 0, (ndec), 0) 3822b15cb3dSCy Schubert #define ulfptoms(fpv, ndec) dolfptoa((fpv)->l_ui, (fpv)->l_uf, 0, (ndec), 1) 3832b15cb3dSCy Schubert #define umfptoa(fpi, fpf, ndec) dolfptoa((fpi), (fpf), 0, (ndec), 0) 384c0b746e5SOllivier Robert 3852b15cb3dSCy Schubert /* 3862b15cb3dSCy Schubert * Optional callback from libntp step_systime() to ntpd. Optional 3872b15cb3dSCy Schubert * because other libntp clients like ntpdate don't use it. 3882b15cb3dSCy Schubert */ 3892b15cb3dSCy Schubert typedef void (*time_stepped_callback)(void); 3902b15cb3dSCy Schubert extern time_stepped_callback step_callback; 3912b15cb3dSCy Schubert 3922b15cb3dSCy Schubert /* 3932b15cb3dSCy Schubert * Multi-thread locking for get_systime() 3942b15cb3dSCy Schubert * 3952b15cb3dSCy Schubert * On most systems, get_systime() is used solely by the main ntpd 3962b15cb3dSCy Schubert * thread, but on Windows it's also used by the dedicated I/O thread. 3972b15cb3dSCy Schubert * The [Bug 2037] changes to get_systime() have it keep state between 3982b15cb3dSCy Schubert * calls to ensure time moves in only one direction, which means its 3992b15cb3dSCy Schubert * use on Windows needs to be protected against simultaneous execution 4002b15cb3dSCy Schubert * to avoid falsely detecting Lamport violations by ensuring only one 4012b15cb3dSCy Schubert * thread at a time is in get_systime(). 4022b15cb3dSCy Schubert */ 4032b15cb3dSCy Schubert #ifdef SYS_WINNT 4042b15cb3dSCy Schubert extern CRITICAL_SECTION get_systime_cs; 4052b15cb3dSCy Schubert # define INIT_GET_SYSTIME_CRITSEC() \ 4062b15cb3dSCy Schubert InitializeCriticalSection(&get_systime_cs) 4072b15cb3dSCy Schubert # define ENTER_GET_SYSTIME_CRITSEC() \ 4082b15cb3dSCy Schubert EnterCriticalSection(&get_systime_cs) 4092b15cb3dSCy Schubert # define LEAVE_GET_SYSTIME_CRITSEC() \ 4102b15cb3dSCy Schubert LeaveCriticalSection(&get_systime_cs) 4112b15cb3dSCy Schubert # define INIT_WIN_PRECISE_TIME() \ 4122b15cb3dSCy Schubert init_win_precise_time() 4132b15cb3dSCy Schubert #else /* !SYS_WINNT follows */ 4142b15cb3dSCy Schubert # define INIT_GET_SYSTIME_CRITSEC() \ 4152b15cb3dSCy Schubert do {} while (FALSE) 4162b15cb3dSCy Schubert # define ENTER_GET_SYSTIME_CRITSEC() \ 4172b15cb3dSCy Schubert do {} while (FALSE) 4182b15cb3dSCy Schubert # define LEAVE_GET_SYSTIME_CRITSEC() \ 4192b15cb3dSCy Schubert do {} while (FALSE) 4202b15cb3dSCy Schubert # define INIT_WIN_PRECISE_TIME() \ 4212b15cb3dSCy Schubert do {} while (FALSE) 4222b15cb3dSCy Schubert #endif 423c0b746e5SOllivier Robert 424c0b746e5SOllivier Robert #endif /* NTP_FP_H */ 425