[Git][NTPsec/ntpsec][master] Change some nasty macros to inline functions with saner calling conventions.

Eric S. Raymond gitlab at mg.gitlab.com
Sat Dec 31 17:21:46 UTC 2016


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


Commits:
c731bd52 by Eric S. Raymond at 2016-12-31T12:21:30-05:00
Change some nasty macros to inline functions with saner calling conventions.

- - - - -


12 changed files:

- include/ntp_fp.h
- libntp/systime.c
- ntpd/ntp_control.c
- ntpd/ntp_packetstamp.c
- ntpd/ntp_proto.c
- ntpd/ntp_refclock.c
- ntpd/refclock_gpsd.c
- ntpd/refclock_jupiter.c
- ntpd/refclock_nmea.c
- ntpd/refclock_oncore.c
- ntpfrob/jitter.c
- tests/libntp/lfpfunc.c


Changes:

=====================================
include/ntp_fp.h
=====================================
--- a/include/ntp_fp.h
+++ b/include/ntp_fp.h
@@ -188,72 +188,79 @@ typedef uint32_t u_fp;
 
 #include <math.h>	/* ldexp() */
 
-#define M_DTOLFP(d, r_ui, r_uf)		/* double to l_fp */	\
-	do {							\
-		double	d_tmp;					\
-		uint64_t	q_tmp;					\
-		int	M_isneg;					\
-								\
-		d_tmp = (d);					\
-		M_isneg = (d_tmp < 0.);				\
-		if (M_isneg) {					\
-			d_tmp = -d_tmp;				\
-		}						\
-		q_tmp = (uint64_t)ldexp(d_tmp, 32);		\
-		if (M_isneg) {					\
-			q_tmp = ~q_tmp + 1;			\
-		}						\
-		(r_uf) = (uint32_t)q_tmp;			\
-		(r_ui) = (uint32_t)(q_tmp >> 32);		\
-	} while (false)
+static inline l_fp dtolfp(double d)
+/* double to l_fp */
+{
+	double	d_tmp;
+	uint64_t	q_tmp;
+	int	M_isneg;
+	l_fp	r;
+
+	d_tmp = (d);
+	M_isneg = (d_tmp < 0.);
+	if (M_isneg) {
+		d_tmp = -d_tmp;
+	}
+	q_tmp = (uint64_t)ldexp(d_tmp, 32);
+	if (M_isneg) {
+		q_tmp = ~q_tmp + 1;
+	}
+	setlfpfrac(r, (uint32_t)q_tmp);
+	setlfpuint(r, (uint32_t)(q_tmp >> 32));
+	return r;
+}
 
