Module Name:    src
Committed By:   christos
Date:           Sat Sep 28 15:10:58 UTC 2019

Modified Files:
        src/sys/sys: param.h

Log Message:
For 32 bit the mstohz and hztoms functions evaluate their parameter multiple
times. This is inefficient for cases like:
    unsigned ms = hztoms(MIN(timeout, mstohz(INT_MAX)));
Make them inline functions; also provide the 64 bit versions for them here
so all the LP64 machines can use them (before only amd64 and sparc64
specialized mstohz).
Make them both return unsigned int.


To generate a diff of this commit:
cvs rdiff -u -r1.614 -r1.615 src/sys/sys/param.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/sys/param.h
diff -u src/sys/sys/param.h:1.614 src/sys/sys/param.h:1.615
--- src/sys/sys/param.h:1.614	Thu Sep 26 20:32:03 2019
+++ src/sys/sys/param.h	Sat Sep 28 11:10:58 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: param.h,v 1.614 2019/09/27 00:32:03 pgoyette Exp $	*/
+/*	$NetBSD: param.h,v 1.615 2019/09/28 15:10:58 christos Exp $	*/
 
 /*-
  * Copyright (c) 1982, 1986, 1989, 1993
@@ -486,23 +486,36 @@
 #endif
 
 #ifdef _KERNEL
+extern int hz;
 /*
  * macro to convert from milliseconds to hz without integer overflow
- * Default version using only 32bits arithmetics.
- * 64bit port can define 64bit version in their <machine/param.h>
- * 0x20000 is safe for hz < 20000
+ * The 32 bit version uses only 32bit arithmetic; 0x20000 is safe for hz < 20000
+ * the 64 bit version does the computation directly.
  */
 #ifndef mstohz
-#define mstohz(ms) \
-	(__predict_false((ms) >= 0x20000) ? \
-	    ((ms +0u) / 1000u) * hz : \
-	    ((ms +0u) * hz) / 1000u)
+# ifdef _LP64
+#  define mstohz(ms) ((unsigned int)((ms + 0ul) * hz / 1000ul))
+# else
+static __inline unsigned int
+mstohz(unsigned int ms)
+{
+	return __predict_false(ms >= 0x20000u) ?
+	    (ms / 1000u) * hz : (ms * hz) / 1000u;
+}
+# endif
 #endif
+
 #ifndef hztoms
-#define hztoms(t) \
-	(__predict_false((t) >= 0x20000) ? \
-	    ((t +0u) / hz) * 1000u : \
-	    ((t +0u) * 1000u) / hz)
+# ifdef _LP64
+#  define hztoms(t) ((unsigned int)(((t) + 0ul) * 1000ul / hz))
+# else
+static __inline unsigned int
+hztoms(unsigned int t)
+{
+	return __predict_false(t >= 0x20000u) ?
+	    (t / hz) * 1000u : (t * 1000u) / hz;
+}
+# endif
 #endif
 
 #define	hz2bintime(t)	(ms2bintime(hztoms(t)))

Reply via email to