From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2181) id 487593858D1E; Sat, 13 Jan 2024 00:14:17 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 487593858D1E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1705104857; bh=gLVhHAiDhXC99JXE99z0RNKUzgstVUBabVfbx60bmSU=; h=From:To:Subject:Date:From; b=bra3Dc15ALBbp8/FgmGg+MCICrjSkWn9egkQdDsDA1iLjW2w/dvMZZzyLNoSy1CDx hzMC4U6b8nnjLkr0qP8czPe140Tv4RKhccKm0b3Qo9Vb2te+F1eODisrcAgg3O3XQj 3crCczpBiYK7tqqoFtxTBihzVA9wGpoZCtI6gh7g= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jonathan Wakely To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r14-7216] libstdc++: Fix non-portable results from 64-bit std::subtract_with_carry_engine [PR107466] X-Act-Checkin: gcc X-Git-Author: Jonathan Wakely X-Git-Refname: refs/heads/master X-Git-Oldrev: 8b447fa89d5e6b052c9309dfd1dafebdbd829ff9 X-Git-Newrev: c224dec0e7c88e7a95633023018cdcb6ee87c65f Message-Id: <20240113001417.487593858D1E@sourceware.org> Date: Sat, 13 Jan 2024 00:14:17 +0000 (GMT) List-Id: https://gcc.gnu.org/g:c224dec0e7c88e7a95633023018cdcb6ee87c65f commit r14-7216-gc224dec0e7c88e7a95633023018cdcb6ee87c65f Author: Jonathan Wakely Date: Thu Jan 11 15:09:12 2024 +0000 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. Diff: --- libstdc++-v3/include/bits/random.tcc | 5 ++++- .../testsuite/26_numerics/random/pr60037-neg.cc | 2 +- .../random/subtract_with_carry_engine/cons/lwg3809.cc | 19 +++++++++++++++++-- 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/libstdc++-v3/include/bits/random.tcc b/libstdc++-v3/include/bits/random.tcc index 7f4bf5ea183..ade416390b3 100644 --- a/libstdc++-v3/include/bits/random.tcc +++ b/libstdc++-v3/include/bits/random.tcc @@ -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 supposed to work? + // 4014. LWG 3809 changes behavior of some existing code std::linear_congruential_engine - __lcg(__value == 0u ? default_seed : __value); + __lcg(__value == 0u ? default_seed : __value % 2147483563u); const size_t __n = (__w + 31) / 32; diff --git a/libstdc++-v3/testsuite/26_numerics/random/pr60037-neg.cc b/libstdc++-v3/testsuite/26_numerics/random/pr60037-neg.cc index c58f480640f..59cf84adb48 100644 --- a/libstdc++-v3/testsuite/26_numerics/random/pr60037-neg.cc +++ b/libstdc++-v3/testsuite/26_numerics/random/pr60037-neg.cc @@ -12,4 +12,4 @@ auto x = std::generate_canonical #include -// LWG 3809. Is std::subtract_with_carry_engine supposed to work? // PR 107466 - invalid -Wnarrowing error with std::subtract_with_carry_engine -int main() +// LWG 3809. Is std::subtract_with_carry_engine 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 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(); +}