-#define M_LFPTOD(r_ui, r_uf, d) 	/* l_fp to double */	\
-	do {							\
-		double	d_tmp;					\
-		uint64_t	q_tmp;					\
-		int	M_isneg;				\
-								\
-		q_tmp = ((uint64_t)(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)
+static inline double lfptod(l_fp r)
+/* l_fp to double */
+{
+	double	d;
+	uint64_t	q_tmp;
+	int	M_isneg;
+
+	q_tmp = ((uint64_t)lfpuint(r) << 32) + lfpfrac(r);
+	M_isneg = M_ISNEG(lfpuint(r));
+	if (M_isneg) {
+		q_tmp = ~q_tmp + 1;
+	}
+	d = ldexp((double)q_tmp, -32);
+	if (M_isneg) {
+		d = -d;
+	}
+	return d;
+}
 
 #else /* use only 32 bit unsigned values */
 
-#define M_DTOLFP(d, r_ui, r_uf) 		/* double to l_fp */ \
-	do { \
-		double d_tmp; \
-		if ((d_tmp = (d)) < 0) { \
-			(r_ui) = (uint32_t)(-d_tmp); \
-			(r_uf) = (uint32_t)(-(d_tmp + (double)(r_ui)) * FRAC); \
-			M_NEG((r_ui), (r_uf)); \
-		} else { \
-			(r_ui) = (uint32_t)d_tmp; \
-			(r_uf) = (uint32_t)((d_tmp - (double)(r_ui)) * FRAC); \
-		} \
-	} while (0)
-#define M_LFPTOD(r_ui, r_uf, d) 		/* l_fp to double */ \
-	do { \
-		uint32_t l_thi, l_tlo; \
-		l_thi = (r_ui); l_tlo = (r_uf); \
-		if (M_ISNEG(l_thi)) { \
-			M_NEG(l_thi, l_tlo); \
-			(d) = -((double)l_thi + (double)l_tlo / FRAC); \
-		} else { \
-			(d) = (double)l_thi + (double)l_tlo / FRAC; \
-		} \
-	} while (0)
-#endif
+static inline l_fp dtolfp(double d)
+/* double to l_fp */
+{
+	double d_tmp;
+	l_fp r;
+	if ((d_tmp = (d)) < 0) {
+		setlfpuint(r, (uint32_t)(-d_tmp));
+		setlfpfrac(r, (uint32_t)(-(d_tmp + (double)lfpuint(r)) * FRAC));
+		M_NEG((r_ui), (r_uf));
+	} else { \
+		setlfpuint(r, (uint32_t)d_tmp);
+		setlfpfrac(r, (uint32_t)((d_tmp - (double)lfpuint(r)) * FRAC));
+	}
+	return r;
+}
 
-#define DTOLFP(d, v) 	M_DTOLFP((d), lfpuint(*v), lfpfrac(*v))
-#define LFPTOD(v, d) 	M_LFPTOD(lfpuint(*v), lfpfrac(*v), (d))
+static inline double lfptod(l_fp r)
+/* l_fp to double */
+{
+	uint32_t l_thi, l_tlo;
+	l_thi = lfpuint(r); l_tlo = lfpfrac(r);
+	if (M_ISNEG(l_thi)) {
+		M_NEG(l_thi, l_tlo);
+		(d) = -((double)l_thi + (double)l_tlo / FRAC);
+	} else {
+		(d) = (double)l_thi + (double)l_tlo / FRAC;
+	}
+	return d;
+}
+#endif
 
 /*
  * Prototypes


=====================================
libntp/systime.c
=====================================
--- a/libntp/systime.c
+++ b/libntp/systime.c
@@ -187,7 +187,7 @@ normalize_time(
 	 * Add in the fuzz.
 	 */
 	dfuzz = rand * 2. / FRAC * sys_fuzz;
-	DTOLFP(dfuzz, &lfpfuzz);
+	lfpfuzz = dtolfp(dfuzz);
 	L_ADD(&result, &lfpfuzz);
 
 	/*
@@ -208,7 +208,7 @@ normalize_time(
 				dfuzz);
 			lfpdelta = lfp_prev;
 			L_SUB(&lfpdelta, &result);
-			LFPTOD(&lfpdelta, ddelta);
+			ddelta = lfptod(lfpdelta);
 			msyslog(LOG_ERR,
 				"prev get_systime 0x%x.%08x is %.9f later than 0x%x.%08x",
 				lfpuint(lfp_prev), lfpfrac(lfp_prev),
@@ -362,8 +362,8 @@ step_systime(
 #endif
 
 	/* get the complete jump distance as l_fp */
-	DTOLFP(sys_residual, &fp_sys);
-	DTOLFP(step,         &fp_ofs);
+	fp_sys = dtolfp(sys_residual);
+	fp_ofs = dtolfp(step);
 	L_ADD(&fp_ofs, &fp_sys);
 
 	/* ---> time-critical path starts ---> */


=====================================
ntpd/ntp_control.c
=====================================
--- a/ntpd/ntp_control.c
+++ b/ntpd/ntp_control.c
@@ -1757,7 +1757,7 @@ ctl_putsys(
 		break;
 
 	case CS_AUTHDELAY:
-		LFPTOD(&sys_authdelay, dtemp);
+		dtemp = lfptod(sys_authdelay);
 		ctl_putdbl(sys_var[varid].text, dtemp * 1e3);
 		break;
 


=====================================
ntpd/ntp_packetstamp.c
=====================================
--- a/ntpd/ntp_packetstamp.c
+++ b/ntpd/ntp_packetstamp.c
@@ -190,7 +190,7 @@ fetch_packetstamp(
 #endif  /* USE_SCM_TIMESTAMP */
 			}
 			fuzz = ntp_random() * 2. / FRAC * sys_fuzz;
-			DTOLFP(fuzz, &lfpfuzz);
+			lfpfuzz = dtolfp(fuzz);
 			L_ADD(&nts, &lfpfuzz);
 #ifdef ENABLE_DEBUG_TIMING
 			dts = ts;


=====================================
ntpd/ntp_proto.c
=====================================
--- a/ntpd/ntp_proto.c
+++ b/ntpd/ntp_proto.c
@@ -2670,7 +2670,7 @@ measure_tick_fuzz(void)
 	max_repeats = 0;
 	repeats = 0;
 	changes = 0;
-	DTOLFP(MINSTEP, &minstep);
+	minstep = dtolfp(MINSTEP);
 	get_systime(&last);
 	for (i = 0; i < MAXLOOPS && changes < MINCHANGES; i++) {
 		get_systime(&val);
@@ -2681,7 +2681,7 @@ measure_tick_fuzz(void)
 			max_repeats = max(repeats, max_repeats);
 			repeats = 0;
 			changes++;
-			LFPTOD(&ldiff, diff);
+			diff = lfptod(ldiff);
 			tick = min(diff, tick);
 		} else {
 			repeats++;


=====================================
ntpd/ntp_refclock.c
=====================================
--- a/ntpd/ntp_refclock.c
+++ b/ntpd/ntp_refclock.c
@@ -364,7 +364,7 @@ refclock_process_offset(
 	pp->lastrec = lastrec;
 	lftemp = lasttim;
 	L_SUB(&lftemp, &lastrec);
-	LFPTOD(&lftemp, doffset);
+	doffset = lfptod(lftemp);
 	SAMPLE(doffset + fudge);
 }
 
@@ -408,7 +408,7 @@ refclock_process_f(
 
 	setlfpuint(offset, sec);
 	setlfpfrac(offset, 0);
-	DTOLFP(pp->nsec / 1e9, &ltemp);
+	ltemp = dtolfp(pp->nsec / 1e9);
 	L_ADD(&offset, &ltemp);
 	refclock_process_offset(pp, offset, pp->lastrec, fudge);
 	return true;


=====================================
ntpd/refclock_gpsd.c
=====================================
--- a/ntpd/refclock_gpsd.c
+++ b/ntpd/refclock_gpsd.c
@@ -772,13 +772,13 @@ gpsd_control(
 	UNUSED_ARG(out_st);
 
 	if (peer == up->pps_peer) {
-		DTOLFP(pp->fudgetime1, &up->pps_fudge2);
+		up->pps_fudge2 = dtolfp(pp->fudgetime1);
 		if ( ! (pp->sloppyclockflag & CLK_FLAG1))
 			peer->flags &= ~FLAG_PPS;
 	} else {
 		/* save preprocessed fudge times */
-		DTOLFP(pp->fudgetime1, &up->pps_fudge);
-		DTOLFP(pp->fudgetime2, &up->ibt_fudge);
+		up->pps_fudge = dtolfp(pp->fudgetime1);
+		up->ibt_fudge = dtolfp(pp->fudgetime2);
 
 		if (MODE_OP_MODE(up->mode ^ peer->ttl)) {
 			leave_opmode(peer, up->mode);


=====================================
ntpd/refclock_jupiter.c
=====================================
--- a/ntpd/refclock_jupiter.c
+++ b/ntpd/refclock_jupiter.c
@@ -714,7 +714,7 @@ jupiter_control(
 	pp = peer->procptr;
 	instance = pp->unitptr;
 
-	DTOLFP(pp->fudgetime2, &instance->limit);
+	instance->limit = dtolfp(pp->fudgetime2);
 	/* Force positive value. */
 	if (L_ISNEG(&instance->limit))
 		L_NEG(&instance->limit);


=====================================
ntpd/refclock_nmea.c
=====================================
--- a/ntpd/refclock_nmea.c
+++ b/ntpd/refclock_nmea.c
@@ -721,7 +721,7 @@ refclock_ppsrelate(
 	pp_stamp = tspec_stamp_to_lfp(timeout);
 	pp_delta = *rd_stamp;
 	L_SUB(&pp_delta, &pp_stamp);
-	LFPTOD(&pp_delta, delta);
+	delta = lfptod(pp_delta);
 	delta += pp_fudge - *rd_fudge;
 	if (fabs(delta) > 1.5)
 		return PPS_RELATE_NONE; /* PPS timeout control */
@@ -744,7 +744,7 @@ refclock_ppsrelate(
 	/* check against reftime if PPS PLL can be used */
 	pp_delta = *reftime;
 	L_SUB(&pp_delta, &pp_stamp);
-	LFPTOD(&pp_delta, delta);
+	delta = lfptod(pp_delta);
 	delta += pp_fudge;
 	if (fabs(delta) > 0.45)
 		return PPS_RELATE_EDGE; /* cannot PLL with PPS code */


=====================================
ntpd/refclock_oncore.c
=====================================
--- a/ntpd/refclock_oncore.c
+++ b/ntpd/refclock_oncore.c
@@ -1765,7 +1765,7 @@ oncore_get_timestamp(
 
 	ts_tmp = ts;
 	setlfpuint(ts_tmp, 0);	/* zero integer part */
-	LFPTOD(&ts_tmp, dmy);	/* convert fractional part to a double */
+	dmy = lfptod(ts_tmp);	/* convert fractional part to a double */
 	j = 1.0e9*dmy;		/* then to integer ns */
 
 	Rsm = 0;


=====================================
ntpfrob/jitter.c
=====================================
--- a/ntpfrob/jitter.c
+++ b/ntpfrob/jitter.c
@@ -84,7 +84,7 @@ void jitter(const iomode mode)
 	 */
 	for (i = 0; i < NBUF; i ++) {
 		get_clocktime(&tr);
-		LFPTOD(&tr, gtod[i]);
+		gtod[i] = lfptod(tr);
 	}
 
 	/*


=====================================
tests/libntp/lfpfunc.c
=====================================
--- a/tests/libntp/lfpfunc.c
+++ b/tests/libntp/lfpfunc.c
@@ -131,20 +131,6 @@ static int l_fp_signum(const l_fp first)
 	return (lfpuint(first) || lfpfrac(first));
 }
 
-static double l_fp_convert_to_double(const l_fp first)
-{
-	double res;
-	LFPTOD(&first, res);
-	return res;
-}
-
-static l_fp l_fp_init_from_double( double rhs)
-{
-	l_fp temp;
-	DTOLFP(rhs, &temp);
-	return temp;
-}
-
 static void l_fp_swap(l_fp * first, l_fp *second)
 {
 	l_fp temp = *second;
@@ -372,11 +358,11 @@ TEST(lfpfunc, FDF_RoundTrip) {
 
 	for (idx = 0; idx < addsub_cnt; ++idx) {
 		l_fp op1 = l_fp_init(addsub_tab[idx][0].h, addsub_tab[idx][0].l);
-		double op2 = l_fp_convert_to_double(op1);
-		l_fp op3 = l_fp_init_from_double(op2); 
+		double op2 = lfptod(op1);
+		l_fp op3 = dtolfp(op2);
 
 		l_fp temp = l_fp_subtract(op1, op3);
-		double d = l_fp_convert_to_double(temp);
+		double d = lfptod(temp);
 		TEST_ASSERT_DOUBLE_WITHIN(eps(op2), 0.0, fabs(d));
 	}
 



View it on GitLab: https://gitlab.com/NTPsec/ntpsec/commit/c731bd52ed327a063aab0ff650b58eb30b3acf74
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.ntpsec.org/pipermail/vc/attachments/20161231/1aba8b75/attachment.html>


More information about the vc mailing list