Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752774AbbDFGkK (ORCPT ); Mon, 6 Apr 2015 02:40:10 -0400 Received: from hofr.at ([212.69.189.236]:48462 "EHLO mail.hofr.at" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750890AbbDFGkI (ORCPT ); Mon, 6 Apr 2015 02:40:08 -0400 Date: Mon, 6 Apr 2015 08:40:05 +0200 From: Nicholas Mc Guire To: Joe Perches Cc: Nicholas Mc Guire , Michal Marek , Masahiro Yamada , Sam Ravnborg , Thomas Gleixner , "H. Peter Alvin" , John Stultz , Andrew Hunter , Paul Turner , linux-kernel@vger.kernel.org Subject: Re: [PATCH 2/3] time: allow gcc to fold constants when using msecs_to_jiffies Message-ID: <20150406064005.GA32350@opentech.at> References: <1428218636-3780-1-git-send-email-hofrat@osadl.org> <1428218636-3780-3-git-send-email-hofrat@osadl.org> <1428278627.2775.75.camel@perches.com> <20150406010025.GA5956@opentech.at> <1428286502.2775.92.camel@perches.com> <20150406042654.GA28155@opentech.at> <1428294823.2775.94.camel@perches.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1428294823.2775.94.camel@perches.com> User-Agent: Mutt/1.5.18 (2008-05-17) Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 3508 Lines: 137 On Sun, 05 Apr 2015, Joe Perches wrote: > On Mon, 2015-04-06 at 06:26 +0200, Nicholas Mc Guire wrote: > > On Sun, 05 Apr 2015, Joe Perches wrote: > > > Try it and look at the generated .lst files with and > > > without the patch I sent. > [] > > from all that I understood it should > > be doable both as macro and inline. > > I think it _should_ be doable too but I also think > the only reason gcc doesn't optimize the inline > is because gcc's optimizer isn't good enough yet. > "unfortunately" I can't blame it on gcc - here is the initial toy-case - test.c and either testi.h or testm.h included - m = TIMEOUT or m = atoi(argv[1]); both in the inline and the macro case gcc reduced the code to a single load mediate or register instruction for the constant - so the optimizer is doing its job. test.c: #include #define HZ 100 #define MSECS_PER_SEC 1000 #define TIMEOUT 100 #include "testi.h" /* inline msecs_to_jiffies */ //#include "testm.h" /* macro versions */ int main(int argc, char **argv) { //int m = atoi(argv[0]); /* non-const */ int m = TIMEOUT; /* const */ printf("%lu\n",msecs_to_jiffies(m)); return 0; } testm.h: #define msecs_to_jiffies(m) \ (__builtin_constant_p (m) \ ? ((m) * HZ / MSECS_PER_SEC ) : __msecs_to_jiffies(m)) unsigned long __msecs_to_jiffies(int m) { return m * HZ / MSECS_PER_SEC ; } first case with a non-const main: .LFB12: .cfi_startproc subq $8, %rsp #, .cfi_def_cfa_offset 16 movq 8(%rsi), %rdi # MEM[(char * *)argv_2(D) + 8B], MEM[(char * *)argv_2(D) + 8B] xorl %eax, %eax # call atoi # movl $1717986919, %edx #, tmp69 movl %eax, %ecx #, m movl $.LC0, %edi #, imull %edx # tmp69 sarl $31, %ecx #, tmp71 xorl %eax, %eax # sarl $2, %edx #, tmp67 subl %ecx, %edx # tmp71, tmp67 movslq %edx, %rsi # tmp67, tmp72 call printf # o second with a constant: main: .LFB12: .cfi_startproc subq $8, %rsp #, .cfi_def_cfa_offset 16 movl $10, %esi #, movl $.LC0, %edi #, xorl %eax, %eax # call printf # inline: ------- testi.h: static inline unsigned long __msecs_to_jiffies(int m) { return m * HZ / MSECS_PER_SEC; } static inline unsigned long msecs_to_jiffies(int m) { return __builtin_constant_p (m) ? (m) * HZ / MSECS_PER_SEC : __msecs_to_jiffies(m); } first case with a non-const main: .LFB13: .cfi_startproc subq $8, %rsp #, .cfi_def_cfa_offset 16 movq (%rsi), %rdi # *argv_1(D), xorl %eax, %eax # call atoi # movl $1717986919, %edx #, tmp68 movl %eax, %ecx #, m movl $.LC0, %edi #, imull %edx # tmp68 sarl $31, %ecx #, tmp70 xorl %eax, %eax # sarl $2, %edx #, tmp66 subl %ecx, %edx # tmp70, tmp66 movslq %edx, %rsi # tmp66, tmp71 call printf # second with a constant: main: .LFB13: .cfi_startproc subq $8, %rsp #, .cfi_def_cfa_offset 16 xorl %esi, %esi # movl $.LC0, %edi #, xorl %eax, %eax # call printf # giving it another run from scratch somewhere I simply screwed up or overlooked some detail. thx! hofrat -- 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/