[Git][NTPsec/ntpsec][master] 3 commits: Change vint64 to be packed into an unsigned 64-bit scalar.

Eric S. Raymond gitlab at mg.gitlab.com
Mon Dec 12 20:18:28 UTC 2016


Eric S. Raymond pushed to branch master at NTPsec / ntpsec


Commits:
4784cd2a by Eric S. Raymond at 2016-12-12T14:45:15-05:00
Change vint64 to be packed into an unsigned 64-bit scalar.

- - - - -
15483fdf by Eric S. Raymond at 2016-12-12T15:00:14-05:00
Simplify some code now that vint64 is a scalar type.

- - - - -
d57c78d3 by Eric S. Raymond at 2016-12-12T15:16:50-05:00
Eliminate ucmpv64() now that vint64s are scalars directly comparable.

- - - - -


5 changed files:

- include/ntp_types.h
- include/vint64ops.h
- libntp/vint64ops.c
- ntpd/ntp_leapsec.c
- tests/libntp/vi64ops.c


Changes:

=====================================
include/ntp_types.h
=====================================
--- a/include/ntp_types.h
+++ b/include/ntp_types.h
@@ -43,36 +43,27 @@
 
 /*
  * We now assume the platform supports a 64-bit scalar type (the ISC
- * library wouldn't compile otherwise). Sadly, getting rid of vint64
- * is not as simple as turning it into a scalar due to same strange
- * code in the calendar calculations.
+ * library wouldn't compile otherwise).
  */
 
-typedef union {
-#   ifdef WORDS_BIGENDIAN
-	struct {
-		uint32_t hi; uint32_t lo;
-	} h;
-#   else
-	struct {
-		uint32_t lo; uint32_t hi;
-	} h;
-#   endif
-
-	int64_t	s;	/*   signed quad scalar */
-	uint64_t u;	/* unsigned quad scalar */
-} vint64; /* variant int 64 */
-
-/* hide the structure of a vint64 */
-#define vint64lo(n)       (n).h.lo
-#define setvint64lo(n,v)  (n).h.lo = (v)
-#define vint64hiu(n)      (n).h.hi
-#define setvint64hiu(n,v) (n).h.hi = (v)
-#define vint64s(n)        (n).s
-#define setvint64s(n,v)   (n).s = (v)
-#define vint64u(n)        (n).u
-#define setvint64u(n,v)   (n).u = (v)
-#define negvint64(n)      (n).s *= -1
+typedef uint64_t vint64;
+#define LAST32MASK	0x00000000ffffffffUL
+#define FIRST32MASK	0xffffffff00000000UL
+#define GET32LAST(n)	((n) & LAST32MASK)
+#define SET32LAST(n, v) (n) = (((n) & FIRST32MASK) | ((v) & LAST32MASK))
+#define GET32FIRST(n)	((n) >> 32)
+#define SET32FIRST(n,v) (n) = ((((v) & LAST32MASK) << 32) | ((n) & LAST32MASK))
+#define vint64lo(n)       ((uint32_t)GET32LAST(n))
+#define setvint64lo(n,v)  SET32LAST(n,v)
+#define vint64his(n)      ((int32_t)(GET32FIRST(n)))
+#define setvint64his(n,v) SET32FIRST(n,v)
+#define vint64hiu(n)      ((uint32_t)(GET32FIRST(n)))
+#define setvint64hiu(n,v) SET32FIRST(n,v)
+#define vint64s(n)        ((int64_t)(n))
+#define setvint64s(n,v)   (n) = ((int64_t)(v))
+#define vint64u(n)        (n)
+#define setvint64u(n,v)   (n) = (v)
+#define negvint64(n)      (n = ((uint64_t)((((int64_t)(n)) * -1))))
 
 typedef uint16_t	associd_t; /* association ID */
 #define ASSOCID_MAX	USHRT_MAX


