From patchwork Mon Nov 28 15:20:03 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 61171 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id D5C1F381D469 for ; Mon, 28 Nov 2022 15:20:38 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D5C1F381D469 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1669648838; bh=uV41w64G+GLllB+rjE+wnw9qxqnJCeRr4B75Qfb6IWs=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=czk0SGK22KPu6ahyAXRZLDhObWRJzsNpzgn2C5yf1tvWfOQ2rw/tBmmQ0mSAksyLd c9XXjEEI8KUoG0l2JnzhvhklsNynZueppdZ2w1lLrKa2V+EnQwqQVV2PkFQ5wO80UR TpnMHqRhp9lbGPVvNT+Yl4k6sp5TfzbA9Xv9H1+8= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 32B3D3858C62 for ; Mon, 28 Nov 2022 15:20:08 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 32B3D3858C62 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-604-zQWai4buOVmHs0gVvCUeFQ-1; Mon, 28 Nov 2022 10:20:06 -0500 X-MC-Unique: zQWai4buOVmHs0gVvCUeFQ-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 20D1A811E7A; Mon, 28 Nov 2022 15:20:06 +0000 (UTC) Received: from localhost (unknown [10.33.36.137]) by smtp.corp.redhat.com (Postfix) with ESMTP id DCE6A40C6EC2; Mon, 28 Nov 2022 15:20:05 +0000 (UTC) To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Make 16-bit std::subtract_with_carry_engine work [PR107466] Date: Mon, 28 Nov 2022 15:20:03 +0000 Message-Id: <20221128152003.41709-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.2 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.8 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, PP_MIME_FAKE_ASCII_TEXT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jonathan Wakely via Gcc-patches From: Jonathan Wakely Reply-To: Jonathan Wakely Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" Tested x86_64-linux. Pushed to trunk. -- >8 -- 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. --- libstdc++-v3/include/bits/random.h | 6 ++--- libstdc++-v3/include/bits/random.tcc | 4 +-- .../cons/lwg3809.cc | 26 +++++++++++++++++++ 3 files changed, 31 insertions(+), 5 deletions(-) create mode 100644 libstdc++-v3/testsuite/26_numerics/random/subtract_with_carry_engine/cons/lwg3809.cc diff --git a/libstdc++-v3/include/bits/random.h b/libstdc++-v3/include/bits/random.h index 3b4e7d42bb5..523ef2d6565 100644 --- a/libstdc++-v3/include/bits/random.h +++ b/libstdc++-v3/include/bits/random.h @@ -721,9 +721,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) { } /** @@ -758,7 +758,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 cb1d3675783..7ec2b3f6c35 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() ); +}