Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752687AbbDFADz (ORCPT ); Sun, 5 Apr 2015 20:03:55 -0400 Received: from smtprelay0133.hostedemail.com ([216.40.44.133]:43759 "EHLO smtprelay.hostedemail.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1752561AbbDFADw (ORCPT ); Sun, 5 Apr 2015 20:03:52 -0400 X-Session-Marker: 6A6F6540706572636865732E636F6D X-Spam-Summary: 2,0,0,,d41d8cd98f00b204,joe@perches.com,:::::::::::::::::::,RULES_HIT:41:69:355:379:541:599:960:973:988:989:1260:1277:1311:1313:1314:1345:1359:1373:1437:1515:1516:1518:1534:1543:1593:1594:1711:1730:1747:1777:1792:2393:2553:2559:2562:2828:3138:3139:3140:3141:3142:3354:3622:3865:3866:3867:3868:3870:3871:4321:5007:6119:6120:6261:7875:7901:7903:7904:8603:9389:9592:10004:10400:10848:11026:11232:11473:11658:11914:12043:12291:12438:12517:12519:12555:12683:12740:21080,0,RBL:none,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:fn,MSBL:0,DNSBL:none,Custom_rules:0:0:0 X-HE-Tag: move80_5b9c997376144 X-Filterd-Recvd-Size: 4992 Message-ID: <1428278627.2775.75.camel@perches.com> Subject: Re: [PATCH 2/3] time: allow gcc to fold constants when using msecs_to_jiffies From: Joe Perches To: Nicholas Mc Guire Cc: Michal Marek , Masahiro Yamada , Sam Ravnborg , Thomas Gleixner , "H. Peter Alvin" , John Stultz , Andrew Hunter , Paul Turner , linux-kernel@vger.kernel.org Date: Sun, 05 Apr 2015 17:03:47 -0700 In-Reply-To: <1428218636-3780-3-git-send-email-hofrat@osadl.org> References: <1428218636-3780-1-git-send-email-hofrat@osadl.org> <1428218636-3780-3-git-send-email-hofrat@osadl.org> Content-Type: text/plain; charset="ISO-8859-1" X-Mailer: Evolution 3.12.10-0ubuntu1~14.10.1 Mime-Version: 1.0 Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 3973 Lines: 125 On Sun, 2015-04-05 at 09:23 +0200, Nicholas Mc Guire wrote: > The majority of the msecs_to_jiffies() users in the kernel are passing in > constants which would allow gcc to do constant folding by checking with > __builtin_constant_p() in msecs_to_jiffies(). > > The original msecs_to_jiffies is renamed to __msecs_to_jiffies and aside > from the removal of the check for negative values being moved out, is > unaltered. At least for gcc 4.9, this doesn't allow the compiler to optimize / precalculation msecs_to_jiffies calls with a constant. This does: (on top of your patch x86-64 defconfig) $ size vmlinux.o.* text data bss dec hex filename 11770523 1505971 1018454 14294948 da1fa4 vmlinux.o.next-b0a12fb5bc8 11770530 1505971 1018454 14294955 da1fab vmlinux.o.next-b0a12fb5bc8-inline 11768734 1505971 1018454 14293159 da18a7 vmlinux.o.next-b0a12fb5bc8-macro I think this should still move the if (m) < 0 back into the original __msecs_to_jiffies function. --- include/linux/jiffies.h | 71 ++++++++++++++++++++++++++++++++----------------- 1 file changed, 46 insertions(+), 25 deletions(-) diff --git a/include/linux/jiffies.h b/include/linux/jiffies.h index a75158e..f8fe9f7 100644 --- a/include/linux/jiffies.h +++ b/include/linux/jiffies.h @@ -291,6 +291,39 @@ static inline u64 jiffies_to_nsecs(const unsigned long j) extern unsigned long __msecs_to_jiffies(const unsigned int m); +#if HZ <= MSEC_PER_SEC && !(MSEC_PER_SEC % HZ) +#define const_msecs_to_jiffies(m) \ +({ \ + unsigned long j; \ + if ((int)m < 0) \ + j = MAX_JIFFY_OFFSET; \ + else \ + j = (m + (MSEC_PER_SEC / HZ) - 1) / (MSEC_PER_SEC / HZ); \ + j; \ +}) +#elif HZ > MSEC_PER_SEC && !(HZ % MSEC_PER_SEC) +#define const_msecs_to_jiffies(m) \ +({ \ + unsigned long j; \ + if (m > jiffies_to_msecs(MAX_JIFFY_OFFSET)) \ + j = MAX_JIFFY_OFFSET; \ + else \ + j = m * (HZ / MSEC_PER_SEC); \ + j; \ +}) +#else +#define const_msecs_to_jiffies(m) \ +({ \ + unsigned long j; \ + if (HZ > MSEC_PER_SEC && m > jiffies_to_msecs(MAX_JIFFY_OFFSET))\ + j = MAX_JIFFY_OFFSET; \ + else \ + j = (MSEC_TO_HZ_MUL32 * m + MSEC_TO_HZ_ADJ32) \ + >> MSEC_TO_HZ_SHR32; \ + j; \ +}) +#endif + /** * msecs_to_jiffies: - convert milliseconds to jiffies * @m: time in millisecons @@ -313,31 +346,19 @@ extern unsigned long __msecs_to_jiffies(const unsigned int m); * allow constant folding and the actual conversion must be done at * runtime. */ -static inline unsigned long msecs_to_jiffies(const unsigned int m) -{ - /* - * Negative value, means infinite timeout: - */ - if ((int)m < 0) - return MAX_JIFFY_OFFSET; - - if (__builtin_constant_p(m)) { -#if HZ <= MSEC_PER_SEC && !(MSEC_PER_SEC % HZ) - return (m + (MSEC_PER_SEC / HZ) - 1) / (MSEC_PER_SEC / HZ); -#elif HZ > MSEC_PER_SEC && !(HZ % MSEC_PER_SEC) - if (m > jiffies_to_msecs(MAX_JIFFY_OFFSET)) - return MAX_JIFFY_OFFSET; - return m * (HZ / MSEC_PER_SEC); -#else - if (HZ > MSEC_PER_SEC && m > jiffies_to_msecs(MAX_JIFFY_OFFSET)) - return MAX_JIFFY_OFFSET; - - return (MSEC_TO_HZ_MUL32 * m + MSEC_TO_HZ_ADJ32) - >> MSEC_TO_HZ_SHR32; -#endif - } else - return __msecs_to_jiffies(m); -} +#define msecs_to_jiffies(m) \ +({ \ + unsigned long j; \ + if (__builtin_constant_p(m)) { \ + if ((int)m < 0) \ + j = MAX_JIFFY_OFFSET; \ + else \ + j = const_msecs_to_jiffies(m); \ + } else { \ + j = __msecs_to_jiffies(m); \ + } \ + j; \ +}) extern unsigned long usecs_to_jiffies(const unsigned int u); extern unsigned long timespec_to_jiffies(const struct timespec *value); -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/