From: Ingo Molnar <mingo@elte.hu>

We have various different implementations of MSEC[S]_TO_JIFFIES and
JIFFIES_TO_MSEC[S].  We recently had a compile-time clash in USB.

Fix all that up.

- The SCTP version was very inefficient.  Hopefully this version is accurate
  enough.

- Optimise for the HZ=100 and HZ=1000 cases

- This version does round-up, so sleep(9 milliseconds) works OK on 100HZ.

- We still have lots of jiffies_to_msec and msec_to_jiffies implementations.


---

 25-akpm/include/asm-i386/param.h |    2 --
 25-akpm/include/linux/time.h     |   18 ++++++++++++++++++
 25-akpm/include/net/irda/irda.h  |    2 --
 25-akpm/include/net/sctp/sctp.h  |    5 -----
 25-akpm/kernel/sched.c           |    9 +--------
 5 files changed, 19 insertions(+), 17 deletions(-)

diff -puN include/asm-i386/param.h~MSEC_TO_JIFFIES-fixups include/asm-i386/param.h
--- 25/include/asm-i386/param.h~MSEC_TO_JIFFIES-fixups	2004-05-12 21:08:44.610035832 -0700
+++ 25-akpm/include/asm-i386/param.h	2004-05-12 21:08:44.619034464 -0700
@@ -5,8 +5,6 @@
 # define HZ		1000		/* Internal kernel timer frequency */
 # define USER_HZ	100		/* .. some user interfaces are in "ticks" */
 # define CLOCKS_PER_SEC		(USER_HZ)	/* like times() */
-# define JIFFIES_TO_MSEC(x)	(x)
-# define MSEC_TO_JIFFIES(x)	(x)
 #endif
 
 #ifndef HZ
diff -puN include/linux/time.h~MSEC_TO_JIFFIES-fixups include/linux/time.h
--- 25/include/linux/time.h~MSEC_TO_JIFFIES-fixups	2004-05-12 21:08:44.611035680 -0700
+++ 25-akpm/include/linux/time.h	2004-05-12 21:08:44.619034464 -0700
@@ -177,6 +177,24 @@ struct timezone {
 	(SH_DIV((MAX_JIFFY_OFFSET >> SEC_JIFFIE_SC) * TICK_NSEC, NSEC_PER_SEC, 1) - 1)
 
 #endif
+
+/*
+ * Convert jiffies to milliseconds and back.
+ *
+ * Avoid unnecessary multiplications/divisions in the
+ * two most common HZ cases:
+ */
+#if HZ == 1000
+# define JIFFIES_TO_MSECS(x)	(x)
+# define MSECS_TO_JIFFIES(x)	(x)
+#elif HZ == 100
+# define JIFFIES_TO_MSECS(x)	((x) * 10)
+# define MSECS_TO_JIFFIES(x)	(((x) + 9) / 10)
+#else
+# define JIFFIES_TO_MSECS(x)	(((x) * 1000) / HZ)
+# define MSECS_TO_JIFFIES(x)	(((x) * HZ + 999) / 1000)
+#endif
+
 /*
  * The TICK_NSEC - 1 rounds up the value to the next resolution.  Note
  * that a remainder subtract here would not do the right thing as the
diff -puN include/net/irda/irda.h~MSEC_TO_JIFFIES-fixups include/net/irda/irda.h
--- 25/include/net/irda/irda.h~MSEC_TO_JIFFIES-fixups	2004-05-12 21:08:44.613035376 -0700
+++ 25-akpm/include/net/irda/irda.h	2004-05-12 21:08:44.620034312 -0700
@@ -83,8 +83,6 @@ if(!(expr)) do { \
 #define MESSAGE(args...) printk(KERN_INFO args)
 #define ERROR(args...)   printk(KERN_ERR args)
 
-#define MSECS_TO_JIFFIES(ms) (((ms)*HZ+999)/1000)
-
 /*
  *  Magic numbers used by Linux-IrDA. Random numbers which must be unique to 
  *  give the best protection
diff -puN include/net/sctp/sctp.h~MSEC_TO_JIFFIES-fixups include/net/sctp/sctp.h
--- 25/include/net/sctp/sctp.h~MSEC_TO_JIFFIES-fixups	2004-05-12 21:08:44.614035224 -0700
+++ 25-akpm/include/net/sctp/sctp.h	2004-05-12 21:08:44.620034312 -0700
@@ -116,11 +116,6 @@
 #define SCTP_STATIC static
 #endif
 
-#define MSECS_TO_JIFFIES(msec) \
-	(((msec / 1000) * HZ) + ((msec % 1000) * HZ) / 1000)
-#define JIFFIES_TO_MSECS(jiff) \
-	(((jiff / HZ) * 1000) + ((jiff % HZ) * 1000) / HZ)
-
 /*
  * Function declarations.
  */
diff -puN kernel/sched.c~MSEC_TO_JIFFIES-fixups kernel/sched.c
--- 25/kernel/sched.c~MSEC_TO_JIFFIES-fixups	2004-05-12 21:08:44.616034920 -0700
+++ 25-akpm/kernel/sched.c	2004-05-12 21:08:44.623033856 -0700
@@ -75,13 +75,6 @@
 #define NS_TO_JIFFIES(TIME)	((TIME) / (1000000000 / HZ))
 #define JIFFIES_TO_NS(TIME)	((TIME) * (1000000000 / HZ))
 
-#ifndef JIFFIES_TO_MSEC
-# define JIFFIES_TO_MSEC(x) ((x) * 1000 / HZ)
-#endif
-#ifndef MSEC_TO_JIFFIES
-# define MSEC_TO_JIFFIES(x) ((x) * HZ / 1000)
-#endif
-
 /*
  * These are the 'tuning knobs' of the scheduler:
  *
@@ -1880,7 +1873,7 @@ static void rebalance_tick(int this_cpu,
 			interval *= sd->busy_factor;
 
 		/* scale ms to jiffies */
-		interval = MSEC_TO_JIFFIES(interval);
+		interval = MSECS_TO_JIFFIES(interval);
 		if (unlikely(!interval))
 			interval = 1;
 

_