Merge pull request #3114 from alexanderkyte/mono_conf_gc
[mono.git] / mono / utils / monobitset.c
index 5ff68938f6d94ed70392744cc0e429be1c00278c..b86ab331dbf7e2cab2180e69cef51ae5060c10a4 100644 (file)
@@ -35,7 +35,7 @@ mono_bitset_new (guint32 max_size, guint32 flags) {
        guint32 real_size = (max_size + BITS_PER_CHUNK - 1) / BITS_PER_CHUNK;
        MonoBitSet *result;
 
-       result = g_malloc0 (sizeof (MonoBitSet) + sizeof (gsize) * (real_size - MONO_ZERO_LEN_ARRAY));
+       result = (MonoBitSet *) g_malloc0 (sizeof (MonoBitSet) + sizeof (gsize) * (real_size - MONO_ZERO_LEN_ARRAY));
        result->size = real_size * BITS_PER_CHUNK;
        result->flags = flags;
        return result;
@@ -54,7 +54,7 @@ mono_bitset_new (guint32 max_size, guint32 flags) {
 MonoBitSet *
 mono_bitset_mem_new (gpointer mem, guint32 max_size, guint32 flags) {
        guint32 real_size = (max_size + BITS_PER_CHUNK - 1) / BITS_PER_CHUNK;
-       MonoBitSet *result = mem;
+       MonoBitSet *result = (MonoBitSet *) mem;
 
        result->size = real_size * BITS_PER_CHUNK;
        result->flags = flags | MONO_BITSET_DONT_FREE;
@@ -210,25 +210,17 @@ mono_bitset_count (const MonoBitSet *set) {
        count = 0;
        for (i = 0; i < set->size / BITS_PER_CHUNK; ++i) {
                d = set->data [i];
-               /* there is probably some asm code that can do this much faster */
-               if (d) {
-#if SIZEOF_VOID_P == 8
-                       /* http://www.jjj.de/bitwizardry/bitwizardrypage.html */
-                       d -=  (d>>1) & 0x5555555555555555;
-                       d  = ((d>>2) & 0x3333333333333333) + (d & 0x3333333333333333);
-                       d  = ((d>>4) + d) & 0x0f0f0f0f0f0f0f0f;
-                       d *= 0x0101010101010101;
-                       count += d >> 56;
+#ifdef __GNUC__
+               if (sizeof (gsize) == sizeof (unsigned long))
+                       count += __builtin_popcountl (d);
+               else
+                       count += __builtin_popcount (d);
 #else
-                       /* http://aggregate.org/MAGIC/ */
-                       d -= ((d >> 1) & 0x55555555);
-                       d = (((d >> 2) & 0x33333333) + (d & 0x33333333));
-                       d = (((d >> 4) + d) & 0x0f0f0f0f);
-                       d += (d >> 8);
-                       d += (d >> 16);
-                       count += (d & 0x0000003f);
-#endif
+               while (d) {
+                       count ++;
+                       d &= (d - 1);
                }
+#endif
        }
        return count;
 }