Browse Source

optimization for aligned bit arrays

Kajetan Johannes Hammerle 3 years ago
parent
commit
ce4658813c
1 changed files with 3 additions and 2 deletions
  1. 3 2
      utils/BitArray.h

+ 3 - 2
utils/BitArray.h

@@ -12,6 +12,7 @@ class BitArray final {
 
     static constexpr int MASK = (1 << BITS) - 1;
     static constexpr int LENGTH = (N * BITS) / INT_BITS + (((N * BITS) % INT_BITS) > 0);
+    static constexpr bool ALIGNED = (INT_BITS % BITS) == 0;
 
     constexpr static int getDivideBits() {
         int c = 0;
@@ -31,7 +32,7 @@ class BitArray final {
         int dataIndexA = (index * BITS) >> DIVIDE_BITS;
         int dataIndexB = ((index + 1) * BITS) >> DIVIDE_BITS;
         int shifts = (index * BITS) & (INT_BITS - 1);
-        if(dataIndexA == dataIndexB) {
+        if(dataIndexA == dataIndexB || ALIGNED) {
             return (data[dataIndexA] >> shifts) & MASK;
         }
         int bitsInA = INT_BITS - shifts;
@@ -71,7 +72,7 @@ public:
             int shifts = (index * BITS) & (INT_BITS - 1);
             data[dataIndexA] &= ~(MASK << shifts);
             data[dataIndexA] |= (i << shifts);
-            if(dataIndexA != dataIndexB) {
+            if(dataIndexA != dataIndexB && !ALIGNED) {
                 int leftBits = BITS - (INT_BITS - shifts);
                 int mask = (1 << leftBits) - 1;
                 data[dataIndexB] &= ~mask;