public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "redi at gcc dot gnu.org" <gcc-bugzilla@gcc.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	[thread overview]
Message-ID: <bug-86880-4-Dz8XL6m7XP@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-86880-4@http.gcc.gnu.org/bugzilla/>

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.

  parent reply	other threads:[~2023-05-30 11:21 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [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 [this message]
2023-05-30 11:21 ` redi at gcc dot gnu.org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-86880-4-Dz8XL6m7XP@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).