Merge
authorkbarrett
Fri, 19 Aug 2016 23:57:42 +0000
changeset 40640 0e8df237a83e
parent 40638 4313c9c96890 (current diff)
parent 40639 83c879c8db57 (diff)
child 40641 544f0108203f
child 40653 bf64831e7e73
Merge
--- a/hotspot/src/share/vm/utilities/bitMap.cpp	Fri Aug 19 22:28:32 2016 +0000
+++ b/hotspot/src/share/vm/utilities/bitMap.cpp	Fri Aug 19 23:57:42 2016 +0000
@@ -376,77 +376,99 @@
   par_put_range_within_word(bit_index(end_full_word), end, value);
 }
 
+inline bm_word_t tail_mask(idx_t tail_bits) {
+  assert(tail_bits != 0, "precondition"); // Works, but shouldn't be called.
+  assert(tail_bits < (idx_t)BitsPerWord, "precondition");
+  return (bm_word_t(1) << tail_bits) - 1;
+}
+
+// Get the low tail_bits of value, which is the last partial word of a map.
+inline bm_word_t tail_of_map(bm_word_t value, idx_t tail_bits) {
+  return value & tail_mask(tail_bits);
+}
+
+// Compute the new last word of a map with a non-aligned length.
+// new_value has the new trailing bits of the map in the low tail_bits.
+// old_value is the last word of the map, including bits beyond the end.
+// Returns old_value with the low tail_bits replaced by the corresponding
+// bits in new_value.
+inline bm_word_t merge_tail_of_map(bm_word_t new_value,
+                                   bm_word_t old_value,
+                                   idx_t tail_bits) {
+  bm_word_t mask = tail_mask(tail_bits);
+  return (new_value & mask) | (old_value & ~mask);
+}
+
 bool BitMap::contains(const BitMap& other) const {
   assert(size() == other.size(), "must have same size");
   const bm_word_t* dest_map = map();
   const bm_word_t* other_map = other.map();
-  idx_t size = size_in_words();
-  for (idx_t index = 0; index < size_in_words(); index++) {
-    bm_word_t word_union = dest_map[index] | other_map[index];
-    // If this has more bits set than dest_map[index], then other is not a
-    // subset.
-    if (word_union != dest_map[index]) return false;
+  idx_t limit = word_index(size());
+  for (idx_t index = 0; index < limit; ++index) {
+    // false if other bitmap has bits set which are clear in this bitmap.
+    if ((~dest_map[index] & other_map[index]) != 0) return false;
   }
-  return true;
+  idx_t rest = bit_in_word(size());
+  // true unless there is a partial-word tail in which the other
+  // bitmap has bits set which are clear in this bitmap.
+  return (rest == 0) || tail_of_map(~dest_map[limit] & other_map[limit], rest) == 0;
 }
 
 bool BitMap::intersects(const BitMap& other) const {
   assert(size() == other.size(), "must have same size");
   const bm_word_t* dest_map = map();
   const bm_word_t* other_map = other.map();
-  idx_t size = size_in_words();
-  for (idx_t index = 0; index < size_in_words(); index++) {
+  idx_t limit = word_index(size());
+  for (idx_t index = 0; index < limit; ++index) {
     if ((dest_map[index] & other_map[index]) != 0) return true;
   }
-  // Otherwise, no intersection.
-  return false;
+  idx_t rest = bit_in_word(size());
+  // false unless there is a partial-word tail with non-empty intersection.
+  return (rest > 0) && tail_of_map(dest_map[limit] & other_map[limit], rest) != 0;
 }
 
 void BitMap::set_union(const BitMap& other) {
   assert(size() == other.size(), "must have same size");
   bm_word_t* dest_map = map();
   const bm_word_t* other_map = other.map();
-  idx_t size = size_in_words();
-  for (idx_t index = 0; index < size_in_words(); index++) {
-    dest_map[index] = dest_map[index] | other_map[index];
+  idx_t limit = word_index(size());
+  for (idx_t index = 0; index < limit; ++index) {
+    dest_map[index] |= other_map[index];
+  }
+  idx_t rest = bit_in_word(size());
+  if (rest > 0) {
+    bm_word_t orig = dest_map[limit];
+    dest_map[limit] = merge_tail_of_map(orig | other_map[limit], orig, rest);
   }
 }
 
-
 void BitMap::set_difference(const BitMap& other) {
   assert(size() == other.size(), "must have same size");
   bm_word_t* dest_map = map();
   const bm_word_t* other_map = other.map();
-  idx_t size = size_in_words();
-  for (idx_t index = 0; index < size_in_words(); index++) {
-    dest_map[index] = dest_map[index] & ~(other_map[index]);
+  idx_t limit = word_index(size());
+  for (idx_t index = 0; index < limit; ++index) {
+    dest_map[index] &= ~other_map[index];
+  }
+  idx_t rest = bit_in_word(size());
+  if (rest > 0) {
+    bm_word_t orig = dest_map[limit];
+    dest_map[limit] = merge_tail_of_map(orig & ~other_map[limit], orig, rest);
   }
 }
 
-
 void BitMap::set_intersection(const BitMap& other) {
   assert(size() == other.size(), "must have same size");
   bm_word_t* dest_map = map();
   const bm_word_t* other_map = other.map();
-  idx_t size = size_in_words();
-  for (idx_t index = 0; index < size; index++) {
-    dest_map[index]  = dest_map[index] & other_map[index];
+  idx_t limit = word_index(size());
+  for (idx_t index = 0; index < limit; ++index) {
+    dest_map[index] &= other_map[index];
   }
-}
-
-
-void BitMap::set_intersection_at_offset(const BitMap& other, idx_t offset) {
-  assert(other.size() >= offset, "offset not in range");
-  assert(other.size() - offset >= size(), "other not large enough");
-  // XXX Ideally, we would remove this restriction.
-  guarantee((offset % (sizeof(bm_word_t) * BitsPerByte)) == 0,
-            "Only handle aligned cases so far.");
-  bm_word_t* dest_map = map();
-  const bm_word_t* other_map = other.map();
-  idx_t offset_word_ind = word_index(offset);
-  idx_t size = size_in_words();
-  for (idx_t index = 0; index < size; index++) {
-    dest_map[index] = dest_map[index] & other_map[offset_word_ind + index];
+  idx_t rest = bit_in_word(size());
+  if (rest > 0) {
+    bm_word_t orig = dest_map[limit];
+    dest_map[limit] = merge_tail_of_map(orig & other_map[limit], orig, rest);
   }
 }
 
@@ -455,88 +477,111 @@
   bool changed = false;
   bm_word_t* dest_map = map();
   const bm_word_t* other_map = other.map();
-  idx_t size = size_in_words();
-  for (idx_t index = 0; index < size; index++) {
-    idx_t temp = dest_map[index] | other_map[index];
-    changed = changed || (temp != dest_map[index]);
+  idx_t limit = word_index(size());
+  for (idx_t index = 0; index < limit; ++index) {
+    bm_word_t orig = dest_map[index];
+    bm_word_t temp = orig | other_map[index];
+    changed = changed || (temp != orig);
     dest_map[index] = temp;
   }
+  idx_t rest = bit_in_word(size());
+  if (rest > 0) {
+    bm_word_t orig = dest_map[limit];
+    bm_word_t temp = merge_tail_of_map(orig | other_map[limit], orig, rest);
+    changed = changed || (temp != orig);
+    dest_map[limit] = temp;
+  }
   return changed;
 }
 
-
 bool BitMap::set_difference_with_result(const BitMap& other) {
   assert(size() == other.size(), "must have same size");
   bool changed = false;
   bm_word_t* dest_map = map();
   const bm_word_t* other_map = other.map();
-  idx_t size = size_in_words();
-  for (idx_t index = 0; index < size; index++) {
-    bm_word_t temp = dest_map[index] & ~(other_map[index]);
-    changed = changed || (temp != dest_map[index]);
+  idx_t limit = word_index(size());
+  for (idx_t index = 0; index < limit; ++index) {
+    bm_word_t orig = dest_map[index];
+    bm_word_t temp = orig & ~other_map[index];
+    changed = changed || (temp != orig);
     dest_map[index] = temp;
   }
+  idx_t rest = bit_in_word(size());
+  if (rest > 0) {
+    bm_word_t orig = dest_map[limit];
+    bm_word_t temp = merge_tail_of_map(orig & ~other_map[limit], orig, rest);
+    changed = changed || (temp != orig);
+    dest_map[limit] = temp;
+  }
   return changed;
 }
 
-
 bool BitMap::set_intersection_with_result(const BitMap& other) {
   assert(size() == other.size(), "must have same size");
   bool changed = false;
   bm_word_t* dest_map = map();
   const bm_word_t* other_map = other.map();
-  idx_t size = size_in_words();
-  for (idx_t index = 0; index < size; index++) {
+  idx_t limit = word_index(size());
+  for (idx_t index = 0; index < limit; ++index) {
     bm_word_t orig = dest_map[index];
     bm_word_t temp = orig & other_map[index];
     changed = changed || (temp != orig);
-    dest_map[index]  = temp;
+    dest_map[index] = temp;
+  }
+  idx_t rest = bit_in_word(size());
+  if (rest > 0) {
+    bm_word_t orig = dest_map[limit];
+    bm_word_t temp = merge_tail_of_map(orig & other_map[limit], orig, rest);
+    changed = changed || (temp != orig);
+    dest_map[limit] = temp;
   }
   return changed;
 }
 
-
 void BitMap::set_from(const BitMap& other) {
   assert(size() == other.size(), "must have same size");
   bm_word_t* dest_map = map();
   const bm_word_t* other_map = other.map();
-  idx_t size = size_in_words();
-  for (idx_t index = 0; index < size; index++) {
-    dest_map[index] = other_map[index];
+  idx_t copy_words = word_index(size());
+  Copy::disjoint_words((HeapWord*)other_map, (HeapWord*)dest_map, copy_words);
+  idx_t rest = bit_in_word(size());
+  if (rest > 0) {
+    dest_map[copy_words] = merge_tail_of_map(other_map[copy_words],
+                                             dest_map[copy_words],
+                                             rest);
   }
 }
 
-
-bool BitMap::is_same(const BitMap& other) {
+bool BitMap::is_same(const BitMap& other) const {
   assert(size() == other.size(), "must have same size");
-  bm_word_t* dest_map = map();
+  const bm_word_t* dest_map = map();
   const bm_word_t* other_map = other.map();
-  idx_t size = size_in_words();
-  for (idx_t index = 0; index < size; index++) {
+  idx_t limit = word_index(size());
+  for (idx_t index = 0; index < limit; ++index) {
     if (dest_map[index] != other_map[index]) return false;
   }
-  return true;
+  idx_t rest = bit_in_word(size());
+  return (rest == 0) || (tail_of_map(dest_map[limit] ^ other_map[limit], rest) == 0);
 }
 
 bool BitMap::is_full() const {
-  const bm_word_t* word = map();
-  idx_t rest = size();
-  for (; rest >= (idx_t) BitsPerWord; rest -= BitsPerWord) {
-    if (*word != ~(bm_word_t)0) return false;
-    word++;
+  const bm_word_t* words = map();
+  idx_t limit = word_index(size());
+  for (idx_t index = 0; index < limit; ++index) {
+    if (~words[index] != 0) return false;
   }
-  return rest == 0 || (*word | ~right_n_bits((int)rest)) == ~(bm_word_t)0;
+  idx_t rest = bit_in_word(size());
+  return (rest == 0) || (tail_of_map(~words[limit], rest) == 0);
 }
 
-
 bool BitMap::is_empty() const {
-  const bm_word_t* word = map();
-  idx_t rest = size();
-  for (; rest >= (idx_t) BitsPerWord; rest -= BitsPerWord) {
-    if (*word != 0) return false;
-    word++;
+  const bm_word_t* words = map();
+  idx_t limit = word_index(size());
+  for (idx_t index = 0; index < limit; ++index) {
+    if (words[index] != 0) return false;
   }
-  return rest == 0 || (*word & right_n_bits((int)rest)) == 0;
+  idx_t rest = bit_in_word(size());
+  return (rest == 0) || (tail_of_map(words[limit], rest) == 0);
 }
 
 void BitMap::clear_large() {
--- a/hotspot/src/share/vm/utilities/bitMap.hpp	Fri Aug 19 22:28:32 2016 +0000
+++ b/hotspot/src/share/vm/utilities/bitMap.hpp	Fri Aug 19 23:57:42 2016 +0000
@@ -284,18 +284,9 @@
   bool set_difference_with_result(const BitMap& bits);
   bool set_intersection_with_result(const BitMap& bits);
 
-  // Requires the submap of "bits" starting at offset to be at least as
-  // large as "this".  Modifies "this" to be the intersection of its
-  // current contents and the submap of "bits" starting at "offset" of the
-  // same length as "this."
-  // (For expedience, currently requires the offset to be aligned to the
-  // bitsize of a uintptr_t.  This should go away in the future though it
-  // will probably remain a good case to optimize.)
-  void set_intersection_at_offset(const BitMap& bits, idx_t offset);
-
   void set_from(const BitMap& bits);
 
-  bool is_same(const BitMap& bits);
+  bool is_same(const BitMap& bits) const;
 
   // Test if all bits are set or cleared
   bool is_full() const;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/hotspot/test/native/utilities/test_bitMap_setops.cpp	Fri Aug 19 23:57:42 2016 +0000
@@ -0,0 +1,417 @@
+/*
+ * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ *
+ */
+
+#include "precompiled.hpp"
+#include "utilities/bitMap.inline.hpp"
+#include "utilities/copy.hpp"
+#include "utilities/debug.hpp"
+#include "utilities/globalDefinitions.hpp"
+#include <stdlib.h>
+#include "unittest.hpp"
+
+typedef BitMap::idx_t idx_t;
+typedef BitMap::bm_word_t bm_word_t;
+
+class BitMapMemory {
+private:
+  idx_t _words;
+  bm_word_t* _memory;
+
+public:
+  BitMapMemory(idx_t bits) :
+    _words(BitMap::calc_size_in_words(bits)),
+    _memory(static_cast<bm_word_t*>(malloc(_words * sizeof(bm_word_t))))
+  { }
+
+  ~BitMapMemory() {
+    free(_memory);
+  }
+
+  BitMapView make_view(idx_t bits, bm_word_t value) {
+    vmassert(BitMap::calc_size_in_words(bits) <= _words, "invalid request");
+    STATIC_ASSERT(sizeof(bm_word_t) == sizeof(HeapWord));
+    Copy::fill_to_aligned_words((HeapWord*)_memory, _words, value);
+    return BitMapView(_memory, bits);
+  }
+
+  bm_word_t* memory() { return _memory; }
+};
+
+const idx_t aligned_size = 4 * BitsPerWord;
+const idx_t unaligned_size = aligned_size - (BitsPerWord / 2);
+
+static bm_word_t make_even_bits() {
+  bm_word_t result = 1;
+  while (true) {
+    bm_word_t next = (result << 2) | 1;
+    if (next == result) {
+      return result;
+    }
+    result = next;
+  }
+}
+
+const bm_word_t even_bits = make_even_bits();
+const bm_word_t odd_bits = ~even_bits;
+const bm_word_t one_bits = ~bm_word_t(0);
+const bm_word_t zero_bits = 0;
+
+// Scoped set a clear bit and restore to clear.
+class WithBitSet {
+private:
+  BitMap& _bm;
+  idx_t _index;
+
+public:
+  WithBitSet(BitMap& bm, idx_t index) : _bm(bm), _index(index) {
+    // Failure may indicate test bug; can't use ASSERT_xxx in constructor.
+    EXPECT_FALSE(_bm.at(_index));
+    bm.set_bit(_index);
+  }
+
+  ~WithBitSet() {
+    _bm.clear_bit(_index);
+  }
+};
+
+// Scoped clear a set bit and restore to set.
+class WithBitClear {
+private:
+  BitMap& _bm;
+  idx_t _index;
+
+public:
+  WithBitClear(BitMap& bm, idx_t index) : _bm(bm), _index(index) {
+    // Failure may indicate test bug; can't use ASSERT_xxx in constructor.
+    EXPECT_TRUE(_bm.at(_index));
+    bm.clear_bit(_index);
+  }
+
+  ~WithBitClear() {
+    _bm.set_bit(_index);
+  }
+};
+
+//////////////////////////////////////////////////////////////////////////////
+// bool is_same(const BitMap& bits);
+
+TEST(BitMap, is_same__aligned) {
+  BitMapMemory mx(aligned_size);
+  BitMapMemory my(aligned_size);
+
+  BitMapView x = mx.make_view(aligned_size, even_bits);
+  BitMapView y = my.make_view(aligned_size, even_bits);
+  EXPECT_TRUE(x.is_same(y));
+
+  WithBitClear wbc(x, aligned_size / 2);
+  EXPECT_FALSE(x.is_same(y));
+}
+
+TEST(BitMap, is_same__unaligned) {
+  BitMapMemory mx(aligned_size);
+  BitMapMemory my(aligned_size);
+
+  BitMapView x = mx.make_view(unaligned_size, even_bits);
+  BitMapView y = my.make_view(unaligned_size, even_bits);
+
+  // Check that a difference beyond the end of x/y doesn't count.
+  {
+    BitMapView aligned = BitMapView(mx.memory(), aligned_size);
+    const idx_t index = aligned_size - 2;
+    STATIC_ASSERT(unaligned_size <= index);
+
+    WithBitClear wbc(aligned, index);
+    EXPECT_TRUE(x.is_same(y));
+  }
+
+  // Check that a difference in the final partial word does count.
+  {
+    idx_t index = unaligned_size - 2;
+    ASSERT_LE(BitMap::word_align_down(unaligned_size), index);
+
+    WithBitClear wbc(y, index);
+    EXPECT_FALSE(x.is_same(y));
+  }
+}
+
+//////////////////////////////////////////////////////////////////////////////
+// bool is_full();
+// bool is_empty();
+
+TEST(BitMap, is_full_or_empty__aligned) {
+  BitMapMemory mx(aligned_size);
+
+  {
+    BitMapView x = mx.make_view(aligned_size, even_bits);
+    EXPECT_FALSE(x.is_full());
+    EXPECT_FALSE(x.is_empty());
+  }
+
+  {
+    BitMapView x = mx.make_view(aligned_size, zero_bits);
+    EXPECT_FALSE(x.is_full());
+    EXPECT_TRUE(x.is_empty());
+  }
+
+  {
+    BitMapView x = mx.make_view(aligned_size, one_bits);
+    EXPECT_TRUE(x.is_full());
+    EXPECT_FALSE(x.is_empty());
+  }
+}
+
+TEST(BitMap, is_full__unaligned) {
+  BitMapMemory mx(aligned_size);
+
+  BitMapView x = mx.make_view(unaligned_size, one_bits);
+  EXPECT_TRUE(x.is_full());
+
+  // Check that a missing bit beyond the end doesn't count.
+  {
+    idx_t index = aligned_size - 1;
+    BitMapView aligned = BitMapView(mx.memory(), aligned_size);
+
+    WithBitClear wcb(aligned, index);
+    EXPECT_FALSE(aligned.is_full());
+    EXPECT_TRUE(x.is_full());
+  }
+
+  // Check that a missing bit in the final partial word does count.
+  {
+    WithBitClear wcb(x, unaligned_size - 1);
+    EXPECT_FALSE(x.is_full());
+  }
+}
+
+TEST(BitMap, is_empty__unaligned) {
+  BitMapMemory mx(aligned_size);
+
+  BitMapView x = mx.make_view(unaligned_size, zero_bits);
+  EXPECT_TRUE(x.is_empty());
+
+  // Check that a set bit beyond the end doesn't count.
+  {
+    idx_t index = aligned_size - 1;
+    BitMapView aligned = BitMapView(mx.memory(), aligned_size);
+
+    WithBitSet wbs(aligned, index);
+    EXPECT_FALSE(aligned.is_empty());
+    EXPECT_TRUE(x.is_empty());
+  }
+
+  // Check that a set bit in the final partial word does count.
+  {
+    WithBitSet wbs(x, unaligned_size - 1);
+    EXPECT_FALSE(x.is_empty());
+  }
+}
+
+//////////////////////////////////////////////////////////////////////////////
+// bool contains(const BitMap& bits);
+
+TEST(BitMap, contains__aligned) {
+  BitMapMemory mx(aligned_size);
+  BitMapMemory my(aligned_size);
+
+  BitMapView x = mx.make_view(aligned_size, even_bits);
+  BitMapView y = my.make_view(aligned_size, even_bits);
+  EXPECT_TRUE(x.contains(y));
+
+  WithBitClear wbc(x, aligned_size / 2);
+  EXPECT_FALSE(x.contains(y));
+}
+
+TEST(BitMap, contains__unaligned) {
+  BitMapMemory mx(aligned_size);
+  BitMapMemory my(aligned_size);
+
+  BitMapView x = mx.make_view(unaligned_size, even_bits);
+  BitMapView y = my.make_view(unaligned_size, even_bits);
+
+  // Check that a missing bit beyond the end of x doesn't count.
+  {
+    BitMapView aligned = BitMapView(mx.memory(), aligned_size);
+    const idx_t index = aligned_size - 2;
+    STATIC_ASSERT(unaligned_size <= index);
+
+    WithBitClear wbc(aligned, index);
+    EXPECT_TRUE(x.contains(y));
+  }
+
+  // Check that a missing bit in the final partial word does count.
+  {
+    idx_t index = unaligned_size - 2;
+    ASSERT_LE(BitMap::word_align_down(unaligned_size), index);
+
+    WithBitClear wbc(x, index);
+    EXPECT_FALSE(x.contains(y));
+  }
+}
+
+//////////////////////////////////////////////////////////////////////////////
+// bool intersects(const BitMap& bits);
+
+TEST(BitMap, intersects__aligned) {
+  BitMapMemory mx(aligned_size);
+  BitMapMemory my(aligned_size);
+
+  BitMapView x = mx.make_view(aligned_size, even_bits);
+  BitMapView y = my.make_view(aligned_size, zero_bits);
+  EXPECT_FALSE(x.intersects(y));
+
+  ASSERT_TRUE(x.at(aligned_size / 2));
+  WithBitSet wbs(y, aligned_size / 2);
+  EXPECT_TRUE(x.intersects(y));
+}
+
+TEST(BitMap, intersects__unaligned) {
+  BitMapMemory mx(aligned_size);
+  BitMapMemory my(aligned_size);
+
+  BitMapView x = mx.make_view(unaligned_size, even_bits);
+  BitMapView y = my.make_view(unaligned_size, zero_bits);
+  EXPECT_FALSE(x.intersects(y));
+
+  // Check that adding a bit beyond the end of y doesn't count.
+  {
+    BitMapView aligned_x = BitMapView(mx.memory(), aligned_size);
+    BitMapView aligned_y = BitMapView(my.memory(), aligned_size);
+    const idx_t index = aligned_size - 2;
+    STATIC_ASSERT(unaligned_size <= index);
+    ASSERT_TRUE(aligned_x.at(index));
+
+    WithBitSet wbs(aligned_y, index);
+    EXPECT_FALSE(x.intersects(y));
+  }
+
+  // Check that adding a bit in the final partial word does count.
+  {
+    idx_t index = unaligned_size - 2;
+    ASSERT_LE(BitMap::word_align_down(unaligned_size), index);
+    ASSERT_TRUE(x.at(index));
+
+    WithBitSet wbs(y, index);
+    EXPECT_TRUE(x.intersects(y));
+  }
+}
+
+//////////////////////////////////////////////////////////////////////////////
+// void set_from(const BitMap& bits);
+// void set_union(const BitMap& bits);
+// void set_difference(const BitMap& bits);
+// void set_intersection(const BitMap& bits);
+//
+// bool set_union_with_result(const BitMap& bits);
+// bool set_difference_with_result(const BitMap& bits);
+// bool set_intersection_with_result(const BitMap& bits);
+
+static void check_tail_unmodified(BitMapMemory& mem,
+                                  idx_t bits,
+                                  bm_word_t fill_word) {
+  if (!BitMap::is_word_aligned(bits)) {
+    idx_t last_word_bit_index = BitMap::word_align_down(bits);
+    idx_t last_word_index = BitMap::calc_size_in_words(last_word_bit_index);
+    bm_word_t last_word = mem.memory()[last_word_index];
+    idx_t shift = bits - last_word_bit_index;
+    EXPECT_EQ(fill_word >> shift, last_word >> shift);
+  }
+}
+
+static void check_mod_setop(void (BitMap::*f)(const BitMap&),
+                            idx_t bits,
+                            bm_word_t wx,
+                            bm_word_t wy,
+                            bm_word_t wexp) {
+  BitMapMemory mx(bits);
+  BitMapMemory my(bits);
+  BitMapMemory mexp(bits);
+
+  BitMapView x = mx.make_view(bits, wx);
+  BitMapView y = my.make_view(bits, wy);
+  BitMapView exp = mexp.make_view(bits, wexp);
+
+  (x.*f)(y);
+
+  EXPECT_TRUE(exp.is_same(x));
+  check_tail_unmodified(mx, bits, wx);
+}
+
+static void check_mod_setop_with_result(bool (BitMap::*f)(const BitMap&),
+                                        idx_t bits,
+                                        bm_word_t wx,
+                                        bm_word_t wy,
+                                        bm_word_t wexp) {
+  BitMapMemory mx(bits);
+  BitMapMemory my(bits);
+  BitMapMemory mexp(bits);
+
+  BitMapView x = mx.make_view(bits, wx);
+  BitMapView y = my.make_view(bits, wy);
+  BitMapView exp = mexp.make_view(bits, wexp);
+
+  bool value = (x.*f)(y);
+  EXPECT_EQ(value, wx != wexp);
+
+  EXPECT_TRUE(exp.is_same(x));
+  check_tail_unmodified(mx, bits, wx);
+}
+
+#define CHECK_MOD_SETOP_AUX(checker, name, x, y, exp)   \
+  TEST(BitMap, name ## __ ## x ## _ ## y) {             \
+    checker(&BitMap::name, aligned_size,                \
+            x ## _bits, y ## _bits, exp ## _bits);      \
+    checker(&BitMap::name, unaligned_size,              \
+            x ## _bits, y ## _bits, exp ## _bits);      \
+  }
+
+#define CHECK_MOD_SETOP(name, x, y, exp) \
+  CHECK_MOD_SETOP_AUX(check_mod_setop, name, x, y, exp)
+
+#define CHECK_MOD_SETOP_WITH_RESULT(name, x, y, exp) \
+  CHECK_MOD_SETOP_AUX(check_mod_setop_with_result, name, x, y, exp)
+
+#define CHECK_MOD_SETOPS(name, x, y, exp)                       \
+  CHECK_MOD_SETOP(name, x, y, exp)                              \
+  CHECK_MOD_SETOP_WITH_RESULT(name ## _with_result, x, y, exp)
+
+CHECK_MOD_SETOP(set_from, even, even, even)
+CHECK_MOD_SETOP(set_from, even, odd, odd)
+CHECK_MOD_SETOP(set_from, even, one, one)
+CHECK_MOD_SETOP(set_from, even, zero, zero)
+
+CHECK_MOD_SETOPS(set_union, even, even, even)
+CHECK_MOD_SETOPS(set_union, even, odd, one)
+CHECK_MOD_SETOPS(set_union, even, one, one)
+CHECK_MOD_SETOPS(set_union, even, zero, even)
+
+CHECK_MOD_SETOPS(set_difference, even, even, zero)
+CHECK_MOD_SETOPS(set_difference, even, odd, even)
+CHECK_MOD_SETOPS(set_difference, even, one, zero)
+CHECK_MOD_SETOPS(set_difference, even, zero, even)
+
+CHECK_MOD_SETOPS(set_intersection, even, even, even)
+CHECK_MOD_SETOPS(set_intersection, even, odd, zero)
+CHECK_MOD_SETOPS(set_intersection, even, one, even)
+CHECK_MOD_SETOPS(set_intersection, even, zero, zero)
+