bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [PATCH] ffs: new module


From: Pádraig Brady
Subject: Re: [PATCH] ffs: new module
Date: Tue, 12 Jul 2011 01:11:41 +0100
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.8) Gecko/20100227 Thunderbird/3.0.3

On 12/07/11 00:21, Eric Blake wrote:
> Libvirt wants to use ffs() to avoid dragging in -lm for log2().

This is a useful module.

> +#include <strings.h>
> +
> +int
> +ffs (int i)
> +{
> +#if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
> +  return __builtin_ffs (i);
> +#else
> +  /* 
> http://graphics.stanford.edu/~seander/bithacks.html#ZerosOnRightMultLookup
> +     gives this deBruijn constant for a branch-less computation, although
> +     that table counted trailing zeros rather than bit position.  */
> +  static unsigned int table[] = {
> +    1, 2, 29, 3, 30, 15, 25, 4, 31, 23, 21, 16, 26, 18, 5, 9,
> +    32, 28, 14, 24, 22, 20, 17, 8, 27, 13, 19, 7, 12, 6, 11, 10
> +  };
> +  unsigned int u = i;
> +  unsigned int bit = u & -u;
> +  return table[(bit * 0x077cb531U) >> 27] - !i;
> +#endif
> +}

Should uint32_t be used above?
For my own reference I'd previously noticed these:
http://www.hackersdelight.org/HDcode/ntz.c.txt

cheers,
Pádraig.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]