libstdc++: Fix non-portable results from 64-bit std::subtract_with_carry_engine ...
commitc224dec0e7c88e7a95633023018cdcb6ee87c65f
authorJonathan Wakely <jwakely@redhat.com>
Thu, 11 Jan 2024 15:09:12 +0000 (11 15:09 +0000)
committerJonathan Wakely <jwakely@redhat.com>
Sat, 13 Jan 2024 00:11:44 +0000 (13 00:11 +0000)
tree3bde38a9fdabe2695c9220f36e5f4b947718f361
parent8b447fa89d5e6b052c9309dfd1dafebdbd829ff9
libstdc++: Fix non-portable results from 64-bit std::subtract_with_carry_engine [PR107466]

I implemented the resolution of LWG 3809 in r13-4364-ga64775a0edd469 but
it was recently noted in the MSVC STL github repo that the change causes
possible truncation for 64-bit seeds. Whether the truncation occurs (and
to what value) depends on the width of uint_least32_t which is not
portable, so the output of the PRNG for 64-bit seed values is no longer
the same as in C++20, and no longer portable across platforms.

That new issue was filed as LWG 4014. I proposed a new change which
reduces the seed by the LCG's modulus before the conversion to
uint_least32_t. This ensures that 64-bit seed values are consistently
reduced by the modulus before any truncation. This removes the
platform-dependent behaviour and restores the old behaviour for
std::subtract_with_carry_engine specializations using a 64-bit result
type (such as std::ranlux48_base).

libstdc++-v3/ChangeLog:

PR libstdc++/107466
* include/bits/random.tcc (subtract_with_carry_engine::seed):
Implement proposed resolution of LWG 4014.
* testsuite/26_numerics/random/pr60037-neg.cc: Adjust dg-error
line number.
* testsuite/26_numerics/random/subtract_with_carry_engine/cons/lwg3809.cc:
Check for expected result of 64-bit engine with seed that
doesn't fit in 32-bits.
libstdc++-v3/include/bits/random.tcc
libstdc++-v3/testsuite/26_numerics/random/pr60037-neg.cc
libstdc++-v3/testsuite/26_numerics/random/subtract_with_carry_engine/cons/lwg3809.cc