Skip to contentSkip to conversation

NTP shiftR cleanup

RFC PATCH In review john stultz 3 participants 3 messages Sep 10, 2005 Timekeeping, clocksource core, NTP, alarmtimer

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 NTP shiftR cleanup

All,
Here is hopefully a fairly uncontroversial cleanup for the NTP code. It
uses min/max to simplify some conditionals and creates a macro shiftR that avoids the numerous ugly conditionals in the NTP code that look like:

	if(a < 0)
		b = -(-a >> shift);
	else
		b = a >> shift;

Replacing it with:

	b = shiftR(a, shift);

This should have zero effect on the logic, however it should probably have a bit of testing just to be sure. I've compiled it for alpha, arm, i386, ppc, ppc64, and s390 and its been running fine on my laptop all day. It applies against Linus' tree from this morning.

Also Thanks to Ingo Oeser for catching a bug earlier in the shiftR macro.

Please let me know if you have any comments or feedback.

thanks
-john

include/linux/timex.h +5 -0
@@ -282,6 +282,11 @@ static inline int ntp_synced(void)
 	return !(time_status & STA_UNSYNC);
 }
 
+/* Required to safely shift negative values */
+#define shiftR(x, s) ({ __typeof__(x) __x = x;\
+		__typeof__(s) __s = s; \
+		(__x < 0) ? (-((-__x) >> (__s))) : ((__x) >> (__s));})
+
 
 #ifdef CONFIG_TIME_INTERPOLATION
 
kernel/time.c +6 -19
@@ -338,30 +338,20 @@ int do_adjtimex(struct timex *txc)
 		        if (mtemp >= MINSEC) {
 			    ltemp = (time_offset / mtemp) << (SHIFT_USEC -
 							      SHIFT_UPDATE);
-			    if (ltemp < 0)
-			        time_freq -= -ltemp >> SHIFT_KH;
-			    else
-			        time_freq += ltemp >> SHIFT_KH;
+			    time_freq = shiftR(ltemp, SHIFT_KH);
 			} else /* calibration interval too short (p. 12) */
 				result = TIME_ERROR;
 		    } else {	/* PLL mode */
 		        if (mtemp < MAXSEC) {
 			    ltemp *= mtemp;
-			    if (ltemp < 0)
-			        time_freq -= -ltemp >> (time_constant +
-							time_constant +
-							SHIFT_KF - SHIFT_USEC);
-			    else
-			        time_freq += ltemp >> (time_constant +
+			    time_freq += shiftR(ltemp,(time_constant +
 						       time_constant +
-						       SHIFT_KF - SHIFT_USEC);
+						       SHIFT_KF - SHIFT_USEC));
 			} else /* calibration interval too long (p. 12) */
 				result = TIME_ERROR;
 		    }
-		    if (time_freq > time_tolerance)
-		        time_freq = time_tolerance;
-		    else if (time_freq < -time_tolerance)
-		        time_freq = -time_tolerance;
+		    time_freq = min(time_freq, time_tolerance);
+		    time_freq = max(time_freq, -time_tolerance);
 		} /* STA_PLL || STA_PPSTIME */
 	    } /* txc->modes & ADJ_OFFSET */
 	    if (txc->modes & ADJ_TICK) {
@@ -384,10 +374,7 @@ leave:	if ((time_status & (STA_UNSYNC|ST
 	if ((txc->modes & ADJ_OFFSET_SINGLESHOT) == ADJ_OFFSET_SINGLESHOT)
 	    txc->offset	   = save_adjust;
 	else {
-	    if (time_offset < 0)
-		txc->offset = -(-time_offset >> SHIFT_UPDATE);
-	    else
-		txc->offset = time_offset >> SHIFT_UPDATE;
+	    txc->offset = shiftR(time_offset, SHIFT_UPDATE);
 	}
 	txc->freq	   = time_freq + pps_freq;
 	txc->maxerror	   = time_maxerror;
kernel/timer.c +9 -37

85 lines not on this page · raw message

#1 lore raw

In article <[email protected]> (at Fri, 09 Sep 2005 18:03:37 -0700), john stultz <[email protected]> says:

Quoted 4 lines

kernel/time.c int do_adjtimex(struct timex *txc) · line 343

Quoted 10 lines
kernel/time.c +1 -4
@@ -338,30 +338,20 @@ int do_adjtimex(struct timex *txc)
 		        if (mtemp >= MINSEC) {
 			    ltemp = (time_offset / mtemp) << (SHIFT_USEC -
 							      SHIFT_UPDATE);
-			    if (ltemp < 0)
-			        time_freq -= -ltemp >> SHIFT_KH;
-			    else
-			        time_freq += ltemp >> SHIFT_KH;
+			    time_freq = shiftR(ltemp, SHIFT_KH);
 			} else /* calibration interval too short (p. 12) */
 				result = TIME_ERROR;

Maybe, "time_freq += shiftR(ltemp, SHIFT_KH);"?

--yoshfuji

#2 lore raw
In reply to john stultz

Hi,

include/linux/timex.h static inline int ntp_synced(void) · line 289

Quoted 5 lines · john stultz
include/linux/timex.h +5 -0
+/* Required to safely shift negative values */
+#define shiftR(x, s) ({ __typeof__(x) __x = x;\
+		__typeof__(s) __s = s; \
+		(__x < 0) ? (-((-__x) >> (__s))) : ((__x) >> (__s));})
+

Some parenthesis are missing and some are redundant. Formatting it so it looks more like normal function makes it more readable:

#define shiftR(x, s) ({				\
	__typeof__(x) __x = (x);		\
	__typeof__(s) __s = (s);		\
	__x < 0 ? -(-__x >> __s) : __x >> __s;	\
})
Quoted 6 lines
kernel/timer.c +0 -2
@@ -792,13 +769,8 @@ static void update_wall_time_one_tick(vo
 	 * advance the tick more.
 	 */
 	time_phase += time_adj;
-	if (time_phase <= -FINENSEC) {
-		long ltemp = -time_phase >> (SHIFT_SCALE - 10);

kernel/timer.c static void update_wall_time_one_tick(vo · line 776

Quoted 10 lines
kernel/timer.c +2 -5
-		time_phase += ltemp << (SHIFT_SCALE - 10);
-		delta_nsec -= ltemp;
-	}
-	else if (time_phase >= FINENSEC) {
-		long ltemp = time_phase >> (SHIFT_SCALE - 10);
+	if (abs(time_phase) >= FINENSEC) {
+		long ltemp = shiftR(time_phase, (SHIFT_SCALE - 10));
 		time_phase -= ltemp << (SHIFT_SCALE - 10);
 		delta_nsec += ltemp;
 	}

It would be interesting to check, whether gcc produces the same code here.

bye, Roman