Re: git: ee8b0c436d72 - main - lib/libc/string: replace ffs/fls implementations with clang builtins

From: John Baldwin <jhb_at_FreeBSD.org>
Date: Mon, 10 Jul 2023 19:56:19 UTC
On 7/3/23 1:23 PM, Robert Clausecker wrote:
> The branch main has been updated by fuz:
> 
> URL: https://cgit.FreeBSD.org/src/commit/?id=ee8b0c436d7221c25e8be3c3fe1f9da78b9d5b16
> 
> commit ee8b0c436d7221c25e8be3c3fe1f9da78b9d5b16
> Author:     Robert Clausecker <fuz@FreeBSD.org>
> AuthorDate: 2023-06-23 16:22:54 +0000
> Commit:     Robert Clausecker <fuz@FreeBSD.org>
> CommitDate: 2023-07-03 20:18:27 +0000
> 
>      lib/libc/string: replace ffs/fls implementations with clang builtins
>      
>      Most architectures we support (except for riscv64) have instructions
>      to compute these functions very quickly.  Replace old code with the
>      ffs and clz builtin functions, allowing clang to generate good code
>      for all architectures.
>      
>      As a consequence, toss out arm and i386 ffs() implementations.
>      
>      Sponsored by:   FreeBSD Foundation
>      Approved by:    mhorne
>      MFC after:      1 week
>      Differential Revision: https://reviews.freebsd.org/D40730
> ---
>   lib/libc/arm/string/Makefile.inc  |  1 -
>   lib/libc/arm/string/ffs.S         | 57 ---------------------------------------
>   lib/libc/i386/string/Makefile.inc |  1 -
>   lib/libc/i386/string/ffs.S        | 56 --------------------------------------
>   lib/libc/string/ffs.c             | 12 ++++-----
>   lib/libc/string/ffsl.c            | 12 ++++-----
>   lib/libc/string/ffsll.c           | 12 ++++-----
>   lib/libc/string/fls.c             | 13 +++++----
>   lib/libc/string/flsl.c            | 14 +++++-----
>   lib/libc/string/flsll.c           | 13 +++++----
>   10 files changed, 34 insertions(+), 157 deletions(-)
> 
> diff --git a/lib/libc/string/ffs.c b/lib/libc/string/ffs.c
> index 738ef90ce091..c011b3390612 100644
> --- a/lib/libc/string/ffs.c
> +++ b/lib/libc/string/ffs.c
> @@ -3,6 +3,10 @@
>    *
>    * Copyright (c) 1990, 1993
>    *	The Regents of the University of California.  All rights reserved.
> + * Copyright (c) 2023 The FreeBSD Foundation
> + *
> + * Portions of this software were developed by Robert Clausecker
> + * <fuz@FreeBSD.org> under sponsorship from the FreeBSD Foundation.
>    *
>    * Redistribution and use in source and binary forms, with or without
>    * modification, are permitted provided that the following conditions
> @@ -43,11 +47,5 @@ __FBSDID("$FreeBSD$");
>   int
>   ffs(int mask)
>   {
> -	int bit;
> -
> -	if (mask == 0)
> -		return(0);
> -	for (bit = 1; !(mask & 1); bit++)
> -		mask = (unsigned int)mask >> 1;
> -	return (bit);
> +	return (__builtin_ffs(mask));

This breaks the build on GCC:

/usr/home/john/work/freebsd/main/lib/libc/string/ffs.c: In function 'ffs':
/usr/home/john/work/freebsd/main/lib/libc/string/ffs.c:48:1: error: infinite recursion detected [-Werror=infinite-recursion]
    48 | ffs(int mask)
       | ^~~
/usr/home/john/work/freebsd/main/lib/libc/string/ffs.c:50:17: note: recursive call
    50 |         return (__builtin_ffs(mask));
       |                ~^~~~~~~~~~~~~~~~~~~~
cc1: all warnings being treated as errors
*** Error code 1

-- 
John Baldwin