From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2181) id 1855F3858C50; Thu, 27 Apr 2023 14:43:49 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 1855F3858C50 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1682606629; bh=rMJKl+scS8A3ZTjK1zCnnIxccqDJ9ZuqycKlfMEUzQg=; h=From:To:Subject:Date:From; b=lQWpJu1C9TojLCIzKHQfuoA9gK3Z+P5mUEPbopQXMTs3NwSnBbArpbdIQEhNPqCBq sknG05qRNO9NayWy/fVyzdFu5L/ILOu5JcMoLyI55p9a6Xr+DLxB2HKy2F7yTbeuEy QpzHKWtYuQdkm/j8ZzU2hCEPiYCnEBuUcWCBNi4o= MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="utf-8" From: Jonathan Wakely To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r12-9485] libstdc++: Make 16-bit std::subtract_with_carry_engine work [PR107466] X-Act-Checkin: gcc X-Git-Author: Jonathan Wakely X-Git-Refname: refs/heads/releases/gcc-12 X-Git-Oldrev: 8aa615e0230cc73bd06281dddb187d371a6d2824 X-Git-Newrev: d8def0b55572fc5bc02a0fee6a98787cd50ee885 Message-Id: <20230427144349.1855F3858C50@sourceware.org> Date: Thu, 27 Apr 2023 14:43:49 +0000 (GMT) List-Id: https://gcc.gnu.org/g:d8def0b55572fc5bc02a0fee6a98787cd50ee885 commit r12-9485-gd8def0b55572fc5bc02a0fee6a98787cd50ee885 Author: Jonathan Wakely Date: Mon Nov 28 09:44:52 2022 +0000 libstdc++: Make 16-bit std::subtract_with_carry_engine work [PR107466] This implements the proposed resolution of LWG 3809, so that std::subtract_with_carry_engine can be used with a 16-bit result_type. Currently this produces a narrowing error when instantiating the std::linear_congruential_engine to create the initial state. It also truncates the default_seed constant when passing it as a result_type argument. Change the type of the constant to uint_least32_t and pass 0u when the default_seed should be used. libstdc++-v3/ChangeLog: PR libstdc++/107466 * include/bits/random.h (subtract_with_carry_engine): Use 32-bit type for default seed. Use 0u as default argument for subtract_with_carry_engine(result_type) constructor and seed(result_type) member function. * include/bits/random.tcc (subtract_with_carry_engine): Use 32-bit type for default seed and engine used for initial state. * testsuite/26_numerics/random/subtract_with_carry_engine/cons/lwg3809.cc: New test. (cherry picked from commit a64775a0edd46980036b757041f0c065ed9f8d22) Diff: --- libstdc++-v3/include/bits/random.h | 6 ++--- libstdc++-v3/include/bits/random.tcc | 4 ++-- .../subtract_with_carry_engine/cons/lwg3809.cc | 26 ++++++++++++++++++++++ 3 files changed, 31 insertions(+), 5 deletions(-) diff --git a/libstdc++-v3/include/bits/random.h b/libstdc++-v3/include/bits/random.h index 3e6eb9de7d9..8af571e406d 100644 --- a/libstdc++-v3/include/bits/random.h +++ b/libstdc++-v3/include/bits/random.h @@ -713,9 +713,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION static constexpr size_t word_size = __w; static constexpr size_t short_lag = __s; static constexpr size_t long_lag = __r; - static constexpr result_type default_seed = 19780503u; + static constexpr uint_least32_t default_seed = 19780503u; - subtract_with_carry_engine() : subtract_with_carry_engine(default_seed) + subtract_with_carry_engine() : subtract_with_carry_engine(0u) { } /** @@ -750,7 +750,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION * set carry to 1, otherwise sets carry to 0. */ void - seed(result_type __sd = default_seed); + seed(result_type __sd = 0u); /** * @brief Seeds the initial state @f$x_0@f$ of the diff --git a/libstdc++-v3/include/bits/random.tcc b/libstdc++-v3/include/bits/random.tcc index 87a16a21336..6b4e440db45 100644 --- a/libstdc++-v3/include/bits/random.tcc +++ b/libstdc++-v3/include/bits/random.tcc @@ -532,7 +532,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION subtract_with_carry_engine<_UIntType, __w, __s, __r>::long_lag; template - constexpr _UIntType + constexpr uint_least32_t subtract_with_carry_engine<_UIntType, __w, __s, __r>::default_seed; #endif @@ -541,7 +541,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION subtract_with_carry_engine<_UIntType, __w, __s, __r>:: seed(result_type __value) { - std::linear_congruential_engine + std::linear_congruential_engine __lcg(__value == 0u ? default_seed : __value); const size_t __n = (__w + 31) / 32; diff --git a/libstdc++-v3/testsuite/26_numerics/random/subtract_with_carry_engine/cons/lwg3809.cc b/libstdc++-v3/testsuite/26_numerics/random/subtract_with_carry_engine/cons/lwg3809.cc new file mode 100644 index 00000000000..21f246b8dc0 --- /dev/null +++ b/libstdc++-v3/testsuite/26_numerics/random/subtract_with_carry_engine/cons/lwg3809.cc @@ -0,0 +1,26 @@ +// { dg-do run { target c++11 } } +#include +#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() +{ + // It should be possible to construct this engine with a 16-bit result_type: + std::subtract_with_carry_engine s16; + std::subtract_with_carry_engine s32; + // It should have been seeded with the same sequence as the 32-bit version + // and produce random numbers in the same range, [0, 1<<12). + for (int i = 0; i < 10; ++i) + VERIFY( s16() == s32() ); + // The default seed should be usable without truncation to uint16_t: + s16.seed(); + s32.seed(); + for (int i = 0; i < 10; ++i) + VERIFY( s16() == s32() ); + s16.seed(101); + s32.seed(101); + for (int i = 0; i < 10; ++i) + VERIFY( s16() == s32() ); +}