Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys/sys For 32 bit the mstohz and hztoms functions evaluate ...



details:   https://anonhg.NetBSD.org/src/rev/629734f2c007
branches:  trunk
changeset: 454821:629734f2c007
user:      christos <christos%NetBSD.org@localhost>
date:      Sat Sep 28 15:10:58 2019 +0000

description:
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.

diffstat:

 sys/sys/param.h |  37 +++++++++++++++++++++++++------------
 1 files changed, 25 insertions(+), 12 deletions(-)

diffs (57 lines):

diff -r 0d2f43dc518b -r 629734f2c007 sys/sys/param.h
--- a/sys/sys/param.h   Sat Sep 28 12:34:56 2019 +0000
+++ b/sys/sys/param.h   Sat Sep 28 15:10:58 2019 +0000
@@ -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)))



Home | Main Index | Thread Index | Old Index