From patchwork Wed Dec 3 19:43:06 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolas Pitre X-Patchwork-Id: 5432861 Return-Path: X-Original-To: patchwork-linux-arm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 227C99F1D4 for ; Wed, 3 Dec 2014 19:45:50 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 42DAB20357 for ; Wed, 3 Dec 2014 19:45:49 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.9]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 70DC120353 for ; Wed, 3 Dec 2014 19:45:48 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1XwFpn-0004am-Pu; Wed, 03 Dec 2014 19:43:35 +0000 Received: from mail-qg0-f49.google.com ([209.85.192.49]) by bombadil.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1XwFpk-0004Wn-1I for linux-arm-kernel@lists.infradead.org; Wed, 03 Dec 2014 19:43:32 +0000 Received: by mail-qg0-f49.google.com with SMTP id a108so11293822qge.8 for ; Wed, 03 Dec 2014 11:43:08 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:date:from:to:cc:subject:message-id:user-agent :mime-version:content-type; bh=IyEcQ9LTVP9Z0ynxL3mJQuK84b7O91ebUK8a2l1O1rc=; b=bQeubPb+RMVnwhIgwvPzEcHtGZFiOs2FCb0iDTuoyPXzDf0PqxtcdJWvDq+exh7cwi Egnld7/V5BFp3EfZtYopXUfhxGY57x2UgKN/OjL7Lsur7RHn1LxjiVm+Ek9GtCZAGLMd 3dF5NoRx58JHTQN7uflFcg+ftCihccIe42+XWlO0YKjqjzHZi7U8zlY1RhMeyyQn//tf LDINsY4r+tDDWRGtBdQB8QiCNPaoLKDzo2Rr2vzSlk0LdfPNevVma9piRH6KJP+ozbEz Ljz5xOJrd67hxlSh5M69Rn2lgwKx2OCluK6dQjVMGz/dGQwBcHB7M95TPOUgUk2/K9Uf RKlQ== X-Gm-Message-State: ALoCoQklmk+iemAhz5wAGQn4ZIyAgrprxwGXHklzZjMr92QYVtF/TvYmhJTLAUkszTVk8xrbFIQN X-Received: by 10.224.69.200 with SMTP id a8mr10529741qaj.40.1417635788806; Wed, 03 Dec 2014 11:43:08 -0800 (PST) Received: from xanadu.home ([2607:fa48:6d48:fb10:21c:c0ff:fe2a:325b]) by mx.google.com with ESMTPSA id t5sm24328412qge.16.2014.12.03.11.43.07 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Wed, 03 Dec 2014 11:43:07 -0800 (PST) Date: Wed, 3 Dec 2014 14:43:06 -0500 (EST) From: Nicolas Pitre To: Thomas Gleixner , John Stultz Subject: [PATCH] optimize ktime_divns for constant divisors Message-ID: User-Agent: Alpine 2.11 (LFD 23 2013-08-11) MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20141203_114332_154935_FC3A49B5 X-CRM114-Status: GOOD ( 12.67 ) X-Spam-Score: -0.7 (/) Cc: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.18-1 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Spam-Status: No, score=-2.6 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_LOW, T_RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP 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. Signed-off-by: Nicolas Pitre Acked-by: Arnd Bergmann diff --git a/include/linux/ktime.h b/include/linux/ktime.h index c9d645ad98..411dd8bfe5 100644 --- a/include/linux/ktime.h +++ b/include/linux/ktime.h @@ -166,7 +166,17 @@ static inline bool ktime_before(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/time/hrtimer.c b/kernel/time/hrtimer.c index 37e50aadd4..890535c41c 100644 --- a/kernel/time/hrtimer.c +++ b/kernel/time/hrtimer.c @@ -266,7 +266,7 @@ lock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags) /* * 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; @@ -282,7 +282,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 */ /*