From: Dave Hart Date: Thu, 5 Jan 2012 19:29:42 +0000 (+0000) Subject: [Bug 2104] ntpdc fault with oversize -c command. X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=a5ea3dcb7befc95a16d4e24dc06ef7ee10902325;p=thirdparty%2Fntp.git [Bug 2104] ntpdc fault with oversize -c command. [Bug 2106] Fix warnings when using -Wformat-security. Refactor timespecops.h and timevalops.h into inline functions. bk: 4f05fa26c1UYZQIpfBC5sBb8myZ9iQ --- diff --git a/ChangeLog b/ChangeLog index 7484f612b0..843f03c77e 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,6 @@ +[Bug 2104] ntpdc fault with oversize -c command. +[Bug 2106] Fix warnings when using -Wformat-security. +Refactor timespecops.h and timevalops.h into inline functions. (4.2.7p244) 2011/12/25 Released by Harlan Stenn (4.2.7p243) 2011/12/23 Released by Harlan Stenn * [Bug 2095] ntptrace now needs 'rv' instead of 'pstat', reported diff --git a/deps-ver b/deps-ver index 5ec949d12f..16d66a8bdb 100644 --- a/deps-ver +++ b/deps-ver @@ -1 +1 @@ -Mon Jan 31 21:14:28 UTC 2011 +Fri Dec 30 11:24:57 UTC 2011 diff --git a/depsver.mf b/depsver.mf index 3e4a6185a1..c0c486e13f 100644 --- a/depsver.mf +++ b/depsver.mf @@ -13,17 +13,10 @@ $(DEPDIR)/deps-ver: $(top_srcdir)/deps-ver .) \ ./config.status Makefile depfiles \ ;; \ - ..) \ - cd .. && \ - ./config.status $(subdir)/Makefile depfiles && \ - cd $(subdir) \ - ;; \ *) \ - echo 'Fatal: depsver.mf Automake fragment limited' \ - 'to immediate subdirectories.' && \ - echo "top_builddir: $(top_builddir)" && \ - echo "subdir: $(subdir)" && \ - exit 1 \ + cd "$(top_builddir)" && \ + ./config.status $(subdir)/Makefile depfiles && \ + cd $(subdir) \ ;; \ esac && \ echo -n "Cleaned $(subdir)/$(DEPDIR) version " && \ diff --git a/include/ntp_fp.h b/include/ntp_fp.h index 0ac965148c..1eeca3747b 100644 --- a/include/ntp_fp.h +++ b/include/ntp_fp.h @@ -32,16 +32,11 @@ typedef struct { u_int32 Xl_ui; int32 Xl_i; } Ul_i; - union { - u_int32 Xl_uf; - int32 Xl_f; - } Ul_f; + u_int32 l_uf; } l_fp; #define l_ui Ul_i.Xl_ui /* unsigned integral part */ #define l_i Ul_i.Xl_i /* signed integral part */ -#define l_uf Ul_f.Xl_uf /* unsigned fractional part */ -#define l_f Ul_f.Xl_f /* signed fractional part */ /* * Fractional precision (of an l_fp) is actually the number of @@ -78,23 +73,32 @@ typedef u_int32 u_fp; * Byte order conversions */ #define HTONS_FP(x) (htonl(x)) -#define HTONL_FP(h, n) do { (n)->l_ui = htonl((h)->l_ui); \ - (n)->l_uf = htonl((h)->l_uf); } while (0) #define NTOHS_FP(x) (ntohl(x)) -#define NTOHL_FP(n, h) do { (h)->l_ui = ntohl((n)->l_ui); \ - (h)->l_uf = ntohl((n)->l_uf); } while (0) -#define NTOHL_MFP(ni, nf, hi, hf) \ - do { (hi) = ntohl(ni); (hf) = ntohl(nf); } while (0) -#define HTONL_MFP(hi, hf, ni, nf) \ - do { (ni) = htonl(hi); (nf) = htonl(hf); } while (0) - -/* funny ones. Converts ts fractions to net order ts */ -#define HTONL_UF(uf, nts) \ - do { (nts)->l_ui = 0u; (nts)->l_uf = htonl(uf); } while (0) -#define HTONL_F(f, nts) \ - do { (nts)->l_uf = htonl(f); \ - (nts)->l_ui = 0u - ((nts)->l_uf >> 31); \ - } while (0) + +#define NTOHL_MFP(ni, nf, hi, hf) \ + do { \ + (hi) = ntohl(ni); \ + (hf) = ntohl(nf); \ + } while (FALSE) + +#define HTONL_MFP(hi, hf, ni, nf) \ + do { \ + (ni) = htonl(hi); \ + (nf) = htonl(hf); \ + } while (FALSE) + +#define HTONL_FP(h, n) \ + HTONL_MFP((h)->l_ui, (h)->l_uf, (n)->l_ui, (n)->l_uf) + +#define NTOHL_FP(n, h) \ + NTOHL_MFP((n)->l_ui, (n)->l_uf, (h)->l_ui, (h)->l_uf) + +/* Convert unsigned ts fraction to net order ts */ +#define HTONL_UF(uf, nts) \ + do { \ + (nts)->l_ui = 0; \ + (nts)->l_uf = htonl(uf); \ + } while (FALSE) /* * Conversions between the two fixed point types @@ -102,7 +106,7 @@ typedef u_int32 u_fp; #define MFPTOFP(x_i, x_f) (((x_i) >= 0x00010000) ? 0x7fffffff : \ (((x_i) <= -0x00010000) ? 0x80000000 : \ (((x_i)<<16) | (((x_f)>>16)&0xffff)))) -#define LFPTOFP(v) MFPTOFP((v)->l_i, (v)->l_f) +#define LFPTOFP(v) MFPTOFP((v)->l_i, (v)->l_uf) #define UFPTOLFP(x, v) ((v)->l_ui = (u_fp)(x)>>16, (v)->l_uf = (x)<<16) #define FPTOLFP(x, v) (UFPTOLFP((x), (v)), (x) < 0 ? (v)->l_ui -= 0x10000 : 0) @@ -120,20 +124,20 @@ typedef u_int32 u_fp; do { \ (v_f) = ~(v_f) + 1u; \ (v_i) = ~(v_i) + ((v_f) == 0); \ - } while(0) + } while (FALSE) #define M_NEGM(r_i, r_f, a_i, a_f) /* r = -a */ \ do { \ (r_f) = ~(a_f) + 1u; \ (r_i) = ~(a_i) + ((r_f) == 0); \ - } while(0) + } while (FALSE) #define M_ADD(r_i, r_f, a_i, a_f) /* r += a */ \ do { \ u_int32 add_t = (r_f); \ (r_f) += (a_f); \ (r_i) += (a_i) + ((u_int32)(r_f) < add_t); \ - } while (0) + } while (FALSE) #define M_ADD3(r_o, r_i, r_f, a_o, a_i, a_f) /* r += a, three word */ \ do { \ @@ -147,39 +151,39 @@ typedef u_int32 u_fp; (r_i) += (a_i); \ add_c |= ((u_int32)(r_i) < add_t); \ (r_o) += (a_o) + add_c; \ - } while (0) + } while (FALSE) #define M_SUB(r_i, r_f, a_i, a_f) /* r -= a */ \ do { \ u_int32 sub_t = (r_f); \ (r_f) -= (a_f); \ (r_i) -= (a_i) + ((u_int32)(r_f) > sub_t); \ - } while (0) + } while (FALSE) #define M_RSHIFTU(v_i, v_f) /* v >>= 1, v is unsigned */ \ do { \ (v_f) = ((u_int32)(v_f) >> 1) | ((u_int32)(v_i) << 31); \ (v_i) = ((u_int32)(v_i) >> 1); \ - } while (0) + } while (FALSE) #define M_RSHIFT(v_i, v_f) /* v >>= 1, v is signed */ \ do { \ (v_f) = ((u_int32)(v_f) >> 1) | ((u_int32)(v_i) << 31); \ - (v_i) = ((u_int32)(v_i) >> 1) | ((u_int32)(v_i) & 0x80000000u); \ - } while (0) + (v_i) = ((u_int32)(v_i) >> 1) | ((u_int32)(v_i) & 0x80000000); \ + } while (FALSE) #define M_LSHIFT(v_i, v_f) /* v <<= 1 */ \ do { \ (v_i) = ((u_int32)(v_i) << 1) | ((u_int32)(v_f) >> 31); \ (v_f) = ((u_int32)(v_f) << 1); \ - } while (0) + } while (FALSE) #define M_LSHIFT3(v_o, v_i, v_f) /* v <<= 1, with overflow */ \ do { \ (v_o) = ((u_int32)(v_o) << 1) | ((u_int32)(v_i) >> 31); \ (v_i) = ((u_int32)(v_i) << 1) | ((u_int32)(v_f) >> 31); \ (v_f) = ((u_int32)(v_f) << 1); \ - } while (0) + } while (FALSE) #define M_ADDUF(r_i, r_f, uf) /* r += uf, uf is u_int32 fraction */ \ M_ADD((r_i), (r_f), 0, (uf)) /* let optimizer worry about it */ @@ -196,8 +200,12 @@ typedef u_int32 u_fp; M_SUB((r_i), (r_f), 0, (uint32)(-add_f)); \ } while(0) -#define M_ISNEG(v_i, v_f) /* v < 0 */ \ - (((v_i) & 0x80000000u) != 0) +#define M_ISNEG(v_i) /* v < 0 */ \ + (((v_i) & 0x80000000) != 0) + +#define M_ISGT(a_i, a_f, b_i, b_f) /* a > b signed */ \ + (((int32)(a_i)) > ((int32)(b_i)) || \ + ((a_i) == (b_i) && ((u_int32)(a_f)) > ((u_int32)(b_f)))) #define M_ISGTU(a_i, a_f, b_i, b_f) /* a > b unsigned */ \ (((u_int32)(a_i)) > ((u_int32)(b_i)) || \ @@ -228,8 +236,9 @@ typedef u_int32 u_fp; #define L_LSHIFT(v) M_LSHIFT((v)->l_ui, (v)->l_uf) #define L_CLR(v) ((v)->l_ui = (v)->l_uf = 0) -#define L_ISNEG(v) M_ISNEG((v)->l_ui, (v)->l_uf) +#define L_ISNEG(v) M_ISNEG((v)->l_ui) #define L_ISZERO(v) (((v)->l_ui | (v)->l_uf) == 0) +#define L_ISGT(a, b) M_ISGT((a)->l_i, (a)->l_uf, (b)->l_i, (b)->l_uf) #define L_ISGTU(a, b) M_ISGTU((a)->l_ui, (a)->l_uf, (b)->l_ui, (b)->l_uf) #define L_ISHIS(a, b) M_ISHIS((a)->l_ui, (a)->l_uf, (b)->l_ui, (b)->l_uf) #define L_ISGEQ(a, b) M_ISGEQ((a)->l_ui, (a)->l_uf, (b)->l_ui, (b)->l_uf) @@ -238,7 +247,7 @@ typedef u_int32 u_fp; /* * s_fp/double and u_fp/double conversions */ -#define FRIC 65536.0 /* 2^16 as a double */ +#define FRIC 65536.0 /* 2^16 as a double */ #define DTOFP(r) ((s_fp)((r) * FRIC)) #define DTOUFP(r) ((u_fp)((r) * FRIC)) #define FPTOD(r) ((double)(r) / FRIC) @@ -262,27 +271,42 @@ typedef u_int32 u_fp; #include /* ldexp() */ -#define M_DTOLFP(d, r_ui, r_uf) /* double to l_fp */ \ - do { \ - double d_tmp; \ - u_int64 q_tmp; \ - if ((d_tmp = (d)) < 0.0) \ - q_tmp = ~(u_int64)ldexp(-d_tmp, 32) + 1; \ - else \ - q_tmp = (u_int64)ldexp(d_tmp, 32); \ - (r_uf) = (u_int32)(q_tmp & U_INT32_MAX); \ - (r_ui) = (u_int32)(q_tmp >> 32); \ - } while(0) - -#define M_LFPTOD(r_ui, r_uf, d) /* l_fp to double */ \ - do { \ - u_int64 q_tmp; \ - q_tmp = ((u_int64)(r_ui) << 32) + (u_int64)(r_uf); \ - if (M_ISNEG((r_ui), (r_uf))) \ - d = -ldexp((double)(~q_tmp + 1), -32); \ - else \ - d = ldexp((double)q_tmp, -32); \ - } while (0) +#define M_DTOLFP(d, r_ui, r_uf) /* double to l_fp */ \ + do { \ + double d_tmp; \ + u_int64 q_tmp; \ + int M_isneg; \ + \ + d_tmp = (d); \ + M_isneg = (d_tmp < 0.); \ + if (M_isneg) { \ + d_tmp = -d_tmp; \ + } \ + q_tmp = (u_int64)ldexp(d_tmp, 32); \ + if (M_isneg) { \ + q_tmp = ~q_tmp + 1; \ + } \ + (r_uf) = (u_int32)q_tmp; \ + (r_ui) = (u_int32)(q_tmp >> 32); \ + } while (FALSE) + +#define M_LFPTOD(r_ui, r_uf, d) /* l_fp to double */ \ + do { \ + double d_tmp; \ + u_int64 q_tmp; \ + int M_isneg; \ + \ + q_tmp = ((u_int64)(r_ui) << 32) + (r_uf); \ + M_isneg = M_ISNEG(r_ui); \ + if (M_isneg) { \ + q_tmp = ~q_tmp + 1; \ + } \ + d_tmp = ldexp((double)q_tmp, -32); \ + if (M_isneg) { \ + d_tmp = -d_tmp; \ + } \ + (d) = d_tmp; \ + } while (FALSE) #else /* use only 32 bit unsigned values */ @@ -302,7 +326,7 @@ typedef u_int32 u_fp; do { \ u_int32 l_thi, l_tlo; \ l_thi = (r_ui); l_tlo = (r_uf); \ - if (M_ISNEG(l_thi, l_tlo)) { \ + if (M_ISNEG(l_thi)) { \ M_NEG(l_thi, l_tlo); \ (d) = -((double)l_thi + (double)l_tlo / FRAC); \ } else { \ diff --git a/include/ntp_stdlib.h b/include/ntp_stdlib.h index 98b3fb7e2d..a4638c81e8 100644 --- a/include/ntp_stdlib.h +++ b/include/ntp_stdlib.h @@ -149,8 +149,8 @@ extern int hextoint (const char *, u_long *); extern char * humanlogtime (void); extern char * humantime (time_t); extern char * inttoa (long); -extern char * mfptoa (u_long, u_long, short); -extern char * mfptoms (u_long, u_long, short); +extern char * mfptoa (u_int32, u_int32, short); +extern char * mfptoms (u_int32, u_int32, short); extern const char * modetoa (int); extern const char * eventstr (int); extern const char * ceventstr (int); @@ -172,7 +172,6 @@ extern int octtoint (const char *, u_long *); extern u_long ranp2 (int); extern const char *refnumtoa (sockaddr_u *); extern const char *refid_str (u_int32, int); -extern int tsftomsu (u_long, int); extern char * uinttoa (u_long); extern int decodenetnum (const char *, sockaddr_u *); @@ -278,6 +277,7 @@ extern char * ntp_strerror (int e); extern double sys_tick; /* tick size or time to read */ extern double measured_tick; /* non-overridable sys_tick */ extern double sys_fuzz; /* min clock read latency */ +extern int trunc_os_clock; /* sys_tick > measured_tick */ /* version.c */ extern const char *Version; /* version declaration */ diff --git a/include/ntp_types.h b/include/ntp_types.h index 363dd7d306..f850248783 100644 --- a/include/ntp_types.h +++ b/include/ntp_types.h @@ -235,6 +235,15 @@ aligned_ptr( return base + ALIGN_UNITS((minsize < 1) ? 1 : minsize); } +/* + * Macro to use in otherwise-empty source files to comply with ANSI C + * requirement that each translation unit (source file) contain some + * declaration. This has commonly been done by declaring an unused + * global variable of type int or char. An extern reference to exit() + * serves the same purpose without bloat. + */ +#define NONEMPTY_TRANSLATION_UNIT extern void exit(int); + /* * On Unix struct sock_timeval is equivalent to struct timeval. * On Windows built with 64-bit time_t, sock_timeval.tv_sec is a long diff --git a/include/ntp_unixtime.h b/include/ntp_unixtime.h index 0fb738885b..eaa8b1ed45 100644 --- a/include/ntp_unixtime.h +++ b/include/ntp_unixtime.h @@ -1,6 +1,5 @@ /* - * ntp_unixtime.h - contains constants and macros for converting between - * NTP time stamps (l_fp) and Unix times (struct timeval) + * ntp_unixtime.h - much of what was here is now in timevalops.h */ #ifndef NTP_UNIXTIME_H @@ -8,8 +7,6 @@ #include "ntp_types.h" /* picks up time.h via ntp_machine.h */ #include "ntp_calendar.h" -#include "timespecops.h" -#include "timevalops.h" #ifdef SIM # define GETTIMEOFDAY(a, b) (node_gettime(&ntp_node, a)) @@ -47,98 +44,4 @@ int getclock (int clock_type, struct timespec *tp); */ #define JAN_1970 ((u_int)NTP_TO_UNIX_DAYS * (u_int)SECSPERDAY) -/* - * These constants are used to round the time stamps computed from - * a struct timeval to the microsecond (more or less). This keeps - * things neat. - */ -#define TS_MASK 0xfffff000 /* mask to usec, for time stamps */ -#define TS_ROUNDBIT 0x00000800 /* round at this bit */ - - -/* - * Convert usec to a time stamp fraction. If you use this the program - * must include the following declarations: - */ -extern const u_int32 ustotslo[]; -extern const u_int32 ustotsmid[]; -extern const u_int32 ustotshi[]; - -#define TVUTOTSF(tvu, tsf) \ - (tsf) = ustotslo[(tvu) & 0xff] \ - + ustotsmid[((tvu) >> 8) & 0xff] \ - + ustotshi[((tvu) >> 16) & 0xf] - -/* - * Convert a struct timeval to a time stamp. - */ -#define TVTOTS(tv, ts) \ - do { \ - (ts)->l_ui = (u_long)(tv)->tv_sec; \ - TVUTOTSF((tv)->tv_usec, (ts)->l_uf); \ - } while(0) - -#define sTVTOTS(tv, ts) \ - do { \ - int isneg = 0; \ - long usec; \ - (ts)->l_ui = (tv)->tv_sec; \ - usec = (tv)->tv_usec; \ - if (((tv)->tv_sec < 0) || ((tv)->tv_usec < 0)) { \ - usec = -usec; \ - (ts)->l_ui = -(ts)->l_ui; \ - isneg = 1; \ - } \ - TVUTOTSF(usec, (ts)->l_uf); \ - if (isneg) { \ - L_NEG((ts)); \ - } \ - } while(0) - -/* - * TV_SHIFT is used to turn the table result into a usec value. To round, - * add in TV_ROUNDBIT before shifting - */ -#define TV_SHIFT 3 -#define TV_ROUNDBIT 0x4 - - -/* - * Convert a time stamp fraction to microseconds. The time stamp - * fraction is assumed to be unsigned. To use this in a program, declare: - */ -extern const u_int32 tstouslo[]; -extern const u_int32 tstousmid[]; -extern const u_int32 tstoushi[]; - -#define TSFTOTVU(tsf, tvu) \ - (tvu) = (tstoushi[((tsf) >> 24) & 0xff] \ - + tstousmid[((tsf) >> 16) & 0xff] \ - + tstouslo[((tsf) >> 9) & 0x7f] \ - + TV_ROUNDBIT) >> TV_SHIFT -/* - * Convert a time stamp to a struct timeval. The time stamp - * has to be positive. - */ -#define TSTOTV(ts, tv) \ - do { \ - (tv)->tv_sec = (ts)->l_ui; \ - TSFTOTVU((ts)->l_uf, (tv)->tv_usec); \ - if ((tv)->tv_usec == 1000000) { \ - (tv)->tv_sec++; \ - (tv)->tv_usec = 0; \ - } \ - } while (0) - -/* - * Convert milliseconds to a time stamp fraction. This shouldn't be - * here, but it is convenient since the guys who use the definition will - * often be including this file anyway. - */ -extern const u_int32 msutotsflo[]; -extern const u_int32 msutotsfhi[]; - -#define MSUTOTSF(msu, tsf) \ - (tsf) = msutotsfhi[((msu) >> 5) & 0x1f] + msutotsflo[(msu) & 0x1f] - #endif /* !defined(NTP_UNIXTIME_H) */ diff --git a/include/timespecops.h b/include/timespecops.h index 6b1e2eeeac..fa32e42a60 100644 --- a/include/timespecops.h +++ b/include/timespecops.h @@ -40,105 +40,354 @@ #include #include +#include #include "ntp.h" -#include "ntp_unixtime.h" +#include "timetoa.h" -/* - * We avoid to use/define NANOSECONDS here, as it is also defined in - * some other files and we don't want to resolve the possible clashes - * here. - */ +/* nanoseconds per second */ +#define NANOSECONDS 1000000000 /* predicate: returns TRUE if the nanoseconds are in nominal range */ #define timespec_isnormal(x) \ - ((x)->tv_nsec >= 0 && (x)->tv_nsec < 1000000000) + ((x)->tv_nsec >= 0 && (x)->tv_nsec < NANOSECONDS) /* predicate: returns TRUE if the nanoseconds are out-of-bounds */ #define timespec_isdenormal(x) (!timespec_isnormal(x)) +/* conversion between l_fp fractions and nanoseconds */ +#ifdef HAVE_U_INT64 +# define FTOTVN(tsf) \ + ((int32) \ + (((u_int64)(tsf) * NANOSECONDS + 0x80000000) >> 32)) +# define TVNTOF(tvu) \ + ((u_int32) \ + ((((u_int64)(tvu) << 32) + NANOSECONDS / 2) / \ + NANOSECONDS)) +#else +# define NSECFRAC (FRAC / NANOSECONDS) +# define FTOTVN(tsf) \ + ((int32)((tsf) / NSECFRAC + 0.5)) +# define TVNTOF(tvu) \ + ((u_int32)((tvu) * NSECFRAC + 0.5)) +#endif + + -/*make sure nanoseconds are in nominal range */ -extern void timespec_norm(struct timespec *x); +/* make sure nanoseconds are in nominal range */ +static inline struct timespec +normalize_tspec( + struct timespec x + ) +{ +#if SIZEOF_LONG > 4 + long z; -/* x = a, normalised */ -extern void timespec_copy(struct timespec *x, const struct timespec *a); + /* + * tv_nsec is of type 'long', and on a 64-bit machine using only + * loops becomes prohibitive once the upper 32 bits get + * involved. On the other hand, division by constant should be + * fast enough; so we do a division of the nanoseconds in that + * case. The floor adjustment step follows with the standard + * normalisation loops. And labs() is intentionally not used + * here: it has implementation-defined behaviour when applied + * to LONG_MIN. + */ + if (x.tv_nsec < -3l * NANOSECONDS || + x.tv_nsec > 3l * NANOSECONDS) { + z = x.tv_nsec / NANOSECONDS; + x.tv_nsec -= z * NANOSECONDS; + x.tv_sec += z; + } +#endif + /* since 10**9 is close to 2**32, we don't divide but do a + * normalisation in a loop; this takes 3 steps max, and should + * outperform a division even if the mul-by-inverse trick is + * employed. */ + if (x.tv_nsec < 0) + do { + x.tv_nsec += NANOSECONDS; + x.tv_sec--; + } while (x.tv_nsec < 0); + else if (x.tv_nsec >= NANOSECONDS) + do { + x.tv_nsec -= NANOSECONDS; + x.tv_sec++; + } while (x.tv_nsec >= NANOSECONDS); + + return x; +} /* x = a + b */ -extern void timespec_add(struct timespec *x, const struct timespec *a, - const struct timespec *b); +static inline struct timespec +add_tspec( + struct timespec a, + struct timespec b + ) +{ + struct timespec x; + + x = a; + x.tv_sec += b.tv_sec; + x.tv_nsec += b.tv_nsec; + + return normalize_tspec(x); +} /* x = a + b, b is fraction only */ -extern void timespec_addns(struct timespec *x, const struct timespec *a, - long b); +static inline struct timespec +add_tspec_ns( + struct timespec a, + long b + ) +{ + struct timespec x; + + x = a; + x.tv_nsec += b; + + return normalize_tspec(x); +} /* x = a - b */ -extern void timespec_sub(struct timespec *x, const struct timespec *a, - const struct timespec *b); +static inline struct timespec +sub_tspec( + struct timespec a, + struct timespec b + ) +{ + struct timespec x; + + x = a; + x.tv_sec -= b.tv_sec; + x.tv_nsec -= b.tv_nsec; + + return normalize_tspec(x); +} /* x = a - b, b is fraction only */ -extern void timespec_subns(struct timespec *x, const struct timespec *a, - long b); +static inline struct timespec +sub_tspec_ns( + struct timespec a, + long b + ) +{ + struct timespec x; + + x = a; + x.tv_nsec -= b; + + return normalize_tspec(x); +} /* x = -a */ -extern void timespec_neg(struct timespec *x, const struct timespec *a); +static inline struct timespec +neg_tspec( + struct timespec a + ) +{ + struct timespec x; -/* - * x = abs(a) - * returns nonzero if negation was needed - */ -extern int timespec_abs(struct timespec *x, const struct timespec *a); + x.tv_sec = -a.tv_sec; + x.tv_nsec = -a.tv_nsec; + + return normalize_tspec(x); +} + +/* x = abs(a) */ +static inline struct timespec +abs_tspec( + struct timespec a + ) +{ + struct timespec c; + + c = normalize_tspec(a); + if (c.tv_sec < 0) { + if (c.tv_nsec != 0) { + c.tv_sec = -c.tv_sec - 1; + c.tv_nsec = NANOSECONDS - c.tv_nsec; + } else { + c.tv_sec = -c.tv_sec; + } + } + + return c; +} /* * compare previously-normalised a and b - * return 1 / 0 / -1 if a < / == / > b + * return 1 / 0 / -1 if a < / == / > b */ -extern int timespec_cmp_fast(const struct timespec *a, - const struct timespec *b); +static inline int +cmp_tspec( + struct timespec a, + struct timespec b + ) +{ + int r; + + r = (a.tv_sec > b.tv_sec) - (a.tv_sec < b.tv_sec); + if (0 == r) + r = (a.tv_nsec > b.tv_nsec) - + (a.tv_nsec < b.tv_nsec); + + return r; +} /* * compare possibly-denormal a and b - * return 1 / 0 / -1 if a < / == / > b + * return 1 / 0 / -1 if a < / == / > b */ -extern int timespec_cmp(const struct timespec *a, - const struct timespec *b); +static inline int +cmp_tspec_denorm( + struct timespec a, + struct timespec b + ) +{ + return cmp_tspec(normalize_tspec(a), normalize_tspec(b)); +} /* * test previously-normalised a - * return 1 / 0 / -1 if a < / == / > 0 + * return 1 / 0 / -1 if a < / == / > 0 */ -extern int timespec_test_fast(const struct timespec *a); +static inline int +test_tspec( + struct timespec a + ) +{ + int r; + + r = (a.tv_sec > 0) - (a.tv_sec < 0); + if (r == 0) + r = (a.tv_nsec > 0); + + return r; +} /* * test possibly-denormal a * return 1 / 0 / -1 if a < / == / > 0 */ -extern int timespec_test(const struct timespec *a); +static inline int +test_tspec_denorm( + struct timespec a + ) +{ + return test_tspec(normalize_tspec(a)); +} /* return LIB buffer ptr to string rep */ -extern const char * timespec_tostr(const struct timespec *x); +static inline const char * +tspectoa( + struct timespec x + ) +{ + return format_time_fraction(x.tv_sec, x.tv_nsec, 9); +} /* - convert to l_fp type, relative and absolute -*/ + * convert to l_fp type, relative and absolute + */ /* convert from timespec duration to l_fp duration */ -extern void timespec_reltolfp(l_fp *y, const struct timespec *x); +static inline l_fp +tspec_intv_to_lfp( + struct timespec x + ) +{ + struct timespec v; + l_fp y; + + v = normalize_tspec(x); + y.l_uf = TVNTOF(v.tv_nsec); + y.l_i = (int32)v.tv_sec; + + return y; +} -/* x must be UN*X epoch, output *y will be in NTP epoch */ -extern void timespec_abstolfp(l_fp *y, const struct timespec *x); +/* x must be UN*X epoch, output will be in NTP epoch */ +static inline l_fp +tspec_stamp_to_lfp( + struct timespec x + ) +{ + l_fp y; -/* convert to l_fp type, relative signed/unsigned and absolute */ -extern void timespec_relfromlfp(struct timespec *y, const l_fp *x); -extern void timespec_urelfromlfp(struct timespec *y, const l_fp *x); + y = tspec_intv_to_lfp(x); + y.l_ui += JAN_1970; + + return y; +} + +/* convert from l_fp type, relative signed/unsigned and absolute */ +static inline struct timespec +lfp_intv_to_tspec( + l_fp x + ) +{ + struct timespec out; + l_fp absx; + int neg; + + neg = L_ISNEG(&x); + absx = x; + if (neg) { + L_NEG(&absx); + } + out.tv_nsec = FTOTVN(absx.l_uf); + out.tv_sec = absx.l_i; + if (neg) { + out.tv_sec = -out.tv_sec; + out.tv_nsec = -out.tv_nsec; + out = normalize_tspec(out); + } + + return out; +} + +static inline struct timespec +lfp_uintv_to_tspec( + l_fp x + ) +{ + struct timespec out; + + out.tv_nsec = FTOTVN(x.l_uf); + out.tv_sec = x.l_ui; + + return out; +} /* * absolute (timestamp) conversion. Input is time in NTP epoch, output * is in UN*X epoch. The NTP time stamp will be expanded around the * pivot time *p or the current time, if p is NULL. */ -extern void timespec_absfromlfp(struct timespec *y, const l_fp *x, - const time_t *p); +static inline struct timespec +lfp_stamp_to_tspec( + l_fp x, + const time_t * p + ) +{ + struct timespec out; + vint64 sec; + + sec = ntpcal_ntp_to_time(x.l_ui, p); + out.tv_nsec = FTOTVN(x.l_uf); + + /* copying a vint64 to a time_t needs some care... */ +#if SIZEOF_TIME_T <= 4 + out.tv_sec = (time_t)sec.d_s.lo; +#elif defined(HAVE_INT64) + out.tv_sec = (time_t)sec.q_s; +#else + out.tv_sec = ((time_t)sec.d_s.hi << 32) | sec.d_s.lo; +#endif + + return out; +} #endif /* TIMESPECOPS_H */ diff --git a/include/timevalops.h b/include/timevalops.h index 7b475a8d42..e873b8b800 100644 --- a/include/timevalops.h +++ b/include/timevalops.h @@ -14,20 +14,116 @@ #include #include "ntp.h" -#include "ntp_unixtime.h" +#include "timetoa.h" + + +/* microseconds per second */ +#define MICROSECONDS 1000000 + +#ifndef HAVE_U_INT64 +# define USE_TSF_USEC_TABLES +#endif + +/* + * Convert usec to a time stamp fraction. + */ +#ifdef USE_TSF_USEC_TABLES +extern const u_int32 ustotslo[]; +extern const u_int32 ustotsmid[]; +extern const u_int32 ustotshi[]; + +# define TVUTOTSF(tvu, tsf) \ + ((tsf) = ustotslo[(tvu) & 0xff] \ + + ustotsmid[((tvu) >> 8) & 0xff] \ + + ustotshi[((tvu) >> 16) & 0xf]) +#else +# define TVUTOTSF(tvu, tsf) \ + ((tsf) = (u_int32) \ + ((((u_int64)(tvu) << 32) + MICROSECONDS / 2) / \ + MICROSECONDS)) +#endif + +/* + * Convert a time stamp fraction to microseconds. The time stamp + * fraction is assumed to be unsigned. + */ +#ifdef USE_TSF_USEC_TABLES +extern const u_int32 tstouslo[256]; +extern const u_int32 tstousmid[256]; +extern const u_int32 tstoushi[128]; + +/* + * TV_SHIFT is used to turn the table result into a usec value. To + * round, add in TV_ROUNDBIT before shifting. + */ +#define TV_SHIFT 3 +#define TV_ROUNDBIT 0x4 + +# define TSFTOTVU(tsf, tvu) \ + ((tvu) = (tstoushi[((tsf) >> 24) & 0xff] \ + + tstousmid[((tsf) >> 16) & 0xff] \ + + tstouslo[((tsf) >> 9) & 0x7f] \ + + TV_ROUNDBIT) >> TV_SHIFT) +#else +# define TSFTOTVU(tsf, tvu) \ + ((tvu) = (int32) \ + (((u_int64)(tsf) * MICROSECONDS + 0x80000000) >> 32)) +#endif /* - * We avoid to use/define MICROSECONDS here, as it is also possibly - * Defined in some other files and we don't want to resolve the possible - * clashes here. + * Convert a struct timeval to a time stamp. */ +#define TVTOTS(tv, ts) \ + do { \ + (ts)->l_ui = (u_long)(tv)->tv_sec; \ + TVUTOTSF((tv)->tv_usec, (ts)->l_uf); \ + } while (FALSE) + +#define sTVTOTS(tv, ts) \ + do { \ + int isneg = 0; \ + long usec; \ + (ts)->l_ui = (tv)->tv_sec; \ + usec = (tv)->tv_usec; \ + if (((tv)->tv_sec < 0) || ((tv)->tv_usec < 0)) { \ + usec = -usec; \ + (ts)->l_ui = -(ts)->l_ui; \ + isneg = 1; \ + } \ + TVUTOTSF(usec, (ts)->l_uf); \ + if (isneg) { \ + L_NEG((ts)); \ + } \ + } while (FALSE) + +/* + * Convert a time stamp to a struct timeval. The time stamp + * has to be positive. + */ +#define TSTOTV(ts, tv) \ + do { \ + (tv)->tv_sec = (ts)->l_ui; \ + TSFTOTVU((ts)->l_uf, (tv)->tv_usec); \ + if ((tv)->tv_usec == 1000000) { \ + (tv)->tv_sec++; \ + (tv)->tv_usec = 0; \ + } \ + } while (FALSE) + /* * predicate: returns TRUE if the microseconds are in nominal range * use like: int timeval_isnormal(const struct timeval *x) */ #define timeval_isnormal(x) \ - ((x)->tv_usec >= 0 && (x)->tv_usec < 1000000) + ((x)->tv_usec >= 0 && (x)->tv_usec < MICROSECONDS) + +/* + * Convert milliseconds to a time stamp fraction. Unused except for + * refclock_leitch.c, so accompanying lookup tables were removed in + * favor of reusing the microseconds conversion tables. + */ +#define MSUTOTSF(msu, tsf) TVUTOTSF((msu) * 1000, tsf) /* * predicate: returns TRUE if the microseconds are out-of-bounds @@ -36,80 +132,315 @@ #define timeval_isdenormal(x) (!timeval_isnormal(x)) /* make sure microseconds are in nominal range */ -extern void timeval_norm(struct timeval *x); +static inline struct timeval +normalize_tval( + struct timeval x + ) +{ + long z; + + /* + * If the fraction becomes excessive denormal, we use division + * to do first partial normalisation. The normalisation loops + * following will do the remaining cleanup. Since the size of + * tv_usec has a peculiar definition by the standard the range + * check is coded manually. And labs() is intentionally not used + * here: it has implementation-defined behaviour when applied + * to LONG_MIN. + */ + if (x.tv_usec < -3l * MICROSECONDS || + x.tv_usec > 3l * MICROSECONDS ) { + z = x.tv_usec / MICROSECONDS; + x.tv_usec -= z * MICROSECONDS; + x.tv_sec += z; + } -/* x = a, normalised */ -extern void timeval_copy(struct timeval *x, const struct timeval *a); + /* + * Do any remaining normalisation steps in loops. This takes 3 + * steps max, and should outperform a division even if the + * mul-by-inverse trick is employed. (It also does the floor + * division adjustment if the above division was executed.) + */ + if (x.tv_usec < 0) + do { + x.tv_usec += MICROSECONDS; + x.tv_sec--; + } while (x.tv_usec < 0); + else if (x.tv_usec >= MICROSECONDS) + do { + x.tv_usec -= MICROSECONDS; + x.tv_sec++; + } while (x.tv_usec >= MICROSECONDS); + + return x; +} /* x = a + b */ -extern void timeval_add(struct timeval *x, const struct timeval *a, - const struct timeval *b); +static inline struct timeval +add_tval( + struct timeval a, + struct timeval b + ) +{ + struct timeval x; + + x = a; + x.tv_sec += b.tv_sec; + x.tv_usec += b.tv_usec; + + return normalize_tval(x); +} /* x = a + b, b is fraction only */ -extern void timeval_addus(struct timeval *x, const struct timeval *a, - long b); +static inline struct timeval +add_tval_us( + struct timeval a, + long b + ) +{ + struct timeval x; + + x = a; + x.tv_usec += b; + + return normalize_tval(x); +} /* x = a - b */ -extern void timeval_sub(struct timeval *x, const struct timeval *a, - const struct timeval *b); +static inline struct timeval +sub_tval( + struct timeval a, + struct timeval b + ) +{ + struct timeval x; + + x = a; + x.tv_sec -= b.tv_sec; + x.tv_usec -= b.tv_usec; + + return normalize_tval(x); +} /* x = a - b, b is fraction only */ -extern void timeval_subus(struct timeval *x, const struct timeval *a, - long b); +static inline struct timeval +sub_tval_us( + struct timeval a, + long b + ) +{ + struct timeval x; + + x = a; + x.tv_usec -= b; + + return normalize_tval(x); +} /* x = -a */ -extern void timeval_neg(struct timeval *x, const struct timeval *a); +static inline struct timeval +neg_tval( + struct timeval a + ) +{ + struct timeval x; -/* - * x = abs(a) - * returns nonzero if negation was needed - */ -extern int timeval_abs(struct timeval *x, const struct timeval *a); + x.tv_sec = -a.tv_sec; + x.tv_usec = -a.tv_usec; + + return normalize_tval(x); +} + +/* x = abs(a) */ +static inline struct timeval +abs_tval( + struct timeval a + ) +{ + struct timeval c; + + c = normalize_tval(a); + if (c.tv_sec < 0) { + if (c.tv_usec != 0) { + c.tv_sec = -c.tv_sec - 1; + c.tv_usec = MICROSECONDS - c.tv_usec; + } else { + c.tv_sec = -c.tv_sec; + } + } + + return c; +} /* * compare previously-normalised a and b - * return 1 / 0 / -1 if a < / == / > b + * return 1 / 0 / -1 if a < / == / > b */ -extern int timeval_cmp_fast(const struct timeval *a, - const struct timeval *b); +static inline int +cmp_tval( + struct timeval a, + struct timeval b + ) +{ + int r; + + r = (a.tv_sec > b.tv_sec) - (a.tv_sec < b.tv_sec); + if (0 == r) + r = (a.tv_usec > b.tv_usec) - + (a.tv_usec < b.tv_usec); + + return r; +} + /* * compare possibly-denormal a and b - * return 1 / 0 / -1 if a < / == / > b + * return 1 / 0 / -1 if a < / == / > b */ -extern int timeval_cmp(const struct timeval *a, - const struct timeval *b); +static inline int +cmp_tval_denorm( + struct timeval a, + struct timeval b + ) +{ + return cmp_tval(normalize_tval(a), normalize_tval(b)); +} /* * test previously-normalised a - * return 1 / 0 / -1 if a < / == / > 0 + * return 1 / 0 / -1 if a < / == / > 0 */ -extern int timeval_test_fast(const struct timeval *a); +static inline int +test_tval( + struct timeval a + ) +{ + int r; + + r = (a.tv_sec > 0) - (a.tv_sec < 0); + if (r == 0) + r = (a.tv_usec > 0); + + return r; +} /* * test possibly-denormal a * return 1 / 0 / -1 if a < / == / > 0 */ -extern int timeval_test(const struct timeval *a); +static inline int +test_tval_denorm( + struct timeval a + ) +{ + return test_tval(normalize_tval(a)); +} /* return LIB buffer ptr to string rep */ -extern const char * timeval_tostr(const struct timeval *x); +static inline const char * +tvaltoa( + struct timeval x + ) +{ + return format_time_fraction(x.tv_sec, x.tv_usec, 6); +} /* convert from timeval duration to l_fp duration */ -extern void timeval_reltolfp(l_fp *y, const struct timeval *x); +static inline l_fp +tval_intv_to_lfp( + struct timeval x + ) +{ + struct timeval v; + l_fp y; + + v = normalize_tval(x); + TVUTOTSF(v.tv_usec, y.l_uf); + y.l_i = (int32)v.tv_sec; + + return y; +} /* x must be UN*X epoch, output *y will be in NTP epoch */ -extern void timeval_abstolfp(l_fp *y, const struct timeval *x); +static inline l_fp +tval_stamp_to_lfp( + struct timeval x + ) +{ + l_fp y; + + y = tval_intv_to_lfp(x); + y.l_ui += JAN_1970; + + return y; +} /* convert to l_fp type, relative signed/unsigned and absolute */ -extern void timeval_relfromlfp(struct timeval *y, const l_fp *x); -extern void timeval_urelfromlfp(struct timeval *y, const l_fp *x); +static inline struct timeval +lfp_intv_to_tval( + l_fp x + ) +{ + struct timeval out; + l_fp absx; + int neg; + + neg = L_ISNEG(&x); + absx = x; + if (neg) { + L_NEG(&absx); + } + TSFTOTVU(absx.l_uf, out.tv_usec); + out.tv_sec = absx.l_i; + if (neg) { + out.tv_sec = -out.tv_sec; + out.tv_usec = -out.tv_usec; + out = normalize_tval(out); + } + + return out; +} + +static inline struct timeval +lfp_uintv_to_tval( + l_fp x + ) +{ + struct timeval out; + + TSFTOTVU(x.l_uf, out.tv_usec); + out.tv_sec = x.l_ui; + + return out; +} /* * absolute (timestamp) conversion. Input is time in NTP epoch, output * is in UN*X epoch. The NTP time stamp will be expanded around the * pivot time *p or the current time, if p is NULL. */ -extern void timeval_absfromlfp(struct timeval *y, const l_fp *x, - const time_t *p); +static inline struct timeval +lfp_stamp_to_tval( + l_fp x, + const time_t * p + ) +{ + struct timeval out; + vint64 sec; + + sec = ntpcal_ntp_to_time(x.l_ui, p); + TSFTOTVU(x.l_uf, out.tv_usec); + + /* copying a vint64 to a time_t needs some care... */ +#if SIZEOF_TIME_T <= 4 + out.tv_sec = (time_t)sec.d_s.lo; +#elif defined(HAVE_INT64) + out.tv_sec = (time_t)sec.q_s; +#else + out.tv_sec = ((time_t)sec.d_s.hi << 32) | sec.d_s.lo; +#endif + out = normalize_tval(out); + + return out; +} #endif /* TIMEVALOPS_H */ diff --git a/lib/isc/include/isc/util.h b/lib/isc/include/isc/util.h index 82abbed88b..4481eb5c71 100644 --- a/lib/isc/include/isc/util.h +++ b/lib/isc/include/isc/util.h @@ -70,7 +70,7 @@ * Use this in translation units that would otherwise be empty, to * suppress compiler warnings. */ -#define EMPTY_TRANSLATION_UNIT static char nonempty_translation_unit; +#define EMPTY_TRANSLATION_UNIT extern void exit(int); /*% * We use macros instead of calling the routines directly because diff --git a/lib/isc/unix/ifiter_sysctl.c b/lib/isc/unix/ifiter_sysctl.c index 072e3494f7..2c56f3ebb1 100644 --- a/lib/isc/unix/ifiter_sysctl.c +++ b/lib/isc/unix/ifiter_sysctl.c @@ -272,10 +272,11 @@ internal_current(isc_interfaceiter_t *iter) { return (ISC_R_SUCCESS); } else { - printf(isc_msgcat_get(isc_msgcat, ISC_MSGSET_IFITERSYSCTL, - ISC_MSG_UNEXPECTEDTYPE, - "warning: unexpected interface list " - "message type\n")); + printf("%s", isc_msgcat_get(isc_msgcat, + ISC_MSGSET_IFITERSYSCTL, + ISC_MSG_UNEXPECTEDTYPE, + "warning: unexpected interface " + "list message type\n")); return (ISC_R_IGNORE); } } diff --git a/libntp/Makefile.am b/libntp/Makefile.am index 31a28995cd..49202fae6f 100644 --- a/libntp/Makefile.am +++ b/libntp/Makefile.am @@ -61,8 +61,6 @@ libntp_a_SRCS = \ dolfptoa.c \ emalloc.c \ findconfig.c \ - fptoa.c \ - fptoms.c \ getopt.c \ hextoint.c \ hextolfp.c \ @@ -72,12 +70,9 @@ libntp_a_SRCS = \ iosignal.c \ lib_strbuf.c \ machines.c \ - mfptoa.c \ - mfptoms.c \ mktime.c \ modetoa.c \ mstolfp.c \ - msutotsf.c \ msyslog.c \ netof.c \ ntp_calendar.c \ @@ -102,12 +97,8 @@ libntp_a_SRCS = \ strdup.c \ strl_obsd.c \ syssignal.c \ - timespecops.c \ timetoa.c \ timevalops.c \ - tsftomsu.c \ - tstotv.c \ - tvtots.c \ uglydate.c \ uinttoa.c \ work_fork.c \ diff --git a/libntp/buftvtots.c b/libntp/buftvtots.c index d6d9300f12..a77e9f7c9b 100644 --- a/libntp/buftvtots.c +++ b/libntp/buftvtots.c @@ -9,7 +9,7 @@ #endif #include "ntp_fp.h" #include "ntp_string.h" -#include "ntp_unixtime.h" +#include "timevalops.h" #ifndef SYS_WINNT int @@ -28,26 +28,11 @@ buftvtots( /* * and use it */ - ts->l_ui = tv.tv_sec + (u_long)JAN_1970; - if (tv.tv_usec > 999999) - return 0; - TVUTOTSF(tv.tv_usec, ts->l_uf); - return 1; -} -#else /* SYS_WINNT */ -/* - * Windows doesn't have the tty_clock line discipline, so - * don't look for a timestamp where there is none. - */ -int -buftvtots( - const char *bufp, - l_fp *ts - ) -{ - UNUSED_ARG(bufp); - UNUSED_ARG(ts); + if (tv.tv_usec > MICROSECONDS - 1) + return FALSE; + + *ts = tval_stamp_to_lfp(tv); - return 0; + return TRUE; } -#endif /* SYS_WINNT */ +#endif /* !SYS_WINNT */ diff --git a/libntp/dofptoa.c b/libntp/dofptoa.c index 7a000afbb7..758af2fd7f 100644 --- a/libntp/dofptoa.c +++ b/libntp/dofptoa.c @@ -117,3 +117,43 @@ dofptoa( *bp = '\0'; return buf; } + + +char * +fptoa( + s_fp fpv, + short ndec + ) +{ + u_fp plusfp; + int neg; + + neg = (fpv < 0); + if (neg) { + plusfp = (u_fp)(-fpv); + } else { + plusfp = (u_fp)fpv; + } + + return dofptoa(plusfp, neg, ndec, FALSE); +} + + +char * +fptoms( + s_fp fpv, + short ndec + ) +{ + u_fp plusfp; + int neg; + + neg = (fpv < 0); + if (neg) { + plusfp = (u_fp)(-fpv); + } else { + plusfp = (u_fp)fpv; + } + + return dofptoa(plusfp, neg, ndec, TRUE); +} diff --git a/libntp/dolfptoa.c b/libntp/dolfptoa.c index 0beddf7498..07ead950f4 100644 --- a/libntp/dolfptoa.c +++ b/libntp/dolfptoa.c @@ -54,7 +54,8 @@ dolfptoa( * Done that, now deal with the problem of the fraction. First * determine the number of decimal places. */ - if ((dec = ndec) < 0) + dec = ndec; + if (dec < 0) dec = 0; if (msec) { dec += 3; @@ -92,14 +93,14 @@ dolfptoa( } else { /* some bits remain in 'fpv'; do round */ u_char *tp = cpend; - int carry = (fpv & 0x80000000U) != 0; + int carry = ((fpv & 0x80000000) != 0); for (dec = tp - cbuf; carry && dec > 0; dec--) { *--tp += 1; if (*tp == 10) *tp = 0; else - carry = 0; + carry = FALSE; } if (tp < cp) /* rounding from 999 to 1000 or similiar? */ @@ -133,3 +134,41 @@ dolfptoa( */ return buf; } + + +char * +mfptoa( + u_int32 fpi, + u_int32 fpf, + short ndec + ) +{ + int isneg; + + isneg = M_ISNEG(fpi); + if (isneg) { + M_NEG(fpi, fpf); + } + + return dolfptoa(fpi, fpf, isneg, ndec, FALSE); +} + + +char * +mfptoms( + u_int32 fpi, + u_int32 fpf, + short ndec + ) +{ + int isneg; + + isneg = M_ISNEG(fpi); + if (isneg) { + M_NEG(fpi, fpf); + } + + return dolfptoa(fpi, fpf, isneg, ndec, TRUE); +} + + diff --git a/libntp/fptoa.c b/libntp/fptoa.c deleted file mode 100644 index 771c6dfcee..0000000000 --- a/libntp/fptoa.c +++ /dev/null @@ -1,26 +0,0 @@ -/* - * fptoa - return an asciized representation of an s_fp number - */ -#include -#include "ntp_fp.h" -#include "ntp_stdlib.h" - -char * -fptoa( - s_fp fpv, - short ndec - ) -{ - u_fp plusfp; - int neg; - - if (fpv < 0) { - plusfp = (u_fp)(-fpv); - neg = 1; - } else { - plusfp = (u_fp)fpv; - neg = 0; - } - - return dofptoa(plusfp, neg, ndec, 0); -} diff --git a/libntp/fptoms.c b/libntp/fptoms.c deleted file mode 100644 index 9a1ab77acd..0000000000 --- a/libntp/fptoms.c +++ /dev/null @@ -1,25 +0,0 @@ -/* - * fptoms - return an asciized s_fp number in milliseconds - */ -#include -#include "ntp_fp.h" - -char * -fptoms( - s_fp fpv, - short ndec - ) -{ - u_fp plusfp; - int neg; - - if (fpv < 0) { - plusfp = (u_fp)(-fpv); - neg = 1; - } else { - plusfp = (u_fp)fpv; - neg = 0; - } - - return dofptoa(plusfp, neg, ndec, 1); -} diff --git a/libntp/mfptoa.c b/libntp/mfptoa.c deleted file mode 100644 index af83d00be6..0000000000 --- a/libntp/mfptoa.c +++ /dev/null @@ -1,24 +0,0 @@ -/* - * mfptoa - Return an asciized representation of a signed long fp number - */ -#include -#include "ntp_fp.h" -#include "ntp_stdlib.h" - -char * -mfptoa( - u_long fpi, - u_long fpf, - short ndec - ) -{ - int isneg; - - if (M_ISNEG(fpi, fpf)) { - isneg = 1; - M_NEG(fpi, fpf); - } else - isneg = 0; - - return dolfptoa(fpi, fpf, isneg, ndec, 0); -} diff --git a/libntp/mfptoms.c b/libntp/mfptoms.c deleted file mode 100644 index 678eacc114..0000000000 --- a/libntp/mfptoms.c +++ /dev/null @@ -1,24 +0,0 @@ -/* - * mfptoms - Return an asciized signed long fp number in milliseconds - */ -#include -#include "ntp_fp.h" -#include "ntp_stdlib.h" - -char * -mfptoms( - u_long fpi, - u_long fpf, - short ndec - ) -{ - int isneg; - - if (M_ISNEG(fpi, fpf)) { - isneg = 1; - M_NEG(fpi, fpf); - } else - isneg = 0; - - return dolfptoa(fpi, fpf, isneg, ndec, 1); -} diff --git a/libntp/msutotsf.c b/libntp/msutotsf.c deleted file mode 100644 index 22ae6119b9..0000000000 --- a/libntp/msutotsf.c +++ /dev/null @@ -1,36 +0,0 @@ -/* - * msutotsf - tables for converting from a subsecond millisecond value - * to a time stamp fraction. - */ -#include -#include - -#include "ntp_types.h" - -/* - * Index each of these tables with five bits of the (less than) 10 - * bit millisecond value. Note that the tables are rounded (not - * truncated). The error in the result will thus be +-1 low order - * bit in the time stamp fraction. - */ -const u_int32 msutotsflo[32] = { - 0x00000000, 0x00418937, 0x0083126f, 0x00c49ba6, - 0x010624dd, 0x0147ae14, 0x0189374c, 0x01cac083, - 0x020c49ba, 0x024dd2f2, 0x028f5c29, 0x02d0e560, - 0x03126e98, 0x0353f7cf, 0x03958106, 0x03d70a3d, - 0x04189375, 0x045a1cac, 0x049ba5e3, 0x04dd2f1b, - 0x051eb852, 0x05604189, 0x05a1cac1, 0x05e353f8, - 0x0624dd2f, 0x06666666, 0x06a7ef9e, 0x06e978d5, - 0x072b020c, 0x076c8b44, 0x07ae147b, 0x07ef9db2 -}; - -const u_int32 msutotsfhi[32] = { - 0x00000000, 0x083126e9, 0x10624dd3, 0x189374bc, - 0x20c49ba6, 0x28f5c28f, 0x3126e979, 0x39581062, - 0x4189374c, 0x49ba5e35, 0x51eb851f, 0x5a1cac08, - 0x624dd2f2, 0x6a7ef9db, 0x72b020c5, 0x7ae147ae, - 0x83126e98, 0x8b439581, 0x9374bc6a, 0x9ba5e354, - 0xa3d70a3d, 0xac083127, 0xb4395810, 0xbc6a7efa, - 0xc49ba5e3, 0xcccccccd, 0xd4fdf3b6, 0xdd2f1aa0, - 0xe5604189, 0xed916873, 0xf5c28f5c, 0xfdf3b646 -}; diff --git a/libntp/ntp_calendar.c b/libntp/ntp_calendar.c index b61590192c..7c3d52d67e 100644 --- a/libntp/ntp_calendar.c +++ b/libntp/ntp_calendar.c @@ -507,7 +507,7 @@ ntpcal_daysplit( memcpy(&op, ts, sizeof(op)); /* fix sign */ - isneg = M_ISNEG(op.D_s.hi, op.D_s.lo); + isneg = M_ISNEG(op.D_s.hi); if (isneg) M_NEG(op.D_s.hi, op.D_s.lo); diff --git a/libntp/systime.c b/libntp/systime.c index 88429e491d..03ebc3dd7d 100644 --- a/libntp/systime.c +++ b/libntp/systime.c @@ -8,10 +8,8 @@ #include "ntp.h" #include "ntp_syslog.h" -#include "ntp_unixtime.h" #include "ntp_stdlib.h" #include "ntp_random.h" -#include "ntpd.h" /* for sys_precision */ #include "timevalops.h" #include "timespecops.h" #include "ntp_calendar.h" @@ -73,6 +71,7 @@ double sys_fuzz = 0; /* min. time to read the clock (s) */ long sys_fuzz_nsec = 0; /* min. time to read the clock (ns) */ double measured_tick; /* non-overridable sys_tick (s) */ double sys_residual = 0; /* adjustment residue (s) */ +int trunc_os_clock; /* sys_tick > measured_tick */ time_stepped_callback step_callback; #ifndef SIM @@ -118,7 +117,7 @@ get_ostime( exit(1); } - if (sys_tick > measured_tick) { + if (trunc_os_clock) { ticks = (long)((tsp->tv_nsec * 1e-9) / sys_tick); tsp->tv_nsec = (long)(ticks * 1e9 * sys_tick); } @@ -154,65 +153,53 @@ get_systime( * fuzzed result is strictly later than the prior. Limit the * necessary fiction to 1 second. */ - if (sys_fuzz_nsec > 0 && !lamport_violated) { - timespec_addns(&ts_min, &ts_prev, sys_fuzz_nsec); - if (timespec_cmp_fast(&ts, &ts_min) < 0) { - timespec_sub(&ts_lam, &ts_min, &ts); - ts = ts_min; - if (ts_lam.tv_sec > 0) { - msyslog(LOG_ERR, - "get_systime Lamport advance exceeds one second (%.9f)", - ts_lam.tv_sec + 1e-9 * ts_lam.tv_nsec); - exit(1); - } + ts_min = add_tspec_ns(ts_prev, sys_fuzz_nsec); + if (cmp_tspec(ts, ts_min) < 0) { + ts_lam = sub_tspec(ts_min, ts); + if (ts_lam.tv_sec > 0 && !lamport_violated) { + msyslog(LOG_ERR, + "get_systime Lamport advance exceeds one second (%.9f)", + ts_lam.tv_sec + 1e-9 * ts_lam.tv_nsec); + exit(1); } + if (!lamport_violated) + ts = ts_min; } ts_prev_log = ts_prev; ts_prev = ts; /* convert from timespec to l_fp fixed-point */ - timespec_abstolfp(&result, &ts); + result = tspec_stamp_to_lfp(ts); /* * Add in the fuzz. */ - if (sys_fuzz > 0.) { - dfuzz = ntp_random() * 2. / FRAC * sys_fuzz; - DTOLFP(dfuzz, &lfpfuzz); - L_ADD(&result, &lfpfuzz); - } else { - dfuzz = 0; - } + dfuzz = ntp_random() * 2. / FRAC * sys_fuzz; + DTOLFP(dfuzz, &lfpfuzz); + L_ADD(&result, &lfpfuzz); /* * Ensure result is strictly greater than prior result (ignoring * sys_residual's effect for now) once sys_fuzz has been * determined. */ - if (sys_fuzz > 0.) { - if (!L_ISZERO(&lfp_prev) && !lamport_violated) { - if (!L_ISGTU(&result, &lfp_prev)) { - msyslog(LOG_ERR, - "%sts_min %s ts_prev %s ts %s", - (lamport_violated) - ? "LAMPORT " - : "", - timespec_tostr(&ts_min), - timespec_tostr(&ts_prev_log), - timespec_tostr(&ts)); - msyslog(LOG_ERR, "sys_fuzz %ld nsec, this fuzz %.9f", - sys_fuzz_nsec, dfuzz); - lfpdelta = lfp_prev; - L_SUB(&lfpdelta, &result); - LFPTOD(&lfpdelta, ddelta); - msyslog(LOG_ERR, - "get_systime prev result 0x%x.%08x is %.9f later than 0x%x.%08x", - lfp_prev.l_ui, lfp_prev.l_uf, - ddelta, result.l_ui, result.l_uf); - } + if (!L_ISZERO(&lfp_prev) && !lamport_violated) { + if (!L_ISGTU(&result, &lfp_prev) && sys_fuzz > 0.) { + msyslog(LOG_ERR, "ts_min %s ts_prev %s ts %s", + tspectoa(ts_min), tspectoa(ts_prev_log), + tspectoa(ts)); + msyslog(LOG_ERR, "sys_fuzz %ld nsec, this fuzz %.9f", + sys_fuzz_nsec, dfuzz); + lfpdelta = lfp_prev; + L_SUB(&lfpdelta, &result); + LFPTOD(&lfpdelta, ddelta); + msyslog(LOG_ERR, + "get_systime prev result 0x%x.%08x is %.9f later than 0x%x.%08x", + lfp_prev.l_ui, lfp_prev.l_uf, + ddelta, result.l_ui, result.l_uf); } - lfp_prev = result; } + lfp_prev = result; if (lamport_violated) lamport_violated = FALSE; @@ -356,7 +343,7 @@ step_systime( /* get the current time as l_fp (without fuzz) and as struct timeval */ get_ostime(&timets); - timespec_abstolfp(&fp_sys, &timets); + fp_sys = tspec_stamp_to_lfp(timets); tvlast.tv_sec = timets.tv_sec; tvlast.tv_usec = (timets.tv_nsec + 500) / 1000; @@ -364,7 +351,7 @@ step_systime( L_ADD(&fp_sys, &fp_ofs); /* unfold the new system time */ - timeval_absfromlfp(&timetv, &fp_sys, &pivot); + timetv = lfp_stamp_to_tval(fp_sys, &pivot); /* now set new system time */ if (ntp_set_tod(&timetv, NULL) != 0) { @@ -407,8 +394,7 @@ step_systime( * * This might become even Uglier... */ - timeval_sub(&tvdiff, &timetv, &tvlast); - timeval_abs(&tvdiff, &tvdiff); + tvdiff = abs_tval(sub_tval(timetv, tvlast)); if (tvdiff.tv_sec > 0) { #ifdef HAVE_UTMP_H struct utmp ut; diff --git a/libntp/timespecops.c b/libntp/timespecops.c deleted file mode 100644 index bbbabc6cc0..0000000000 --- a/libntp/timespecops.c +++ /dev/null @@ -1,374 +0,0 @@ -/* - * timespecops.c -- calculations on 'struct timespec' values - * - * Written by Juergen Perlinger (perlinger@ntp.org) for the NTP project. - * The contents of 'html/copyright.html' apply. - */ - -#include -#include - -#include "timespecops.h" - -#include "timetoa.h" -#include "ntp_types.h" -#include "ntp_calendar.h" - -/* make sure we have the right definition for NANOSECONDS */ -#undef NANOSECONDS -#define NANOSECONDS 1000000000 - -/* conversion between l_fp fractions and nanoseconds */ -#ifdef HAVE_U_INT64 -# define MYFTOTVN(tsf, tvu) \ - ((tvu) = (int32) \ - (((u_int64)(tsf) * NANOSECONDS + 0x80000000) >> 32)) -# define MYTVNTOF(tvu, tsf) \ - ((tsf) = (u_int32) \ - ((((u_int64)(tvu) << 32) + NANOSECONDS / 2) / \ - NANOSECONDS)) -#else -# define MYFTOTVN(tsf, tvu) \ - ((tvu) = (int32)floor((tsf) / 4.294967296 + 0.5)) -# define MYTVNTOF(tvu, tsf) \ - ((tsf) = (u_int32)floor((tvu) * 4.294967296 + 0.5)) -#endif - - -/* copy and normalise. Used often enough to warrant a macro. */ -#define COPYNORM(dst, src) \ - do { \ - *(dst) = *(src); \ - if (timespec_isdenormal(dst)) \ - timespec_norm(dst); \ - } while (0) - - -void -timespec_norm( - struct timespec *x - ) -{ -#if SIZEOF_LONG > 4 - /* tv_nsec is of type 'long', and on a 64 bit machine using only - * loops becomes prohibitive once the upper 32 bits get - * involved. On the other hand, division by constant should be - * fast enough; so we do a division of the nanoseconds in that - * case. The floor adjustment step follows with the standard - * normalisation loops. And 'labs' is intentinally not used - * here: its has implementation defined behaviour when applied - * to LONG_MIN. */ - if (x->tv_nsec < -3l * NANOSECONDS || - x->tv_nsec > 3l * NANOSECONDS ) { - long z; - z = x->tv_nsec / NANOSECONDS; - x->tv_nsec -= z * NANOSECONDS; - x->tv_sec += z; - } -#endif - /* since 10**9 is close to 2**32, we don't divide but do a - * normalisation in a loop; this takes 3 steps max, and should - * outperform a division even if the mul-by-inverse trick is - * employed. */ - if (x->tv_nsec < 0) - do { - x->tv_nsec += NANOSECONDS; - x->tv_sec -= 1; - } while (x->tv_nsec < 0); - else if (x->tv_nsec >= NANOSECONDS) - do { - x->tv_nsec -= NANOSECONDS; - x->tv_sec += 1; - } while (x->tv_nsec >= NANOSECONDS); -} - -/* x = a, normalised */ -void -timespec_copy( - struct timespec * x, - const struct timespec * a - ) -{ - COPYNORM(x, a); -} - -/* x = a + b */ -void -timespec_add( - struct timespec * x, - const struct timespec * a, - const struct timespec * b - ) -{ - struct timespec c; - - c.tv_sec = a->tv_sec + b->tv_sec; - c.tv_nsec = a->tv_nsec + b->tv_nsec; - COPYNORM(x, &c); -} - -/* x = a + b, b is fraction only */ -void -timespec_addns( - struct timespec * x, - const struct timespec * a, - long b - ) -{ - struct timespec c; - - c.tv_sec = a->tv_sec; - c.tv_nsec = a->tv_nsec + b; - COPYNORM(x, &c); -} - -/* x = a - b */ -void -timespec_sub( - struct timespec * x, - const struct timespec * a, - const struct timespec * b - ) -{ - struct timespec c; - - c.tv_sec = a->tv_sec - b->tv_sec; - c.tv_nsec = a->tv_nsec - b->tv_nsec; - COPYNORM(x, &c); -} - -/* x = a - b, b is fraction only */ -void -timespec_subns( - struct timespec * x, - const struct timespec * a, - long b - ) -{ - struct timespec c; - - c.tv_sec = a->tv_sec; - c.tv_nsec = a->tv_nsec - b; - COPYNORM(x, &c); -} - -/* x = -a */ -void -timespec_neg( - struct timespec * x, - const struct timespec * a - ) -{ - struct timespec c; - - c.tv_sec = - a->tv_sec; - c.tv_nsec = - a->tv_nsec; - COPYNORM(x, &c); -} - -/* - * x = abs(a) - * return value is nonzero if negation was needed - */ -int -timespec_abs( - struct timespec * x, - const struct timespec * a - ) -{ - struct timespec c; - int r; - - COPYNORM(&c, a); - r = (c.tv_sec < 0); - if (r != 0) { - c.tv_sec = - c.tv_sec; - c.tv_nsec = - c.tv_nsec; - if (c.tv_nsec < 0) { - c.tv_sec -= 1; - c.tv_nsec += NANOSECONDS; - } - } - *x = c; - - return r; -} - -/* - * compare previously-normalised a and b - * return -1 / 0 / 1 if a < / == / > b - */ -int -timespec_cmp_fast( - const struct timespec *a, - const struct timespec *b - ) -{ - int r; - - r = (a->tv_sec > b->tv_sec) - (a->tv_sec < b->tv_sec); - if (r == 0) - r = (a->tv_nsec > b->tv_nsec) - - (a->tv_nsec < b->tv_nsec); - - return r; -} - -/* - * compare possibly denormal a and b - * return -1 / 0 / 1 if a < / == / > b - */ -int -timespec_cmp( - const struct timespec *a, - const struct timespec *b - ) -{ - int r; - struct timespec A; - struct timespec B; - - COPYNORM(&A, a); - COPYNORM(&B, b); - r = (A.tv_sec > B.tv_sec) - (A.tv_sec < B.tv_sec); - if (r == 0) - r = (A.tv_nsec > B.tv_nsec) - - (A.tv_nsec < B.tv_nsec); - - return r; -} - -/* - * test previously-normalised a - * return -1 / 0 / 1 if a < / == / > 0 - */ -int -timespec_test_fast( - const struct timespec *a - ) -{ - int r; - - r = (a->tv_sec > 0) - (a->tv_sec < 0); - if (r == 0) - r = (a->tv_nsec > 0); - - return r; -} - -/* - * test possibly denormal a - * return -1 / 0 / 1 if a < / == / > 0 - */ -int -timespec_test( - const struct timespec *a - ) -{ - struct timespec A; - int r; - - COPYNORM(&A, a); - r = (A.tv_sec > 0) - (A.tv_sec < 0); - if (r == 0) - r = (A.tv_nsec > 0); - - return r; -} - -/* return LIB buffer ptr to string rep */ -const char * -timespec_tostr( - const struct timespec *x - ) -{ - return format_time_fraction(x->tv_sec, x->tv_nsec, 9); -} - -void -timespec_abstolfp( - l_fp * y, - const struct timespec * x - ) -{ - struct timespec v; - - COPYNORM(&v, x); - MYTVNTOF(v.tv_nsec, y->l_uf); - y->l_ui = (u_int32)v.tv_sec + JAN_1970; -} - -void -timespec_reltolfp( - l_fp * y, - const struct timespec * x - ) -{ - struct timespec v; - - COPYNORM(&v, x); - MYTVNTOF(v.tv_nsec, y->l_uf); - y->l_i = (int32)v.tv_sec; -} - -void -timespec_relfromlfp( - struct timespec *y, - const l_fp *x - ) -{ - struct timespec out; - l_fp tmp; - int neg; - - tmp = *x; - neg = L_ISNEG(&tmp); - if (neg != 0) - L_NEG(&tmp); - MYFTOTVN(x->l_uf, out.tv_nsec); - out.tv_sec = x->l_ui; - if (neg != 0) { - out.tv_sec = -out.tv_sec; - out.tv_nsec = -out.tv_nsec; - } - COPYNORM(y, &out); -} - -void -timespec_urelfromlfp( - struct timespec *y, - const l_fp *x - ) -{ - struct timespec out; - - MYFTOTVN(x->l_uf, out.tv_nsec); - out.tv_sec = x->l_ui; - COPYNORM(y, &out); -} - -void -timespec_absfromlfp( - struct timespec *y, - const l_fp *x, - const time_t *p - ) -{ - struct timespec out; - vint64 sec; - - sec = ntpcal_ntp_to_time(x->l_ui, p); - MYFTOTVN(x->l_uf, out.tv_nsec); - - /* copying a vint64 to a time_t needs some care... */ -#if SIZEOF_TIME_T <= 4 - out.tv_sec = (time_t)sec.d_s.lo; -#elif defined(HAVE_INT64) - out.tv_sec = (time_t)sec.q_s; -#else - out.tv_sec = ((time_t)sec.d_s.hi << 32) + sec.d_s.lo; -#endif - - COPYNORM(y, &out); -} -/* -*- EOF -*- */ diff --git a/libntp/timevalops.c b/libntp/timevalops.c index d524c87b7c..17ad4cc8aa 100644 --- a/libntp/timevalops.c +++ b/libntp/timevalops.c @@ -6,362 +6,293 @@ */ #include -#include #include "timevalops.h" -#include "timetoa.h" -#include "ntp_calendar.h" - -/* make sure we have the right definition for MICROSECONDS */ -#undef MICROSECONDS -#define MICROSECONDS 1000000 - -/* conversion between l_fp fractions and microseconds */ -#if SIZEOF_LONG >= 8 -# define MYFTOTVU(tsf, tvu) \ - ((tvu) = (int32) \ - (((u_long)(tsf) * MICROSECONDS + 0x80000000) >> 32)) -# define MYTVUTOF(tvu, tsf) \ - ((tsf) = (u_int32) \ - ((((u_long)(tvu) << 32) + MICROSECONDS / 2) / \ - MICROSECONDS)) -#else -# define MYFTOTVU(tsf, tvu) TSFTOTVU(tsf, tvu) -# define MYTVUTOF(tvu, tsf) TVUTOTSF(tvu, tsf) -#endif - -/* copy and normalise. Used often enough to warrant a macro. */ -#define COPYNORM(dst, src) \ - do { \ - *(dst) = *(src); \ - if (timeval_isdenormal(dst)) \ - timeval_norm(dst); \ - } while (0) - - -void -timeval_norm( - struct timeval *x - ) -{ - /* If the fraction becomes excessive denormal, we use division - * to do first partial normalisation. The normalisation loops - * following will do the remaining cleanup. Since the size of - * tv_usec has a peculiar definition by the standard the range - * check is coded manually. - */ - if (x->tv_usec < -3l * MICROSECONDS || - x->tv_usec > 3l * MICROSECONDS ) { - long z; - z = x->tv_usec / MICROSECONDS; - x->tv_usec -= z * MICROSECONDS; - x->tv_sec += z; - } - /* Do any remaining normalisation steps in loops. This takes 3 - * steps max, and should outperform a division even if the - * mul-by-inverse trick is employed. (It also does the floor - * division adjustment if the above division was executed.) */ - if (x->tv_usec < 0) - do { - x->tv_usec += MICROSECONDS; - x->tv_sec -= 1; - } while (x->tv_usec < 0); - else if (x->tv_usec >= MICROSECONDS) - do { - x->tv_usec -= MICROSECONDS; - x->tv_sec += 1; - } while (x->tv_usec >= MICROSECONDS); -} - -/* x = a, normalised */ -void -timeval_copy( - struct timeval * x, - const struct timeval * a - ) -{ - COPYNORM(x, a); -} - -/* x = a + b */ -void -timeval_add( - struct timeval * x, - const struct timeval * a, - const struct timeval * b - ) -{ - struct timeval c; - - c.tv_sec = a->tv_sec + b->tv_sec; - c.tv_usec = a->tv_usec + b->tv_usec; - COPYNORM(x, &c); -} - -/* x = a + b, b is fraction only */ -void -timeval_addus( - struct timeval * x, - const struct timeval * a, - long b - ) -{ - struct timeval c; - - c.tv_sec = a->tv_sec; - c.tv_usec = a->tv_usec + b; - COPYNORM(x, &c); -} - -/* x = a - b */ -void -timeval_sub( - struct timeval * x, - const struct timeval * a, - const struct timeval * b - ) -{ - struct timeval c; - - c.tv_sec = a->tv_sec - b->tv_sec; - c.tv_usec = a->tv_usec - b->tv_usec; - COPYNORM(x, &c); -} - -/* x = a - b, b is fraction only */ -void -timeval_subus( - struct timeval * x, - const struct timeval * a, - long b - ) -{ - struct timeval c; - - c.tv_sec = a->tv_sec; - c.tv_usec = a->tv_usec - b; - COPYNORM(x, &c); -} - -/* x = -a */ -void -timeval_neg( - struct timeval * x, - const struct timeval * a - ) -{ - struct timeval c; - - c.tv_sec = -a->tv_sec; - c.tv_usec = -a->tv_usec; - COPYNORM(x, &c); -} - +#ifdef USE_TSF_USEC_TABLES /* - * x = abs(a) - * return value is nonzero if negation was needed - */ -int -timeval_abs( - struct timeval * x, - const struct timeval * a - ) -{ - struct timeval c; - int r; - - COPYNORM(&c, a); - r = (c.tv_sec < 0); - if (r != 0) { - c.tv_sec = - c.tv_sec; - c.tv_usec = - c.tv_usec; - if (c.tv_usec < 0) { - c.tv_sec -= 1; - c.tv_usec += MICROSECONDS; - } - } - *x = c; - - return r; -} - -/* - * compare previously-normalised a and b - * return -1 / 0 / 1 if a < / == / > b - */ -int -timeval_cmp_fast( - const struct timeval *a, - const struct timeval *b - ) -{ - int r; - - r = (a->tv_sec > b->tv_sec) - (a->tv_sec < b->tv_sec); - if (r == 0) - r = (a->tv_usec > b->tv_usec) - - (a->tv_usec < b->tv_usec); - - return r; -} - -/* - * compare possibly denormal a and b - * return -1 / 0 / 1 if a < / == / > b - */ -int -timeval_cmp( - const struct timeval *a, - const struct timeval *b - ) -{ - int r; - struct timeval A; - struct timeval B; - - COPYNORM(&A, a); - COPYNORM(&B, b); - r = (A.tv_sec > B.tv_sec) - (A.tv_sec < B.tv_sec); - if (r == 0) - r = (A.tv_usec > B.tv_usec) - - (A.tv_usec < B.tv_usec); - - return r; -} - -/* - * test previously-normalised a - * return -1 / 0 / 1 if a < / == / > 0 + * Tables to calculate time stamp fractions from usecs. The entries + * in these tables are offset into using each of the two low order + * bytes plus the next 4 bits in a usec value (from a struct timeval). + * These are summed to produce the time stamp fraction. + * + * Note that these tables are rounded (not truncated) to the nearest + * low order bit in the fraction. The timestamp computed should be + * +- 1.5 low order bits. */ -int -timeval_test_fast( - const struct timeval *a - ) -{ - int r; - r = (a->tv_sec > 0) - (a->tv_sec < 0); - if (r == 0) - r = (a->tv_usec > 0); - - return r; -} +const u_int32 ustotslo[256] = { + 0x00000000, 0x000010c7, 0x0000218e, 0x00003255, + 0x0000431c, 0x000053e3, 0x000064aa, 0x00007571, + 0x00008638, 0x000096ff, 0x0000a7c6, 0x0000b88d, + 0x0000c954, 0x0000da1b, 0x0000eae2, 0x0000fba9, + 0x00010c6f, 0x00011d36, 0x00012dfd, 0x00013ec4, + 0x00014f8b, 0x00016052, 0x00017119, 0x000181e0, + 0x000192a7, 0x0001a36e, 0x0001b435, 0x0001c4fc, + 0x0001d5c3, 0x0001e68a, 0x0001f751, 0x00020818, + 0x000218df, 0x000229a6, 0x00023a6d, 0x00024b34, + 0x00025bfb, 0x00026cc2, 0x00027d89, 0x00028e50, + 0x00029f17, 0x0002afde, 0x0002c0a5, 0x0002d16c, + 0x0002e233, 0x0002f2fa, 0x000303c0, 0x00031487, + 0x0003254e, 0x00033615, 0x000346dc, 0x000357a3, + 0x0003686a, 0x00037931, 0x000389f8, 0x00039abf, + 0x0003ab86, 0x0003bc4d, 0x0003cd14, 0x0003dddb, + 0x0003eea2, 0x0003ff69, 0x00041030, 0x000420f7, + 0x000431be, 0x00044285, 0x0004534c, 0x00046413, + 0x000474da, 0x000485a1, 0x00049668, 0x0004a72f, + 0x0004b7f6, 0x0004c8bd, 0x0004d984, 0x0004ea4b, + 0x0004fb12, 0x00050bd8, 0x00051c9f, 0x00052d66, + 0x00053e2d, 0x00054ef4, 0x00055fbb, 0x00057082, + 0x00058149, 0x00059210, 0x0005a2d7, 0x0005b39e, + 0x0005c465, 0x0005d52c, 0x0005e5f3, 0x0005f6ba, + 0x00060781, 0x00061848, 0x0006290f, 0x000639d6, + 0x00064a9d, 0x00065b64, 0x00066c2b, 0x00067cf2, + 0x00068db9, 0x00069e80, 0x0006af47, 0x0006c00e, + 0x0006d0d5, 0x0006e19c, 0x0006f263, 0x0007032a, + 0x000713f0, 0x000724b7, 0x0007357e, 0x00074645, + 0x0007570c, 0x000767d3, 0x0007789a, 0x00078961, + 0x00079a28, 0x0007aaef, 0x0007bbb6, 0x0007cc7d, + 0x0007dd44, 0x0007ee0b, 0x0007fed2, 0x00080f99, + 0x00082060, 0x00083127, 0x000841ee, 0x000852b5, + 0x0008637c, 0x00087443, 0x0008850a, 0x000895d1, + 0x0008a698, 0x0008b75f, 0x0008c826, 0x0008d8ed, + 0x0008e9b4, 0x0008fa7b, 0x00090b41, 0x00091c08, + 0x00092ccf, 0x00093d96, 0x00094e5d, 0x00095f24, + 0x00096feb, 0x000980b2, 0x00099179, 0x0009a240, + 0x0009b307, 0x0009c3ce, 0x0009d495, 0x0009e55c, + 0x0009f623, 0x000a06ea, 0x000a17b1, 0x000a2878, + 0x000a393f, 0x000a4a06, 0x000a5acd, 0x000a6b94, + 0x000a7c5b, 0x000a8d22, 0x000a9de9, 0x000aaeb0, + 0x000abf77, 0x000ad03e, 0x000ae105, 0x000af1cc, + 0x000b0293, 0x000b1359, 0x000b2420, 0x000b34e7, + 0x000b45ae, 0x000b5675, 0x000b673c, 0x000b7803, + 0x000b88ca, 0x000b9991, 0x000baa58, 0x000bbb1f, + 0x000bcbe6, 0x000bdcad, 0x000bed74, 0x000bfe3b, + 0x000c0f02, 0x000c1fc9, 0x000c3090, 0x000c4157, + 0x000c521e, 0x000c62e5, 0x000c73ac, 0x000c8473, + 0x000c953a, 0x000ca601, 0x000cb6c8, 0x000cc78f, + 0x000cd856, 0x000ce91d, 0x000cf9e4, 0x000d0aaa, + 0x000d1b71, 0x000d2c38, 0x000d3cff, 0x000d4dc6, + 0x000d5e8d, 0x000d6f54, 0x000d801b, 0x000d90e2, + 0x000da1a9, 0x000db270, 0x000dc337, 0x000dd3fe, + 0x000de4c5, 0x000df58c, 0x000e0653, 0x000e171a, + 0x000e27e1, 0x000e38a8, 0x000e496f, 0x000e5a36, + 0x000e6afd, 0x000e7bc4, 0x000e8c8b, 0x000e9d52, + 0x000eae19, 0x000ebee0, 0x000ecfa7, 0x000ee06e, + 0x000ef135, 0x000f01fc, 0x000f12c2, 0x000f2389, + 0x000f3450, 0x000f4517, 0x000f55de, 0x000f66a5, + 0x000f776c, 0x000f8833, 0x000f98fa, 0x000fa9c1, + 0x000fba88, 0x000fcb4f, 0x000fdc16, 0x000fecdd, + 0x000ffda4, 0x00100e6b, 0x00101f32, 0x00102ff9, + 0x001040c0, 0x00105187, 0x0010624e, 0x00107315, + 0x001083dc, 0x001094a3, 0x0010a56a, 0x0010b631 +}; + +const u_int32 ustotsmid[256] = { + 0x00000000, 0x0010c6f8, 0x00218def, 0x003254e7, + 0x00431bdf, 0x0053e2d6, 0x0064a9ce, 0x007570c5, + 0x008637bd, 0x0096feb5, 0x00a7c5ac, 0x00b88ca4, + 0x00c9539c, 0x00da1a93, 0x00eae18b, 0x00fba882, + 0x010c6f7a, 0x011d3672, 0x012dfd69, 0x013ec461, + 0x014f8b59, 0x01605250, 0x01711948, 0x0181e03f, + 0x0192a737, 0x01a36e2f, 0x01b43526, 0x01c4fc1e, + 0x01d5c316, 0x01e68a0d, 0x01f75105, 0x020817fc, + 0x0218def4, 0x0229a5ec, 0x023a6ce3, 0x024b33db, + 0x025bfad3, 0x026cc1ca, 0x027d88c2, 0x028e4fb9, + 0x029f16b1, 0x02afdda9, 0x02c0a4a0, 0x02d16b98, + 0x02e23290, 0x02f2f987, 0x0303c07f, 0x03148777, + 0x03254e6e, 0x03361566, 0x0346dc5d, 0x0357a355, + 0x03686a4d, 0x03793144, 0x0389f83c, 0x039abf34, + 0x03ab862b, 0x03bc4d23, 0x03cd141a, 0x03dddb12, + 0x03eea20a, 0x03ff6901, 0x04102ff9, 0x0420f6f1, + 0x0431bde8, 0x044284e0, 0x04534bd7, 0x046412cf, + 0x0474d9c7, 0x0485a0be, 0x049667b6, 0x04a72eae, + 0x04b7f5a5, 0x04c8bc9d, 0x04d98394, 0x04ea4a8c, + 0x04fb1184, 0x050bd87b, 0x051c9f73, 0x052d666b, + 0x053e2d62, 0x054ef45a, 0x055fbb51, 0x05708249, + 0x05814941, 0x05921038, 0x05a2d730, 0x05b39e28, + 0x05c4651f, 0x05d52c17, 0x05e5f30e, 0x05f6ba06, + 0x060780fe, 0x061847f5, 0x06290eed, 0x0639d5e5, + 0x064a9cdc, 0x065b63d4, 0x066c2acc, 0x067cf1c3, + 0x068db8bb, 0x069e7fb2, 0x06af46aa, 0x06c00da2, + 0x06d0d499, 0x06e19b91, 0x06f26289, 0x07032980, + 0x0713f078, 0x0724b76f, 0x07357e67, 0x0746455f, + 0x07570c56, 0x0767d34e, 0x07789a46, 0x0789613d, + 0x079a2835, 0x07aaef2c, 0x07bbb624, 0x07cc7d1c, + 0x07dd4413, 0x07ee0b0b, 0x07fed203, 0x080f98fa, + 0x08205ff2, 0x083126e9, 0x0841ede1, 0x0852b4d9, + 0x08637bd0, 0x087442c8, 0x088509c0, 0x0895d0b7, + 0x08a697af, 0x08b75ea6, 0x08c8259e, 0x08d8ec96, + 0x08e9b38d, 0x08fa7a85, 0x090b417d, 0x091c0874, + 0x092ccf6c, 0x093d9664, 0x094e5d5b, 0x095f2453, + 0x096feb4a, 0x0980b242, 0x0991793a, 0x09a24031, + 0x09b30729, 0x09c3ce21, 0x09d49518, 0x09e55c10, + 0x09f62307, 0x0a06e9ff, 0x0a17b0f7, 0x0a2877ee, + 0x0a393ee6, 0x0a4a05de, 0x0a5accd5, 0x0a6b93cd, + 0x0a7c5ac4, 0x0a8d21bc, 0x0a9de8b4, 0x0aaeafab, + 0x0abf76a3, 0x0ad03d9b, 0x0ae10492, 0x0af1cb8a, + 0x0b029281, 0x0b135979, 0x0b242071, 0x0b34e768, + 0x0b45ae60, 0x0b567558, 0x0b673c4f, 0x0b780347, + 0x0b88ca3e, 0x0b999136, 0x0baa582e, 0x0bbb1f25, + 0x0bcbe61d, 0x0bdcad15, 0x0bed740c, 0x0bfe3b04, + 0x0c0f01fc, 0x0c1fc8f3, 0x0c308feb, 0x0c4156e2, + 0x0c521dda, 0x0c62e4d2, 0x0c73abc9, 0x0c8472c1, + 0x0c9539b9, 0x0ca600b0, 0x0cb6c7a8, 0x0cc78e9f, + 0x0cd85597, 0x0ce91c8f, 0x0cf9e386, 0x0d0aaa7e, + 0x0d1b7176, 0x0d2c386d, 0x0d3cff65, 0x0d4dc65c, + 0x0d5e8d54, 0x0d6f544c, 0x0d801b43, 0x0d90e23b, + 0x0da1a933, 0x0db2702a, 0x0dc33722, 0x0dd3fe19, + 0x0de4c511, 0x0df58c09, 0x0e065300, 0x0e1719f8, + 0x0e27e0f0, 0x0e38a7e7, 0x0e496edf, 0x0e5a35d6, + 0x0e6afcce, 0x0e7bc3c6, 0x0e8c8abd, 0x0e9d51b5, + 0x0eae18ad, 0x0ebedfa4, 0x0ecfa69c, 0x0ee06d94, + 0x0ef1348b, 0x0f01fb83, 0x0f12c27a, 0x0f238972, + 0x0f34506a, 0x0f451761, 0x0f55de59, 0x0f66a551, + 0x0f776c48, 0x0f883340, 0x0f98fa37, 0x0fa9c12f, + 0x0fba8827, 0x0fcb4f1e, 0x0fdc1616, 0x0fecdd0e, + 0x0ffda405, 0x100e6afd, 0x101f31f4, 0x102ff8ec, + 0x1040bfe4, 0x105186db, 0x10624dd3, 0x107314cb, + 0x1083dbc2, 0x1094a2ba, 0x10a569b1, 0x10b630a9 +}; + +const u_int32 ustotshi[16] = { + 0x00000000, 0x10c6f7a1, 0x218def41, 0x3254e6e2, + 0x431bde83, 0x53e2d624, 0x64a9cdc4, 0x7570c565, + 0x8637bd06, 0x96feb4a6, 0xa7c5ac47, 0xb88ca3e8, + 0xc9539b89, 0xda1a9329, 0xeae18aca, 0xfba8826b +}; /* - * test possibly denormal a - * return -1 / 0 / 1 if a < / == / > 0 + * Tables to convert from a time stamp fraction to usecs. Note that + * the units of these tables are actually (usec<<3). We carry three + * guard bits so that the result can be properly truncated (or rounded) + * to be correct to the least significant bit. + * + * These tables are rounded. */ -int -timeval_test( - const struct timeval *a - ) -{ - struct timeval A; - int r; - - COPYNORM(&A, a); - r = (A.tv_sec > 0) - (A.tv_sec < 0); - if (r == 0) - r = (A.tv_usec > 0); - - return r; -} - -/* return LIB buffer ptr to string rep */ -const char* -timeval_tostr( - const struct timeval *x - ) -{ - return format_time_fraction(x->tv_sec, x->tv_usec, 6); -} - -void -timeval_abstolfp( - l_fp * y, - const struct timeval * x - ) -{ - struct timeval v; - - COPYNORM(&v, x); - MYTVUTOF(v.tv_usec, y->l_uf); - y->l_ui = (u_int32)v.tv_sec + JAN_1970; -} - -void -timeval_reltolfp( - l_fp * y, - const struct timeval * x - ) -{ - struct timeval v; - - COPYNORM(&v, x); - MYTVUTOF(v.tv_usec, y->l_uf); - y->l_i = (int32)v.tv_sec; -} - -void -timeval_relfromlfp( - struct timeval *y, - const l_fp * x - ) -{ - struct timeval out; - l_fp tmp; - int neg; - - tmp = *x; - neg = L_ISNEG(&tmp); - if (neg != 0) - L_NEG(&tmp); - MYFTOTVU(x->l_uf, out.tv_usec); - out.tv_sec = x->l_ui; - if (neg != 0) { - out.tv_sec = -out.tv_sec; - out.tv_usec = -out.tv_usec; - } - COPYNORM(y, &out); -} - -void -timeval_urelfromlfp( - struct timeval *y, - const l_fp * x - ) -{ - struct timeval out; - - MYFTOTVU(x->l_uf, out.tv_usec); - out.tv_sec = x->l_ui; - COPYNORM(y, &out); -} - -void -timeval_absfromlfp( - struct timeval *y, - const l_fp * x, - const time_t * p - ) -{ - struct timeval out; - vint64 sec; - - sec = ntpcal_ntp_to_time(x->l_ui, p); - MYFTOTVU(x->l_uf, out.tv_usec); - /* copying a vint64 to a time_t needs some care... */ -#if SIZEOF_TIME_T == 4 - out.tv_sec = (time_t)sec.d_s.lo; -#elif defined(HAVE_INT64) - out.tv_sec = (time_t)sec.q_s; -#else - out.tv_sec = ((time_t)sec.d_s.hi << 32) + sec.d_s.lo; -#endif - - COPYNORM(y, &out); -} +const u_int32 tstoushi[256] = { + 0x000000, 0x007a12, 0x00f424, 0x016e36, + 0x01e848, 0x02625a, 0x02dc6c, 0x03567e, + 0x03d090, 0x044aa2, 0x04c4b4, 0x053ec6, + 0x05b8d8, 0x0632ea, 0x06acfc, 0x07270e, + 0x07a120, 0x081b32, 0x089544, 0x090f56, + 0x098968, 0x0a037a, 0x0a7d8c, 0x0af79e, + 0x0b71b0, 0x0bebc2, 0x0c65d4, 0x0cdfe6, + 0x0d59f8, 0x0dd40a, 0x0e4e1c, 0x0ec82e, + 0x0f4240, 0x0fbc52, 0x103664, 0x10b076, + 0x112a88, 0x11a49a, 0x121eac, 0x1298be, + 0x1312d0, 0x138ce2, 0x1406f4, 0x148106, + 0x14fb18, 0x15752a, 0x15ef3c, 0x16694e, + 0x16e360, 0x175d72, 0x17d784, 0x185196, + 0x18cba8, 0x1945ba, 0x19bfcc, 0x1a39de, + 0x1ab3f0, 0x1b2e02, 0x1ba814, 0x1c2226, + 0x1c9c38, 0x1d164a, 0x1d905c, 0x1e0a6e, + 0x1e8480, 0x1efe92, 0x1f78a4, 0x1ff2b6, + 0x206cc8, 0x20e6da, 0x2160ec, 0x21dafe, + 0x225510, 0x22cf22, 0x234934, 0x23c346, + 0x243d58, 0x24b76a, 0x25317c, 0x25ab8e, + 0x2625a0, 0x269fb2, 0x2719c4, 0x2793d6, + 0x280de8, 0x2887fa, 0x29020c, 0x297c1e, + 0x29f630, 0x2a7042, 0x2aea54, 0x2b6466, + 0x2bde78, 0x2c588a, 0x2cd29c, 0x2d4cae, + 0x2dc6c0, 0x2e40d2, 0x2ebae4, 0x2f34f6, + 0x2faf08, 0x30291a, 0x30a32c, 0x311d3e, + 0x319750, 0x321162, 0x328b74, 0x330586, + 0x337f98, 0x33f9aa, 0x3473bc, 0x34edce, + 0x3567e0, 0x35e1f2, 0x365c04, 0x36d616, + 0x375028, 0x37ca3a, 0x38444c, 0x38be5e, + 0x393870, 0x39b282, 0x3a2c94, 0x3aa6a6, + 0x3b20b8, 0x3b9aca, 0x3c14dc, 0x3c8eee, + 0x3d0900, 0x3d8312, 0x3dfd24, 0x3e7736, + 0x3ef148, 0x3f6b5a, 0x3fe56c, 0x405f7e, + 0x40d990, 0x4153a2, 0x41cdb4, 0x4247c6, + 0x42c1d8, 0x433bea, 0x43b5fc, 0x44300e, + 0x44aa20, 0x452432, 0x459e44, 0x461856, + 0x469268, 0x470c7a, 0x47868c, 0x48009e, + 0x487ab0, 0x48f4c2, 0x496ed4, 0x49e8e6, + 0x4a62f8, 0x4add0a, 0x4b571c, 0x4bd12e, + 0x4c4b40, 0x4cc552, 0x4d3f64, 0x4db976, + 0x4e3388, 0x4ead9a, 0x4f27ac, 0x4fa1be, + 0x501bd0, 0x5095e2, 0x510ff4, 0x518a06, + 0x520418, 0x527e2a, 0x52f83c, 0x53724e, + 0x53ec60, 0x546672, 0x54e084, 0x555a96, + 0x55d4a8, 0x564eba, 0x56c8cc, 0x5742de, + 0x57bcf0, 0x583702, 0x58b114, 0x592b26, + 0x59a538, 0x5a1f4a, 0x5a995c, 0x5b136e, + 0x5b8d80, 0x5c0792, 0x5c81a4, 0x5cfbb6, + 0x5d75c8, 0x5defda, 0x5e69ec, 0x5ee3fe, + 0x5f5e10, 0x5fd822, 0x605234, 0x60cc46, + 0x614658, 0x61c06a, 0x623a7c, 0x62b48e, + 0x632ea0, 0x63a8b2, 0x6422c4, 0x649cd6, + 0x6516e8, 0x6590fa, 0x660b0c, 0x66851e, + 0x66ff30, 0x677942, 0x67f354, 0x686d66, + 0x68e778, 0x69618a, 0x69db9c, 0x6a55ae, + 0x6acfc0, 0x6b49d2, 0x6bc3e4, 0x6c3df6, + 0x6cb808, 0x6d321a, 0x6dac2c, 0x6e263e, + 0x6ea050, 0x6f1a62, 0x6f9474, 0x700e86, + 0x708898, 0x7102aa, 0x717cbc, 0x71f6ce, + 0x7270e0, 0x72eaf2, 0x736504, 0x73df16, + 0x745928, 0x74d33a, 0x754d4c, 0x75c75e, + 0x764170, 0x76bb82, 0x773594, 0x77afa6, + 0x7829b8, 0x78a3ca, 0x791ddc, 0x7997ee +}; + +const u_int32 tstousmid[256] = { + 0x0000, 0x007a, 0x00f4, 0x016e, 0x01e8, 0x0262, 0x02dc, 0x0356, + 0x03d1, 0x044b, 0x04c5, 0x053f, 0x05b9, 0x0633, 0x06ad, 0x0727, + 0x07a1, 0x081b, 0x0895, 0x090f, 0x0989, 0x0a03, 0x0a7e, 0x0af8, + 0x0b72, 0x0bec, 0x0c66, 0x0ce0, 0x0d5a, 0x0dd4, 0x0e4e, 0x0ec8, + 0x0f42, 0x0fbc, 0x1036, 0x10b0, 0x112b, 0x11a5, 0x121f, 0x1299, + 0x1313, 0x138d, 0x1407, 0x1481, 0x14fb, 0x1575, 0x15ef, 0x1669, + 0x16e3, 0x175d, 0x17d8, 0x1852, 0x18cc, 0x1946, 0x19c0, 0x1a3a, + 0x1ab4, 0x1b2e, 0x1ba8, 0x1c22, 0x1c9c, 0x1d16, 0x1d90, 0x1e0a, + 0x1e84, 0x1eff, 0x1f79, 0x1ff3, 0x206d, 0x20e7, 0x2161, 0x21db, + 0x2255, 0x22cf, 0x2349, 0x23c3, 0x243d, 0x24b7, 0x2531, 0x25ac, + 0x2626, 0x26a0, 0x271a, 0x2794, 0x280e, 0x2888, 0x2902, 0x297c, + 0x29f6, 0x2a70, 0x2aea, 0x2b64, 0x2bde, 0x2c59, 0x2cd3, 0x2d4d, + 0x2dc7, 0x2e41, 0x2ebb, 0x2f35, 0x2faf, 0x3029, 0x30a3, 0x311d, + 0x3197, 0x3211, 0x328b, 0x3306, 0x3380, 0x33fa, 0x3474, 0x34ee, + 0x3568, 0x35e2, 0x365c, 0x36d6, 0x3750, 0x37ca, 0x3844, 0x38be, + 0x3938, 0x39b3, 0x3a2d, 0x3aa7, 0x3b21, 0x3b9b, 0x3c15, 0x3c8f, + 0x3d09, 0x3d83, 0x3dfd, 0x3e77, 0x3ef1, 0x3f6b, 0x3fe5, 0x405f, + 0x40da, 0x4154, 0x41ce, 0x4248, 0x42c2, 0x433c, 0x43b6, 0x4430, + 0x44aa, 0x4524, 0x459e, 0x4618, 0x4692, 0x470c, 0x4787, 0x4801, + 0x487b, 0x48f5, 0x496f, 0x49e9, 0x4a63, 0x4add, 0x4b57, 0x4bd1, + 0x4c4b, 0x4cc5, 0x4d3f, 0x4db9, 0x4e34, 0x4eae, 0x4f28, 0x4fa2, + 0x501c, 0x5096, 0x5110, 0x518a, 0x5204, 0x527e, 0x52f8, 0x5372, + 0x53ec, 0x5466, 0x54e1, 0x555b, 0x55d5, 0x564f, 0x56c9, 0x5743, + 0x57bd, 0x5837, 0x58b1, 0x592b, 0x59a5, 0x5a1f, 0x5a99, 0x5b13, + 0x5b8d, 0x5c08, 0x5c82, 0x5cfc, 0x5d76, 0x5df0, 0x5e6a, 0x5ee4, + 0x5f5e, 0x5fd8, 0x6052, 0x60cc, 0x6146, 0x61c0, 0x623a, 0x62b5, + 0x632f, 0x63a9, 0x6423, 0x649d, 0x6517, 0x6591, 0x660b, 0x6685, + 0x66ff, 0x6779, 0x67f3, 0x686d, 0x68e7, 0x6962, 0x69dc, 0x6a56, + 0x6ad0, 0x6b4a, 0x6bc4, 0x6c3e, 0x6cb8, 0x6d32, 0x6dac, 0x6e26, + 0x6ea0, 0x6f1a, 0x6f94, 0x700f, 0x7089, 0x7103, 0x717d, 0x71f7, + 0x7271, 0x72eb, 0x7365, 0x73df, 0x7459, 0x74d3, 0x754d, 0x75c7, + 0x7641, 0x76bc, 0x7736, 0x77b0, 0x782a, 0x78a4, 0x791e, 0x7998 +}; + +const u_int32 tstouslo[128] = { + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0a, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, + 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, + 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, + 0x1f, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, + 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, + 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x33, 0x34, + 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, + 0x3d, 0x3e, 0x3f, 0x40, 0x41, 0x42, 0x43, 0x44, + 0x45, 0x46, 0x47, 0x48, 0x48, 0x49, 0x4a, 0x4b, + 0x4c, 0x4d, 0x4e, 0x4f, 0x50, 0x51, 0x52, 0x53, + 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x5b, + 0x5c, 0x5c, 0x5d, 0x5e, 0x5f, 0x60, 0x61, 0x62, + 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, + 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x71, + 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79 +}; +#else /* !USE_TSF_USEC_TABLES follows */ +NONEMPTY_TRANSLATION_UNIT +#endif /* !USE_TSF_USEC_TABLES */ /* -*- EOF -*- */ diff --git a/libntp/tsftomsu.c b/libntp/tsftomsu.c deleted file mode 100644 index 2a8c15022d..0000000000 --- a/libntp/tsftomsu.c +++ /dev/null @@ -1,42 +0,0 @@ -/* - * tsftomsu - convert from a time stamp fraction to milliseconds - */ -#include -#include "ntp_fp.h" -#include "ntp_stdlib.h" - -int -tsftomsu( - u_long tsf, - int round_it - ) -{ - long val_ui; - long val_uf; - long tmp_ui; - long tmp_uf; - int i; - - /* - * Essentially, multiply by 10 three times in l_fp form. - * The integral part is the milliseconds. - */ - val_ui = 0; - val_uf = tsf; - for (i = 3; i > 0; i--) { - M_LSHIFT(val_ui, val_uf); - tmp_ui = val_ui; - tmp_uf = val_uf; - M_LSHIFT(val_ui, val_uf); - M_LSHIFT(val_ui, val_uf); - M_ADD(val_ui, val_uf, tmp_ui, tmp_uf); - } - - /* - * Round the value if need be, then return it. - */ - if (round_it && (0x80000000 & val_uf)) - val_ui++; - - return (int)val_ui; -} diff --git a/libntp/tstotv.c b/libntp/tstotv.c deleted file mode 100644 index 664bf127bc..0000000000 --- a/libntp/tstotv.c +++ /dev/null @@ -1,136 +0,0 @@ -/* - * tstotv - tables for converting from NTP time stamps to struct timeval - */ - -#include -#include "ntp_types.h" - -/* - * Tables to convert from a time stamp fraction to usecs. Note that - * the units of these tables are actually (usec<<3). We carry three - * guard bits so that the result can be properly truncated (or rounded) - * to be correct to the least significant bit. - * - * These tables are rounded. - */ - -const u_int32 tstoushi[256] = { - 0x000000, 0x007a12, 0x00f424, 0x016e36, - 0x01e848, 0x02625a, 0x02dc6c, 0x03567e, - 0x03d090, 0x044aa2, 0x04c4b4, 0x053ec6, - 0x05b8d8, 0x0632ea, 0x06acfc, 0x07270e, - 0x07a120, 0x081b32, 0x089544, 0x090f56, - 0x098968, 0x0a037a, 0x0a7d8c, 0x0af79e, - 0x0b71b0, 0x0bebc2, 0x0c65d4, 0x0cdfe6, - 0x0d59f8, 0x0dd40a, 0x0e4e1c, 0x0ec82e, - 0x0f4240, 0x0fbc52, 0x103664, 0x10b076, - 0x112a88, 0x11a49a, 0x121eac, 0x1298be, - 0x1312d0, 0x138ce2, 0x1406f4, 0x148106, - 0x14fb18, 0x15752a, 0x15ef3c, 0x16694e, - 0x16e360, 0x175d72, 0x17d784, 0x185196, - 0x18cba8, 0x1945ba, 0x19bfcc, 0x1a39de, - 0x1ab3f0, 0x1b2e02, 0x1ba814, 0x1c2226, - 0x1c9c38, 0x1d164a, 0x1d905c, 0x1e0a6e, - 0x1e8480, 0x1efe92, 0x1f78a4, 0x1ff2b6, - 0x206cc8, 0x20e6da, 0x2160ec, 0x21dafe, - 0x225510, 0x22cf22, 0x234934, 0x23c346, - 0x243d58, 0x24b76a, 0x25317c, 0x25ab8e, - 0x2625a0, 0x269fb2, 0x2719c4, 0x2793d6, - 0x280de8, 0x2887fa, 0x29020c, 0x297c1e, - 0x29f630, 0x2a7042, 0x2aea54, 0x2b6466, - 0x2bde78, 0x2c588a, 0x2cd29c, 0x2d4cae, - 0x2dc6c0, 0x2e40d2, 0x2ebae4, 0x2f34f6, - 0x2faf08, 0x30291a, 0x30a32c, 0x311d3e, - 0x319750, 0x321162, 0x328b74, 0x330586, - 0x337f98, 0x33f9aa, 0x3473bc, 0x34edce, - 0x3567e0, 0x35e1f2, 0x365c04, 0x36d616, - 0x375028, 0x37ca3a, 0x38444c, 0x38be5e, - 0x393870, 0x39b282, 0x3a2c94, 0x3aa6a6, - 0x3b20b8, 0x3b9aca, 0x3c14dc, 0x3c8eee, - 0x3d0900, 0x3d8312, 0x3dfd24, 0x3e7736, - 0x3ef148, 0x3f6b5a, 0x3fe56c, 0x405f7e, - 0x40d990, 0x4153a2, 0x41cdb4, 0x4247c6, - 0x42c1d8, 0x433bea, 0x43b5fc, 0x44300e, - 0x44aa20, 0x452432, 0x459e44, 0x461856, - 0x469268, 0x470c7a, 0x47868c, 0x48009e, - 0x487ab0, 0x48f4c2, 0x496ed4, 0x49e8e6, - 0x4a62f8, 0x4add0a, 0x4b571c, 0x4bd12e, - 0x4c4b40, 0x4cc552, 0x4d3f64, 0x4db976, - 0x4e3388, 0x4ead9a, 0x4f27ac, 0x4fa1be, - 0x501bd0, 0x5095e2, 0x510ff4, 0x518a06, - 0x520418, 0x527e2a, 0x52f83c, 0x53724e, - 0x53ec60, 0x546672, 0x54e084, 0x555a96, - 0x55d4a8, 0x564eba, 0x56c8cc, 0x5742de, - 0x57bcf0, 0x583702, 0x58b114, 0x592b26, - 0x59a538, 0x5a1f4a, 0x5a995c, 0x5b136e, - 0x5b8d80, 0x5c0792, 0x5c81a4, 0x5cfbb6, - 0x5d75c8, 0x5defda, 0x5e69ec, 0x5ee3fe, - 0x5f5e10, 0x5fd822, 0x605234, 0x60cc46, - 0x614658, 0x61c06a, 0x623a7c, 0x62b48e, - 0x632ea0, 0x63a8b2, 0x6422c4, 0x649cd6, - 0x6516e8, 0x6590fa, 0x660b0c, 0x66851e, - 0x66ff30, 0x677942, 0x67f354, 0x686d66, - 0x68e778, 0x69618a, 0x69db9c, 0x6a55ae, - 0x6acfc0, 0x6b49d2, 0x6bc3e4, 0x6c3df6, - 0x6cb808, 0x6d321a, 0x6dac2c, 0x6e263e, - 0x6ea050, 0x6f1a62, 0x6f9474, 0x700e86, - 0x708898, 0x7102aa, 0x717cbc, 0x71f6ce, - 0x7270e0, 0x72eaf2, 0x736504, 0x73df16, - 0x745928, 0x74d33a, 0x754d4c, 0x75c75e, - 0x764170, 0x76bb82, 0x773594, 0x77afa6, - 0x7829b8, 0x78a3ca, 0x791ddc, 0x7997ee -}; - -const u_int32 tstousmid[256] = { - 0x0000, 0x007a, 0x00f4, 0x016e, 0x01e8, 0x0262, 0x02dc, 0x0356, - 0x03d1, 0x044b, 0x04c5, 0x053f, 0x05b9, 0x0633, 0x06ad, 0x0727, - 0x07a1, 0x081b, 0x0895, 0x090f, 0x0989, 0x0a03, 0x0a7e, 0x0af8, - 0x0b72, 0x0bec, 0x0c66, 0x0ce0, 0x0d5a, 0x0dd4, 0x0e4e, 0x0ec8, - 0x0f42, 0x0fbc, 0x1036, 0x10b0, 0x112b, 0x11a5, 0x121f, 0x1299, - 0x1313, 0x138d, 0x1407, 0x1481, 0x14fb, 0x1575, 0x15ef, 0x1669, - 0x16e3, 0x175d, 0x17d8, 0x1852, 0x18cc, 0x1946, 0x19c0, 0x1a3a, - 0x1ab4, 0x1b2e, 0x1ba8, 0x1c22, 0x1c9c, 0x1d16, 0x1d90, 0x1e0a, - 0x1e84, 0x1eff, 0x1f79, 0x1ff3, 0x206d, 0x20e7, 0x2161, 0x21db, - 0x2255, 0x22cf, 0x2349, 0x23c3, 0x243d, 0x24b7, 0x2531, 0x25ac, - 0x2626, 0x26a0, 0x271a, 0x2794, 0x280e, 0x2888, 0x2902, 0x297c, - 0x29f6, 0x2a70, 0x2aea, 0x2b64, 0x2bde, 0x2c59, 0x2cd3, 0x2d4d, - 0x2dc7, 0x2e41, 0x2ebb, 0x2f35, 0x2faf, 0x3029, 0x30a3, 0x311d, - 0x3197, 0x3211, 0x328b, 0x3306, 0x3380, 0x33fa, 0x3474, 0x34ee, - 0x3568, 0x35e2, 0x365c, 0x36d6, 0x3750, 0x37ca, 0x3844, 0x38be, - 0x3938, 0x39b3, 0x3a2d, 0x3aa7, 0x3b21, 0x3b9b, 0x3c15, 0x3c8f, - 0x3d09, 0x3d83, 0x3dfd, 0x3e77, 0x3ef1, 0x3f6b, 0x3fe5, 0x405f, - 0x40da, 0x4154, 0x41ce, 0x4248, 0x42c2, 0x433c, 0x43b6, 0x4430, - 0x44aa, 0x4524, 0x459e, 0x4618, 0x4692, 0x470c, 0x4787, 0x4801, - 0x487b, 0x48f5, 0x496f, 0x49e9, 0x4a63, 0x4add, 0x4b57, 0x4bd1, - 0x4c4b, 0x4cc5, 0x4d3f, 0x4db9, 0x4e34, 0x4eae, 0x4f28, 0x4fa2, - 0x501c, 0x5096, 0x5110, 0x518a, 0x5204, 0x527e, 0x52f8, 0x5372, - 0x53ec, 0x5466, 0x54e1, 0x555b, 0x55d5, 0x564f, 0x56c9, 0x5743, - 0x57bd, 0x5837, 0x58b1, 0x592b, 0x59a5, 0x5a1f, 0x5a99, 0x5b13, - 0x5b8d, 0x5c08, 0x5c82, 0x5cfc, 0x5d76, 0x5df0, 0x5e6a, 0x5ee4, - 0x5f5e, 0x5fd8, 0x6052, 0x60cc, 0x6146, 0x61c0, 0x623a, 0x62b5, - 0x632f, 0x63a9, 0x6423, 0x649d, 0x6517, 0x6591, 0x660b, 0x6685, - 0x66ff, 0x6779, 0x67f3, 0x686d, 0x68e7, 0x6962, 0x69dc, 0x6a56, - 0x6ad0, 0x6b4a, 0x6bc4, 0x6c3e, 0x6cb8, 0x6d32, 0x6dac, 0x6e26, - 0x6ea0, 0x6f1a, 0x6f94, 0x700f, 0x7089, 0x7103, 0x717d, 0x71f7, - 0x7271, 0x72eb, 0x7365, 0x73df, 0x7459, 0x74d3, 0x754d, 0x75c7, - 0x7641, 0x76bc, 0x7736, 0x77b0, 0x782a, 0x78a4, 0x791e, 0x7998 -}; - -const u_int32 tstouslo[128] = { - 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, - 0x08, 0x09, 0x0a, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, - 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, - 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, - 0x1f, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, - 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, - 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x33, 0x34, - 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, - 0x3d, 0x3e, 0x3f, 0x40, 0x41, 0x42, 0x43, 0x44, - 0x45, 0x46, 0x47, 0x48, 0x48, 0x49, 0x4a, 0x4b, - 0x4c, 0x4d, 0x4e, 0x4f, 0x50, 0x51, 0x52, 0x53, - 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x5b, - 0x5c, 0x5c, 0x5d, 0x5e, 0x5f, 0x60, 0x61, 0x62, - 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, - 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x71, - 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79 -}; diff --git a/libntp/tvtots.c b/libntp/tvtots.c deleted file mode 100644 index 550c648e70..0000000000 --- a/libntp/tvtots.c +++ /dev/null @@ -1,160 +0,0 @@ -/* - * tvtots - tables for converting from Unix struct timeval's to - * NTP time stamp format. - */ -#include -#include - -#include "ntp_types.h" - -/* - * Tables to calculate time stamp fractions from usecs. The entries - * in these tables are offset into using each of the two low order - * bytes plus the next 4 bits in a usec value (from a struct timeval). - * These are summed to produce the time stamp fraction. - * - * Note that these tables are rounded (not truncated) to the nearest - * low order bit in the fraction. The timestamp computed should be - * +- 1.5 low order bits. - */ - -const u_int32 ustotslo[256] = { - 0x00000000, 0x000010c7, 0x0000218e, 0x00003255, - 0x0000431c, 0x000053e3, 0x000064aa, 0x00007571, - 0x00008638, 0x000096ff, 0x0000a7c6, 0x0000b88d, - 0x0000c954, 0x0000da1b, 0x0000eae2, 0x0000fba9, - 0x00010c6f, 0x00011d36, 0x00012dfd, 0x00013ec4, - 0x00014f8b, 0x00016052, 0x00017119, 0x000181e0, - 0x000192a7, 0x0001a36e, 0x0001b435, 0x0001c4fc, - 0x0001d5c3, 0x0001e68a, 0x0001f751, 0x00020818, - 0x000218df, 0x000229a6, 0x00023a6d, 0x00024b34, - 0x00025bfb, 0x00026cc2, 0x00027d89, 0x00028e50, - 0x00029f17, 0x0002afde, 0x0002c0a5, 0x0002d16c, - 0x0002e233, 0x0002f2fa, 0x000303c0, 0x00031487, - 0x0003254e, 0x00033615, 0x000346dc, 0x000357a3, - 0x0003686a, 0x00037931, 0x000389f8, 0x00039abf, - 0x0003ab86, 0x0003bc4d, 0x0003cd14, 0x0003dddb, - 0x0003eea2, 0x0003ff69, 0x00041030, 0x000420f7, - 0x000431be, 0x00044285, 0x0004534c, 0x00046413, - 0x000474da, 0x000485a1, 0x00049668, 0x0004a72f, - 0x0004b7f6, 0x0004c8bd, 0x0004d984, 0x0004ea4b, - 0x0004fb12, 0x00050bd8, 0x00051c9f, 0x00052d66, - 0x00053e2d, 0x00054ef4, 0x00055fbb, 0x00057082, - 0x00058149, 0x00059210, 0x0005a2d7, 0x0005b39e, - 0x0005c465, 0x0005d52c, 0x0005e5f3, 0x0005f6ba, - 0x00060781, 0x00061848, 0x0006290f, 0x000639d6, - 0x00064a9d, 0x00065b64, 0x00066c2b, 0x00067cf2, - 0x00068db9, 0x00069e80, 0x0006af47, 0x0006c00e, - 0x0006d0d5, 0x0006e19c, 0x0006f263, 0x0007032a, - 0x000713f0, 0x000724b7, 0x0007357e, 0x00074645, - 0x0007570c, 0x000767d3, 0x0007789a, 0x00078961, - 0x00079a28, 0x0007aaef, 0x0007bbb6, 0x0007cc7d, - 0x0007dd44, 0x0007ee0b, 0x0007fed2, 0x00080f99, - 0x00082060, 0x00083127, 0x000841ee, 0x000852b5, - 0x0008637c, 0x00087443, 0x0008850a, 0x000895d1, - 0x0008a698, 0x0008b75f, 0x0008c826, 0x0008d8ed, - 0x0008e9b4, 0x0008fa7b, 0x00090b41, 0x00091c08, - 0x00092ccf, 0x00093d96, 0x00094e5d, 0x00095f24, - 0x00096feb, 0x000980b2, 0x00099179, 0x0009a240, - 0x0009b307, 0x0009c3ce, 0x0009d495, 0x0009e55c, - 0x0009f623, 0x000a06ea, 0x000a17b1, 0x000a2878, - 0x000a393f, 0x000a4a06, 0x000a5acd, 0x000a6b94, - 0x000a7c5b, 0x000a8d22, 0x000a9de9, 0x000aaeb0, - 0x000abf77, 0x000ad03e, 0x000ae105, 0x000af1cc, - 0x000b0293, 0x000b1359, 0x000b2420, 0x000b34e7, - 0x000b45ae, 0x000b5675, 0x000b673c, 0x000b7803, - 0x000b88ca, 0x000b9991, 0x000baa58, 0x000bbb1f, - 0x000bcbe6, 0x000bdcad, 0x000bed74, 0x000bfe3b, - 0x000c0f02, 0x000c1fc9, 0x000c3090, 0x000c4157, - 0x000c521e, 0x000c62e5, 0x000c73ac, 0x000c8473, - 0x000c953a, 0x000ca601, 0x000cb6c8, 0x000cc78f, - 0x000cd856, 0x000ce91d, 0x000cf9e4, 0x000d0aaa, - 0x000d1b71, 0x000d2c38, 0x000d3cff, 0x000d4dc6, - 0x000d5e8d, 0x000d6f54, 0x000d801b, 0x000d90e2, - 0x000da1a9, 0x000db270, 0x000dc337, 0x000dd3fe, - 0x000de4c5, 0x000df58c, 0x000e0653, 0x000e171a, - 0x000e27e1, 0x000e38a8, 0x000e496f, 0x000e5a36, - 0x000e6afd, 0x000e7bc4, 0x000e8c8b, 0x000e9d52, - 0x000eae19, 0x000ebee0, 0x000ecfa7, 0x000ee06e, - 0x000ef135, 0x000f01fc, 0x000f12c2, 0x000f2389, - 0x000f3450, 0x000f4517, 0x000f55de, 0x000f66a5, - 0x000f776c, 0x000f8833, 0x000f98fa, 0x000fa9c1, - 0x000fba88, 0x000fcb4f, 0x000fdc16, 0x000fecdd, - 0x000ffda4, 0x00100e6b, 0x00101f32, 0x00102ff9, - 0x001040c0, 0x00105187, 0x0010624e, 0x00107315, - 0x001083dc, 0x001094a3, 0x0010a56a, 0x0010b631 -}; - -const u_int32 ustotsmid[256] = { - 0x00000000, 0x0010c6f8, 0x00218def, 0x003254e7, - 0x00431bdf, 0x0053e2d6, 0x0064a9ce, 0x007570c5, - 0x008637bd, 0x0096feb5, 0x00a7c5ac, 0x00b88ca4, - 0x00c9539c, 0x00da1a93, 0x00eae18b, 0x00fba882, - 0x010c6f7a, 0x011d3672, 0x012dfd69, 0x013ec461, - 0x014f8b59, 0x01605250, 0x01711948, 0x0181e03f, - 0x0192a737, 0x01a36e2f, 0x01b43526, 0x01c4fc1e, - 0x01d5c316, 0x01e68a0d, 0x01f75105, 0x020817fc, - 0x0218def4, 0x0229a5ec, 0x023a6ce3, 0x024b33db, - 0x025bfad3, 0x026cc1ca, 0x027d88c2, 0x028e4fb9, - 0x029f16b1, 0x02afdda9, 0x02c0a4a0, 0x02d16b98, - 0x02e23290, 0x02f2f987, 0x0303c07f, 0x03148777, - 0x03254e6e, 0x03361566, 0x0346dc5d, 0x0357a355, - 0x03686a4d, 0x03793144, 0x0389f83c, 0x039abf34, - 0x03ab862b, 0x03bc4d23, 0x03cd141a, 0x03dddb12, - 0x03eea20a, 0x03ff6901, 0x04102ff9, 0x0420f6f1, - 0x0431bde8, 0x044284e0, 0x04534bd7, 0x046412cf, - 0x0474d9c7, 0x0485a0be, 0x049667b6, 0x04a72eae, - 0x04b7f5a5, 0x04c8bc9d, 0x04d98394, 0x04ea4a8c, - 0x04fb1184, 0x050bd87b, 0x051c9f73, 0x052d666b, - 0x053e2d62, 0x054ef45a, 0x055fbb51, 0x05708249, - 0x05814941, 0x05921038, 0x05a2d730, 0x05b39e28, - 0x05c4651f, 0x05d52c17, 0x05e5f30e, 0x05f6ba06, - 0x060780fe, 0x061847f5, 0x06290eed, 0x0639d5e5, - 0x064a9cdc, 0x065b63d4, 0x066c2acc, 0x067cf1c3, - 0x068db8bb, 0x069e7fb2, 0x06af46aa, 0x06c00da2, - 0x06d0d499, 0x06e19b91, 0x06f26289, 0x07032980, - 0x0713f078, 0x0724b76f, 0x07357e67, 0x0746455f, - 0x07570c56, 0x0767d34e, 0x07789a46, 0x0789613d, - 0x079a2835, 0x07aaef2c, 0x07bbb624, 0x07cc7d1c, - 0x07dd4413, 0x07ee0b0b, 0x07fed203, 0x080f98fa, - 0x08205ff2, 0x083126e9, 0x0841ede1, 0x0852b4d9, - 0x08637bd0, 0x087442c8, 0x088509c0, 0x0895d0b7, - 0x08a697af, 0x08b75ea6, 0x08c8259e, 0x08d8ec96, - 0x08e9b38d, 0x08fa7a85, 0x090b417d, 0x091c0874, - 0x092ccf6c, 0x093d9664, 0x094e5d5b, 0x095f2453, - 0x096feb4a, 0x0980b242, 0x0991793a, 0x09a24031, - 0x09b30729, 0x09c3ce21, 0x09d49518, 0x09e55c10, - 0x09f62307, 0x0a06e9ff, 0x0a17b0f7, 0x0a2877ee, - 0x0a393ee6, 0x0a4a05de, 0x0a5accd5, 0x0a6b93cd, - 0x0a7c5ac4, 0x0a8d21bc, 0x0a9de8b4, 0x0aaeafab, - 0x0abf76a3, 0x0ad03d9b, 0x0ae10492, 0x0af1cb8a, - 0x0b029281, 0x0b135979, 0x0b242071, 0x0b34e768, - 0x0b45ae60, 0x0b567558, 0x0b673c4f, 0x0b780347, - 0x0b88ca3e, 0x0b999136, 0x0baa582e, 0x0bbb1f25, - 0x0bcbe61d, 0x0bdcad15, 0x0bed740c, 0x0bfe3b04, - 0x0c0f01fc, 0x0c1fc8f3, 0x0c308feb, 0x0c4156e2, - 0x0c521dda, 0x0c62e4d2, 0x0c73abc9, 0x0c8472c1, - 0x0c9539b9, 0x0ca600b0, 0x0cb6c7a8, 0x0cc78e9f, - 0x0cd85597, 0x0ce91c8f, 0x0cf9e386, 0x0d0aaa7e, - 0x0d1b7176, 0x0d2c386d, 0x0d3cff65, 0x0d4dc65c, - 0x0d5e8d54, 0x0d6f544c, 0x0d801b43, 0x0d90e23b, - 0x0da1a933, 0x0db2702a, 0x0dc33722, 0x0dd3fe19, - 0x0de4c511, 0x0df58c09, 0x0e065300, 0x0e1719f8, - 0x0e27e0f0, 0x0e38a7e7, 0x0e496edf, 0x0e5a35d6, - 0x0e6afcce, 0x0e7bc3c6, 0x0e8c8abd, 0x0e9d51b5, - 0x0eae18ad, 0x0ebedfa4, 0x0ecfa69c, 0x0ee06d94, - 0x0ef1348b, 0x0f01fb83, 0x0f12c27a, 0x0f238972, - 0x0f34506a, 0x0f451761, 0x0f55de59, 0x0f66a551, - 0x0f776c48, 0x0f883340, 0x0f98fa37, 0x0fa9c12f, - 0x0fba8827, 0x0fcb4f1e, 0x0fdc1616, 0x0fecdd0e, - 0x0ffda405, 0x100e6afd, 0x101f31f4, 0x102ff8ec, - 0x1040bfe4, 0x105186db, 0x10624dd3, 0x107314cb, - 0x1083dbc2, 0x1094a2ba, 0x10a569b1, 0x10b630a9 -}; - -const u_int32 ustotshi[16] = { - 0x00000000, 0x10c6f7a1, 0x218def41, 0x3254e6e2, - 0x431bde83, 0x53e2d624, 0x64a9cdc4, 0x7570c565, - 0x8637bd06, 0x96feb4a6, 0xa7c5ac47, 0xb88ca3e8, - 0xc9539b89, 0xda1a9329, 0xeae18aca, 0xfba8826b -}; diff --git a/libntp/work_thread.c b/libntp/work_thread.c index 22f92e7141..6c3e6656bc 100644 --- a/libntp/work_thread.c +++ b/libntp/work_thread.c @@ -603,7 +603,7 @@ wait_for_sem( msec = INFINITE; } else { getclock(TIMEOFDAY, &now); - timespec_sub(&delta, timeout, &now); + delta = sub_tspec(*timeout, now); if (delta.tv_sec < 0) { msec = 0; } else if ((delta.tv_sec + 1) >= (MAXDWORD / 1000)) { diff --git a/libparse/clk_trimtsip.c b/libparse/clk_trimtsip.c index 58b0f87e35..743e39c3bc 100644 --- a/libparse/clk_trimtsip.c +++ b/libparse/clk_trimtsip.c @@ -44,7 +44,7 @@ #include "ntp_syslog.h" #include "ntp_types.h" #include "ntp_fp.h" -#include "ntp_unixtime.h" +#include "timevalops.h" #include "ntp_calendar.h" #include "ntp_machine.h" #include "ntp_stdlib.h" diff --git a/libparse/parse.c b/libparse/parse.c index a751bdfbb5..e9e3a9c942 100644 --- a/libparse/parse.c +++ b/libparse/parse.c @@ -51,7 +51,7 @@ static char rcsid[] = "parse.c,v 4.20 2005/08/06 17:39:40 kardel RELEASE_2005080 #endif #include "ntp_fp.h" -#include "ntp_unixtime.h" +#include "timevalops.h" #include "ntp_calendar.h" #include "ntp_stdlib.h" #include "ntp_machine.h" diff --git a/ntpd/ntp_io.c b/ntpd/ntp_io.c index d8537be6ff..801ce1cc7b 100644 --- a/ntpd/ntp_io.c +++ b/ntpd/ntp_io.c @@ -31,8 +31,8 @@ #include "ntp_stdlib.h" #include "ntp_worker.h" #include "ntp_request.h" -#include "ntp_unixtime.h" #include "ntp_assert.h" +#include "timevalops.h" #include "ntpd-opts.h" /* Don't include ISC's version of IPv6 variables and structures */ @@ -3187,7 +3187,7 @@ fetch_timestamp( } DPRINTF(4, ("fetch_timestamp: system network time stamp: %ld.%06ld\n", tvp->tv_sec, tvp->tv_usec)); - timeval_abstolfp(&nts, tvp); + nts = tval_stamp_to_lfp(*tvp); fuzz = ntp_random() * 2. / FRAC * sys_fuzz; DTOLFP(fuzz, &lfpfuzz); L_ADD(&nts, &lfpfuzz); @@ -3196,7 +3196,7 @@ fetch_timestamp( L_SUB(&dts, &nts); collect_timing(rb, "input processing delay", 1, &dts); - DPRINTF(4, ("fetch_timestamp: timestamp delta: %s (incl. prec fuzz)\n", + DPRINTF(4, ("fetch_timestamp: timestamp delta: %s (incl. fuzz)\n", lfptoa(&dts, 9))); #endif /* DEBUG_TIMING */ ts = nts; /* network time stamp */ diff --git a/ntpd/ntp_proto.c b/ntpd/ntp_proto.c index 47f9e30053..0ae1679d96 100644 --- a/ntpd/ntp_proto.c +++ b/ntpd/ntp_proto.c @@ -141,6 +141,7 @@ static void fast_xmit (struct recvbuf *, int, keyid_t, int); static void pool_xmit (struct peer *); static void clock_update (struct peer *); static void measure_precision(void); +static double measure_tick_fuzz(void); static int local_refid (struct peer *); static int peer_unfit (struct peer *); #ifdef AUTOKEY @@ -3807,7 +3808,7 @@ peer_unfit( */ #define MINSTEP 20e-9 /* minimum clock increment (s) */ #define MAXSTEP 1 /* maximum clock increment (s) */ -#define MINCHANGES 5 /* minimum number of step samples */ +#define MINCHANGES 12 /* minimum number of step samples */ #define MAXLOOPS ((int)(1. / MINSTEP)) /* avoid infinite loop */ /* @@ -3833,40 +3834,61 @@ peer_unfit( void measure_precision(void) { + /* + * With sys_fuzz set to zero, get_systime() fuzzing of low bits + * is effectively disabled. trunc_os_clock is FALSE to disable + * get_ostime() simulation of a low-precision system clock. + */ + set_sys_fuzz(0.); + trunc_os_clock = FALSE; + measured_tick = measure_tick_fuzz(); + set_sys_tick_precision(measured_tick); + msyslog(LOG_NOTICE, "proto: precision = %.3f usec (%d)", + sys_tick * 1e6, sys_precision); + if (sys_fuzz < sys_tick) { + msyslog(LOG_NOTICE, "proto: fuzz beneath %.3f usec", + sys_fuzz * 1e6); + } +} + + +/* + * measure_tick_fuzz() + * + * measures the minimum time to read the clock (stored in sys_fuzz) + * and returns the tick, the larger of the minimum increment observed + * between successive clock readings and the time to read the clock. + */ +double +measure_tick_fuzz(void) +{ + l_fp minstep; /* MINSTEP as l_fp */ l_fp val; /* current seconds fraction */ l_fp last; /* last seconds fraction */ - l_fp ldiff; /* difference */ + l_fp ldiff; /* val - last */ double tick; /* computed tick value */ - double diff; /* scratch */ - int i; /* log2 precision */ - int changes; + double diff; long repeats; long max_repeats; + int changes; + int i; /* log2 precision */ - /* - * Loop to find precision value in seconds. With sys_fuzz set - * to zero, get_systime() disables its fuzzing of low bits. - * measured_tick and sys_tick are zeroed to disable get_ostime() - * low-precision clock simulation. - */ tick = MAXSTEP; - sys_tick = 0; - measured_tick = 0; - set_sys_fuzz(0.); max_repeats = 0; repeats = 0; changes = 0; + DTOLFP(MINSTEP, &minstep); get_systime(&last); for (i = 0; i < MAXLOOPS && changes < MINCHANGES; i++) { get_systime(&val); ldiff = val; L_SUB(&ldiff, &last); last = val; - LFPTOD(&ldiff, diff); - if (diff > MINSTEP) { + if (L_ISGT(&ldiff, &minstep)) { max_repeats = max(repeats, max_repeats); repeats = 0; changes++; + LFPTOD(&ldiff, diff); tick = min(diff, tick); } else { repeats++; @@ -3876,16 +3898,14 @@ measure_precision(void) msyslog(LOG_ERR, "Fatal error: precision could not be measured (MINSTEP too large?)"); exit(1); } - measured_tick = tick; - set_sys_tick_precision(tick); + if (0 == max_repeats) { set_sys_fuzz(tick); } else { set_sys_fuzz(tick / max_repeats); - msyslog(LOG_NOTICE, "proto: fuzz beneath %.3f usec", - sys_fuzz * 1e6); } + return tick; } @@ -3907,6 +3927,7 @@ set_sys_tick_precision( tick, measured_tick); return; } else if (tick > measured_tick) { + trunc_os_clock = TRUE; msyslog(LOG_NOTICE, "proto: truncating system clock to multiples of %.9f", tick); @@ -3922,8 +3943,6 @@ set_sys_tick_precision( i++; sys_precision = (s_char)i; - msyslog(LOG_NOTICE, "proto: precision = %.3f usec (%d)", - sys_tick * 1e6, sys_precision); } diff --git a/ntpd/ntp_request.c b/ntpd/ntp_request.c index a626e9c42f..56ad94e607 100644 --- a/ntpd/ntp_request.c +++ b/ntpd/ntp_request.c @@ -1130,7 +1130,7 @@ sys_info( if (stats_control) is->flags |= INFO_FLAG_FILEGEN; is->bdelay = HTONS_FP(DTOFP(sys_bdelay)); - HTONL_UF(sys_authdelay.l_f, &is->authdelay); + HTONL_UF(sys_authdelay.l_uf, &is->authdelay); (void) more_pkt(); flush_pkt(); } diff --git a/ntpd/refclock_arc.c b/ntpd/refclock_arc.c index 1b14d7fdf8..037f4d50e3 100644 --- a/ntpd/refclock_arc.c +++ b/ntpd/refclock_arc.c @@ -6,6 +6,8 @@ #include #endif +#include "ntp_types.h" + #if defined(REFCLOCK) && defined(CLOCK_ARCRON_MSF) static const char arc_version[] = { "V1.3 2003/02/21" }; @@ -1003,7 +1005,7 @@ arc_receive( diff = up->lastrec; L_SUB(&diff, ×tamp); printf("arc: adjusted timestamp by -%sms.\n", - mfptoms(diff.l_i, diff.l_f, 3)); + mfptoms(diff.l_ui, diff.l_uf, 3)); } #endif } @@ -1578,5 +1580,5 @@ arc_poll( } #else -int refclock_arc_bs; +NONEMPTY_TRANSLATION_UNIT #endif diff --git a/ntpd/refclock_chu.c b/ntpd/refclock_chu.c index e6a1a3c520..fd9513afab 100644 --- a/ntpd/refclock_chu.c +++ b/ntpd/refclock_chu.c @@ -5,6 +5,8 @@ #include #endif +#include "ntp_types.h" + #if defined(REFCLOCK) && defined(CLOCK_CHU) #include "ntpd.h" @@ -1258,7 +1260,7 @@ chu_a( up->status |= AVALID; up->second = pp->second = 30 + temp; offset.l_ui = 30 + temp; - offset.l_f = 0; + offset.l_uf = 0; i = 0; if (k < 0) offset = up->charstamp; @@ -1675,5 +1677,5 @@ chu_gain( #else -int refclock_chu_bs; +NONEMPTY_TRANSLATION_UNIT #endif /* REFCLOCK */ diff --git a/ntpd/refclock_datum.c b/ntpd/refclock_datum.c index 5476525688..cdf3569db2 100644 --- a/ntpd/refclock_datum.c +++ b/ntpd/refclock_datum.c @@ -11,6 +11,8 @@ # include #endif +#include "ntp_types.h" + #if defined(REFCLOCK) && defined(CLOCK_DATUM) /* @@ -21,7 +23,7 @@ #include "ntp_io.h" #include "ntp_tty.h" #include "ntp_refclock.h" -#include "ntp_unixtime.h" +#include "timevalops.h" #include "ntp_stdlib.h" #include @@ -774,5 +776,5 @@ datum_pts_receive( } #else -int refclock_datum_bs; +NONEMPTY_TRANSLATION_UNIT #endif /* REFCLOCK */ diff --git a/ntpd/refclock_leitch.c b/ntpd/refclock_leitch.c index 07e1429754..69ffdc5ed5 100644 --- a/ntpd/refclock_leitch.c +++ b/ntpd/refclock_leitch.c @@ -6,6 +6,8 @@ # include #endif +#include "ntp_types.h" + #if defined(REFCLOCK) && defined(CLOCK_LEITCH) #include @@ -14,7 +16,7 @@ #include "ntpd.h" #include "ntp_io.h" #include "ntp_refclock.h" -#include "ntp_unixtime.h" +#include "timevalops.h" #include "ntp_stdlib.h" @@ -594,5 +596,5 @@ leitch_get_time( } #else -int refclock_leitch_bs; +NONEMPTY_TRANSLATION_UNIT #endif /* REFCLOCK */ diff --git a/ntpd/refclock_msfees.c b/ntpd/refclock_msfees.c index 5571e9ab6b..f34327558e 100644 --- a/ntpd/refclock_msfees.c +++ b/ntpd/refclock_msfees.c @@ -4,6 +4,8 @@ #include #endif +#include "ntp_types.h" + #if defined(REFCLOCK) && defined(CLOCK_MSFEES) && defined(PPS) /* Currently REQUIRES STREAM and PPSCD. CLK and CBREAK modes @@ -14,8 +16,7 @@ #include "ntpd.h" #include "ntp_io.h" #include "ntp_refclock.h" -#include "ntp_unixtime.h" -#include "ntp_calendar.h" +#include "timevalops.h" #include #if defined(HAVE_BSD_TTYS) @@ -917,9 +918,9 @@ ees_receive( if (pps_step != 1 && pps_step != 2) fprintf(stderr, "PPS step: %d too far off %ld (%d)\n", ppsclockev.serial, ees->last_pps_no, pps_step); - else if (!buftvtots((char *) &(ppsclockev.tv), &pps_arrvstamp)) - fprintf(stderr, "buftvtots failed\n"); - else { /* if ((ABS(time difference) - 0.25) < 0) + else { + pps_arrvstamp = tval_stamp_to_lfp(ppsclockev.tv); + /* if ((ABS(time difference) - 0.25) < 0) * then believe it ... */ l_fp diff; @@ -1445,5 +1446,5 @@ msfees_poll( #else -int refclock_msfees_bs; +NONEMPTY_TRANSLATION_UNIT #endif /* REFCLOCK */ diff --git a/ntpd/refclock_nmea.c b/ntpd/refclock_nmea.c index 240de5a6f1..51c8750135 100644 --- a/ntpd/refclock_nmea.c +++ b/ntpd/refclock_nmea.c @@ -20,6 +20,8 @@ #include #endif +#include "ntp_types.h" + #if defined(REFCLOCK) && defined(CLOCK_NMEA) #define NMEA_WRITE_SUPPORT 0 /* no write support at the moment */ @@ -651,7 +653,7 @@ refclock_ppsrelate( return PPS_RELATE_NONE; /* WHICH edge, please?!? */ /* get delta between receive time and PPS time */ - timespec_abstolfp(&pp_stamp, &timeout); + pp_stamp = tspec_stamp_to_lfp(timeout); pp_delta = *rd_stamp; L_SUB(&pp_delta, &pp_stamp); LFPTOD(&pp_delta, delta); @@ -934,7 +936,7 @@ nmea_receive( * timecode timestamp, but only if the PPS is not in control. * Discard sentence if reference time did not change. */ - timespec_reltolfp(&rd_reftime, &tofs); + rd_reftime = tspec_intv_to_lfp(tofs); rd_reftime.l_ui += caltontp(&date); if (L_ISEQU(&up->last_reftime, &rd_reftime)) return; @@ -1709,5 +1711,5 @@ nmead_open( return fd; } #else -int refclock_nmea_bs; +NONEMPTY_TRANSLATION_UNIT #endif /* REFCLOCK && CLOCK_NMEA */ diff --git a/ntpd/refclock_parse.c b/ntpd/refclock_parse.c index 8f20ab7210..8af0c0129a 100644 --- a/ntpd/refclock_parse.c +++ b/ntpd/refclock_parse.c @@ -48,6 +48,8 @@ # include "config.h" #endif +#include "ntp_types.h" + #if defined(REFCLOCK) && defined(CLOCK_PARSE) /* @@ -96,7 +98,7 @@ #include "ntpd.h" #include "ntp_refclock.h" -#include "ntp_unixtime.h" /* includes */ +#include "timevalops.h" /* includes */ #include "ntp_control.h" #include "ntp_string.h" @@ -1722,29 +1724,6 @@ static bind_t io_bindings[] = #ifdef STREAM -#define fix_ts(_X_) \ - if ((&(_X_))->tv.tv_usec >= 1000000) \ - { \ - (&(_X_))->tv.tv_usec -= 1000000; \ - (&(_X_))->tv.tv_sec += 1; \ - } - -#define cvt_ts(_X_, _Y_) \ - { \ - l_fp ts; \ - fix_ts((_X_)); \ - if (!buftvtots((const char *)&(&(_X_))->tv, &ts)) \ - { \ - ERR(ERR_BADDATA) \ - msyslog(LOG_ERR,"parse: stream_receive: timestamp conversion error (buftvtots) (%s) (%ld.%06ld) ", (_Y_), (long)(&(_X_))->tv.tv_sec, (long)(&(_X_))->tv.tv_usec);\ - return; \ - } \ - else \ - { \ - (&(_X_))->fp = ts; \ - } \ - } - /*-------------------------------------------------- * ppsclock STREAM init */ @@ -2029,15 +2008,17 @@ stream_receive( * errors. */ - cvt_ts(parsetime.parse_stime, "parse_stime"); + parsetime.parse_stime.fp = tval_stamp_to_lfp(parsetime.parse_stime.tv); if (PARSE_TIMECODE(parsetime.parse_state)) { - cvt_ts(parsetime.parse_time, "parse_time"); + parsetime.parse_time.fp = tval_stamp_to_lfp(parsetime.parse_time.tv); } if (PARSE_PPS(parsetime.parse_state)) - cvt_ts(parsetime.parse_ptime, "parse_ptime"); + { + parsetime.parse_ptime.fp = tval_stamp_to_lfp(parsetime.parse_ptime.tv); + } parse_process(parse, &parsetime); } @@ -3932,8 +3913,8 @@ parse_process( #endif if (PARSE_TIMECODE(parsetime->parse_state)) { - if (M_ISGEQ(off.l_i, off.l_f, -1, 0x80000000) && - M_ISGEQ(0, 0x7fffffff, off.l_i, off.l_f)) + if (M_ISGEQ(off.l_i, off.l_uf, -1, 0x80000000) && + M_ISGEQ(0, 0x7fffffff, off.l_i, off.l_uf)) { fudge = ppsphaseadjust; /* pick PPS fudge factor */ @@ -3944,7 +3925,7 @@ parse_process( if (parse->parse_type->cl_flags & PARSE_F_PPSONSECOND) { reftime = off = offset; - if (reftime.l_uf & (unsigned)0x80000000) + if (reftime.l_uf & 0x80000000) reftime.l_ui++; reftime.l_uf = 0; @@ -3953,7 +3934,7 @@ parse_process( * implied on second offset */ off.l_uf = ~off.l_uf; /* map [0.5..1[ -> [-0.5..0[ */ - off.l_ui = (off.l_f < 0) ? ~0 : 0; /* sign extend */ + off.l_i = (off.l_uf & 0x8000000) ? -1 : 0; /* sign extend */ } else { @@ -3978,14 +3959,14 @@ parse_process( */ off = offset; reftime = offset; - if (reftime.l_uf & (unsigned)0x80000000) + if (reftime.l_uf & 0x80000000) reftime.l_ui++; reftime.l_uf = 0; /* * implied on second offset */ off.l_uf = ~off.l_uf; /* map [0.5..1[ -> [-0.5..0[ */ - off.l_ui = (off.l_f < 0) ? ~0 : 0; /* sign extend */ + off.l_i = (off.l_uf & 0x80000000) ? -1 : 0; /* sign extend */ } } else @@ -5856,7 +5837,7 @@ rawdcf_init_2( #endif /* DTR initialisation type */ #else /* defined(REFCLOCK) && defined(PARSE) */ -int refclock_parse_bs; +NONEMPTY_TRANSLATION_UNIT #endif /* defined(REFCLOCK) && defined(PARSE) */ /* diff --git a/ntpd/refclock_shm.c b/ntpd/refclock_shm.c index 5d201053c9..a0d66b51b2 100644 --- a/ntpd/refclock_shm.c +++ b/ntpd/refclock_shm.c @@ -11,6 +11,8 @@ # include #endif +#include "ntp_types.h" + #if defined(REFCLOCK) && defined(CLOCK_SHM) #include "ntpd.h" @@ -19,7 +21,7 @@ #undef fileno #include "ntp_refclock.h" #undef fileno -#include "ntp_unixtime.h" +#include "timevalops.h" #undef fileno #include "ntp_stdlib.h" @@ -404,14 +406,14 @@ void shm_clockstats( if (!(pp->sloppyclockflag & CLK_FLAG4)) return; - snprintf(logbuf, sizeof(logbuf), "%3d %3d %3d %3d %3d", - up->ticks, up->good, up->notready, up->bad, up->clash); - record_clock_stats(&peer->srcadr, logbuf); + snprintf(logbuf, sizeof(logbuf), "%3d %3d %3d %3d %3d", + up->ticks, up->good, up->notready, up->bad, up->clash); + record_clock_stats(&peer->srcadr, logbuf); up->ticks = up->good = up->notready =up->bad = up->clash = 0; } #else -int refclock_shm_bs; +NONEMPTY_TRANSLATION_UNIT #endif /* REFCLOCK */ diff --git a/ntpdate/ntpdate.c b/ntpdate/ntpdate.c index 09cacd923f..4cd3a5d0c6 100644 --- a/ntpdate/ntpdate.c +++ b/ntpdate/ntpdate.c @@ -14,7 +14,7 @@ #include "ntp_fp.h" #include "ntp.h" #include "ntp_io.h" -#include "ntp_unixtime.h" +#include "timevalops.h" #include "ntpdate.h" #include "ntp_string.h" #include "ntp_syslog.h" diff --git a/ntpdc/ntpdc.c b/ntpdc/ntpdc.c index f40b40af9d..33f9c4627c 100644 --- a/ntpdc/ntpdc.c +++ b/ntpdc/ntpdc.c @@ -1122,6 +1122,11 @@ docmd( /* * Tokenize the command line. If nothing on it, return. */ + if (strlen(cmdline) >= MAXLINE) { + fprintf(stderr, "***Command ignored, more than %d characters:\n%s\n", + MAXLINE - 1, cmdline); + return; + } tokenize(cmdline, tokens, &ntok); if (ntok == 0) return; diff --git a/ports/winnt/include/termios.h b/ports/winnt/include/termios.h index a2166f7c64..350ee47fb8 100644 --- a/ports/winnt/include/termios.h +++ b/ports/winnt/include/termios.h @@ -208,5 +208,6 @@ extern int closeserial (int); extern int ioctl (int, int, int *); extern int tcsetattr (int, int, const struct termios *); extern int tcgetattr (int, struct termios *); +extern int tcflush (int, int); #endif /* NTP_WIN_TERMIOS_H */ diff --git a/ports/winnt/vs2005/libntp.vcproj b/ports/winnt/vs2005/libntp.vcproj index d7df6717f1..c998cc842f 100644 --- a/ports/winnt/vs2005/libntp.vcproj +++ b/ports/winnt/vs2005/libntp.vcproj @@ -268,14 +268,6 @@ RelativePath="..\..\..\libntp\findconfig.c" > - - - - @@ -348,14 +340,6 @@ RelativePath="..\..\..\lib\isc\md5.c" > - - - - @@ -372,10 +356,6 @@ RelativePath="..\..\..\libntp\mstolfp.c" > - - @@ -536,10 +516,6 @@ RelativePath="..\..\..\lib\isc\win32\time.c" > - - @@ -548,18 +524,6 @@ RelativePath="..\..\..\libntp\timevalops.c" > - - - - - - diff --git a/ports/winnt/vs2008/libntp/libntp.vcproj b/ports/winnt/vs2008/libntp/libntp.vcproj index 6dbd6ee160..74c1969b95 100644 --- a/ports/winnt/vs2008/libntp/libntp.vcproj +++ b/ports/winnt/vs2008/libntp/libntp.vcproj @@ -283,14 +283,6 @@ RelativePath="..\..\..\..\libntp\findconfig.c" > - - - - @@ -363,14 +355,6 @@ RelativePath="..\..\..\..\lib\isc\md5.c" > - - - - @@ -387,10 +371,6 @@ RelativePath="..\..\..\..\libntp\mstolfp.c" > - - @@ -555,10 +535,6 @@ RelativePath="..\..\..\..\lib\isc\win32\time.c" > - - @@ -567,18 +543,6 @@ RelativePath="..\..\..\..\libntp\timevalops.c" > - - - - - - @@ -901,11 +865,11 @@ > + + + + diff --git a/sntp/main.c b/sntp/main.c index 01ee9e13c5..4b82401293 100644 --- a/sntp/main.c +++ b/sntp/main.c @@ -1459,7 +1459,7 @@ gettimeofday_cached( progname); exit(1); } - timeval_sub(&diff, &systemt, &latest); + diff = sub_tval(systemt, latest); if (debug > 1) printf("system minus cached %+ld.%06ld\n", (long)diff.tv_sec, diff.tv_usec); @@ -1471,13 +1471,13 @@ gettimeofday_cached( } else { mono.tv_sec = ts.tv_sec; mono.tv_usec = ts.tv_nsec / 1000; - timeval_sub(&diff, &latest, &mono); + diff = sub_tval(latest, mono); if (debug > 1) printf("cached minus monotonic %+ld.%06ld\n", (long)diff.tv_sec, diff.tv_usec); if (labs((long)diff.tv_sec) < 3600) { /* older libevent2 using monotonic */ - timeval_sub(&offset, &systemt, &mono); + offset = sub_tval(systemt, mono); TRACE(1, ("%s: Offsetting libevent CLOCK_MONOTONIC times by %+ld.%06ld\n", "gettimeofday_cached", (long)offset.tv_sec, offset.tv_usec)); @@ -1485,7 +1485,7 @@ gettimeofday_cached( } offset_ready = TRUE; } - timeval_add(&adj_cached, &cached, &offset); + adj_cached = add_tval(cached, offset); *caller_tv = adj_cached; return 0; diff --git a/tests/libntp/Makefile.am b/tests/libntp/Makefile.am index 7be168a5da..8740a44b3c 100644 --- a/tests/libntp/Makefile.am +++ b/tests/libntp/Makefile.am @@ -56,7 +56,6 @@ tests_SOURCES = $(top_srcdir)/sntp/tests_main.cpp \ timespecops.cpp \ timestructs.cpp \ timevalops.cpp \ - tsftomsu.cpp \ tstotv.cpp \ tvtots.cpp \ uglydate.cpp \ diff --git a/tests/libntp/lfptostr.cpp b/tests/libntp/lfptostr.cpp index 476e5fbae8..aa5f5112d6 100644 --- a/tests/libntp/lfptostr.cpp +++ b/tests/libntp/lfptostr.cpp @@ -25,8 +25,8 @@ protected: TEST_F(lfptostrTest, PositiveInteger) { l_fp test = {200, 0}; // exact 200.0000000000 - EXPECT_STREQ("200.0000000000", mfptoa(test.l_i, test.l_f, LFP_MAX_PRECISION)); - EXPECT_STREQ("200000.0000000", mfptoms(test.l_i, test.l_f, LFP_MAX_PRECISION_MS)); + EXPECT_STREQ("200.0000000000", mfptoa(test.l_ui, test.l_uf, LFP_MAX_PRECISION)); + EXPECT_STREQ("200000.0000000", mfptoms(test.l_ui, test.l_uf, LFP_MAX_PRECISION_MS)); } TEST_F(lfptostrTest, NegativeInteger) { diff --git a/tests/libntp/strtolfp.cpp b/tests/libntp/strtolfp.cpp index 953a4e01ad..729996e0b1 100644 --- a/tests/libntp/strtolfp.cpp +++ b/tests/libntp/strtolfp.cpp @@ -25,7 +25,7 @@ TEST_F(strtolfpTest, NegativeInteger) { l_fp expected; expected.l_i = -300; - expected.l_f = 0; + expected.l_uf = 0; l_fp actual, actual_ms; diff --git a/tests/libntp/timespecops.cpp b/tests/libntp/timespecops.cpp index 0083492987..1283a66cf7 100644 --- a/tests/libntp/timespecops.cpp +++ b/tests/libntp/timespecops.cpp @@ -1,15 +1,21 @@ #include "libntptest.h" +#include "timestructs.h" extern "C" { +#include #include "timespecops.h" } #include #include +using namespace timeStruct; + class timespecTest : public libntptest { protected: - static const long NANOSECONDS; + static u_int32 my_tick_to_tsf(u_int32 ticks); + static u_int32 my_tsf_to_tick(u_int32 tsf); + // that's it... struct lfpfracdata { long nsec; @@ -17,7 +23,39 @@ protected: }; static const lfpfracdata fdata[]; }; -const long timespecTest::NANOSECONDS = 1000000000; + +u_int32 +timespecTest::my_tick_to_tsf( + u_int32 ticks + ) +{ + // convert nanoseconds to l_fp fractional units, using double + // precision float calculations or, if available, 64bit integer + // arithmetic. This should give the precise fraction, rounded to + // the nearest representation. +#ifdef HAVE_U_INT64 + return u_int32(((u_int64(ticks) << 32) + 500000000) / 1000000000); +#else + return u_int32(double(ticks) * 4.294967296 + 0.5); +#endif + // And before you ask: if ticks >= 1000000000, the result is + // truncated nonsense, so don't use it out-of-bounds. +} + +u_int32 +timespecTest::my_tsf_to_tick( + u_int32 tsf + ) +{ + // Inverse operation: converts fraction to microseconds. +#ifdef HAVE_U_INT64 + return u_int32((u_int64(tsf) * 1000000000 + 0x80000000) >> 32); +#else + return u_int32(double(tsf) / 4.294967296 + 0.5); +#endif + // Beware: The result might be 10^9 due to rounding! +} + const timespecTest::lfpfracdata timespecTest::fdata [] = { { 0, 0x00000000 }, { 2218896, 0x00916ae6 }, { 16408100, 0x0433523d }, { 125000000, 0x20000000 }, @@ -35,82 +73,50 @@ const timespecTest::lfpfracdata timespecTest::fdata [] = { { 956805507, 0xf4f134a9 }, { 982570733, 0xfb89c16c } }; -class TSPEC { -public: - struct timespec V; - - TSPEC() - { ZERO(V); } - TSPEC(time_t hi, long lo) - { V.tv_sec = hi; V.tv_nsec = lo; } - bool operator == (const TSPEC& rhs) const - { return timespec_cmp(&V, &rhs.V) == 0; } - bool valid() const - { return timespec_isnormal(&V); } - operator struct timespec* () - { return &V; } - operator struct timespec& () - { return V; } - TSPEC& operator = (const TSPEC& rhs) - { V = rhs.V; return *this; } - TSPEC& operator = (const struct timespec& rhs) - { V = rhs; return *this; } -}; - -static std::ostream& -operator << (std::ostream& os, const TSPEC& val) -{ - os << timespec_tostr(&val.V); - return os; -} +// --------------------------------------------------------------------- +// test support stuff -- part 1 +// --------------------------------------------------------------------- -class LFP { -public: - l_fp V; - - LFP() - { ZERO(V); } - LFP(u_int32 hi, u_int32 lo) - { V.l_ui = hi; V.l_uf = lo; } - bool operator == (const LFP& rhs) const - { return L_ISEQU(&V, &rhs.V); } - operator l_fp* () - { return &V; } - operator l_fp& () - { return V; } - LFP& operator = (const LFP& rhs) - { V = rhs.V; return *this; } - LFP& operator = (const l_fp& rhs) - { V = rhs; return *this; } -}; +TEST_F(timespecTest, Helpers1) { + timespec_wrap x; -std::ostream& -operator << (std::ostream& os, const LFP &val) -{ - os << ulfptoa(&val.V, 10); - return os; + for (x.V.tv_sec = -2; x.V.tv_sec < 3; x.V.tv_sec++) { + x.V.tv_nsec = -1; + ASSERT_FALSE(x.valid()); + x.V.tv_nsec = 0; + ASSERT_TRUE(x.valid()); + x.V.tv_nsec = 999999999; + ASSERT_TRUE(x.valid()); + x.V.tv_nsec = 1000000000; + ASSERT_FALSE(x.valid()); + } } - -// --------------------------------------------------------------------- -// test support stuff -// --------------------------------------------------------------------- +//---------------------------------------------------------------------- +// test normalisation +//---------------------------------------------------------------------- TEST_F(timespecTest, Normalise) { for (long ns = -2000000000; ns <= 2000000000; ns += 10000000) { - TSPEC x(0, ns); - timespec_norm(x); + timespec_wrap x(0, ns); + + x = normalize_tspec(x); ASSERT_TRUE(x.valid()); } } +//---------------------------------------------------------------------- +// test classification +//---------------------------------------------------------------------- + TEST_F(timespecTest, SignNoFrac) { // sign test, no fraction for (int i = -4; i <= 4; ++i) { - TSPEC a(i, 0); + timespec_wrap a(i, 0); int E = (i > 0) - (i < 0); - int r = timespec_test(a); + int r = test_tspec(a); + ASSERT_EQ(E, r); } } @@ -118,23 +124,25 @@ TEST_F(timespecTest, SignNoFrac) { TEST_F(timespecTest, SignWithFrac) { // sign test, with fraction for (int i = -4; i <= 4; ++i) { - TSPEC a(i, 10); + timespec_wrap a(i, 10); int E = (i >= 0) - (i < 0); - int r = timespec_test(a); + int r = test_tspec(a); ASSERT_EQ(E, r); } } +//---------------------------------------------------------------------- // test compare +//---------------------------------------------------------------------- TEST_F(timespecTest, CmpFracEQ) { // fractions are equal for (int i = -4; i <= 4; ++i) for (int j = -4; j <= 4; ++j) { - TSPEC a( i , 200); - TSPEC b( j , 200); + timespec_wrap a( i , 200); + timespec_wrap b( j , 200); int E = (i > j) - (i < j); - int r = timespec_cmp(a, b); + int r = cmp_tspec_denorm(a, b); ASSERT_EQ(E, r); } @@ -144,10 +152,10 @@ TEST_F(timespecTest, CmpFracGT) { // fraction a bigger fraction b for (int i = -4; i <= 4; ++i) for (int j = -4; j <= 4; ++j) { - TSPEC a(i, 999999800); - TSPEC b(j, 200); + timespec_wrap a(i, 999999800); + timespec_wrap b(j, 200); int E = (i >= j) - (i < j); - int r = timespec_cmp(a, b); + int r = cmp_tspec_denorm(a, b); ASSERT_EQ(E, r); } @@ -157,25 +165,28 @@ TEST_F(timespecTest, CmpFracLT) { // fraction a less fraction b for (int i = -4; i <= 4; ++i) for (int j = -4; j <= 4; ++j) { - TSPEC a(i, 200); - TSPEC b(j, 999999800); + timespec_wrap a(i, 200); + timespec_wrap b(j, 999999800); int E = (i > j) - (i <= j); - int r = timespec_cmp(a, b); + int r = cmp_tspec_denorm(a, b); ASSERT_EQ(E, r); } } +//---------------------------------------------------------------------- // Test addition (sum) +//---------------------------------------------------------------------- + TEST_F(timespecTest, AddFullNorm) { for (int i = -4; i <= 4; ++i) for (int j = -4; j <= 4; ++j) { - TSPEC a(i, 200); - TSPEC b(j, 400); - TSPEC E(i + j, 200 + 400); - TSPEC c; + timespec_wrap a(i, 200); + timespec_wrap b(j, 400); + timespec_wrap E(i + j, 200 + 400); + timespec_wrap c; - timespec_add(c, a, b); + c = add_tspec(a, b); ASSERT_EQ(E, c); } } @@ -183,48 +194,51 @@ TEST_F(timespecTest, AddFullNorm) { TEST_F(timespecTest, AddFullOflow1) { for (int i = -4; i <= 4; ++i) for (int j = -4; j <= 4; ++j) { - TSPEC a(i, 200); - TSPEC b(j, 999999900); - TSPEC E(i + j + 1, 100); - TSPEC c; + timespec_wrap a(i, 200); + timespec_wrap b(j, 999999900); + timespec_wrap E(i + j + 1, 100); + timespec_wrap c; - timespec_add(c, a, b); + c = add_tspec(a, b); ASSERT_EQ(E, c); } } TEST_F(timespecTest, AddNsecNorm) { for (int i = -4; i <= 4; ++i) { - TSPEC a(i, 200); - TSPEC E(i, 600); - TSPEC c; + timespec_wrap a(i, 200); + timespec_wrap E(i, 600); + timespec_wrap c; - timespec_addns(c, a, 400); + c = add_tspec_ns(a, 600 - 200); ASSERT_EQ(E, c); } } TEST_F(timespecTest, AddNsecOflow1) { for (int i = -4; i <= 4; ++i) { - TSPEC a(i, 200); - TSPEC E(i + 1, 100); - TSPEC c; + timespec_wrap a(i, 200); + timespec_wrap E(i + 1, 100); + timespec_wrap c; - timespec_addns(c, a, NANOSECONDS - 100); + c = add_tspec_ns(a, NANOSECONDS - 100); ASSERT_EQ(E, c); } } +//---------------------------------------------------------------------- // test subtraction (difference) +//---------------------------------------------------------------------- + TEST_F(timespecTest, SubFullNorm) { for (int i = -4; i <= 4; ++i) for (int j = -4; j <= 4; ++j) { - TSPEC a( i , 600); - TSPEC b( j , 400); - TSPEC E(i-j, 200); - TSPEC c; + timespec_wrap a( i , 600); + timespec_wrap b( j , 400); + timespec_wrap E(i-j, 200); + timespec_wrap c; - timespec_sub(c, a, b); + c = sub_tspec(a, b); ASSERT_EQ(E, c); } } @@ -232,110 +246,212 @@ TEST_F(timespecTest, SubFullNorm) { TEST_F(timespecTest, SubFullOflow) { for (int i = -4; i <= 4; ++i) for (int j = -4; j <= 4; ++j) { - TSPEC a( i , 100); - TSPEC b( j , 999999900); - TSPEC E(i-j-1, 200); - TSPEC c; + timespec_wrap a( i , 100); + timespec_wrap b( j , 999999900); + timespec_wrap E(i-j-1, 200); + timespec_wrap c; - timespec_sub(c, a, b); + c = sub_tspec(a, b); ASSERT_EQ(E, c); } } TEST_F(timespecTest, SubNsecNorm) { for (int i = -4; i <= 4; ++i) { - TSPEC a(i, 600); - TSPEC E(i, 200); - TSPEC c; + timespec_wrap a(i, 600); + timespec_wrap E(i, 200); + timespec_wrap c; - timespec_subns(c, a, 400); + c = sub_tspec_ns(a, 600 - 200); ASSERT_EQ(E, c); } } TEST_F(timespecTest, SubNsecOflow) { for (int i = -4; i <= 4; ++i) { - TSPEC a( i , 100); - TSPEC E(i-1, 200); - TSPEC c; + timespec_wrap a( i , 100); + timespec_wrap E(i-1, 200); + timespec_wrap c; - timespec_subns(c, a, NANOSECONDS - 100); + c = sub_tspec_ns(a, NANOSECONDS - 100); ASSERT_EQ(E, c); } } +//---------------------------------------------------------------------- // test negation +//---------------------------------------------------------------------- + TEST_F(timespecTest, Neg) { for (int i = -4; i <= 4; ++i) { - TSPEC a( i , 100); - TSPEC b; - TSPEC c; + timespec_wrap a( i, 100); + timespec_wrap b; + timespec_wrap c; - timespec_neg(b, a); - timespec_add(c, a, b); - ASSERT_EQ(0, timespec_test(c)); + b = neg_tspec(a); + c = add_tspec(a, b); + ASSERT_EQ(0, test_tspec(c)); } } +//---------------------------------------------------------------------- // test abs value +//---------------------------------------------------------------------- + TEST_F(timespecTest, AbsNoFrac) { for (int i = -4; i <= 4; ++i) { - TSPEC a(i , 0); - TSPEC b; - int c; + timespec_wrap a(i , 0); + timespec_wrap b; - c = timespec_abs(b, a); - ASSERT_EQ((i < 0), c); - ASSERT_EQ((i != 0), timespec_test(b)); + b = abs_tspec(a); + ASSERT_EQ((i != 0), test_tspec(b)); } } TEST_F(timespecTest, AbsWithFrac) { for (int i = -4; i <= 4; ++i) { - TSPEC a(i , 100); - TSPEC b; - int c; + timespec_wrap a(i , 100); + timespec_wrap b; - c = timespec_abs(b, a); - ASSERT_EQ((i < 0), c); - ASSERT_EQ(1, timespec_test(b)); + b = abs_tspec(a); + ASSERT_EQ(1, test_tspec(b)); } } +// --------------------------------------------------------------------- +// test support stuff -- part 2 +// --------------------------------------------------------------------- + +TEST_F(timespecTest, Helpers2) { + AssertTimespecClose isClose(0, 2); + timespec_wrap x, y; + + for (x.V.tv_sec = -2; x.V.tv_sec < 3; x.V.tv_sec++) + for (x.V.tv_nsec = 1; + x.V.tv_nsec < 1000000000; + x.V.tv_nsec += 499999999) { + for (long i = -4; i < 5; i++) { + y = x; + y.V.tv_nsec += i; + if (i >= -2 && i <= 2) + ASSERT_PRED_FORMAT2(isClose, x, y); + else + ASSERT_PRED_FORMAT2(!isClose, x, y); + } + } +} + +// global predicate instances we're using here +static AssertFpClose FpClose(0, 1); +static AssertTimespecClose TimespecClose(0, 2); + +//---------------------------------------------------------------------- // conversion to l_fp +//---------------------------------------------------------------------- + +TEST_F(timespecTest, ToLFPbittest) { + for (u_int32 i = 0; i < 1000000000; i+=1000) { + timespec_wrap a(1, i); + l_fp_wrap E(1, my_tick_to_tsf(i)); + l_fp_wrap r; + + r = tspec_intv_to_lfp(a); + ASSERT_PRED_FORMAT2(FpClose, E, r); + } +} + TEST_F(timespecTest, ToLFPrelPos) { for (int i = 0; i < COUNTOF(fdata); i++) { - TSPEC a(1, fdata[i].nsec); - LFP E(1, fdata[i].frac); - LFP r; + timespec_wrap a(1, fdata[i].nsec); + l_fp_wrap E(1, fdata[i].frac); + l_fp_wrap r; - timespec_reltolfp(r, a); + r = tspec_intv_to_lfp(a); ASSERT_EQ(E, r); } } TEST_F(timespecTest, ToLFPrelNeg) { for (int i = 0; i < COUNTOF(fdata); i++) { - TSPEC a(-1, fdata[i].nsec); - LFP E(~0, fdata[i].frac); - LFP r; + timespec_wrap a(-1, fdata[i].nsec); + l_fp_wrap E(~0, fdata[i].frac); + l_fp_wrap r; - timespec_reltolfp(r, a); + r = tspec_intv_to_lfp(a); ASSERT_EQ(E, r); } } TEST_F(timespecTest, ToLFPabs) { for (int i = 0; i < COUNTOF(fdata); i++) { - TSPEC a(1, fdata[i].nsec); - LFP E(1 + JAN_1970, fdata[i].frac); - LFP r; + timespec_wrap a(1, fdata[i].nsec); + l_fp_wrap E(1 + JAN_1970, fdata[i].frac); + l_fp_wrap r; - timespec_abstolfp(r, a); + r = tspec_stamp_to_lfp(a); ASSERT_EQ(E, r); } } +//---------------------------------------------------------------------- +// conversion from l_fp +//---------------------------------------------------------------------- +TEST_F(timespecTest, FromLFPbittest) { + // Not *exactly* a bittest, because 2**32 tests would take a + // really long time even on very fast machines! So we do test + // every 1000 fractional units. + for (u_int32 tsf = 0; tsf < ~u_int32(1000); tsf += 1000) { + timespec_wrap E(1, my_tsf_to_tick(tsf)); + l_fp_wrap a(1, tsf); + timespec_wrap r; + + r = lfp_intv_to_tspec(a); + // The conversion might be off by one nanosecond when + // comparing to calculated value. + ASSERT_PRED_FORMAT2(TimespecClose, E, r); + } +} + +TEST_F(timespecTest, FromLFPrelPos) { + for (int i = 0; i < COUNTOF(fdata); i++) { + l_fp_wrap a(1, fdata[i].frac); + timespec_wrap E(1, fdata[i].nsec); + timespec_wrap r; + + r = lfp_intv_to_tspec(a); + ASSERT_PRED_FORMAT2(TimespecClose, E, r); + } +} + +TEST_F(timespecTest, FromLFPrelNeg) { + for (int i = 0; i < COUNTOF(fdata); i++) { + l_fp_wrap a(~0, fdata[i].frac); + timespec_wrap E(-1, fdata[i].nsec); + timespec_wrap r; + + r = lfp_intv_to_tspec(a); + ASSERT_PRED_FORMAT2(TimespecClose, E, r); + } +} + + +// nsec -> frac -> nsec roundtrip, using a prime start and increment +TEST_F(timespecTest, LFProundtrip) { + for (int32_t t = -1; t < 2; ++t) + for (u_int32 i = 4999; i < 1000000000; i+=10007) { + timespec_wrap E(t, i); + l_fp_wrap a; + timespec_wrap r; + + a = tspec_intv_to_lfp(E); + r = lfp_intv_to_tspec(a); + ASSERT_EQ(E, r); + } +} + +//---------------------------------------------------------------------- +// string formatting +//---------------------------------------------------------------------- TEST_F(timespecTest, ToString) { static const struct { @@ -353,9 +469,9 @@ TEST_F(timespecTest, ToString) { {-1,-1, "-1.000000001" }, }; for (int i = 0; i < COUNTOF(data); i++) { - TSPEC a(data[i].sec, data[i].nsec); + timespec_wrap a(data[i].sec, data[i].nsec); std::string E(data[i].repr); - std::string r(timespec_tostr(a)); + std::string r(tspectoa(a)); ASSERT_EQ(E, r); } diff --git a/tests/libntp/timestructs.cpp b/tests/libntp/timestructs.cpp index 28f0668dbb..ac7323dac7 100644 --- a/tests/libntp/timestructs.cpp +++ b/tests/libntp/timestructs.cpp @@ -8,11 +8,9 @@ #include "timestructs.h" extern "C" { -#include "ntp_unixtime.h" #include "timetoa.h" -#include "ntp_fp.h" - #include "timevalops.h" +#include "timespecops.h" } std::ostream& @@ -23,7 +21,7 @@ operator << (std::ostream& os, const timeStruct::l_fp_wrap& val) << std::setfill('0') << std::setw(8) << val.V.l_uf << std::dec; // human-readable format - os << '[' << dolfptoa(val.V.l_ui, val.V.l_uf, 0, 10, 0) << ']'; + os << '[' << lfptoa(&val.V, 10) << ']'; return os; } @@ -112,9 +110,8 @@ AssertTimevalClose::operator()( { struct timeval diff; - timeval_sub(&diff, &m, &n); - timeval_abs(&diff, &diff); - if (timeval_cmp(&limit, &diff) >= 0) + diff = abs_tval(sub_tval(m, n)); + if (cmp_tval(limit, diff) >= 0) return ::testing::AssertionSuccess(); return ::testing::AssertionFailure() @@ -124,4 +121,36 @@ AssertTimevalClose::operator()( << "\nare not close; diff=" << timeval_wrap(diff); } +// Implementation of the timespec closeness predicate + +AssertTimespecClose::AssertTimespecClose( + time_t hi, + int32 lo + ) +{ + limit.tv_sec = hi; + limit.tv_nsec = lo; +} + +::testing::AssertionResult +AssertTimespecClose::operator()( + const char* m_expr, + const char* n_expr, + const struct timespec & m, + const struct timespec & n + ) +{ + struct timespec diff; + + diff = abs_tspec(sub_tspec(m, n)); + if (cmp_tspec(limit, diff) >= 0) + return ::testing::AssertionSuccess(); + + return ::testing::AssertionFailure() + << m_expr << " which is " << timespec_wrap(m) + << "\nand\n" + << n_expr << " which is " << timespec_wrap(n) + << "\nare not close; diff=" << timespec_wrap(diff); +} + } // namespace timeStruct diff --git a/tests/libntp/timestructs.h b/tests/libntp/timestructs.h index 14e2c44d45..b869006b2d 100644 --- a/tests/libntp/timestructs.h +++ b/tests/libntp/timestructs.h @@ -17,8 +17,7 @@ #define TIMESTRUCTS_H extern "C" { -#include "ntp_types.h" -#include "ntp_fp.h" +#include "ntp_fp.h" } namespace timeStruct { @@ -57,6 +56,8 @@ public: { V.tv_sec = hi; V.tv_usec = lo; } timeval_wrap(const struct timeval & rhs) { V = rhs; } + timeval_wrap(const timeval_wrap & rhs) + { V = rhs.V; } bool operator == (const timeval_wrap& rhs) const { return V.tv_sec == rhs.V.tv_sec && V.tv_usec == rhs.V.tv_usec ; } @@ -83,6 +84,8 @@ public: { V.tv_sec = hi; V.tv_nsec = lo; } timespec_wrap(const struct timespec & rhs) { V = rhs; } + timespec_wrap(const timespec_wrap & rhs) + { V = rhs.V; } bool operator == (const timespec_wrap& rhs) const { return V.tv_sec == rhs.V.tv_sec && V.tv_nsec == rhs.V.tv_nsec ; } @@ -143,6 +146,29 @@ public: }; +// timespec closeness testing predicate +// +// CAVEAT: This class uses the timevalops functions +// - sub_tspec +// - abs_tspec +// - cmp_tspec +// +// See the equivalent timeval helper. +class AssertTimespecClose { +private: + struct timespec limit; + +public: + // note: (hi,lo) should be a positive normalised timespec; + // the constructor does not normalise the values! + AssertTimespecClose(time_t hi, int32 lo); + + ::testing::AssertionResult + operator()(const char* m_expr, const char* n_expr, + const struct timespec & m, const struct timespec & n); +}; + + } // namespace timeStruct // since googletest wants to string format items, we declare the diff --git a/tests/libntp/timevalops.cpp b/tests/libntp/timevalops.cpp index cb43123afd..7232615b91 100644 --- a/tests/libntp/timevalops.cpp +++ b/tests/libntp/timevalops.cpp @@ -13,11 +13,9 @@ using namespace timeStruct; class timevalTest : public libntptest { protected: - static u_int32 my_tick_to_tsf(u_int32 ticks); static u_int32 my_tsf_to_tick(u_int32 tsf); - static const long MICROSECONDS; // that's it... struct lfpfracdata { long usec; @@ -35,10 +33,10 @@ timevalTest::my_tick_to_tsf( // precision float calculations or, if available, 64bit integer // arithmetic. This should give the precise fraction, rounded to // the nearest representation. -#if SIZEOF_LONG >= 8 - return (u_int32)((((u_long)ticks << 32) + 500000) / 1000000); +#ifdef HAVE_U_INT64 + return u_int32(((u_int64(ticks) << 32) + 500000) / 1000000); #else - return (u_int32)floor((double)ticks * 4294.967296 + 0.5); + return u_int32(double(ticks) * 4294.967296 + 0.5); #endif // And before you ask: if ticks >= 1000000, the result is // truncated nonsense, so don't use it out-of-bounds. @@ -50,16 +48,14 @@ timevalTest::my_tsf_to_tick( ) { // Inverse operation: converts fraction to microseconds. -#if SIZEOF_LONG >= 8 - return (u_int32)(((u_long)tsf * 1000000 + 0x80000000) >> 32); +#ifdef HAVE_U_INT64 + return u_int32((u_int64(tsf) * 1000000 + 0x80000000) >> 32); #else - return (u_int32)floor((double)tsf / 4294.967296 + 0.5); + return u_int32(double(tsf) / 4294.967296 + 0.5); #endif // Beware: The result might be 10^6 due to rounding! } - -const long timevalTest::MICROSECONDS = 1000000; const timevalTest::lfpfracdata timevalTest::fdata [] = { { 0, 0x00000000 }, { 7478, 0x01ea1405 }, { 22077, 0x05a6d699 }, { 125000, 0x20000000 }, @@ -78,31 +74,48 @@ const timevalTest::lfpfracdata timevalTest::fdata [] = { }; -// and the global predicate instances we're using here +// --------------------------------------------------------------------- +// test support stuff - part1 +// --------------------------------------------------------------------- -static AssertFpClose FpClose(0, 1); +TEST_F(timevalTest, Helpers1) { + timeval_wrap x; -static AssertTimevalClose TimevalClose(0, 1); + for (x.V.tv_sec = -2; x.V.tv_sec < 3; x.V.tv_sec++) { + x.V.tv_usec = -1; + ASSERT_FALSE(x.valid()); + x.V.tv_usec = 0; + ASSERT_TRUE(x.valid()); + x.V.tv_usec = 999999; + ASSERT_TRUE(x.valid()); + x.V.tv_usec = 1000000; + ASSERT_FALSE(x.valid()); + } +} -// --------------------------------------------------------------------- -// test support stuff -// --------------------------------------------------------------------- +//---------------------------------------------------------------------- +// test normalisation +//---------------------------------------------------------------------- TEST_F(timevalTest, Normalise) { for (long ns = -2000000000; ns <= 2000000000; ns += 10000000) { timeval_wrap x(0, ns); - timeval_norm(x); + x = normalize_tval(x); ASSERT_TRUE(x.valid()); } } +//---------------------------------------------------------------------- +// test classification +//---------------------------------------------------------------------- + TEST_F(timevalTest, SignNoFrac) { // sign test, no fraction for (int i = -4; i <= 4; ++i) { timeval_wrap a(i, 0); int E = (i > 0) - (i < 0); - int r = timeval_test(a); + int r = test_tval(a); ASSERT_EQ(E, r); } @@ -113,13 +126,15 @@ TEST_F(timevalTest, SignWithFrac) { for (int i = -4; i <= 4; ++i) { timeval_wrap a(i, 10); int E = (i >= 0) - (i < 0); - int r = timeval_test(a); + int r = test_tval(a); ASSERT_EQ(E, r); } } +//---------------------------------------------------------------------- // test compare +//---------------------------------------------------------------------- TEST_F(timevalTest, CmpFracEQ) { // fractions are equal for (int i = -4; i <= 4; ++i) @@ -127,7 +142,7 @@ TEST_F(timevalTest, CmpFracEQ) { timeval_wrap a(i, 200); timeval_wrap b(j, 200); int E = (i > j) - (i < j); - int r = timeval_cmp(a, b); + int r = cmp_tval_denorm(a, b); ASSERT_EQ(E, r); } @@ -140,7 +155,7 @@ TEST_F(timevalTest, CmpFracGT) { timeval_wrap a( i , 999800); timeval_wrap b( j , 200); int E = (i >= j) - (i < j); - int r = timeval_cmp(a, b); + int r = cmp_tval_denorm(a, b); ASSERT_EQ(E, r); } @@ -153,13 +168,16 @@ TEST_F(timevalTest, CmpFracLT) { timeval_wrap a(i, 200); timeval_wrap b(j, 999800); int E = (i > j) - (i <= j); - int r = timeval_cmp(a, b); + int r = cmp_tval_denorm(a, b); ASSERT_EQ(E, r); } } +//---------------------------------------------------------------------- // Test addition (sum) +//---------------------------------------------------------------------- + TEST_F(timevalTest, AddFullNorm) { for (int i = -4; i <= 4; ++i) for (int j = -4; j <= 4; ++j) { @@ -168,7 +186,7 @@ TEST_F(timevalTest, AddFullNorm) { timeval_wrap E(i + j, 200 + 400); timeval_wrap c; - timeval_add(c, a, b); + c = add_tval(a, b); ASSERT_EQ(E, c); } } @@ -180,7 +198,8 @@ TEST_F(timevalTest, AddFullOflow1) { timeval_wrap b(j, 999900); timeval_wrap E(i + j + 1, 100); timeval_wrap c; - timeval_add(c, a, b); + + c = add_tval(a, b); ASSERT_EQ(E, c); } } @@ -191,7 +210,7 @@ TEST_F(timevalTest, AddUsecNorm) { timeval_wrap E(i, 600); timeval_wrap c; - timeval_addus(c, a, 400); + c = add_tval_us(a, 600 - 200); ASSERT_EQ(E, c); } } @@ -202,12 +221,15 @@ TEST_F(timevalTest, AddUsecOflow1) { timeval_wrap E(i + 1, 100); timeval_wrap c; - timeval_addus(c, a, MICROSECONDS - 100); + c = add_tval_us(a, MICROSECONDS - 100); ASSERT_EQ(E, c); } } +//---------------------------------------------------------------------- // test subtraction (difference) +//---------------------------------------------------------------------- + TEST_F(timevalTest, SubFullNorm) { for (int i = -4; i <= 4; ++i) for (int j = -4; j <= 4; ++j) { @@ -216,7 +238,7 @@ TEST_F(timevalTest, SubFullNorm) { timeval_wrap E(i - j, 600 - 400); timeval_wrap c; - timeval_sub(c, a, b); + c = sub_tval(a, b); ASSERT_EQ(E, c); } } @@ -229,7 +251,7 @@ TEST_F(timevalTest, SubFullOflow) { timeval_wrap E(i - j - 1, 200); timeval_wrap c; - timeval_sub(c, a, b); + c = sub_tval(a, b); ASSERT_EQ(E, c); } } @@ -240,7 +262,7 @@ TEST_F(timevalTest, SubUsecNorm) { timeval_wrap E(i, 200); timeval_wrap c; - timeval_subus(c, a, 600 - 200); + c = sub_tval_us(a, 600 - 200); ASSERT_EQ(E, c); } } @@ -251,34 +273,38 @@ TEST_F(timevalTest, SubUsecOflow) { timeval_wrap E(i - 1, 200); timeval_wrap c; - timeval_subus(c, a, MICROSECONDS - 100); + c = sub_tval_us(a, MICROSECONDS - 100); ASSERT_EQ(E, c); } } +//---------------------------------------------------------------------- // test negation +//---------------------------------------------------------------------- + TEST_F(timevalTest, Neg) { for (int i = -4; i <= 4; ++i) { timeval_wrap a(i, 100); timeval_wrap b; timeval_wrap c; - timeval_neg(b, a); - timeval_add(c, a, b); - ASSERT_EQ(0, timeval_test(c)); + b = neg_tval(a); + c = add_tval(a, b); + ASSERT_EQ(0, test_tval(c)); } } +//---------------------------------------------------------------------- // test abs value +//---------------------------------------------------------------------- + TEST_F(timevalTest, AbsNoFrac) { for (int i = -4; i <= 4; ++i) { timeval_wrap a(i, 0); timeval_wrap b; - int c; - c = timeval_abs(b, a); - ASSERT_EQ((i < 0), c); - ASSERT_EQ((i != 0), timeval_test(b)); + b = abs_tval(a); + ASSERT_EQ((i != 0), test_tval(b)); } } @@ -286,22 +312,50 @@ TEST_F(timevalTest, AbsWithFrac) { for (int i = -4; i <= 4; ++i) { timeval_wrap a(i, 100); timeval_wrap b; - int c; - c = timeval_abs(b, a); - ASSERT_EQ((i < 0), c); - ASSERT_EQ(1, timeval_test(b)); + b = abs_tval(a); + ASSERT_EQ(1, test_tval(b)); } } +// --------------------------------------------------------------------- +// test support stuff -- part 2 +// --------------------------------------------------------------------- + +TEST_F(timevalTest, Helpers2) { + AssertTimevalClose isClose(0, 2); + timeval_wrap x, y; + + for (x.V.tv_sec = -2; x.V.tv_sec < 3; x.V.tv_sec++) + for (x.V.tv_usec = 1; + x.V.tv_usec < 1000000; + x.V.tv_usec += 499999) { + for (long i = -4; i < 5; i++) { + y = x; + y.V.tv_usec += i; + if (i >= -2 && i <= 2) + ASSERT_PRED_FORMAT2(isClose, x, y); + else + ASSERT_PRED_FORMAT2(!isClose, x, y); + } + } +} + +// and the global predicate instances we're using here +static AssertFpClose FpClose(0, 1); +static AssertTimevalClose TimevalClose(0, 1); + +//---------------------------------------------------------------------- // conversion to l_fp +//---------------------------------------------------------------------- + TEST_F(timevalTest, ToLFPbittest) { for (u_int32 i = 0; i < 1000000; i++) { timeval_wrap a(1, i); l_fp_wrap E(1, my_tick_to_tsf(i)); l_fp_wrap r; - timeval_reltolfp(r, a); + r = tval_intv_to_lfp(a); ASSERT_PRED_FORMAT2(FpClose, E, r); } } @@ -312,7 +366,7 @@ TEST_F(timevalTest, ToLFPrelPos) { l_fp_wrap E(1, fdata[i].frac); l_fp_wrap r; - timeval_reltolfp(r, a); + r = tval_intv_to_lfp(a); ASSERT_PRED_FORMAT2(FpClose, E, r); } } @@ -323,7 +377,7 @@ TEST_F(timevalTest, ToLFPrelNeg) { l_fp_wrap E(~0, fdata[i].frac); l_fp_wrap r; - timeval_reltolfp(r, a); + r = tval_intv_to_lfp(a); ASSERT_PRED_FORMAT2(FpClose, E, r); } } @@ -334,12 +388,15 @@ TEST_F(timevalTest, ToLFPabs) { l_fp_wrap E(1 + JAN_1970, fdata[i].frac); l_fp_wrap r; - timeval_abstolfp(r, a); + r = tval_stamp_to_lfp(a); ASSERT_PRED_FORMAT2(FpClose, E, r); } } +//---------------------------------------------------------------------- // conversion from l_fp +//---------------------------------------------------------------------- + TEST_F(timevalTest, FromLFPbittest) { // Not *exactly* a bittest, because 2**32 tests would take a // really long time even on very fast machines! So we do test @@ -349,29 +406,53 @@ TEST_F(timevalTest, FromLFPbittest) { l_fp_wrap a(1, tsf); timeval_wrap r; - timeval_relfromlfp(r, a); - + r = lfp_intv_to_tval(a); // The conversion might be off by one microsecond when - // comparing to calculated value; the table-driven - // conversion does not use all bits from the FP fraction - // and this truncation will affect the rounding. + // comparing to calculated value. ASSERT_PRED_FORMAT2(TimevalClose, E, r); } } -// usec -> frac -> usec roundtrip -TEST_F(timevalTest, LFProundtrip) { - for (u_int32 i = 0; i < 1000000; i++) { - timeval_wrap E(1, i); - l_fp_wrap a; +TEST_F(timevalTest, FromLFPrelPos) { + for (int i = 0; i < COUNTOF(fdata); i++) { + l_fp_wrap a(1, fdata[i].frac); + timeval_wrap E(1, fdata[i].usec); timeval_wrap r; - timeval_reltolfp(a, E); - timeval_relfromlfp(r, a); - ASSERT_EQ(E, r); + r = lfp_intv_to_tval(a); + ASSERT_PRED_FORMAT2(TimevalClose, E, r); } } +TEST_F(timevalTest, FromLFPrelNeg) { + for (int i = 0; i < COUNTOF(fdata); i++) { + l_fp_wrap a(~0, fdata[i].frac); + timeval_wrap E(-1, fdata[i].usec); + timeval_wrap r; + + r = lfp_intv_to_tval(a); + ASSERT_PRED_FORMAT2(TimevalClose, E, r); + } +} + +// usec -> frac -> usec roundtrip, using a prime start and increment +TEST_F(timevalTest, LFProundtrip) { + for (int32_t t = -1; t < 2; ++t) + for (u_int32 i = 5; i < 1000000; i+=11) { + timeval_wrap E(t, i); + l_fp_wrap a; + timeval_wrap r; + + a = tval_intv_to_lfp(E); + r = lfp_intv_to_tval(a); + ASSERT_EQ(E, r); + } +} + +//---------------------------------------------------------------------- +// string formatting +//---------------------------------------------------------------------- + TEST_F(timevalTest, ToString) { static const struct { time_t sec; @@ -390,7 +471,7 @@ TEST_F(timevalTest, ToString) { for (int i = 0; i < COUNTOF(data); ++i) { timeval_wrap a(data[i].sec, data[i].usec); std::string E(data[i].repr); - std::string r(timeval_tostr(a)); + std::string r(tvaltoa(a)); ASSERT_EQ(E, r); } diff --git a/tests/libntp/tsftomsu.cpp b/tests/libntp/tsftomsu.cpp deleted file mode 100644 index e14f1767f5..0000000000 --- a/tests/libntp/tsftomsu.cpp +++ /dev/null @@ -1,29 +0,0 @@ -#include "libntptest.h" - -class tsftomsuTest : public libntptest { -protected: - static const u_long HALF = 2147483648UL; - static const u_long HALF_PROMILLE_UP = 2147484; // slightly more than 0.0005 - static const u_long HALF_PROMILLE_DOWN = 2147483; // slightly less than 0.0005 - static const u_long QUARTER = 1073741824L; -}; - -TEST_F(tsftomsuTest, Half) { - EXPECT_EQ(500, tsftomsu(HALF, 0)); -} - -TEST_F(tsftomsuTest, Quarter) { - EXPECT_EQ(250, tsftomsu(QUARTER, 1)); -} - -TEST_F(tsftomsuTest, HalfPromilleRoundUp) { - EXPECT_EQ(1, tsftomsu(HALF_PROMILLE_UP, 1)); -} - -TEST_F(tsftomsuTest, HalfPromilleRoundDown) { - EXPECT_EQ(0, tsftomsu(HALF_PROMILLE_DOWN, 1)); -} - -TEST_F(tsftomsuTest, HalfPromilleTruncate) { - EXPECT_EQ(0, tsftomsu(HALF_PROMILLE_UP, 0)); -} diff --git a/tests/libntp/tstotv.cpp b/tests/libntp/tstotv.cpp index 04b3b73c56..41f44d70e0 100644 --- a/tests/libntp/tstotv.cpp +++ b/tests/libntp/tstotv.cpp @@ -2,7 +2,7 @@ extern "C" { #include "ntp_fp.h" -#include "ntp_unixtime.h" +#include "timevalops.h" }; class tstotvTest : public libntptest { diff --git a/tests/libntp/tvtots.cpp b/tests/libntp/tvtots.cpp index 6cc84c86b5..c51b7f59e6 100644 --- a/tests/libntp/tvtots.cpp +++ b/tests/libntp/tvtots.cpp @@ -1,7 +1,7 @@ #include "lfptest.h" extern "C" { -#include "ntp_unixtime.h" +#include "timevalops.h" }; // Required on Solaris for ldexp. diff --git a/util/ntp-keygen.c b/util/ntp-keygen.c index 8bb24c064b..2772707ae7 100644 --- a/util/ntp-keygen.c +++ b/util/ntp-keygen.c @@ -89,6 +89,7 @@ #include #include +#include "ntp.h" #include "ntp_random.h" #include "ntp_stdlib.h" #include "ntp_assert.h" @@ -114,7 +115,6 @@ #define MD5SIZE 20 /* maximum key size */ #define DAY ((long)60*60*24) /* one day in seconds */ #define YEAR ((long)365) /* one year in days */ -#define MAXFILENAME MAXHOSTNAME /* max file name length */ #ifdef AUTOKEY #define PLEN 512 /* default prime modulus size (bits) */ #define ILEN 256 /* default identity modulus size (bits) */ diff --git a/util/ntptime.c b/util/ntptime.c index 6ea38fdd50..dd5605ca7b 100644 --- a/util/ntptime.c +++ b/util/ntptime.c @@ -15,7 +15,7 @@ #endif /* HAVE_CONFIG_H */ #include "ntp_fp.h" -#include "ntp_unixtime.h" +#include "timevalops.h" #include "ntp_syscall.h" #include "ntp_stdlib.h" @@ -48,6 +48,13 @@ #define SCALE_FREQ 65536 /* frequency scale */ +/* + * These constants are used to round the time stamps computed from + * a struct timeval to the microsecond (more or less). This keeps + * things neat. + */ +#define TS_MASK 0xfffff000 /* mask to usec, for time stamps */ +#define TS_ROUNDBIT 0x00000800 /* round at this bit */ /* * Function prototypes