From f2bbaf99a1d7f51e6c98997ecb07a6287a428c97 Mon Sep 17 00:00:00 2001 From: Nicolas Pitre Date: Wed, 3 Dec 2014 14:43:06 -0500 Subject: [PATCH] ktime: Optimize ktime_divns for constant divisors [ Upstream commit 8b618628b2bf83512fc8df5e8672619d65adfdfb ] At least on ARM, do_div() is optimized to turn constant divisors into an inline multiplication by the reciprocal value at compile time. However this optimization is missed entirely whenever ktime_divns() is used and the slow out-of-line division code is used all the time. Let ktime_divns() use do_div() inline whenever the divisor is constant and small enough. This will make things like ktime_to_us() and ktime_to_ms() much faster. Change-Id: I6a672b10d6d6b373b327f9548135ededfd1e1835 Cc: Arnd Bergmann Cc: Thomas Gleixner Cc: Ingo Molnar Cc: Nicolas Pitre Acked-by: Arnd Bergmann Signed-off-by: Nicolas Pitre Signed-off-by: John Stultz Signed-off-by: Sasha Levin Signed-off-by: Kevin F. Haggerty --- include/linux/ktime.h | 12 +++++++++++- kernel/hrtimer.c | 4 ++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/include/linux/ktime.h b/include/linux/ktime.h index b07a9a976aa..ad2c587013e 100644 --- a/include/linux/ktime.h +++ b/include/linux/ktime.h @@ -302,7 +302,17 @@ static inline int ktime_compare(const ktime_t cmp1, const ktime_t cmp2) } #if BITS_PER_LONG < 64 -extern u64 ktime_divns(const ktime_t kt, s64 div); +extern u64 __ktime_divns(const ktime_t kt, s64 div); +static inline u64 ktime_divns(const ktime_t kt, s64 div) +{ + if (__builtin_constant_p(div) && !(div >> 32)) { + u64 ns = kt.tv64; + do_div(ns, div); + return ns; + } else { + return __ktime_divns(kt, div); + } +} #else /* BITS_PER_LONG < 64 */ # define ktime_divns(kt, div) (u64)((kt).tv64 / (div)) #endif diff --git a/kernel/hrtimer.c b/kernel/hrtimer.c index d18f6216190..6ad5e5940e3 100644 --- a/kernel/hrtimer.c +++ b/kernel/hrtimer.c @@ -323,7 +323,7 @@ EXPORT_SYMBOL_GPL(ktime_sub_ns); /* * Divide a ktime value by a nanosecond value */ -u64 ktime_divns(const ktime_t kt, s64 div) +u64 __ktime_divns(const ktime_t kt, s64 div) { u64 dclc; int sft = 0; @@ -339,7 +339,7 @@ u64 ktime_divns(const ktime_t kt, s64 div) return dclc; } -EXPORT_SYMBOL_GPL(ktime_divns); +EXPORT_SYMBOL_GPL(__ktime_divns); #endif /* BITS_PER_LONG >= 64 */ /*