Fix bits_set
authorNathael Pajani <nathael.pajani@ed3l.fr>
Thu, 19 Nov 2020 18:48:20 +0000 (19:48 +0100)
committerNathael Pajani <nathael.pajani@ed3l.fr>
Tue, 8 Nov 2022 16:03:05 +0000 (17:03 +0100)
lib/utils.c

index 96ad730..96ab676 100644 (file)
@@ -93,14 +93,10 @@ uint8_t bits_set(uint32_t x)
        static const uint8_t bval_bsets[] = {0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4};
        uint8_t r = 0; /* Accumulator for the total bits set in x */
 
-       r = bval_bsets[x & 0xFF];
-       x >>= 8;
-       r += bval_bsets[x & 0xFF];
-       x >>= 8;
-       r += bval_bsets[x & 0xFF];
-       x >>= 8;
-       r += bval_bsets[x & 0xFF];
-
+       while (x) {
+               r += bval_bsets[x & 0x0F];
+               x >>= 4;
+       }
        return r;
 }