[PATCH] Workaround for ffs() on LP64 targets

Brian Inglis Brian.Inglis@SystematicSw.ab.ca
Thu Jul 27 21:03:00 GMT 2017


On 2017-07-27 05:24, Sebastian Huber wrote:
> On 27/07/17 13:13, Eric Blake wrote:
>> On 07/27/2017 03:06 AM, Sebastian Huber wrote:
>>> Signed-off-by: Sebastian Huber
>>> ---
>>>   newlib/libc/misc/ffs.c | 11 +++++++++++
>>>   1 file changed, 11 insertions(+)
>>>
>>> diff --git a/newlib/libc/misc/ffs.c b/newlib/libc/misc/ffs.c
>>> index ba5700920..a09cbd3bb 100644
>>> --- a/newlib/libc/misc/ffs.c
>>> +++ b/newlib/libc/misc/ffs.c
>>> @@ -31,6 +31,17 @@ No supporting OS subroutines are required.  */
>>>   int
>>>   ffs(int i)
>>>   {
>>> +#ifdef __LP64__
>>> +    /* GCC would expand the __builtin_ffs() to ffs() in this case */
>>> +    int bit;
>>> +
>>> +    if (i == 0)
>>> +        return (0);
>>> +    for (bit = 1; !(i & 1); bit++)
>>> +        i = (unsigned int)i >> 1;
>>> +    return (bit);
>> If we're going to open-code it to work around the compiler creating an
>> infloop recursion to ffs(), at least code a straight-line version
>> without branches, rather than the painfully slow bit-by-bit loop.
>> There's plenty of examples on the web of writing ffs() by using
>> bit-twiddling without branching.

Definitive twiddling reference is now Hacker's Delight 2nd ed, Henry S. Warren,
Jr., 2013, Pearson/InformIT/AW; available in ebook formats:
https://github.com/jyfc/ebook/blob/master/02_algorithm/Hacker's%20Delight%202nd%20Edition.pdf

https://www.safaribooksonline.com/library/view/hackers-delight-second/9780133084993/

https://en.wikipedia.org/wiki/Hacker%27s_Delight
http://www.hackersdelight.org/

> This is roughly the same implementation we had before. I do not intend to 
> optimize this.

Programmers using these functions expect the usage cost to be trivial and fairly
constant ~ O(log2(bits)) not O(bits); if not, they may implement their own!

Try this one, seems decently short; adjust for different word sizes; with gcc
-O3 on x86-64 compiles to 32 instructions branch free: YMMV

int
ffsll( long long in )
{
/* find first set == 1 + count trailing zeros */
	int index = 64;


	if (!in)			return 0;

	in &= -in;			/* clear all but lsb set */
/*
 * for ctz remove above test and add next line
 *	if (in)				--index;
 */
	if (in & 0x00000000FFFFFFFF)	index -= 32;
	if (in & 0x0000FFFF0000FFFF)	index -= 16;
	if (in & 0x00FF00FF00FF00FF)	index -= 8;
	if (in & 0x0F0F0F0F0F0F0F0F)	index -= 4;
	if (in & 0x3333333333333333)	index -= 2;
	if (in & 0x5555555555555555)	index -= 1;

	return index;
}

-- 
Take care. Thanks, Brian Inglis, Calgary, Alberta, Canada



More information about the Newlib mailing list