=====================================
include/vint64ops.h
=====================================
--- a/include/vint64ops.h
+++ b/include/vint64ops.h
@@ -13,7 +13,6 @@
 
 /* signed/unsigned compare. returns 1/0/-1 if lhs >/=/< rhs */
 extern int icmpv64(const vint64 * lhs,	const vint64 * rhs);
-extern int ucmpv64(const vint64 * lhs,	const vint64 * rhs);
 
 /* add / subtract */
 extern vint64 addv64(const vint64 *lhs, const vint64 *rhs);


=====================================
libntp/vint64ops.c
=====================================
--- a/libntp/vint64ops.c
+++ b/libntp/vint64ops.c
@@ -36,22 +36,6 @@ icmpv64(
 
 /* -------------------------------------------------------------------------*/
 
-int
-ucmpv64(
-	const vint64 * lhs,
-	const vint64 * rhs
-	)
-{
-	int res;
-	
-	res = (vint64u(*lhs) > vint64u(*rhs))
-	    - (vint64u(*lhs) < vint64u(*rhs));
-
-	return res;
-}
-
-/* -------------------------------------------------------------------------*/
-
 vint64
 addv64(
 	const vint64 *lhs,


=====================================
ntpd/ntp_leapsec.c
=====================================
--- a/ntpd/ntp_leapsec.c
+++ b/ntpd/ntp_leapsec.c
@@ -76,11 +76,11 @@ static bool   add_range(leap_table_t*, const leap_info_t*);
 static char * get_line(leapsec_reader, void*, char*, size_t);
 static char * skipws(char*);
 static bool   parsefail(const char * cp, const char * ep);
-static void   reload_limits(leap_table_t*, const vint64*);
+static void   reload_limits(leap_table_t*, const vint64);
 static bool   betweenu32(uint32_t, uint32_t, uint32_t);
 static void   reset_times(leap_table_t*);
-static bool   leapsec_add(leap_table_t*, const vint64*, int);
-static bool   leapsec_raw(leap_table_t*, const vint64 *, int, int);
+static bool   leapsec_add(leap_table_t*, const vint64, int);
+static bool   leapsec_raw(leap_table_t*, const vint64, int, int);
 static char * lstostr(const vint64 * ts);
 
 /* =====================================================================
@@ -205,8 +205,8 @@ leapsec_load(
 			if (   parsefail(cp, ep)
 			    || taiof > SHRT_MAX || taiof < SHRT_MIN)
 				goto fail_read;
-			if (ucmpv64(&ttime, &limit) >= 0) {
-				if (!leapsec_raw(pt, &ttime,
+			if (ttime >= limit) {
+				if (!leapsec_raw(pt, ttime,
 						 taiof, false))
 					goto fail_insn;
 			} else {
@@ -279,13 +279,13 @@ leapsec_query(
 	pt    = leapsec_get_table(false);
 	memset(qr, 0, sizeof(leap_result_t));
 
-	if (ucmpv64(&ts64, &pt->head.ebase) < 0) {
+	if (ts64 < pt->head.ebase) {
 		/* Most likely after leap frame reset. Could also be a
 		 * backstep of the system clock. Anyway, get the new
 		 * leap era frame.
 		 */
-		reload_limits(pt, &ts64);
-	} else if (ucmpv64(&ts64, &pt->head.dtime) >= 0) {
+		reload_limits(pt, ts64);
+	} else if (ts64 >= pt->head.dtime) {
 		/* Boundary crossed in forward direction. This might
 		 * indicate a leap transition, so we prepare for that
 		 * case.
@@ -298,8 +298,8 @@ leapsec_query(
 		qr->warped = (int16_t)(vint64lo(last) -
 				       vint64lo(pt->head.dtime));
 		next = addv64i32(&ts64, qr->warped);
-		reload_limits(pt, &next);
-		fired = ucmpv64(&pt->head.ebase, &last) == 0;
+		reload_limits(pt, next);
+		fired = (pt->head.ebase == last);
 		if (fired) {
 			ts64 = next;
 			ts32 = vint64lo(next);
@@ -311,7 +311,7 @@ leapsec_query(
 	qr->tai_offs = pt->head.this_tai;
 
 	/* If before the next scheduling alert, we're done. */
-	if (ucmpv64(&ts64, &pt->head.stime) < 0)
+	if (ts64 < pt->head.stime)
 		return fired;
 
 	/* now start to collect the remaining data */
@@ -345,7 +345,7 @@ leapsec_frame(
 
         memset(qr, 0, sizeof(leap_result_t));
 	pt = leapsec_get_table(false);
-	if (ucmpv64(&pt->head.ttime, &pt->head.stime) <= 0)
+	if (pt->head.ttime <= pt->head.stime)
                 return false;
 
 	qr->tai_offs = pt->head.this_tai;
@@ -529,7 +529,7 @@ leapsec_expired(
 
 	pt = leapsec_get_table(false);
 	limit = ntpcal_ntp_to_ntp(when, tpiv);
-	return ucmpv64(&limit, &pt->head.expire) >= 0;
+	return (limit >= pt->head.expire);
 }
 
 /* ------------------------------------------------------------------ */
@@ -568,8 +568,8 @@ leapsec_add_fix(
 	tt64 = ntpcal_ntp_to_ntp(ttime, pivot);
 	pt   = leapsec_get_table(true);
 
-	if (   ucmpv64(&et64, &pt->head.expire) <= 0
-	   || !leapsec_raw(pt, &tt64, total, false) )
+	if ((et64 <= pt->head.expire)
+	   || !leapsec_raw(pt, tt64, total, false) )
 		return false;
 
 	pt->lsig.etime = etime;
@@ -593,7 +593,7 @@ leapsec_add_dyn(
 
 	pt = leapsec_get_table(true);
 	now64 = ntpcal_ntp_to_ntp(ntpnow, pivot);
-	return (   leapsec_add(pt, &now64, insert)
+	return (   leapsec_add(pt, now64, insert)
 		&& leapsec_set_table(pt));
 }
 
@@ -720,7 +720,7 @@ parsefail(
 static void
 reload_limits(
 	leap_table_t * pt,
-	const vint64 * ts)
+	const vint64 ts)
 {
 	int idx;
 
@@ -730,7 +730,7 @@ reload_limits(
 	 * table, so there is no shortcut for that case.
 	 */
 	for (idx = 0; idx != pt->head.size; idx++)
-		if (ucmpv64(ts, &pt->info[idx].ttime) >= 0)
+		if (ts >= pt->info[idx].ttime)
 			break;
 
 	/* get time limits with proper bound conditions. Note that the
@@ -781,7 +781,7 @@ reload_limits(
 static bool
 leapsec_add(
 	leap_table_t*  pt    ,
-	const vint64 * now64 ,
+	const vint64   now64 ,
 	int            insert)
 {
 	vint64		ttime, starttime;
@@ -792,13 +792,13 @@ leapsec_add(
 	 * leap entry. Do not permit inserts, only appends, and only if
 	 * the extend the table beyond the expiration!
 	 */
-	if (   ucmpv64(now64, &pt->head.expire) < 0
-	    || (pt->head.size && ucmpv64(now64, &pt->info[0].ttime) <= 0)) {
+	if ((now64 < pt->head.expire)
+	    || (pt->head.size && (now64 <= pt->info[0].ttime))) {
 		errno = ERANGE;
 		return false;
 	}
 
-	ntpcal_ntp64_to_date(&fts, now64);
+	ntpcal_ntp64_to_date(&fts, &now64);
 	/* To guard against dangling leap flags: do not accept leap
 	 * second request on the 1st hour of the 1st day of the month.
 	 */
@@ -832,7 +832,7 @@ leapsec_add(
 bool
 leapsec_raw(
 	leap_table_t * pt,
-	const vint64 * ttime,
+	const vint64   ttime,
 	int            taiof,
 	int            dynls)
 {
@@ -841,12 +841,12 @@ leapsec_raw(
 	leap_info_t	li;
 
 	/* Check that we only extend the table. Paranoia rulez! */
-	if (pt->head.size && ucmpv64(ttime, &pt->info[0].ttime) <= 0) {
+	if (pt->head.size && (ttime <= pt->info[0].ttime)) {
 		errno = ERANGE;
 		return false;
 	}
 
-	ntpcal_ntp64_to_date(&fts, ttime);
+	ntpcal_ntp64_to_date(&fts, &ttime);
 	/* If this does not match the exact month start, bail out. */
 	if (fts.monthday != 1 || fts.hour || fts.minute || fts.second) {
 		errno = EINVAL;
@@ -854,8 +854,8 @@ leapsec_raw(
 	}
 	fts.month--; /* was in range 1..12, no overflow here! */
 	starttime    = ntpcal_date_to_ntp64(&fts);
-	li.ttime = *ttime;
-	li.stime = vint64lo(*ttime) - vint64lo(starttime);
+	li.ttime = ttime;
+	li.stime = vint64lo(ttime) - vint64lo(starttime);
 	li.taiof = (int16_t)taiof;
 	li.dynls = (dynls != 0);
 	return add_range(pt, &li);


=====================================
tests/libntp/vi64ops.c
=====================================
--- a/tests/libntp/vi64ops.c
+++ b/tests/libntp/vi64ops.c
@@ -14,51 +14,45 @@ TEST_TEAR_DOWN(vi64ops) {}
 #include "vint64ops.h"
 
 TEST(vi64ops, HiLoVUI64uh) {
-	vint64 exp;
+	vint64 exp = 0;
 
-	setvint64u(exp, 0);	/* prevent compiler warning */
 	setvint64hiu(exp, 0x01234567);
 	setvint64lo(exp, 0x89ABCDEF);
 	TEST_ASSERT_EQUAL(vint64hiu(exp), 0x01234567);
 }
 
 TEST(vi64ops, HiLoVUI64ul) {
-	vint64 exp;
+	vint64 exp = 0;
 
-	setvint64u(exp, 0);	/* prevent compiler warning */
 	setvint64hiu(exp, 0x01234567);
 	setvint64lo(exp, 0x89ABCDEF);
 	TEST_ASSERT_EQUAL(vint64lo(exp), 0x89ABCDEF);
 }
 
 TEST(vi64ops, SetVUI64s_pos) {
-	vint64 exp;
+	vint64 exp = 0;
 
-	setvint64u(exp, 0);	/* prevent compiler warning */
 	setvint64s(exp, 0x0123456789ABCDEF);
 	TEST_ASSERT_EQUAL(vint64s(exp), 81985529216486895);
 }
 
 TEST(vi64ops, SetVUI64s_neg) {
-	vint64 exp;
+	vint64 exp = 0;
 
-	setvint64u(exp, 0);	/* prevent compiler warning */
 	setvint64s(exp, 0xFEDCBA9876543210);
 	TEST_ASSERT_EQUAL(vint64s(exp), -81985529216486896);
 }
 
 TEST(vi64ops, SetVUI64u) {
-	vint64 exp;
+	vint64 exp = 0;
 
-	setvint64u(exp, 0);	/* prevent compiler warning */
 	setvint64u(exp, 0xFEDCBA9876543210);	/* sign bit is on */
 	TEST_ASSERT_EQUAL(vint64s(exp), 18364758544493064720UL);
 }
 
 TEST(vi64ops, NegVUI64) {
-	vint64 exp;
+	vint64 exp = 0;
 
-	setvint64u(exp, 0);	/* prevent compiler warning */
 	setvint64s(exp, 71985529216486896);
 	TEST_ASSERT_EQUAL(negvint64(exp), -71985529216486896);
 }



View it on GitLab: https://gitlab.com/NTPsec/ntpsec/compare/211a5f3de83689d282e80cc36165f2ea666ce1b1...d57c78d38abc62273ad0dcdd80744f3058775f0f
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ntpsec.org/pipermail/vc/attachments/20161212/8dec31fc/attachment.html>


More information about the vc mailing list