Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

ktime: Optimize ktime_divns for constant divisors

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.

Cc: Arnd Bergmann <arnd.bergmann@linaro.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Nicolas Pitre <nico@linaro.org>
Acked-by: Arnd Bergmann <arnd@arndb.de>
Signed-off-by: Nicolas Pitre <nico@linaro.org>
Signed-off-by: John Stultz <john.stultz@linaro.org>

authored by

Nicolas Pitre and committed by
John Stultz
8b618628 eaa27f34

+13 -3
+11 -1
include/linux/ktime.h
··· 166 166 } 167 167 168 168 #if BITS_PER_LONG < 64 169 - extern u64 ktime_divns(const ktime_t kt, s64 div); 169 + extern u64 __ktime_divns(const ktime_t kt, s64 div); 170 + static inline u64 ktime_divns(const ktime_t kt, s64 div) 171 + { 172 + if (__builtin_constant_p(div) && !(div >> 32)) { 173 + u64 ns = kt.tv64; 174 + do_div(ns, div); 175 + return ns; 176 + } else { 177 + return __ktime_divns(kt, div); 178 + } 179 + } 170 180 #else /* BITS_PER_LONG < 64 */ 171 181 # define ktime_divns(kt, div) (u64)((kt).tv64 / (div)) 172 182 #endif
+2 -2
kernel/time/hrtimer.c
··· 266 266 /* 267 267 * Divide a ktime value by a nanosecond value 268 268 */ 269 - u64 ktime_divns(const ktime_t kt, s64 div) 269 + u64 __ktime_divns(const ktime_t kt, s64 div) 270 270 { 271 271 u64 dclc; 272 272 int sft = 0; ··· 282 282 283 283 return dclc; 284 284 } 285 - EXPORT_SYMBOL_GPL(ktime_divns); 285 + EXPORT_SYMBOL_GPL(__ktime_divns); 286 286 #endif /* BITS_PER_LONG >= 64 */ 287 287 288 288 /*