From 3c4abd4c3c6a308866862febf2f036917ff6b29e Mon Sep 17 00:00:00 2001 From: MITSUNARI Shigeo Date: Fri, 30 Apr 2021 15:41:30 +0900 Subject: [PATCH] fix sign for alised var for Vint::quotRem --- include/mcl/vint.hpp | 6 ++++-- test/vint_test.cpp | 27 +++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/include/mcl/vint.hpp b/include/mcl/vint.hpp index e642cb6..45d8454 100644 --- a/include/mcl/vint.hpp +++ b/include/mcl/vint.hpp @@ -1544,10 +1544,12 @@ public: */ static void quotRem(VintT *q, VintT& r, const VintT& x, const VintT& y) { + assert(q != &r); VintT yy = y; - bool qsign = x.isNeg_ ^ y.isNeg_; + bool yNeg = y.isNeg_; + bool qsign = x.isNeg_ ^ yNeg; udiv(q, r, x.buf_, x.size(), y.buf_, y.size()); - r.isNeg_ = y.isNeg_; + r.isNeg_ = yNeg; if (q) q->isNeg_ = qsign; if (!r.isZero() && qsign) { if (q) { diff --git a/test/vint_test.cpp b/test/vint_test.cpp index 6f34ede..d96c1c7 100644 --- a/test/vint_test.cpp +++ b/test/vint_test.cpp @@ -973,6 +973,33 @@ CYBOZU_TEST_AUTO(Vint) CYBOZU_TEST_EQUAL(q2, tbl[i].q2); CYBOZU_TEST_EQUAL(r2, tbl[i].r2); CYBOZU_TEST_EQUAL(q2 * b + r2, a); + r2 = 0; + Vint::quotRem(&b, r2, a, b); + CYBOZU_TEST_EQUAL(b, tbl[i].q2); + CYBOZU_TEST_EQUAL(r2, tbl[i].r2); + b = tbl[i].b; + r2 = 0; + Vint::quotRem(&a, r2, a, b); + CYBOZU_TEST_EQUAL(a, tbl[i].q2); + CYBOZU_TEST_EQUAL(r2, tbl[i].r2); + a = tbl[i].a; + q = 0; + Vint::quotRem(&q, a, a, b); + CYBOZU_TEST_EQUAL(q, tbl[i].q2); + CYBOZU_TEST_EQUAL(a, tbl[i].r2); + a = tbl[i].a; + q = 0; + Vint::quotRem(&q, a, a, b); + CYBOZU_TEST_EQUAL(q, tbl[i].q2); + CYBOZU_TEST_EQUAL(a, tbl[i].r2); + a = tbl[i].a; + q = 0; + Vint::quotRem(&q, b, a, b); + CYBOZU_TEST_EQUAL(q, tbl[i].q2); + CYBOZU_TEST_EQUAL(b, tbl[i].r2); + Vint::quotRem(&q, a, a, a); + CYBOZU_TEST_EQUAL(q, 1); + CYBOZU_TEST_EQUAL(a, 0); } CYBOZU_TEST_EQUAL(Vint("15") / Vint("3"), Vint("5")); CYBOZU_TEST_EQUAL(Vint("15") / Vint("-3"), Vint("-5"));