[Mesa-dev] [PATCH 1/6] mesa: Create a _mesa_bitcount_64() function.

Paul Berry stereotype441 at gmail.com
Mon Oct 3 15:11:15 PDT 2011


The i965 driver already had a function to do this (brw_count_bits()),
but it was buggy (it only counted the bottom 32 bits) and it was
clumsy (it had a strange and broken fallback for non-GCC-like
compilers, which fortunately was never used).  Since Mesa already has
a _mesa_bitcount() function, it seems better to just create a
_mesa_bitcount_64() function rather than special-case this in the i965
driver.
---
 src/mesa/drivers/dri/i965/brw_util.h |    7 ++-----
 src/mesa/main/imports.c              |   13 +++++++++++++
 src/mesa/main/imports.h              |    3 +++
 3 files changed, 18 insertions(+), 5 deletions(-)

diff --git a/src/mesa/drivers/dri/i965/brw_util.h b/src/mesa/drivers/dri/i965/brw_util.h
index 940a871..bbf36f6 100644
--- a/src/mesa/drivers/dri/i965/brw_util.h
+++ b/src/mesa/drivers/dri/i965/brw_util.h
@@ -34,15 +34,12 @@
 #define BRW_UTIL_H
 
 #include "main/mtypes.h"
+#include "main/imports.h"
 
-#ifdef __GNUC__
-#define brw_count_bits(v) __builtin_popcount(v)
-#else
 static inline GLuint brw_count_bits(uint64_t v)
 {
-	return _mesa_popcount(v>>32) + _mesa_popcount(v&0xffffffff);
+   return _mesa_bitcount_64(v);
 }
-#endif
 extern GLuint brw_parameter_list_state_flags(struct gl_program_parameter_list *paramList);
 extern GLuint brw_translate_blend_factor( GLenum factor );
 extern GLuint brw_translate_blend_equation( GLenum mode );
diff --git a/src/mesa/main/imports.c b/src/mesa/main/imports.c
index 8f09719..345a1c5 100644
--- a/src/mesa/main/imports.c
+++ b/src/mesa/main/imports.c
@@ -527,6 +527,19 @@ _mesa_bitcount(unsigned int n)
    }
    return bits;
 }
+
+/**
+ * Return number of bits set in given 64-bit uint.
+ */
+unsigned int
+_mesa_bitcount_64(uint64_t n)
+{
+   unsigned int bits;
+   for (bits = 0; n > 0; n = n >> 1) {
+      bits += (n & 1);
+   }
+   return bits;
+}
 #endif
 
 
diff --git a/src/mesa/main/imports.h b/src/mesa/main/imports.h
index 5fb5581..20fa148 100644
--- a/src/mesa/main/imports.h
+++ b/src/mesa/main/imports.h
@@ -578,9 +578,12 @@ _mesa_init_sqrt_table(void);
 
 #if ((_GNUC__ == 3 && __GNUC_MINOR__ >= 4) || __GNUC__ >= 4)
 #define _mesa_bitcount(i) __builtin_popcount(i)
+#define _mesa_bitcount_64(i) __builtin_popcountll(i)
 #else
 extern unsigned int
 _mesa_bitcount(unsigned int n);
+extern unsigned int
+_mesa_bitcount_64(uint64_t n);
 #endif
 
 #else
-- 
1.7.6.2



More information about the mesa-dev mailing list