[PATCH 1/5] string: Add fallback implementation for ctz/clz
Adhemerval Zanella Netto
adhemerval.zanella@linaro.org
Mon Sep 1 15:38:47 GMT 2025
On 01/09/25 12:19, Wilco Dijkstra wrote:
> Hi Adhemerval,
>
>> The algorithm is based on De Bruijn sequence, and it might be used
>> if the ABI does not want to use libgcc fallback implementation (for
>> instance on statup code where libgcc might be compiled with some
>> options not support at the time, like stack protector).
>
> This feels like we are moving back in time. We just added stdbit.h, so I
> would suggest moving existing uses of __builtin_clz/ctz/popcount to that
> so that zero case is well defined.
I add some background why I had to do these on the cover letter, but basically
I found on s390x that calling the libgcc builtin is not fully supported without
setting up TLS because the builtin could be built with stack-protection enabled.
It might happen on other ABIs that uses the generic strchrnul implementation and
relying on libgcc for __builtin_c[t,l]z.
>
> Note also that if you don't have fast clz/ctz instruction, it's actually a bad
> idea to emulate it fully. Given I think you can only have bit 7 set in each byte,
> index_first should be as trivial as: (((c & -c) >> 7) * 0x00010203) >> 24;
These is a goo strategy, does it work on both LE and BE? I will check this out
and see if we can remove the ctz/clz functions.
More information about the Libc-alpha
mailing list