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

Wilco Dijkstra Wilco.Dijkstra@arm.com
Mon Sep 1 22:09:39 GMT 2025


Hi Adhemerval,

>> 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.

This is a byte-wise ctz so suitable for little endian index_first. Clz would be like
c |= c >> 8, c |= c >> 16, ((c >> 7) * 0x30f0f0f0) >> 28;

It works similar for 64-bit too.

Cheers,
Wilco


More information about the Libc-alpha mailing list