]> git.ipfire.org Git - thirdparty/vectorscan.git/commitdiff
bitfield: unroll main operators
authorAlex Coyte <a.coyte@intel.com>
Mon, 11 Apr 2016 23:52:53 +0000 (09:52 +1000)
committerMatthew Barr <matthew.barr@intel.com>
Wed, 20 Apr 2016 03:34:56 +0000 (13:34 +1000)
src/util/bitfield.h

index 208c2ef513c9e40a69e8439e97172b26c04ae49e..a71c1f88d5efe88f455820dee81b2d38ecc8d2e9 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015, Intel Corporation
+ * Copyright (c) 2015-2016, Intel Corporation
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions are met:
@@ -187,8 +187,15 @@ public:
     size_t count() const {
         static_assert(block_size == 64, "adjust popcount for block_type");
         size_t sum = 0;
-        for (const auto &e : bits) {
-            sum += popcount64(e);
+        size_t i = 0;
+        for (; i + 4 <= num_blocks; i += 4) {
+            sum += popcount64(bits[i]);
+            sum += popcount64(bits[i + 1]);
+            sum += popcount64(bits[i + 2]);
+            sum += popcount64(bits[i + 3]);
+        }
+        for (; i < num_blocks; i++) {
+            sum += popcount64(bits[i]);
         }
         assert(sum <= size());
         return sum;
@@ -298,49 +305,61 @@ public:
     }
 
     /// Bitwise OR.
-    bitfield operator|(const bitfield &a) const {
-        bitfield cr;
-        for (size_t i = 0; i < bits.size(); i++) {
-            cr.bits[i] = bits[i] | a.bits[i];
-        }
-        return cr;
+    bitfield operator|(bitfield a) const {
+        a |= *this;
+        return a;
     }
 
     /// Bitwise OR-equals.
     void operator|=(const bitfield &a) {
-        for (size_t i = 0; i < bits.size(); i++) {
+        size_t i = 0;
+        for (; i + 4 <= num_blocks; i += 4) {
+            bits[i]     |= a.bits[i];
+            bits[i + 1] |= a.bits[i + 1];
+            bits[i + 2] |= a.bits[i + 2];
+            bits[i + 3] |= a.bits[i + 3];
+        }
+        for (; i < num_blocks; i++) {
             bits[i] |= a.bits[i];
         }
     }
 
     /// Bitwise AND.
-    bitfield operator&(const bitfield &a) const {
-        bitfield cr;
-        for (size_t i = 0; i < bits.size(); i++) {
-            cr.bits[i] = bits[i] & a.bits[i];
-        }
-        return cr;
+    bitfield operator&(bitfield a) const {
+        a &= *this;
+        return a;
     }
 
     /// Bitwise AND-equals.
     void operator&=(const bitfield &a) {
-        for (size_t i = 0; i < bits.size(); i++) {
+        size_t i = 0;
+        for (; i + 4 <= num_blocks; i += 4) {
+            bits[i]     &= a.bits[i];
+            bits[i + 1] &= a.bits[i + 1];
+            bits[i + 2] &= a.bits[i + 2];
+            bits[i + 3] &= a.bits[i + 3];
+        }
+        for (; i < num_blocks; i++) {
             bits[i] &= a.bits[i];
         }
     }
 
     /// Bitwise XOR.
-    bitfield operator^(const bitfield &a) const {
-        bitfield cr;
-        for (size_t i = 0; i < bits.size(); i++) {
-            cr.bits[i] = bits[i] ^ a.bits[i];
-        }
-        return cr;
+    bitfield operator^(bitfield a) const {
+        a ^= *this;
+        return a;
     }
 
     /// Bitwise XOR-equals.
-    void operator^=(const bitfield &a) {
-        for (size_t i = 0; i < bits.size(); i++) {
+    void operator^=(bitfield a) {
+        size_t i = 0;
+        for (; i + 4 <= num_blocks; i += 4) {
+            bits[i]     ^= a.bits[i];
+            bits[i + 1] ^= a.bits[i + 1];
+            bits[i + 2] ^= a.bits[i + 2];
+            bits[i + 3] ^= a.bits[i + 3];
+        }
+        for (; i < num_blocks; i++) {
             bits[i] ^= a.bits[i];
         }
     }