This is an archive of the discontinued LLVM Phabricator instance.

[libc++] Use correct rand.eng.mers all-zeroes seed sequence fallback
ClosedPublic

Authored by hubert.reinterpretcast on Aug 14 2018, 1:53 PM.

Details

Summary

When a seed sequence would lead to having no non-zero significant bits in the initial state of a mersenne_twister_engine, the fallback is to flip the most significant bit of the first value that appears in the textual representation of the initial state.

rand.eng.mers describes this as setting the value to be 2 to the power of one less than w; the previous value encoded in the implementation, namely one less than "2 to the power of w", is replaced by the correct value in this patch.

Diff Detail

Repository
rCXX libc++

Event Timeline

Is this test that's being added libc++ specific, or would it apply to other implementations as well?

test/libcxx/numerics/rand/rand.eng.mers/cnstr_sseq_all_zero.pass.cpp
18 ↗(On Diff #160687)

Please add a comment here about what's being tested, because when I look at this a year from now, I won't know what's going on.

Something like:
// Finally, if the most significant w − r bits of X−n are zero, and if each of the other resulting Xi is 0, changes X−n to 2w−1.

47 ↗(On Diff #160687)

Need a line break before void

Is this test that's being added libc++ specific, or would it apply to other implementations as well?

The test can apply to other implementations as well (although I am not sure how the initializer_list include behaves under C++03 on other implementations). Is there some other place to put such tests?

test/libcxx/numerics/rand/rand.eng.mers/cnstr_sseq_all_zero.pass.cpp
47 ↗(On Diff #160687)

I have no objection to adding a line break, but I would like to clarify that this line came straight out of a recent build of clang-format with --style=llvm. I would like to know whether there is something else here that I should be doing with clang-format, including if it is deemed that clang-format does not do the right thing here.

Is this test that's being added libc++ specific, or would it apply to other implementations as well?

The test can apply to other implementations as well (although I am not sure how the initializer_list include behaves under C++03 on other implementations). Is there some other place to put such tests?

It seems that test/std/numerics/rand/rand.eng/rand.eng.mers/ may be the place to add the test. I am looking into it.

test/libcxx/numerics/rand/rand.eng.mers/cnstr_sseq_all_zero.pass.cpp
18 ↗(On Diff #160687)

I will add a comment to this effect. In an effort to be clear in the formatting, I will use the LaTeX snippet for the text.

Address review comments by Marshall

Add a line break after the template-head. Add a comment with the requested quote from the Standard. Move test from the test/libcxx/ tree to the test/std/ tree, and guard against inclusion of <initializer_list> under C++03.

hubert.reinterpretcast marked 4 inline comments as done.Aug 15 2018, 3:03 PM

Thanks for doing this.
This looks good to me; but I want to play with it a bit before committing. I'll do that tonight.

mclow.lists accepted this revision.Aug 16 2018, 10:24 AM

This LGTM. Do you want me to commit it for you?

This revision is now accepted and ready to land.Aug 16 2018, 10:24 AM

I can commit sometime today; thanks.

  • HT
This revision was automatically updated to reflect the committed changes.