[Mesa-dev] [PATCH 1/2] mesa: Add a _mesa_fls() function to find the last bit set in a word.
Brian Paul
brianp at vmware.com
Wed Sep 12 06:14:33 PDT 2012
On 09/11/2012 11:14 PM, Kenneth Graunke wrote:
> ffs() finds the least significant bit set; _mesa_fls() finds the /most/
> significant bit.
>
> Signed-off-by: Kenneth Graunke<kenneth at whitecape.org>
> ---
> src/mesa/main/imports.c | 22 ++++++++++++++++++++++
> src/mesa/main/imports.h | 2 ++
> 2 files changed, 24 insertions(+)
>
> Wow. NAK my last patch which used ffs(). Totally backwards and just
> happened to work for the one test because it only used limited values.
>
> diff --git a/src/mesa/main/imports.c b/src/mesa/main/imports.c
> index 934a2d0..eac8e21 100644
> --- a/src/mesa/main/imports.c
> +++ b/src/mesa/main/imports.c
> @@ -316,6 +316,28 @@ _mesa_bitcount_64(uint64_t n)
> }
> #endif
>
> +/**
> + * Find the last (most significant) bit set in a word.
> + *
> + * Essentially ffs() in the reverse direction.
> + */
> +unsigned int
> +_mesa_fls(unsigned int n)
> +{
> +#if defined(__GNUC__)&& ((__GNUC__ * 100 + __GNUC_MINOR__)>= 304)
> + return n == 0 ? 0 : 32 - __builtin_clz(n);
> +#else
> + unsigned int v = 1;
> +
> + if (n == 0)
> + return 0;
> +
> + while (n>>= 1)
> + v++;
> +
> + return v;
> +#endif
> +}
>
> /**
> * Convert a 4-byte float to a 2-byte half float.
> diff --git a/src/mesa/main/imports.h b/src/mesa/main/imports.h
> index abf216c..ebe128e 100644
> --- a/src/mesa/main/imports.h
> +++ b/src/mesa/main/imports.h
> @@ -520,6 +520,8 @@ extern unsigned int
> _mesa_bitcount_64(uint64_t n);
> #endif
>
> +extern unsigned int
> +_mesa_fls(unsigned int n);
>
> extern GLhalfARB
> _mesa_float_to_half(float f);
Reviewed-by: Brian Paul <brianp at vmware.com>
I'd be fine with this as an inline function in the .h file if you wanted.
-Brian
More information about the mesa-dev
mailing list