[PATCH v2] string: Use builtins for ffs and ffsll

Richard Henderson richard.henderson@linaro.org
Thu Aug 24 19:44:12 GMT 2023


On 8/24/23 10:13, Adhemerval Zanella via Libc-alpha wrote:
> diff --git a/string/ffsll.c b/string/ffsll.c
> index 0cc461a1cf..2315fe1bd7 100644
> --- a/string/ffsll.c
> +++ b/string/ffsll.c
> @@ -18,20 +18,26 @@
>   #include <limits.h>
>   #define ffsl __something_else
>   #include <string.h>
> -
>   #undef	ffsll
> +#include <math-use-builtins.h>
> +#include <libc-diag.h>
>   
>   /* Find the first bit set in I.  */
>   int
> -ffsll (long long int i)
> +__ffsll (long long int i)
>   {
> +#if USE_FFSLL_BUILTIN
> +  return __builtin_ffsll (i);
> +#else
>     unsigned long long int x = i & -i;
>   
>     if (x <= 0xffffffff)
>       return ffs (i);
>     else
>       return 32 + ffs (i >> 32);
> +#endif
>   }

It seems like you also need

#if USE_FFS_BUILTIN
# define ffs  __builtin_ffs
#endif

in order to avoid a function call for the 32-bit host case, e.g.


> diff --git a/sysdeps/arm/armv6t2/ffsll.S b/sysdeps/arm/armv6t2/ffsll.S
> deleted file mode 100644
> index bc3cbf81b0..0000000000
> --- a/sysdeps/arm/armv6t2/ffsll.S
> +++ /dev/null
> @@ -1,50 +0,0 @@
> -/* ffsll -- find first set bit in a long long, from least significant end.
> -   Copyright (C) 2013-2023 Free Software Foundation, Inc.
> -   This file is part of the GNU C Library.
> -
> -   The GNU C Library is free software; you can redistribute it and/or
> -   modify it under the terms of the GNU Lesser General Public
> -   License as published by the Free Software Foundation; either
> -   version 2.1 of the License, or (at your option) any later version.
> -
> -   The GNU C Library is distributed in the hope that it will be useful,
> -   but WITHOUT ANY WARRANTY; without even the implied warranty of
> -   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
> -   Lesser General Public License for more details.
> -
> -   You should have received a copy of the GNU Lesser General Public
> -   License along with the GNU C Library.  If not, see
> -   <https://www.gnu.org/licenses/>.  */
> -
> -#include <sysdep.h>
> -
> -	.syntax unified
> -	.text
> -
> -ENTRY (ffsll)
> -	@ If low part is 0, operate on the high part.  Ensure that the
> -	@ word on which we operate is in r0.  Set r2 to the bit offset
> -	@ of the word being considered.  Set the flags for the word
> -	@ being operated on.
> -#ifdef __ARMEL__
> -	cmp	r0, #0
> -	itee	ne
> -	movne	r2, #0
> -	moveq	r2, #32
> -	movseq	r0, r1
> -#else
> -	cmp	r1, #0
> -	ittee	ne
> -	movne	r2, #0
> -	movne	r0, r1
> -	moveq	r2, #32
> -	cmpeq	r0, #0
> -#endif
> -	@ Perform the ffs on r0.
> -	rbit	r0, r0
> -	ittt	ne
> -	clzne	r0, r0
> -	addne	r2, r2, #1
> -	addne	r0, r0, r2
> -	bx	lr
> -END (ffsll)
> diff --git a/sysdeps/arm/math-use-builtins-ffs.h b/sysdeps/arm/math-use-builtins-ffs.h
> new file mode 100644
> index 0000000000..c0f108c264
> --- /dev/null
> +++ b/sysdeps/arm/math-use-builtins-ffs.h
> @@ -0,0 +1,2 @@
> +#define USE_FFS_BUILTIN   1
> +#define USE_FFSLL_BUILTIN 0

Here.  Also, this file is misplaced wrt the armv6t2 files you're removing.

This will work, I suppose, but you'll get the libgcc implementation.
If you're willing to settle for that, you might as well drop all of
this extra use-builtins stuff and unconditionally use __builtin_ffs*.


r~


More information about the Libc-alpha mailing list