Merge #12537: [arith_uint256] Make it safe to use "self" in operators
b120f7b
[test] Add tests for self usage in arith_uint256 (Karl-Johan Alm)08b17de
[arith_uint256] Do not destroy *this content if passed-in operator may reference it (Karl-Johan Alm) Pull request description: Before this fix (see test commit), `v *= v` would result in `0` because `operator*=` set `*this` (`==b`) to `0` at the start. This patch changes the code to use `a` as temporary for `*this`~~, with drawback that `*this` is set to `a` at the end, an extra `=` operation in other words~~. Tree-SHA512: 8028a99880c3198a39c4bcc5056169735ba960625d553e15c0317510a52940c875f7a1fefe14e1af7fcf10c07a246411994a328cb1507bf3eaf1b6e7425390dc
This commit is contained in:
commit
4781813b56
2 changed files with 17 additions and 4 deletions
|
@ -69,16 +69,16 @@ base_uint<BITS>& base_uint<BITS>::operator*=(uint32_t b32)
|
|||
template <unsigned int BITS>
|
||||
base_uint<BITS>& base_uint<BITS>::operator*=(const base_uint& b)
|
||||
{
|
||||
base_uint<BITS> a = *this;
|
||||
*this = 0;
|
||||
base_uint<BITS> a;
|
||||
for (int j = 0; j < WIDTH; j++) {
|
||||
uint64_t carry = 0;
|
||||
for (int i = 0; i + j < WIDTH; i++) {
|
||||
uint64_t n = carry + pn[i + j] + (uint64_t)a.pn[j] * b.pn[i];
|
||||
pn[i + j] = n & 0xffffffff;
|
||||
uint64_t n = carry + a.pn[i + j] + (uint64_t)pn[j] * b.pn[i];
|
||||
a.pn[i + j] = n & 0xffffffff;
|
||||
carry = n >> 32;
|
||||
}
|
||||
}
|
||||
*this = a;
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
|
|
@ -266,4 +266,17 @@ BOOST_AUTO_TEST_CASE( conversion )
|
|||
BOOST_CHECK(R2L.GetHex() == UintToArith256(R2L).GetHex());
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE( operator_with_self )
|
||||
{
|
||||
arith_uint256 v = UintToArith256(uint256S("02"));
|
||||
v *= v;
|
||||
BOOST_CHECK(v == UintToArith256(uint256S("04")));
|
||||
v /= v;
|
||||
BOOST_CHECK(v == UintToArith256(uint256S("01")));
|
||||
v += v;
|
||||
BOOST_CHECK(v == UintToArith256(uint256S("02")));
|
||||
v -= v;
|
||||
BOOST_CHECK(v == UintToArith256(uint256S("0")));
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
|
|
Loading…
Reference in a new issue