Source-Changes-HG archive

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

[src-draft/trunk]: src/sys/net80211 Make tick conversion macros type safe and...



details:   https://anonhg.NetBSD.org/src-all/rev/fdc8adde62a9
branches:  trunk
changeset: 371658:fdc8adde62a9
user:      Martin Husemann <martin%NetBSD.org@localhost>
date:      Fri Mar 18 17:51:26 2022 +0100

description:
Make tick conversion macros type safe and optimize slightly
for the common (hz == 100) and trivial (hz == 1000) case,
going via 64bit arithmetic in all other cases.

diffstat:

 sys/net80211/ieee80211_netbsd.h |  20 ++++++++++++++++----
 1 files changed, 16 insertions(+), 4 deletions(-)

diffs (30 lines):

diff -r a400d9434cd4 -r fdc8adde62a9 sys/net80211/ieee80211_netbsd.h
--- a/sys/net80211/ieee80211_netbsd.h   Thu Mar 10 06:50:42 2022 +1100
+++ b/sys/net80211/ieee80211_netbsd.h   Fri Mar 18 17:51:26 2022 +0100
@@ -568,10 +568,22 @@
        (((_ifp)->if_flags & IFF_UP) && \
         ((_ifp)->if_flags & IFF_RUNNING))
 
-/* XXX TODO: cap these at 1, as hz may not be 1000 */
-#define        msecs_to_ticks(ms)      (((ms)*hz)/1000)
-#define        ticks_to_msecs(t)       (1000*(t) / hz)
-#define        ticks_to_secs(t)        ((t) / hz)
+/*
+ * Convert times to ticks and back.
+ * Result will be uint32_t, but we expand to uint64_t to avoid
+ * overflow/underflow. Result will always be at least 1.
+ */
+#define        msecs_to_ticks(m)                       \
+       ulmax(1, (uint32_t)(                    \
+               (hz == 1000) ? (m) :            \
+               (hz == 100) ? ((m)/10) :        \
+                       ((uint64_t)(m) * (uint64_t)hz)/(uint64_t)1000))
+#define        ticks_to_msecs(t)                       \
+       ulmax(1, (uint32_t)(                    \
+               (hz == 1000) ? (t) :            \
+               (hz == 100) ? ((t)*10) :        \
+                       (((uint64_t)(t) * (uint64_t)1000)/(uint64_t)hz)))
+#define        ticks_to_secs(t)        (uint)((t) / hz)
 
 #define ieee80211_time_after(a,b)      ((long)(b) - (long)(a) < 0)
 #define ieee80211_time_before(a,b)     ieee80211_time_after(b,a)



Home | Main Index | Thread Index | Old Index