This is the mail archive of the
newlib@sourceware.org
mailing list for the newlib project.
Re: [PATCH] Workaround for ffs() on LP64 targets
- From: Kito Cheng <kito dot cheng at gmail dot com>
- To: Sebastian Huber <sebastian dot huber at embedded-brains dot de>
- Cc: newlib at sourceware dot org
- Date: Thu, 27 Jul 2017 16:29:04 +0800
- Subject: Re: [PATCH] Workaround for ffs() on LP64 targets
- Authentication-results: sourceware.org; auth=none
- References: <20170727080624.24818-1-sebastian.huber@embedded-brains.de>
it's will make aarch64 gen worse code (compare to __builtin_ffs
version) since aarch64 have clz, but I don't have better idea too...
On Thu, Jul 27, 2017 at 4:06 PM, Sebastian Huber
<sebastian.huber@embedded-brains.de> 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);
> +#else
>
> return (__builtin_ffs(i));
> +#endif
> }
> --
> 2.12.3
>