linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] powerpc: Discard ffs() function and use builtin_ffs instead
@ 2016-05-12 15:32 Christophe Leroy
  2016-05-13  6:16 ` Michael Ellerman
  0 siblings, 1 reply; 5+ messages in thread
From: Christophe Leroy @ 2016-05-12 15:32 UTC (permalink / raw)
  To: Benjamin Herrenschmidt, Paul Mackerras, Michael Ellerman, Scott Wood
  Cc: linux-kernel, linuxppc-dev

With the ffs() function as defined in arch/powerpc/include/asm/bitops.h
GCC will not optimise the code in case of constant parameter, as shown
by the small exemple below.

int ffs_test(void)
{
	return 4 << ffs(31);
}

c0012334 <ffs_test>:
c0012334:       39 20 00 01     li      r9,1
c0012338:       38 60 00 04     li      r3,4
c001233c:       7d 29 00 34     cntlzw  r9,r9
c0012340:       21 29 00 20     subfic  r9,r9,32
c0012344:       7c 63 48 30     slw     r3,r3,r9
c0012348:       4e 80 00 20     blr

With this patch, the same function will compile as follows:

c0012334 <ffs_test>:
c0012334:       38 60 00 08     li      r3,8
c0012338:       4e 80 00 20     blr

Signed-off-by: Christophe Leroy <christophe.leroy@c-s.fr>
---
 arch/powerpc/include/asm/bitops.h | 11 +----------
 1 file changed, 1 insertion(+), 10 deletions(-)

diff --git a/arch/powerpc/include/asm/bitops.h b/arch/powerpc/include/asm/bitops.h
index 59abc62..75e3ebb 100644
--- a/arch/powerpc/include/asm/bitops.h
+++ b/arch/powerpc/include/asm/bitops.h
@@ -218,16 +218,7 @@ static __inline__ unsigned long __ffs(unsigned long x)
 	return __ilog2(x & -x);
 }
 
-/*
- * ffs: find first bit set. This is defined the same way as
- * the libc and compiler builtin ffs routines, therefore
- * differs in spirit from the above ffz (man ffs).
- */
-static __inline__ int ffs(int x)
-{
-	unsigned long i = (unsigned long)x;
-	return __ilog2(i & -i) + 1;
-}
+#include <asm-generic/bitops/builtin-ffs.h>
 
 /*
  * fls: find last (most-significant) bit set.
-- 
2.1.0

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: powerpc: Discard ffs() function and use builtin_ffs instead
  2016-05-12 15:32 [PATCH] powerpc: Discard ffs() function and use builtin_ffs instead Christophe Leroy
@ 2016-05-13  6:16 ` Michael Ellerman
  2016-05-13  6:53   ` Christophe Leroy
  2016-05-13  8:29   ` Gabriel Paubert
  0 siblings, 2 replies; 5+ messages in thread
From: Michael Ellerman @ 2016-05-13  6:16 UTC (permalink / raw)
  To: Christophe Leroy, Benjamin Herrenschmidt, Paul Mackerras, Scott Wood
  Cc: linuxppc-dev, linux-kernel

On Thu, 2016-12-05 at 15:32:22 UTC, Christophe Leroy wrote:
> With the ffs() function as defined in arch/powerpc/include/asm/bitops.h
> GCC will not optimise the code in case of constant parameter, as shown
> by the small exemple below.
> 
> int ffs_test(void)
> {
> 	return 4 << ffs(31);
> }
> 
> c0012334 <ffs_test>:
> c0012334:       39 20 00 01     li      r9,1
> c0012338:       38 60 00 04     li      r3,4
> c001233c:       7d 29 00 34     cntlzw  r9,r9
> c0012340:       21 29 00 20     subfic  r9,r9,32
> c0012344:       7c 63 48 30     slw     r3,r3,r9
> c0012348:       4e 80 00 20     blr
> 
> With this patch, the same function will compile as follows:
> 
> c0012334 <ffs_test>:
> c0012334:       38 60 00 08     li      r3,8
> c0012338:       4e 80 00 20     blr


But what code does it generate when it's not a constant?

And which gcc version first added the builtin version?

cheers

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: powerpc: Discard ffs() function and use builtin_ffs instead
  2016-05-13  6:16 ` Michael Ellerman
@ 2016-05-13  6:53   ` Christophe Leroy
  2016-09-16 10:01     ` Christophe Leroy
  2016-05-13  8:29   ` Gabriel Paubert
  1 sibling, 1 reply; 5+ messages in thread
From: Christophe Leroy @ 2016-05-13  6:53 UTC (permalink / raw)
  To: Michael Ellerman, Benjamin Herrenschmidt, Paul Mackerras, Scott Wood
  Cc: linuxppc-dev, linux-kernel



Le 13/05/2016 à 08:16, Michael Ellerman a écrit :
> On Thu, 2016-12-05 at 15:32:22 UTC, Christophe Leroy wrote:
>> With the ffs() function as defined in arch/powerpc/include/asm/bitops.h
>> GCC will not optimise the code in case of constant parameter, as shown
>> by the small exemple below.
>>
>> int ffs_test(void)
>> {
>> 	return 4 << ffs(31);
>> }
>>
>> c0012334 <ffs_test>:
>> c0012334:       39 20 00 01     li      r9,1
>> c0012338:       38 60 00 04     li      r3,4
>> c001233c:       7d 29 00 34     cntlzw  r9,r9
>> c0012340:       21 29 00 20     subfic  r9,r9,32
>> c0012344:       7c 63 48 30     slw     r3,r3,r9
>> c0012348:       4e 80 00 20     blr
>>
>> With this patch, the same function will compile as follows:
>>
>> c0012334 <ffs_test>:
>> c0012334:       38 60 00 08     li      r3,8
>> c0012338:       4e 80 00 20     blr
>
> But what code does it generate when it's not a constant?

