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

Wilco Dijkstra Wilco.Dijkstra@arm.com
Mon Sep 1 15:19:04 GMT 2025


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.

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;

Cheers,
Wilco


More information about the Libc-alpha mailing list