From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 079B13858C00; Tue, 30 May 2023 11:21:27 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 079B13858C00 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1685445688; bh=PFTRbivyx2b8HQE1XqxwFkdTEtBrYLW9mXuMofa0xEM=; h=From:To:Subject:Date:In-Reply-To:References:From; b=P7CBFou6IzgyvZUEsJ4PYtXAT4v/GARDW9FKFfoJYR0tztWBTnXdsX4+iEY8K/QsN 8h9TA5WyEZy8NOllTnjRXw7PSX+XEWrzVPgAJn+vadQBSX/Fp5MMVNE0IP5N6+Z3Dc ZxgIrWxZAEuRXk3bhdYoocSZsmgV/u4Gs15VP18A= From: "redi at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug libstdc++/86880] Incorrect mersenne_twister_engine equality comparison between rotated states Date: Tue, 30 May 2023 11:21:27 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: libstdc++ X-Bugzilla-Version: 9.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: redi at gcc dot gnu.org X-Bugzilla-Status: NEW X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: Message-ID: In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D86880 --- Comment #2 from Jonathan Wakely --- This would fix the equality operator to correctly compare rotated states: --- a/libstdc++-v3/include/bits/random.h +++ b/libstdc++-v3/include/bits/random.h @@ -601,8 +601,37 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION friend bool operator=3D=3D(const mersenne_twister_engine& __lhs, const mersenne_twister_engine& __rhs) - { return (std::equal(__lhs._M_x, __lhs._M_x + state_size, __rhs._M_x) - && __lhs._M_p =3D=3D __rhs._M_p); } + { + const _UIntType* const __lx =3D __lhs._M_x; + const _UIntType* const __rx =3D __rhs._M_x; + size_t __lp =3D __lhs._M_p % state_size; + size_t __rp =3D __rhs._M_p % state_size; + size_t __n1, __n2; + if (__lp > __rp) + { + __n1 =3D state_size - __lp; + __n2 =3D __lp - __rp; + } + else + { + __n1 =3D state_size - __rp; + __n2 =3D __rp - __lp; + } + if (!std::equal(__lx + __lp, __lx + __lp + __n1, __rx + __rp)) + return false; + if (__n1 =3D=3D state_size) // i.e. __lhs._M_p =3D=3D 0 && __rhs._M= _p =3D=3D 0 + return true; + __lp =3D (__lp + __n1) % state_size; + __rp =3D (__rp + __n1) % state_size; + if (!std::equal(__lx + __lp, __lx + __lp + __n2, __rx + __rp)) + return false; + __lp =3D (__lp + __n2) % state_size; + __rp =3D (__rp + __n2) % state_size; + size_t __n3 =3D state_size - __n1 - __n2; + if (!std::equal(__lx + __lp, __lx + __lp + __n3, __rx + __rp)) + return false; + return true; + } /** * @brief Inserts the current state of a % mersenne_twister_engine But the testcase above still fails, because mteB and mteA have different content in the array, even though they produce the same sequence of numbers= .=