[llvm-bugs] [Bug 38470] New: Incorrect mersenne_twister_engine equality comparison with tempering

via llvm-bugs llvm-bugs at lists.llvm.org
Tue Aug 7 07:18:53 PDT 2018


https://bugs.llvm.org/show_bug.cgi?id=38470

            Bug ID: 38470
           Summary: Incorrect mersenne_twister_engine equality comparison
                    with tempering
           Product: libc++
           Version: 7.0
          Hardware: All
                OS: All
            Status: NEW
          Severity: normal
          Priority: P
         Component: All Bugs
          Assignee: unassignedclangbugs at nondot.org
          Reporter: hstong at ca.ibm.com
                CC: llvm-bugs at lists.llvm.org, mclow.lists at gmail.com

[rand.req.eng] specifies the comparison between two engines as returning true
if the infinite sequences of values that would be generated by repeated calls
to their respective operator()s are equivalent.

Tempering parameters may cause the values resulting from two engines to be the
same even if there is no number for which applying that number of calls to the
two engines will cause the two engines to have the same internal state.

Trivially, tempering_l may be selected as 0u to discard all bits of the
internal state from the output.

The following program seeds two mersenne_twister_engines with specific states.
It asserts that the values returned by the engines match for some number of
calls; and that the engines, after that set of calls, each compare equal to an
engine that is in the corresponding initial state.

It then asserts that the engines, now again in their initial states, compare
equal. The assertion fails; however, either an earlier assertion should have
failed, or the engines will indeed produce equivalent infinite sequences of
values.

Online compiler: https://wandbox.org/permlink/iurpDM59GPZiwugq


=== SOURCE (<stdin>):
#include <random>
#ifdef NDEBUG
#undef NDEBUG
#endif
#include <cassert>
#ifndef _LIBCPP_VERSION
#error
#endif

int main(void) {
  typedef unsigned int EngResType;

  constexpr size_t word_size = 3u;
  constexpr size_t state_size = 1u;
  constexpr size_t shift_size = 1u;

  constexpr size_t tempering_t = 0u;
  constexpr EngResType tempering_c = 0b001;
  constexpr size_t tempering_l = word_size;

  using namespace std;
  using EngineType =
      std::mersenne_twister_engine<EngResType, word_size, state_size,
                                   shift_size,
                                   0u,
                                   0x0,
                                   0u, 0x0,
                                   0u, 0x0,
                                   tempering_t, tempering_c,
                                   tempering_l,
                                   0>;

  const EngineType mteA0(0b100), mteB0(0b101);
  EngineType mteA(0b100), mteB(0b101);

  for (int i = 0; i < 4; ++i) {
    assert(mteA() == mteB());
  }
  assert(mteA == mteA0 && mteB == mteB0);
  assert(mteA == mteB);
}


=== COMPILER INVOCATION:
clang++ -stdlib=libc++ -std=c++17 -pedantic-errors -Wall -Wextra -Werror -xc++
- -o ./a.out


=== RUN SCRIPT:
./a.out


=== EXPECTED RUN RESULT:
(Exits with rc=0).


=== ACTUAL RUN RESULT:
a.out: <stdin>:40: int main(): Assertion `mteA == mteB' failed.


=== COMPILER VERSION INFO (clang++ -v):
clang version 8.0.0 (https://github.com/llvm-mirror/clang.git
074cccc152df061007c8500515698b57a5af7103)
(https://github.com/llvm-mirror/llvm.git
40868d437198d20a353ce4f4fb114b4d33efe5aa)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /opt/wandbox/clang-head/bin
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/5
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/5.4.0
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/6
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/6.0.0
Selected GCC installation: /usr/lib/gcc/x86_64-linux-gnu/5.4.0
Candidate multilib: .;@m64
Selected multilib: .;@m64

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20180807/ae3b5a5c/attachment.html>


More information about the llvm-bugs mailing list