[Git][NTPsec/ntpsec][master] Encapsulate vint4 operations as macros.
Eric S. Raymond
gitlab at mg.gitlab.com
Sat Sep 17 10:51:46 UTC 2016
Eric S. Raymond pushed to branch master at NTPsec / ntpsec
Commits:
13fa1219 by Eric S. Raymond at 2016-09-17T06:51:36-04:00
Encapsulate vint4 operations as macros.
- - - - -
10 changed files:
- include/ntp_types.h
- include/timespecops.h
- include/timevalops.h
- libntp/ntp_calendar.c
- libntp/prettydate.c
- libntp/vint64ops.c
- ntpd/ntp_leapsec.c
- ntpd/ntp_timer.c
- ntpd/refclock_nmea.c
- tests/libntp/vi64ops.c
Changes:
=====================================
include/ntp_types.h
=====================================
--- a/include/ntp_types.h
+++ b/include/ntp_types.h
@@ -69,6 +69,17 @@ typedef union {
uint64_t Q_s; /* unsigned quad scalar */
} vint64; /* variant int 64 */
+/* hide the structure of a vint64 */
+#define vint64lo(n) (n).d_s.lo
+#define setvint64lo(n,v) (n).d_s.lo = (v)
+#define vint64his(n) (n).d_s.hi
+#define setvint64his(n,v) (n).d_s.hi = (v)
+#define vint64hiu(n) (n).D_s.hi
+#define setvint64hiu(n,v) (n).D_s.hi = (v)
+#define vint64s(n) (n).q_s
+#define setvint64s(n,v) (n).q_s = (v)
+#define vint64u(n) (n).Q_s
+#define setvint64u(n,v) (n).Q_s = (v)
typedef uint16_t associd_t; /* association ID */
#define ASSOCID_MAX USHRT_MAX
=====================================
include/timespecops.h
=====================================
--- a/include/timespecops.h
+++ b/include/timespecops.h
@@ -374,9 +374,9 @@ lfp_stamp_to_tspec(
/* copying a vint64 to a time_t needs some care... */
#if SIZEOF_TIME_T <= 4
- out.tv_sec = (time_t)sec.d_s.lo;
+ out.tv_sec = (time_t)vint64lo(sec);
#else
- out.tv_sec = (time_t)sec.q_s;
+ out.tv_sec = (time_t)vint64s(sec);
#endif
return out;
=====================================
include/timevalops.h
=====================================
--- a/include/timevalops.h
+++ b/include/timevalops.h
@@ -393,9 +393,9 @@ lfp_stamp_to_tval(
/* copying a vint64 to a time_t needs some care... */
#if SIZEOF_TIME_T <= 4
- out.tv_sec = (time_t)sec.d_s.lo;
+ out.tv_sec = (time_t)vint64lo(sec);
#else
- out.tv_sec = (time_t)sec.q_s;
+ out.tv_sec = (time_t)vint64s(sec);
#endif
out = normalize_tval(out);
=====================================
libntp/ntp_calendar.c
=====================================
--- a/libntp/ntp_calendar.c
+++ b/libntp/ntp_calendar.c
@@ -62,19 +62,16 @@ time_to_vint64(
tt = *ptt;
#if SIZEOF_TIME_T <= 4
-
- res.D_s.hi = 0;
+ setvint64hiu(res, 0);
if (tt < 0) {
- res.D_s.lo = (uint32_t)-tt;
- M_NEG(res.D_s.hi, res.D_s.lo);
+ setvint64lo(res, (uint32_t)-tt);
+ M_NEG(vint64hiu(res), vint64lo(res));
} else {
- res.D_s.lo = (uint32_t)tt;
+ setvint64lo(res, (uint32_t)tt);
}
#else
-
- res.q_s = tt;
-
+ setvint64s(res, tt);
#endif
return res;
@@ -89,13 +86,10 @@ vint64_to_time(
time_t res;
#if SIZEOF_TIME_T <= 4
-
- res = (time_t)tv->D_s.lo;
+ res = (time_t)vint64lo(*tv);
#else
-
- res = (time_t)tv->q_s;
-
+ res = (time_t)vint64s(*tv);
#endif
return res;
@@ -386,13 +380,11 @@ ntpcal_ntp_to_time(
{
vint64 res;
- res.q_s = (pivot != NULL)
- ? *pivot
- : now();
- res.Q_s -= 0x80000000; /* unshift of half range */
- ntp -= (uint32_t)JAN_1970; /* warp into UN*X domain */
- ntp -= res.D_s.lo; /* cycle difference */
- res.Q_s += (uint64_t)ntp; /* get expanded time */
+ setvint64s(res, (pivot != NULL) ? *pivot : now());
+ setvint64u(res, vint64s(res)-0x80000000); /* unshift of half range */
+ ntp -= (uint32_t)JAN_1970; /* warp into UN*X domain */
+ ntp -= vint64lo(res); /* cycle difference */
+ setvint64u(res, vint64u(res)+(uint64_t)ntp); /* get expanded time */
return res;
}
@@ -418,13 +410,12 @@ ntpcal_ntp_to_ntp(
{
vint64 res;
- res.q_s = (pivot)
- ? *pivot
- : now();
- res.Q_s -= 0x80000000; /* unshift of half range */
- res.Q_s += (uint32_t)JAN_1970; /* warp into NTP domain */
- ntp -= res.D_s.lo; /* cycle difference */
- res.Q_s += (uint64_t)ntp; /* get expanded time */
+ setvint64s(res, (pivot) ? *pivot : now());
+ setvint64u(res, vint64u(res) - 0x80000000); /* unshift of half range */
+ setvint64u(res, vint64u(res) + (uint32_t)JAN_1970); /* warp into NTP domain */
+
+ ntp -= vint64lo(res); /* cycle difference */
+ setvint64u(res, vint64u(res) + (uint64_t)ntp); /* get expanded time */
return res;
}
@@ -454,8 +445,8 @@ ntpcal_daysplit(
ntpcal_split res;
/* manual floor division by SECSPERDAY */
- res.hi = (int32_t)(ts->q_s / SECSPERDAY);
- res.lo = (int32_t)(ts->q_s % SECSPERDAY);
+ res.hi = (int32_t)(vint64s(*ts) / SECSPERDAY);
+ res.lo = (int32_t)(vint64s(*ts) % SECSPERDAY);
if (res.lo < 0) {
res.hi -= 1;
res.lo += SECSPERDAY;
@@ -803,9 +794,9 @@ ntpcal_dayjoin(
{
vint64 res;
- res.q_s = days;
- res.q_s *= SECSPERDAY;
- res.q_s += secs;
+ setvint64s(res, days);
+ setvint64s(res, vint64s(res) * SECSPERDAY);
+ setvint64s(res, vint64s(res) + secs);
return res;
}
@@ -1167,7 +1158,7 @@ ntpcal_date_to_ntp(
/*
* Get lower half of 64-bit NTP timestamp from date/time.
*/
- return ntpcal_date_to_ntp64(jd).d_s.lo;
+ return vint64lo(ntpcal_date_to_ntp64(jd));
}
@@ -1454,7 +1445,7 @@ isocal_date_to_ntp(
/*
* Get lower half of 64-bit NTP timestamp from date/time.
*/
- return isocal_date_to_ntp64(id).d_s.lo;
+ return vint64lo(isocal_date_to_ntp64(id));
}
/* -*-EOF-*- */
=====================================
libntp/prettydate.c
=====================================
--- a/libntp/prettydate.c
+++ b/libntp/prettydate.c
@@ -62,7 +62,7 @@ get_struct_tm(
time_t ts;
int64_t tl;
- ts = tl = stamp->q_s;
+ ts = tl = vint64s(*stamp);
/*
* If there is chance of truncation, try to fix it. Let the
=====================================
libntp/vint64ops.c
=====================================
--- a/libntp/vint64ops.c
+++ b/libntp/vint64ops.c
@@ -94,7 +94,7 @@ strtouv64(
if (digit >= base)
break;
num = 1;
- res.Q_s = res.Q_s * base + digit;
+ setvint64u(res, vint64u(res) * base + digit);
src++;
}
if (!num)
@@ -102,7 +102,7 @@ strtouv64(
if (endp)
*endp = (char*)noconst(src);
if (sig)
- M_NEG(res.D_s.hi, res.D_s.lo);
+ M_NEG(vint64hiu(res), vint64lo(res));
return res;
}
@@ -116,8 +116,8 @@ icmpv64(
{
int res;
- res = (lhs->q_s > rhs->q_s)
- - (lhs->q_s < rhs->q_s);
+ res = (vint64s(*lhs) > vint64s(*rhs))
+ - (vint64s(*lhs) < vint64s(*rhs));
return res;
}
@@ -132,8 +132,8 @@ ucmpv64(
{
int res;
- res = (lhs->Q_s > rhs->Q_s)
- - (lhs->Q_s < rhs->Q_s);
+ res = (vint64u(*lhs) > vint64u(*rhs))
+ - (vint64u(*lhs) < vint64u(*rhs));
return res;
}
@@ -148,7 +148,7 @@ addv64(
{
vint64 res;
- res.Q_s = lhs->Q_s + rhs->Q_s;
+ setvint64u(res, vint64u(*lhs) + vint64u(*rhs));
return res;
}
@@ -163,7 +163,7 @@ subv64(
{
vint64 res;
- res.Q_s = lhs->Q_s - rhs->Q_s;
+ setvint64u(res, vint64u(*lhs) - vint64u(*rhs));
return res;
}
@@ -179,7 +179,7 @@ addv64i32(
vint64 res;
res = *lhs;
- res.q_s += rhs;
+ setvint64u(res, vint64u(res) + rhs);
return res;
}
@@ -195,7 +195,7 @@ subv64i32(
vint64 res;
res = *lhs;
- res.q_s -= rhs;
+ setvint64s(res, vint64s(res) - rhs);
return res;
}
@@ -227,7 +227,7 @@ subv64u32(
vint64 res;
res = *lhs;
- res.Q_s -= rhs;
+ setvint64u(res, vint64u(res) - rhs);
return res;
}
=====================================
ntpd/ntp_leapsec.c
=====================================
--- a/ntpd/ntp_leapsec.c
+++ b/ntpd/ntp_leapsec.c
@@ -204,7 +204,7 @@ leapsec_load(
pt->head.expire = strtouv64(cp, &ep, 10);
if (parsefail(cp, ep))
goto fail_read;
- pt->lsig.etime = pt->head.expire.D_s.lo;
+ pt->lsig.etime = vint64lo(pt->head.expire);
} else if (*cp == '$') {
cp = skipws(cp+1);
pt->head.update = strtouv64(cp, &ep, 10);
@@ -227,7 +227,7 @@ leapsec_load(
} else {
pt->head.base_tai = (int16_t)taiof;
}
- pt->lsig.ttime = ttime.D_s.lo;
+ pt->lsig.ttime = vint64lo(ttime);
pt->lsig.taiof = (int16_t)taiof;
}
}
@@ -310,14 +310,14 @@ leapsec_query(
* both modes is easier to maintain.
*/
last = pt->head.ttime;
- qr->warped = (int16_t)(last.D_s.lo -
- pt->head.dtime.D_s.lo);
+ 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;
if (fired) {
ts64 = next;
- ts32 = next.D_s.lo;
+ ts32 = vint64lo(next);
} else {
qr->warped = 0;
}
@@ -330,7 +330,7 @@ leapsec_query(
return fired;
/* now start to collect the remaing data */
- due32 = pt->head.dtime.D_s.lo;
+ due32 = vint64lo(pt->head.dtime);
qr->tai_diff = pt->head.next_tai - pt->head.this_tai;
qr->ttime = pt->head.ttime;
@@ -832,7 +832,7 @@ leapsec_add(
ttime = ntpcal_date_to_ntp64(&fts);
li.ttime = ttime;
- li.stime = ttime.D_s.lo - starttime.D_s.lo;
+ li.stime = vint64lo(ttime) - vint64lo(starttime);
li.taiof = (pt->head.size ? pt->info[0].taiof : pt->head.base_tai)
+ (insert ? 1 : -1);
li.dynls = 1;
@@ -870,7 +870,7 @@ leapsec_raw(
fts.month--; /* was in range 1..12, no overflow here! */
starttime = ntpcal_date_to_ntp64(&fts);
li.ttime = *ttime;
- li.stime = ttime->D_s.lo - starttime.D_s.lo;
+ li.stime = vint64lo(*ttime) - vint64lo(starttime);
li.taiof = (int16_t)taiof;
li.dynls = (dynls != 0);
return add_range(pt, &li);
=====================================
ntpd/ntp_timer.c
=====================================
--- a/ntpd/ntp_timer.c
+++ b/ntpd/ntp_timer.c
@@ -427,7 +427,7 @@ check_leapsec(
if (lsdata.tai_diff) {
if (leap_smear.interval == 0) {
leap_smear.interval = leap_smear_intv;
- leap_smear.intv_end = lsdata.ttime.Q_s;
+ leap_smear.intv_end = vint64u(lsdata.ttime);
leap_smear.intv_start = leap_smear.intv_end - leap_smear.interval;
DPRINTF(1, ("*** leapsec_query: setting leap_smear interval %li, begin %.0f, end %.0f\n",
leap_smear.interval, leap_smear.intv_start, leap_smear.intv_end));
=====================================
ntpd/refclock_nmea.c
=====================================
--- a/ntpd/refclock_nmea.c
+++ b/ntpd/refclock_nmea.c
@@ -1813,7 +1813,7 @@ eval_gps_time(
/* If we fully trust the GPS receiver, just combine days and
* seconds and be done. */
if (peer->ttl & NMEA_DATETRUST_MASK) {
- retv.l_ui = ntpcal_dayjoin(gps_day, gps_sec).D_s.lo;
+ retv.l_ui = vint64lo(ntpcal_dayjoin(gps_day, gps_sec));
return retv;
}
@@ -1871,7 +1871,7 @@ eval_gps_time(
}
/* - build result and be done */
- retv.l_ui = ntpcal_dayjoin(adj_day, gps_sec).D_s.lo;
+ retv.l_ui = vint64lo(ntpcal_dayjoin(adj_day, gps_sec));
return retv;
}
=====================================
tests/libntp/vi64ops.c
=====================================
--- a/tests/libntp/vi64ops.c
+++ b/tests/libntp/vi64ops.c
@@ -17,14 +17,8 @@ bool IsEqual(const vint64 *expected, const vint64 *actual) {
if (0 == memcmp(expected, actual, sizeof(vint64))) {
return true;
} else {
- printf("Expected: %04x.%04x but was: %04x.%04x\n", expected->D_s.hi, expected->D_s.lo, actual->D_s.hi, actual->D_s.lo);
+ printf("Expected: %04x.%04x but was: %04x.%04x\n", vint64hiu(*expected), vint64lo(*expected), vint64hiu(*actual), vint64lo(*actual));
return false;
-// << "expected: "
-// << std::hex << expected.D_s.hi << '.'
-// << std::hex << expected.D_s.lo
-// << " but was "
-// << std::hex << actual.D_s.hi << '.'
-// << std::hex << actual.D_s.lo;
}
}
@@ -36,8 +30,8 @@ TEST(vi64ops, ParseVUI64_pos) {
char *ep;
sp = "1234x";
- exp.D_s.hi = 0;
- exp.D_s.lo = 1234;
+ setvint64hiu(exp, 0);
+ setvint64lo(exp, 1234);
act = strtouv64(sp, &ep, 0);
TEST_ASSERT_TRUE(IsEqual(&exp, &act));
TEST_ASSERT_EQUAL(*ep, 'x');
@@ -49,8 +43,8 @@ TEST(vi64ops, ParseVUI64_neg) {
char *ep;
sp = "-1234x";
- exp.D_s.hi = ~0;
- exp.D_s.lo = -1234;
+ setvint64hiu(exp, ~0);
+ setvint64lo(exp, -1234);
act = strtouv64(sp, &ep, 0);
TEST_ASSERT_TRUE(IsEqual(&exp, &act));
TEST_ASSERT_EQUAL(*ep, 'x');
@@ -62,8 +56,8 @@ TEST(vi64ops, ParseVUI64_case) {
char *ep;
sp = "0123456789AbCdEf";
- exp.D_s.hi = 0x01234567;
- exp.D_s.lo = 0x89ABCDEF;
+ setvint64hiu(exp, 0x01234567);
+ setvint64lo(exp, 0x89ABCDEF);
act = strtouv64(sp, &ep, 16);
TEST_ASSERT_TRUE(IsEqual(&exp, &act));
TEST_ASSERT_EQUAL(*ep, '\0');
View it on GitLab: https://gitlab.com/NTPsec/ntpsec/commit/13fa1219f94d2b9ec00ae409ac4b54ee12b1e93f
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ntpsec.org/pipermail/vc/attachments/20160917/39881ffa/attachment.html>
More information about the vc
mailing list