cogl-bitmask: Use ffsl to speedup bitmask iteration

Instead of testing each bit when iterating a bitmask, we can use ffsl
to skip over unset bits in single instruction. That way it will scale
by the number of bits set, not the total number of bits.

ffsl is a non-standard function which glibc only provides by defining
GNUC_SOURCE. However if we are compiling with GCC we can avoid that
mess and just use the equivalent builtin. When not compiling for GCC
it will fall back to _cogl_util_ffs if the size of ints and longs are
the same (which is the case on i686). Otherwise it fallbacks to a slow
function implementation.

Reviewed-by: Robert Bragg <robert@linux.intel.com>
This commit is contained in:
Neil Roberts
2011-10-28 20:09:53 +01:00
parent f0f9493f5c
commit 2ba4fe417a
4 changed files with 51 additions and 9 deletions

View File

@ -77,3 +77,26 @@ _cogl_util_ffs (int num)
return i;
}
#endif /* HAVE_FFS */
/* The 'ffsl' is non-standard but when building with GCC we'll use its
builtin instead */
#ifndef COGL_UTIL_HAVE_BUILTIN_FFSL
int
_cogl_util_ffsl_wrapper (long int num)
{
int i = 1;
if (num == 0)
return 0;
while ((num & 1) == 0)
{
num >>= 1;
i++;
}
return i;
}
#endif /* COGL_UTIL_HAVE_BUILTIN_FFSL */