This is the mail archive of the newlib@sourceware.org mailing list for the newlib project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [PATCH] Workaround for ffs() on LP64 targets


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.

-- 
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org

Attachment: signature.asc
Description: OpenPGP digital signature


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]