libstdc++: Fix non-portable results from 64-bit std::subtract_with_carry_engine [PR107466]
Checks
Commit Message
This fixes a regression introduced by the LWG 3809 change, so is needed
on trunk and gcc-13 and gcc-12.
Tested x86_64-linux and aarch64-linux.
-- >8 --
I implemented the resolution of LWG 3809 in r13-4364-ga64775a0edd469 but
more recently it was noted 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. The new
problem 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 a consistent result across paltforms, 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 | 5 ++++-
.../26_numerics/random/pr60037-neg.cc | 2 +-
.../cons/lwg3809.cc | 19 +++++++++++++++++--
3 files changed, 22 insertions(+), 4 deletions(-)
@@ -541,8 +541,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
subtract_with_carry_engine<_UIntType, __w, __s, __r>::
seed(result_type __value)
{
+ // _GLIBCXX_RESOLVE_LIB_DEFECTS
+ // 3809. Is std::subtract_with_carry_engine<uint16_t> supposed to work?
+ // 4014. LWG 3809 changes behavior of some existing code
std::linear_congruential_engine<uint_least32_t, 40014u, 0u, 2147483563u>
- __lcg(__value == 0u ? default_seed : __value);
+ __lcg(__value == 0u ? default_seed : __value % 2147483563u);
const size_t __n = (__w + 31) / 32;
@@ -12,4 +12,4 @@ auto x = std::generate_canonical<std::size_t,
// { dg-error "static assertion failed: template argument must be a floating point type" "" { target *-*-* } 169 }
-// { dg-error "static assertion failed: template argument must be a floating point type" "" { target *-*-* } 3348 }
+// { dg-error "static assertion failed: template argument must be a floating point type" "" { target *-*-* } 3351 }
@@ -2,10 +2,11 @@
#include <random>
#include <testsuite_hooks.h>
-// LWG 3809. Is std::subtract_with_carry_engine<uint16_t> supposed to work?
// PR 107466 - invalid -Wnarrowing error with std::subtract_with_carry_engine
-int main()
+// LWG 3809. Is std::subtract_with_carry_engine<uint16_t> supposed to work?
+void
+test_lwg3809()
{
// It should be possible to construct this engine with a 16-bit result_type:
std::subtract_with_carry_engine<uint16_t, 12, 5, 12> s16;
@@ -24,3 +25,17 @@ int main()
for (int i = 0; i < 10; ++i)
VERIFY( s16() == s32() );
}
+
+// LWG 4014. LWG 3809 changes behavior of some existing code
+void
+test_lwg4014()
+{
+ std::ranlux48_base g(-1U + 1LL);
+ VERIFY( g() == 22575453646312 );
+}
+
+int main()
+{
+ test_lwg3809();
+ test_lwg4014();
+}