The generated code is the same with and without the patch when not a 
constant:

int ffs_test2(int x)
{
     return ffs(x);
}

c001233c <ffs_test2>:
c001233c:       7d 23 00 d0     neg     r9,r3
c0012340:       7d 23 18 38     and     r3,r9,r3
c0012344:       7c 63 00 34     cntlzw  r3,r3
c0012348:       20 63 00 20     subfic  r3,r3,32
c001234c:       4e 80 00 20     blr

>
> And which gcc version first added the builtin version?
Don't know, but __builtin_ffs() is already used in 
arch/powerpc/include/asm/page_32.h

Christophe

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: powerpc: Discard ffs() function and use builtin_ffs instead
  2016-05-13  6:16 ` Michael Ellerman
  2016-05-13  6:53   ` Christophe Leroy
@ 2016-05-13  8:29   ` Gabriel Paubert
  1 sibling, 0 replies; 5+ messages in thread
From: Gabriel Paubert @ 2016-05-13  8:29 UTC (permalink / raw)
  To: Michael Ellerman
  Cc: Christophe Leroy, Benjamin Herrenschmidt, Paul Mackerras,
	Scott Wood, linuxppc-dev, linux-kernel

On Fri, May 13, 2016 at 04:16:57PM +1000, Michael Ellerman wrote:
> On Thu, 2016-12-05 at 15:32:22 UTC, Christophe Leroy wrote:
> > With the ffs() function as defined in arch/powerpc/include/asm/bitops.h
> > GCC will not optimise the code in case of constant parameter, as shown
> > by the small exemple below.
> > 
> > int ffs_test(void)
> > {
> > 	return 4 << ffs(31);
> > }
> > 
> > c0012334 <ffs_test>:
> > c0012334:       39 20 00 01     li      r9,1
> > c0012338:       38 60 00 04     li      r3,4
> > c001233c:       7d 29 00 34     cntlzw  r9,r9
> > c0012340:       21 29 00 20     subfic  r9,r9,32
> > c0012344:       7c 63 48 30     slw     r3,r3,r9
> > c0012348:       4e 80 00 20     blr
> > 
> > With this patch, the same function will compile as follows:
> > 
> > c0012334 <ffs_test>:
> > c0012334:       38 60 00 08     li      r3,8
> > c0012338:       4e 80 00 20     blr
> 
> 
> But what code does it generate when it's not a constant?
> 
> And which gcc version first added the builtin version?

It already existed in gcc-2.95, which you do not want to use to compile
anything today but I have in a corner of a chroot environment to maintain
~1997 vintage embedded stuff, running a 2.2.12 kernel!

Hopefully this clears up your concerns :-)

    Cheers,
    Gabriel

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: powerpc: Discard ffs() function and use builtin_ffs instead
  2016-05-13  6:53   ` Christophe Leroy
@ 2016-09-16 10:01     ` Christophe Leroy
  0 siblings, 0 replies; 5+ messages in thread
From: Christophe Leroy @ 2016-09-16 10:01 UTC (permalink / raw)
  To: Michael Ellerman
  Cc: Benjamin Herrenschmidt, Paul Mackerras, Scott Wood, linuxppc-dev,
	linux-kernel



Le 13/05/2016 à 08:53, Christophe Leroy a écrit :
>
>
> Le 13/05/2016 à 08:16, Michael Ellerman a écrit :
>> On Thu, 2016-12-05 at 15:32:22 UTC, Christophe Leroy wrote:
>>> With the ffs() function as defined in arch/powerpc/include/asm/bitops.h
>>> GCC will not optimise the code in case of constant parameter, as shown
>>> by the small exemple below.
>>>
>>> int ffs_test(void)
>>> {
>>>     return 4 << ffs(31);
>>> }
>>>
>>> c0012334 <ffs_test>:
>>> c0012334:       39 20 00 01     li      r9,1
>>> c0012338:       38 60 00 04     li      r3,4
>>> c001233c:       7d 29 00 34     cntlzw  r9,r9
>>> c0012340:       21 29 00 20     subfic  r9,r9,32
>>> c0012344:       7c 63 48 30     slw     r3,r3,r9
>>> c0012348:       4e 80 00 20     blr
>>>
>>> With this patch, the same function will compile as follows:
>>>
>>> c0012334 <ffs_test>:
>>> c0012334:       38 60 00 08     li      r3,8
>>> c0012338:       4e 80 00 20     blr
>>
>> But what code does it generate when it's not a constant?
>
> The generated code is the same with and without the patch when not a 
> constant:
>
> int ffs_test2(int x)
> {
>     return ffs(x);
> }
>
> c001233c <ffs_test2>:
> c001233c:       7d 23 00 d0     neg     r9,r3
> c0012340:       7d 23 18 38     and     r3,r9,r3
> c0012344:       7c 63 00 34     cntlzw  r3,r3
> c0012348:       20 63 00 20     subfic  r3,r3,32
> c001234c:       4e 80 00 20     blr
>
>>
>> And which gcc version first added the builtin version?
> Don't know, but __builtin_ffs() is already used in 
> arch/powerpc/include/asm/page_32.h
>

Hi Michael,

Any change to get it into 4.9 ?

Christophe

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2016-09-16 10:01 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-05-12 15:32 [PATCH] powerpc: Discard ffs() function and use builtin_ffs instead Christophe Leroy
2016-05-13  6:16 ` Michael Ellerman
2016-05-13  6:53   ` Christophe Leroy
2016-09-16 10:01     ` Christophe Leroy
2016-05-13  8:29   ` Gabriel Paubert

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).