Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement LWG-3809 Is subtract_with_carry_engine<uint16_t> supposed to work? #4194

Merged
merged 3 commits into from
Nov 17, 2023

Conversation

frederick-vs-ja
Copy link
Contributor

Fixes #4162.

Also updates citations in the modified test file.

@frederick-vs-ja frederick-vs-ja requested a review from a team as a code owner November 14, 2023 16:31
stl/inc/random Outdated Show resolved Hide resolved
@StephanTLavavej StephanTLavavej added the LWG Library Working Group issue label Nov 14, 2023
@StephanTLavavej StephanTLavavej self-assigned this Nov 14, 2023
stl/inc/random Outdated
Comment on lines 697 to 701
void seed(_Seed_t _Value = default_seed, bool _Readcy = false) { // set initial values from specified seed value
linear_congruential<_Seed_t, 40014U, 0U, 2147483563U> _Lc{_Value == 0U ? default_seed : _Value};
linear_congruential_engine<uint_least32_t, 40014U, 0U, 2147483563U> _Lc{
static_cast<uint_least32_t>(_Value == 0U ? default_seed : _Value)};
_Reset(_Lc, _Readcy);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @jwakely. Sorry to bother you, but I think you provided the wording for LWG-3809 "Is std::subtract_with_carry_engine<uint16_t> supposed to work?" This part causes the seed value to be truncated to at least 32 bits (maybe 32, maybe more). Is that intentional? It causes the results to change for wider types and also makes it non-portable because the amount of truncation isn't specified. We'd appreciate if you could provide some insight. Thanks!

Copy link

@jwakely jwakely Nov 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The seed value will be reduced mod 2147483563 anyway, and the subtract_with_carry_engine uses the LCG engine to produce n 32-bit values (where n is the number of 32-bit pieces needed for a word of size w), so a 32-bit LCG is the right choice. Using a larger seed is possible, but has no benefit.

I suppose we could do value % 2147483563 before the conversion to UIntType:

linear_congruential_engine<uint_least32_t,
                           40014u,0u,2147483563u> e(value == 0u ? default_seed : value % 2147483563u);

That would remove any dependence on the width of uint_least32_t.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your comment. I can write a new LWG issue explaining the situation and proposing that fix, unless there's a better way to handle it.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would be great, thanks.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For reference: LWG-4014.

stl/inc/random Outdated Show resolved Hide resolved
stl/inc/random Show resolved Hide resolved
@StephanTLavavej
Copy link
Member

Thanks! I pushed very minor changes to the tr1 codepaths to make them consistent with the Standard codepaths, which I think will be far less confusing. I think this needs only one maintainer approval.

@StephanTLavavej StephanTLavavej removed their assignment Nov 15, 2023
Comment on lines +697 to +699
void seed(_Seed_t _Value = 0u, bool _Readcy = false) { // set initial values from specified seed value
linear_congruential_engine<uint_least32_t, 40014U, 0U, 2147483563U> _Lc{
static_cast<uint_least32_t>(_Value == 0U ? default_seed : _Value)};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style:
Case inconsistency between u and U literals, or it doesn't matter?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that the standard wording consistently uses lowercase (u) in [rand], while MSVC STL consistently uses uppercase (U)...

@StephanTLavavej StephanTLavavej self-assigned this Nov 16, 2023
@StephanTLavavej
Copy link
Member

I'm mirroring this to the MSVC-internal repo - please notify me if any further changes are pushed.

@StephanTLavavej StephanTLavavej merged commit 06d2966 into microsoft:main Nov 17, 2023
@StephanTLavavej
Copy link
Member

Thanks for subtracting 1 from the number of remaining LWG issues! 😹 🎉 📉

@frederick-vs-ja frederick-vs-ja deleted the lwg-3809 branch November 17, 2023 03:21
@jwakely
Copy link

jwakely commented Nov 17, 2023

Only until the new one is opened ;-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
LWG Library Working Group issue
Projects
None yet
Development

Successfully merging this pull request may close these issues.

LWG-3809 Is subtract_with_carry_engine<uint16_t> supposed to work?
5 participants