public inbox for gcc-bugs@sourceware.org help / color / mirror / Atom feed
* [Bug libstdc++/86880] Incorrect mersenne_twister_engine equality comparison between rotated states [not found] <bug-86880-4@http.gcc.gnu.org/bugzilla/> @ 2023-05-23 10:17 ` redi at gcc dot gnu.org 2023-05-30 11:21 ` redi at gcc dot gnu.org 2023-05-30 11:21 ` redi at gcc dot gnu.org 2 siblings, 0 replies; 3+ messages in thread From: redi at gcc dot gnu.org @ 2023-05-23 10:17 UTC (permalink / raw) To: gcc-bugs https://gcc.gnu.org/bugzilla/show_bug.cgi?id=86880 --- Comment #1 from Jonathan Wakely <redi at gcc dot gnu.org> --- (In reply to Hubert Tong from comment #0) > Note: libstdc++ does not produce "2 1 2" as the textual representation > (as reported by PR 60441). But even with that fixed, this still fails. We print "2 0 2 1" as the textual representation of B, which means 2 |0 2, but that's not the same as |2 1 2 ^ permalink raw reply [flat|nested] 3+ messages in thread
* [Bug libstdc++/86880] Incorrect mersenne_twister_engine equality comparison between rotated states [not found] <bug-86880-4@http.gcc.gnu.org/bugzilla/> 2023-05-23 10:17 ` [Bug libstdc++/86880] Incorrect mersenne_twister_engine equality comparison between rotated states redi at gcc dot gnu.org @ 2023-05-30 11:21 ` redi at gcc dot gnu.org 2023-05-30 11:21 ` redi at gcc dot gnu.org 2 siblings, 0 replies; 3+ messages in thread From: redi at gcc dot gnu.org @ 2023-05-30 11:21 UTC (permalink / raw) To: gcc-bugs https://gcc.gnu.org/bugzilla/show_bug.cgi?id=86880 --- Comment #2 from Jonathan Wakely <redi at gcc dot gnu.org> --- 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==(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 == __rhs._M_p); } + { + const _UIntType* const __lx = __lhs._M_x; + const _UIntType* const __rx = __rhs._M_x; + size_t __lp = __lhs._M_p % state_size; + size_t __rp = __rhs._M_p % state_size; + size_t __n1, __n2; + if (__lp > __rp) + { + __n1 = state_size - __lp; + __n2 = __lp - __rp; + } + else + { + __n1 = state_size - __rp; + __n2 = __rp - __lp; + } + if (!std::equal(__lx + __lp, __lx + __lp + __n1, __rx + __rp)) + return false; + if (__n1 == state_size) // i.e. __lhs._M_p == 0 && __rhs._M_p == 0 + return true; + __lp = (__lp + __n1) % state_size; + __rp = (__rp + __n1) % state_size; + if (!std::equal(__lx + __lp, __lx + __lp + __n2, __rx + __rp)) + return false; + __lp = (__lp + __n2) % state_size; + __rp = (__rp + __n2) % state_size; + size_t __n3 = 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. ^ permalink raw reply [flat|nested] 3+ messages in thread
* [Bug libstdc++/86880] Incorrect mersenne_twister_engine equality comparison between rotated states [not found] <bug-86880-4@http.gcc.gnu.org/bugzilla/> 2023-05-23 10:17 ` [Bug libstdc++/86880] Incorrect mersenne_twister_engine equality comparison between rotated states redi at gcc dot gnu.org 2023-05-30 11:21 ` redi at gcc dot gnu.org @ 2023-05-30 11:21 ` redi at gcc dot gnu.org 2 siblings, 0 replies; 3+ messages in thread From: redi at gcc dot gnu.org @ 2023-05-30 11:21 UTC (permalink / raw) To: gcc-bugs https://gcc.gnu.org/bugzilla/show_bug.cgi?id=86880 --- Comment #3 from Jonathan Wakely <redi at gcc dot gnu.org> --- We have the same problem with std::subtract_with_carry_engine. Its equality operator doesn't work for rotated states. ^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2023-05-30 11:21 UTC | newest] Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- [not found] <bug-86880-4@http.gcc.gnu.org/bugzilla/> 2023-05-23 10:17 ` [Bug libstdc++/86880] Incorrect mersenne_twister_engine equality comparison between rotated states redi at gcc dot gnu.org 2023-05-30 11:21 ` redi at gcc dot gnu.org 2023-05-30 11:21 ` redi at gcc dot gnu.org
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).