[Git][NTPsec/ntpsec][master] 3 commits: More sealing off of l_fp representation in test code.
Eric S. Raymond
gitlab at mg.gitlab.com
Tue Jan 3 03:52:43 UTC 2017
Eric S. Raymond pushed to branch master at NTPsec / ntpsec
Commits:
12a693e3 by Eric S. Raymond at 2017-01-02T22:52:30-05:00
More sealing off of l_fp representation in test code.
- - - - -
cba2d7cb by Eric S. Raymond at 2017-01-02T22:52:30-05:00
Coalesce duplicate code.
- - - - -
a4e8c84c by Eric S. Raymond at 2017-01-02T22:52:30-05:00
Change the representation of l_fp from a struct to a 64-bit scalar.
This will make possible a large number of small code simplifications
and elimination of a lot of ugly macros.
- - - - -
11 changed files:
- include/ntp_fp.h
- include/timespecops.h
- libntp/refidsmear.c
- libparse/mfp_mul.c
- ntpd/refclock_jupiter.c
- tests/libntp/hextolfp.c
- tests/libntp/lfpfunc.c
- tests/libntp/lfptostr.c
- tests/libntp/prettydate.c
- tests/libntp/strtolfp.c
- tests/libntp/timespecops.c
Changes:
=====================================
include/ntp_fp.h
=====================================
--- a/include/ntp_fp.h
+++ b/include/ntp_fp.h
@@ -29,35 +29,33 @@
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
*
*/
-typedef struct {
- union {
- uint32_t Xl_ui;
- int32_t Xl_i;
- } Ul_i;
- uint32_t 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 lfpfrac(n) ((n).l_uf)
-#define setlfpfrac(n, v) (n).l_uf = (v)
-#define lfpsint(n) (n).l_i
-#define setlfpsint(n, v) (n).l_i = (v)
-#define bumplfpsint(n, i) (n).l_i += (i)
-#define lfpuint(n) (n).l_ui
-#define setlfpuint(n, v) (n).l_ui = (v)
-#define bumplfpuint(n, i) (n).l_ui += (i)
+typedef uint64_t l_fp;
+#define LOW32 0x00000000ffffffffUL
+#define HIGH32 0xffffffff00000000UL
+#define BUMP 0x0000000100000000UL
+#define lfpfrac(n) ((uint32_t)((n) & LOW32))
+#define setlfpfrac(n, v) (n) = (((n) & HIGH32) | ((v) & LOW32))
+#define lfpsint(n) (int32_t)(((n) & HIGH32) >> 32)
+#define setlfpsint(n, v) (n) = (int64_t)((((int64_t)(v)) << 32) | ((n) & LOW32))
+#define bumplfpsint(n, i) (n) += (i)*BUMP
+#define lfpuint(n) (uint32_t)(((n) & HIGH32) >> 32)
+#define setlfpuint(n, v) (n) = (uint64_t)((((uint64_t)(v)) << 32) | ((n) & LOW32))
+#define bumplfpuint(n, i) (n) += (i)*BUMP
+
+static inline l_fp lfpinit(int32_t hi, uint32_t lo)
+{
+ l_fp tmp = 0;
+ setlfpsint(tmp, hi);
+ setlfpfrac(tmp, lo);
+ return tmp;
+}
static inline uint64_t lfp_to_uint64(const l_fp lfp) {
- return (uint64_t)lfpuint(lfp) << 32 | (uint64_t)lfpfrac(lfp);
+ return lfp;
}
static inline l_fp uint64_to_lfp(uint64_t x) {
- l_fp fp;
- setlfpuint(fp, x >> 32);
- setlfpfrac(fp, x & 0xFFFFFFFFUL);
- return fp;
+ return x;
}
/*
@@ -66,7 +64,6 @@ static inline l_fp uint64_to_lfp(uint64_t x) {
*/
#define FRACTION_PREC (32)
-
/*
* The second fixed point format is 32 bits, with the decimal between
* bits 15 and 16. There is a signed version (s_fp) and an unsigned
@@ -190,7 +187,7 @@ static inline l_fp dtolfp(double d)
double d_tmp;
uint64_t q_tmp;
int M_isneg;
- l_fp r;
+ l_fp r = 0;
d_tmp = (d);
M_isneg = (d_tmp < 0.);
=====================================
include/timespecops.h
=====================================
--- a/include/timespecops.h
+++ b/include/timespecops.h
@@ -298,7 +298,7 @@ tspec_intv_to_lfp(
)
{
struct timespec v;
- l_fp y;
+ l_fp y = 0;
v = normalize_tspec(x);
setlfpfrac(y, TVNTOF(v.tv_nsec));
=====================================
libntp/refidsmear.c
=====================================
--- a/libntp/refidsmear.c
+++ b/libntp/refidsmear.c
@@ -17,7 +17,7 @@
l_fp
convertRefIDToLFP(uint32_t r)
{
- l_fp temp;
+ l_fp temp = 0;
r = ntohl(r);
=====================================
libparse/mfp_mul.c
=====================================
--- a/libparse/mfp_mul.c
+++ b/libparse/mfp_mul.c
@@ -41,7 +41,7 @@ mfp_mul(
unsigned long carry;
int32_t a_i = lfpsint(a_op);
uint32_t a_f = lfpfrac(a_op);
- l_fp out;
+ l_fp out = 0;
int neg = 0;
@@ -136,7 +136,7 @@ mfp_mul(
#ifdef DEBUG
if (debug > 6) {
- l_fp b;
+ l_fp b = 0;
setlfpsint(b, b_i);
setlfpfrac(b, b_f);
printf("mfp_mul: %s * %s => %s\n",
=====================================
ntpd/refclock_jupiter.c
=====================================
--- a/ntpd/refclock_jupiter.c
+++ b/ntpd/refclock_jupiter.c
@@ -613,7 +613,7 @@ jupiter_pps(struct instance *instance)
pps_info_t pps_info;
struct timespec timeout, ts;
double dtemp;
- l_fp tstmp;
+ l_fp tstmp = 0;
/*
* Convert the timespec nanoseconds field to ntp l_fp units.
=====================================
tests/libntp/hextolfp.c
=====================================
--- a/tests/libntp/hextolfp.c
+++ b/tests/libntp/hextolfp.c
@@ -13,14 +13,6 @@ TEST_TEAR_DOWN(hextolfp) {}
#include "lfptest.h"
-static l_fp lfpinit(int32_t hi, uint32_t lo)
-{
- l_fp tmp;
- setlfpsint(tmp, hi);
- setlfpfrac(tmp, lo);
- return tmp;
-}
-
TEST(hextolfp, PositiveInteger) {
const char *str = "00001000.00000000";
l_fp actual;
=====================================
tests/libntp/lfpfunc.c
=====================================
--- a/tests/libntp/lfpfunc.c
+++ b/tests/libntp/lfpfunc.c
@@ -83,15 +83,6 @@ static int l_fp_ucmp(const l_fp first, l_fp second)
// This should be easy enough...
//----------------------------------------------------------------------
-static l_fp l_fp_init(int32_t i, uint32_t f)
-{
- l_fp temp;
- setlfpsint(temp, i);
- setlfpfrac(temp, f);
-
- return temp;
-}
-
static l_fp l_fp_add(const l_fp first, const l_fp second)
{
l_fp temp = first;
@@ -231,9 +222,9 @@ TEST(lfpfunc, AdditionLR) {
size_t idx = 0;
for (idx = 0; idx < addsub_cnt; ++idx) {
- l_fp op1 = l_fp_init(addsub_tab[idx][0].h, addsub_tab[idx][0].l);
- l_fp op2 = l_fp_init(addsub_tab[idx][1].h, addsub_tab[idx][1].l);
- l_fp e_res = l_fp_init(addsub_tab[idx][2].h, addsub_tab[idx][2].l);
+ l_fp op1 = lfpinit(addsub_tab[idx][0].h, addsub_tab[idx][0].l);
+ l_fp op2 = lfpinit(addsub_tab[idx][1].h, addsub_tab[idx][1].l);
+ l_fp e_res = lfpinit(addsub_tab[idx][2].h, addsub_tab[idx][2].l);
l_fp res = l_fp_add(op1, op2);
TEST_ASSERT_EQUAL_l_fp(e_res, res);
@@ -245,9 +236,9 @@ TEST(lfpfunc, AdditionRL) {
size_t idx = 0;
for (idx = 0; idx < addsub_cnt; ++idx) {
- l_fp op2 = l_fp_init(addsub_tab[idx][0].h, addsub_tab[idx][0].l);
- l_fp op1 = l_fp_init(addsub_tab[idx][1].h, addsub_tab[idx][1].l);
- l_fp e_res = l_fp_init(addsub_tab[idx][2].h, addsub_tab[idx][2].l);
+ l_fp op2 = lfpinit(addsub_tab[idx][0].h, addsub_tab[idx][0].l);
+ l_fp op1 = lfpinit(addsub_tab[idx][1].h, addsub_tab[idx][1].l);
+ l_fp e_res = lfpinit(addsub_tab[idx][2].h, addsub_tab[idx][2].l);
l_fp res = l_fp_add(op1, op2);
TEST_ASSERT_EQUAL_l_fp(e_res, res);
@@ -263,9 +254,9 @@ TEST(lfpfunc, SubtractionLR) {
size_t idx = 0;
for (idx = 0; idx < addsub_cnt; ++idx) {
- l_fp op2 = l_fp_init(addsub_tab[idx][0].h, addsub_tab[idx][0].l);
- l_fp e_res = l_fp_init(addsub_tab[idx][1].h, addsub_tab[idx][1].l);
- l_fp op1 = l_fp_init(addsub_tab[idx][2].h, addsub_tab[idx][2].l);
+ l_fp op2 = lfpinit(addsub_tab[idx][0].h, addsub_tab[idx][0].l);
+ l_fp e_res = lfpinit(addsub_tab[idx][1].h, addsub_tab[idx][1].l);
+ l_fp op1 = lfpinit(addsub_tab[idx][2].h, addsub_tab[idx][2].l);
l_fp res = l_fp_subtract(op1, op2);
TEST_ASSERT_EQUAL_l_fp(e_res, res);
@@ -277,9 +268,9 @@ TEST(lfpfunc, SubtractionRL) {
size_t idx = 0;
for (idx = 0; idx < addsub_cnt; ++idx) {
- l_fp e_res = l_fp_init(addsub_tab[idx][0].h, addsub_tab[idx][0].l);
- l_fp op2 = l_fp_init(addsub_tab[idx][1].h, addsub_tab[idx][1].l);
- l_fp op1 = l_fp_init(addsub_tab[idx][2].h, addsub_tab[idx][2].l);
+ l_fp e_res = lfpinit(addsub_tab[idx][0].h, addsub_tab[idx][0].l);
+ l_fp op2 = lfpinit(addsub_tab[idx][1].h, addsub_tab[idx][1].l);
+ l_fp op1 = lfpinit(addsub_tab[idx][2].h, addsub_tab[idx][2].l);
l_fp res = l_fp_subtract(op1, op2);
TEST_ASSERT_EQUAL_l_fp(e_res, res);
@@ -295,11 +286,11 @@ TEST(lfpfunc, Negation) {
size_t idx = 0;
for (idx = 0; idx < addsub_cnt; ++idx) {
- l_fp op1 = l_fp_init(addsub_tab[idx][0].h, addsub_tab[idx][0].l);
+ l_fp op1 = lfpinit(addsub_tab[idx][0].h, addsub_tab[idx][0].l);
l_fp op2 = l_fp_negate(op1);
l_fp sum = l_fp_add(op1, op2);
- l_fp zero = l_fp_init(0, 0);
+ l_fp zero = lfpinit(0, 0);
TEST_ASSERT_EQUAL_l_fp(zero, sum);
}
@@ -315,7 +306,7 @@ TEST(lfpfunc, Absolute) {
size_t idx = 0;
for (idx = 0; idx < addsub_cnt; ++idx) {
- l_fp op1 = l_fp_init(addsub_tab[idx][0].h, addsub_tab[idx][0].l);
+ l_fp op1 = lfpinit(addsub_tab[idx][0].h, addsub_tab[idx][0].l);
l_fp op2 = l_fp_abs(op1);
TEST_ASSERT_TRUE(l_fp_signum(op2) >= 0);
@@ -325,7 +316,7 @@ TEST(lfpfunc, Absolute) {
else
op1 = l_fp_add(op1, op2);
- l_fp zero = l_fp_init(0, 0);
+ l_fp zero = lfpinit(0, 0);
TEST_ASSERT_EQUAL_l_fp(zero, op1);
}
@@ -333,7 +324,7 @@ TEST(lfpfunc, Absolute) {
// There is one special case we have to check: the minimum
// value cannot be negated, or, to be more precise, the
// negation reproduces the original pattern.
- l_fp minVal = l_fp_init(0x80000000, 0x00000000);
+ l_fp minVal = lfpinit(0x80000000, 0x00000000);
l_fp minAbs = l_fp_abs(minVal);
TEST_ASSERT_EQUAL(-1, l_fp_signum(minVal));
@@ -357,7 +348,7 @@ TEST(lfpfunc, FDF_RoundTrip) {
// that limit.
for (idx = 0; idx < addsub_cnt; ++idx) {
- l_fp op1 = l_fp_init(addsub_tab[idx][0].h, addsub_tab[idx][0].l);
+ l_fp op1 = lfpinit(addsub_tab[idx][0].h, addsub_tab[idx][0].l);
double op2 = lfptod(op1);
l_fp op3 = dtolfp(op2);
@@ -381,8 +372,8 @@ TEST(lfpfunc, SignedRelOps) {
size_t lc ;
for (lc = addsub_tot - 1; lc; --lc, ++tv) {
- l_fp op1 = l_fp_init(tv[0].h, tv[0].l);
- l_fp op2 = l_fp_init(tv[1].h, tv[1].l);
+ l_fp op1 = lfpinit(tv[0].h, tv[0].l);
+ l_fp op2 = lfpinit(tv[1].h, tv[1].l);
int cmp = l_fp_scmp(op1, op2);
switch (cmp) {
@@ -423,8 +414,8 @@ TEST(lfpfunc, UnsignedRelOps) {
size_t lc;
for (lc = addsub_tot - 1; lc; --lc, ++tv) {
- l_fp op1 = l_fp_init(tv[0].h, tv[0].l);
- l_fp op2 = l_fp_init(tv[1].h, tv[1].l);
+ l_fp op1 = lfpinit(tv[0].h, tv[0].l);
+ l_fp op2 = lfpinit(tv[1].h, tv[1].l);
int cmp = l_fp_ucmp(op1, op2);
switch (cmp) {
=====================================
tests/libntp/lfptostr.c
=====================================
--- a/tests/libntp/lfptostr.c
+++ b/tests/libntp/lfptostr.c
@@ -28,14 +28,6 @@ static const int THREE_FOURTH = -1073741824;
static const int HALF_PROMILLE_UP = 2147484; // slightly more than 0.0005
static const int HALF_PROMILLE_DOWN = 2147483; // slightly less than 0.0005
-static l_fp lfpinit(int32_t hi, uint32_t lo)
-{
- l_fp tmp;
- setlfpsint(tmp, hi);
- setlfpfrac(tmp, lo);
- return tmp;
-}
-
TEST(lfptostr, PositiveInteger) {
l_fp test = lfpinit(200, 0); // exact 200.0000000000
=====================================
tests/libntp/prettydate.c
=====================================
--- a/tests/libntp/prettydate.c
+++ b/tests/libntp/prettydate.c
@@ -15,14 +15,6 @@ TEST_TEAR_DOWN(prettydate) {}
static const u_int32_t HALF = 2147483648UL;
-static l_fp lfpinit(int32_t hi, uint32_t lo)
-{
- l_fp tmp;
- setlfpsint(tmp, hi);
- setlfpfrac(tmp, lo);
- return tmp;
-}
-
TEST(prettydate, ConstantDate) {
l_fp time = lfpinit(3485080800UL, HALF); // 2010-06-09 14:00:00.5
=====================================
tests/libntp/strtolfp.c
=====================================
--- a/tests/libntp/strtolfp.c
+++ b/tests/libntp/strtolfp.c
@@ -14,12 +14,11 @@ TEST_TEAR_DOWN(strtolfp) {}
/* This class tests both atolfp and mstolfp */
-
TEST(strtolfp, PositiveInteger) {
const char *str = "500";
const char *str_ms = "500000";
- l_fp expected = {{500},0};
+ l_fp expected = lfpinit(500, 0);
l_fp actual, actual_ms;
TEST_ASSERT_TRUE(atolfp(str, &actual));
@@ -33,10 +32,7 @@ TEST(strtolfp, NegativeInteger) {
const char *str = "-300";
const char *str_ms = "-300000";
- l_fp expected;
- setlfpsint(expected, -300);
- setlfpfrac(expected, 0);
-
+ l_fp expected = lfpinit(-300, 0);
l_fp actual, actual_ms;
TEST_ASSERT_TRUE(atolfp(str, &actual));
@@ -50,7 +46,7 @@ TEST(strtolfp, PositiveFraction) {
const char *str = "+500.5";
const char *str_ms = "500500.0";
- l_fp expected = {{500}, HALF};
+ l_fp expected = lfpinit(500, HALF);
l_fp actual, actual_ms;
TEST_ASSERT_TRUE(atolfp(str, &actual));
@@ -64,10 +60,7 @@ TEST(strtolfp, NegativeFraction) {
const char *str = "-300.75";
const char *str_ms = "-300750";
- l_fp expected;
- setlfpsint(expected, -301);
- setlfpfrac(expected, QUARTER);
-
+ l_fp expected = lfpinit(-301, QUARTER);
l_fp actual, actual_ms;
TEST_ASSERT_TRUE(atolfp(str, &actual));
@@ -81,7 +74,7 @@ TEST(strtolfp, PositiveMsFraction) {
const char *str = "300.00025";
const char *str_ms = "300000.25";
- l_fp expected = {{300}, QUARTER_PROMILLE_APPRX};
+ l_fp expected = lfpinit(300, QUARTER_PROMILLE_APPRX);
l_fp actual, actual_ms;
TEST_ASSERT_TRUE(atolfp(str, &actual));
@@ -95,10 +88,7 @@ TEST(strtolfp, NegativeMsFraction) {
const char *str = "-199.99975";
const char *str_ms = "-199999.75";
- l_fp expected;
- setlfpsint(expected, -200);
- setlfpfrac(expected, QUARTER_PROMILLE_APPRX);
-
+ l_fp expected = lfpinit(-200, QUARTER_PROMILLE_APPRX);
l_fp actual, actual_ms;
TEST_ASSERT_TRUE(atolfp(str, &actual));
=====================================
tests/libntp/timespecops.c
=====================================
--- a/tests/libntp/timespecops.c
+++ b/tests/libntp/timespecops.c
@@ -57,17 +57,6 @@ static struct timespec timespec_init(time_t hi, long lo)
}
-static l_fp l_fp_init(int32_t i, uint32_t f)
-{
- l_fp temp;
-
- setlfpsint(temp, i);
- setlfpfrac(temp, f);
-
- return temp;
-}
-
-
static bool AssertFpClose(const l_fp m, const l_fp n, const l_fp limit)
{
l_fp diff;
@@ -506,12 +495,12 @@ TEST(timespecops, test_Helpers2) {
//----------------------------------------------------------------------
TEST(timespecops, test_ToLFPbittest) {
- l_fp lfpClose = l_fp_init(0, 1);
+ l_fp lfpClose = lfpinit(0, 1);
uint32_t i;
for (i = 0; i < 1000000000; i+=1000) {
struct timespec a = timespec_init(1, i);
- l_fp E= l_fp_init(1, my_tick_to_tsf(i));
+ l_fp E= lfpinit(1, my_tick_to_tsf(i));
l_fp r;
r = tspec_intv_to_lfp(a);
@@ -527,7 +516,7 @@ TEST(timespecops, test_ToLFPrelPos) {
for (i = 0; i < (int)COUNTOF(fdata); ++i) {
struct timespec a = timespec_init(1, fdata[i].nsec);
- l_fp E = l_fp_init(1, fdata[i].frac);
+ l_fp E = lfpinit(1, fdata[i].frac);
l_fp r;
r = tspec_intv_to_lfp(a);
@@ -543,7 +532,7 @@ TEST(timespecops, test_ToLFPrelNeg) {
for (i = 0; i < (int)COUNTOF(fdata); ++i) {
struct timespec a = timespec_init(-1, fdata[i].nsec);
- l_fp E = l_fp_init(~0, fdata[i].frac);
+ l_fp E = lfpinit(~0, fdata[i].frac);
l_fp r;
r = tspec_intv_to_lfp(a);
@@ -559,7 +548,7 @@ TEST(timespecops, test_ToLFPabs) {
for (i = 0; i < (int)COUNTOF(fdata); ++i) {
struct timespec a = timespec_init(1, fdata[i].nsec);
- l_fp E = l_fp_init(1 + JAN_1970, fdata[i].frac);
+ l_fp E = lfpinit(1 + JAN_1970, fdata[i].frac);
l_fp r;
r = tspec_stamp_to_lfp(a);
@@ -582,7 +571,7 @@ TEST(timespecops, test_FromLFPbittest) {
uint32_t tsf;
for (tsf = 0; tsf < ~((uint32_t)(1000)); tsf += 1000) {
struct timespec E = timespec_init(1, my_tsf_to_tick(tsf));
- l_fp a = l_fp_init(1, tsf);
+ l_fp a = lfpinit(1, tsf);
struct timespec r;
r = lfp_intv_to_tspec(a);
@@ -600,7 +589,7 @@ TEST(timespecops, test_FromLFPrelPos) {
int i;
for (i = 0; i < (int)COUNTOF(fdata); ++i) {
- l_fp a = l_fp_init(1, fdata[i].frac);
+ l_fp a = lfpinit(1, fdata[i].frac);
struct timespec E = timespec_init(1, fdata[i].nsec);
struct timespec r;
@@ -617,7 +606,7 @@ TEST(timespecops, test_FromLFPrelNeg) {
int i;
for (i = 0; i < (int)COUNTOF(fdata); ++i) {
- l_fp a = l_fp_init(~0, fdata[i].frac);
+ l_fp a = lfpinit(~0, fdata[i].frac);
struct timespec E = timespec_init(-1, fdata[i].nsec);
struct timespec r;
View it on GitLab: https://gitlab.com/NTPsec/ntpsec/compare/bb22de588d12d63d8d722ed4f7c26e6d85cd2e18...a4e8c84ca36edf5bd9627516acbbe25b6a3e8a13
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.ntpsec.org/pipermail/vc/attachments/20170103/9ba521a0/attachment.html>
More information about the vc
mailing list