The shift operations for dynamic_bitset fail to zero out words where the
non-zero bits were shifted to a completely different word.
For a right shift we don't need to sanitize the unused bits in the high
word, because we know they were already clear and a right shift doesn't
change that.
libstdc++-v3/ChangeLog:
PR libstdc++/115399
* include/tr2/dynamic_bitset (operator>>=): Remove redundant
call to _M_do_sanitize.
* include/tr2/dynamic_bitset.tcc (_M_do_left_shift): Zero out
low bits in words that should no longer be populated.
(_M_do_right_shift): Likewise for high bits.
* testsuite/tr2/dynamic_bitset/pr115399.cc: New test.
(cherry picked from commit
bd3a312728fbf8c35a09239b9180269f938f872e)
operator>>=(size_type __pos)
{
if (__builtin_expect(__pos < this->_M_Nb, 1))
- {
- this->_M_do_right_shift(__pos);
- this->_M_do_sanitize();
- }
+ this->_M_do_right_shift(__pos);
else
this->_M_do_reset();
return *this;
this->_M_w[__wshift] = this->_M_w[0] << __offset;
}
- //// std::fill(this->_M_w.begin(), this->_M_w.begin() + __wshift,
- //// static_cast<_WordT>(0));
+ std::fill_n(this->_M_w.begin(), __wshift, _WordT(0));
}
}
this->_M_w[__limit] = this->_M_w[_M_w.size()-1] >> __offset;
}
- ////std::fill(this->_M_w.begin() + __limit + 1, this->_M_w.end(),
- //// static_cast<_WordT>(0));
+ std::fill_n(this->_M_w.end() - __wshift, __wshift, _WordT(0));
}
}
--- /dev/null
+// { dg-do run { target c++11 } }
+
+// PR libstdc++/115399
+// std::tr2::dynamic_bitset shift behaves differently from std::bitset
+
+#include <tr2/dynamic_bitset>
+#include <testsuite_hooks.h>
+
+void
+test_left_shift()
+{
+ std::tr2::dynamic_bitset<> b(65);
+ b[0] = 1;
+ auto b2 = b << 64;
+ VERIFY(b2[64] == 1);
+ VERIFY(b2[0] == 0);
+ b <<= 64;
+ VERIFY( b2 == b );
+}
+
+void
+test_right_shift()
+{
+ std::tr2::dynamic_bitset<> b(65);
+ b[64] = 1;
+ auto b2 = b >> 64;
+ VERIFY(b2[64] == 0);
+ VERIFY(b2[0] == 1);
+ b >>= 64;
+ VERIFY( b2 == b );
+}
+
+int main()
+{
+ test_left_shift();
+ test_right_shift();
+}