[PATCH v3 1/5] string: Add fallback implementation for ctz/clz

Wilco Dijkstra Wilco.Dijkstra@arm.com
Tue Nov 18 14:11:06 GMT 2025


Hi Adhemerval,

> The libgcc implementation for __builtin_clzl/__builtin_ctzl may require
> access to additional data that is not marked as hidden, which could
> create additional GOT indirection that necessitates RELATIVE relocs.
> And the RELATIVE reloc is an issue if the code is used during static-pie
> startup before self-relocation (for instance, during an assert).
>
> For this case, the ABI can add a string-bitops.h header that defines
> HAVE_BITOPTS_WORKING to 0. A configure check for this issue is tricky
> because it requires linking against the standard libraries, which create
> a lot of RELATIVE relocations and complicate filtering the ones that
> might be created by the builtins.

Note it is likely better to use arithmetic if clz/ctz are not inlined, so it would
be possible to add a configure check. It's not urgent or needed for this patch
though since most targets have clz/ctz instructions.

One minor comment:

-static __always_inline int
+static __always_inline unsigned int
 ctz (find_t c)

We should really rename these to ctzb/clzb (or similar) to make it clearer
they are about bytes, not bits.

OK with that change.

Reviewed-by: Wilco Dijkstra  <Wilco.Dijkstra@arm.com>

Cheers,
Wilco


More information about the Libc-alpha mailing list