summaryrefslogtreecommitdiff
path: root/cogl/cogl-util.c
diff options
context:
space:
mode:
authorNeil Roberts <neil@linux.intel.com>2011-10-28 20:09:53 +0100
committerNeil Roberts <neil@linux.intel.com>2011-11-16 16:21:31 +0000
commit2ba4fe417acb0352e6eca0e1f61627975c9716e3 (patch)
treec316a6b3688bae8c13d4f64e012e817e789d6af1 /cogl/cogl-util.c
parentf0f9493f5c3abe2e465f5758e21d50292dd43854 (diff)
downloadcogl-2ba4fe417acb0352e6eca0e1f61627975c9716e3.tar.gz
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>
Diffstat (limited to 'cogl/cogl-util.c')
-rw-r--r--cogl/cogl-util.c23
1 files changed, 23 insertions, 0 deletions
diff --git a/cogl/cogl-util.c b/cogl/cogl-util.c
index 649e1e19..1cb38143 100644
--- a/cogl/cogl-util.c
+++ b/cogl/cogl-util.c
@@ -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 */