[PATCH] Workaround for ffs() on LP64 targets

Corinna Vinschen vinschen@redhat.com
Thu Jul 27 12:49:00 GMT 2017


On Jul 27 14:33, Sebastian Huber wrote:
> On 27/07/17 14:27, Corinna Vinschen wrote:
> 
> > On Jul 27 13:24, Sebastian Huber wrote:
> > > On 27/07/17 13:13, Eric Blake wrote:
> > > 
> > > > On 07/27/2017 03:06 AM, Sebastian Huber wrote:
> > > > > Signed-off-by: Sebastian Huber<sebastian.huber@embedded-brains.de>
> > > > > ---
> > > > >    newlib/libc/misc/ffs.c | 11 +++++++++++
> > > > >    1 file changed, 11 insertions(+)
> > > > > 
> > > > > diff --git a/newlib/libc/misc/ffs.c b/newlib/libc/misc/ffs.c
> > > > > index ba5700920..a09cbd3bb 100644
> > > > > --- a/newlib/libc/misc/ffs.c
> > > > > +++ b/newlib/libc/misc/ffs.c
> > > > > @@ -31,6 +31,17 @@ No supporting OS subroutines are required.  */
> > > > >    int
> > > > >    ffs(int i)
> > > > >    {
> > > > > +#ifdef __LP64__
> > > > > +	/* GCC would expand the __builtin_ffs() to ffs() in this case */
> > > > > +	int bit;
> > > > > +
> > > > > +	if (i == 0)
> > > > > +		return (0);
> > > > > +	for (bit = 1; !(i & 1); bit++)
> > > > > +		i = (unsigned int)i >> 1;
> > > > > +	return (bit);
> > > > If we're going to open-code it to work around the compiler creating an
> > > > infloop recursion to ffs(), at least code a straight-line version
> > > > without branches, rather than the painfully slow bit-by-bit loop.
> > > > There's plenty of examples on the web of writing ffs() by using
> > > > bit-twiddling without branching.
> > > This is roughly the same implementation we had before. I do not intend to
> > > optimize this.
> > Still, __LP64__ is unacceptable.  Cygwin would be affected by this as
> > well and would have to revert to its former own ffs implementation.
> > 
> > Reverting to a C-based implementation should only be performed on a
> > case-by-case basis.
> 
> Yes, so maybe something like this
> 
> #if defined(__LP64__) && defined(__riscv)
> 
> or a target-specific ffs.c file similar to memcpy.c, etc.

I'm inclined to favor a target-specific file.  This would also allow
to implement the replacement in assembler easily.


Corinna

-- 
Corinna Vinschen
Cygwin Maintainer
Red Hat
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: not available
URL: <http://sourceware.org/pipermail/newlib/attachments/20170727/16b054d8/attachment.sig>


More information about the Newlib mailing list