mutter/cogl/cogl-util.c
Neil Roberts 2ba4fe417a 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>
2011-11-16 16:21:31 +00:00

103 lines
2.0 KiB
C

/*
* Cogl
*
* An object oriented GL/GLES Abstraction/Utility Layer
*
* Copyright (C) 2007,2008,2009,2010 Intel Corporation.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
*
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "cogl-util.h"
/*
* cogl_util_next_p2:
* @a: Value to get the next power of two
*
* Calculates the next power of two greater than or equal to @a.
*
* Return value: @a if @a is already a power of two, otherwise returns
* the next nearest power of two.
*/
int
_cogl_util_next_p2 (int a)
{
int rval = 1;
while (rval < a)
rval <<= 1;
return rval;
}
unsigned int
_cogl_util_one_at_a_time_mix (unsigned int hash)
{
hash += ( hash << 3 );
hash ^= ( hash >> 11 );
hash += ( hash << 15 );
return hash;
}
/* The 'ffs' function is part of C99 so it isn't always available */
#ifndef HAVE_FFS
int
_cogl_util_ffs (int num)
{
int i = 1;
if (num == 0)
return 0;
while ((num & 1) == 0)
{
num >>= 1;
i++;
}
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 */