From patchwork Thu Jan 11 23:04:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 187505 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:2411:b0:101:2151:f287 with SMTP id m17csp1776654dyi; Thu, 11 Jan 2024 15:06:37 -0800 (PST) X-Google-Smtp-Source: AGHT+IHrKCRo2w7Qe04SdwsuWhYEfvtPk3ARQF3M303WeVfN/yS5BjRvlH0JOO+qNRgMZXImAyos X-Received: by 2002:a05:6808:3c99:b0:3bd:4bf3:9128 with SMTP id gs25-20020a0568083c9900b003bd4bf39128mr111664oib.9.1705014397004; Thu, 11 Jan 2024 15:06:37 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1705014396; cv=pass; d=google.com; s=arc-20160816; b=na/7Pp4KHe6XfcJVPuo//IxeBWr/zTN8dGeJAGmaajcW82gQ69vfeISeEzKFAmQDVQ 32Spu2ubpBNPbHsV49cJCudlGSDalcuf54HtSPLYadT58WIONyNVBQF7h+7kSwdNmg9e nsaWHIxTbeDCrIUu5wqDj+69RFhkf+PFd1AVxEXSvlMzFapHvuqKeTHdzF+M/QMwFL05 448e2y6XwvkZp21aD0pJ4TMDjhoY4rxx0yvt3T1YJqBZw+o+credup/PLif2a5prBlYm /fGzF4wU5wk7WKJFOdguyp48Fye6dPlbSeGJ2QPh/jWi4mW57d0zbzIyHo8dJ+fJYg68 MpnQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:message-id:date:subject:to:from:dkim-signature :arc-filter:dmarc-filter:delivered-to; bh=nre+shovu/WLBZBHRJKq/lUv6xUA3/V/QhiGmFWsGhI=; fh=sJ+2/4g29YdyXkoRrFZSpsL2zxijepB7X/1rB0LDDh8=; b=T4HpTVmAEioEQ1UcvZV9N63WiLhI1J1kYQf40ICk0k/dO8qOI742h5j6wc+I1e43EM FPR7Be2BNS6Jt5i2uwb9C6fKjBCD/ZDYXvqW8PlVzbBkXY8fLYqKEB3cTZ0TGEVJEren kCq7OjVH9jDHN+rPoQqrPWvUsnQKQxkg5xGFcl0j5EORXghrCemuq3wK99wJdsDDn7jJ iBjnfk3x4M+cNePux8qLfZcu3WGNNL/bw9iRCaM7mqw80Pw4harqrhEpFncGuWJ62ygo IJVPVt1xRDAXcxLfziaF+JytjE/NwFy6xWWK3JBRfO+RQm2LHp2cnB0u3ztmSjI6sT9s 4Zrw== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=XMQUxAXH; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id p17-20020a0ce191000000b0067f235c2aa4si1764941qvl.69.2024.01.11.15.06.36 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 11 Jan 2024 15:06:36 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=XMQUxAXH; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 9E4B43857B8B for ; Thu, 11 Jan 2024 23:06:36 +0000 (GMT) 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.129.124]) by sourceware.org (Postfix) with ESMTPS id 693F93858D32 for ; Thu, 11 Jan 2024 23:05:51 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 693F93858D32 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 693F93858D32 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1705014353; cv=none; b=H6wxg8rPEYlEJ4RmowpGOGXfHwvC75AuNF9KM12PY+EXh4EXJi0i+bBC9bAk4pyzwibHShfyIl3cAqMit+9mjyQZh04HIVB+uOm/Ku60Vcl6647X1443gC7dljZt6GG9eP1+vuKdK3SpWpXdT/Omd1/UFTtVD1rf7X/NsHgR1l4= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1705014353; c=relaxed/simple; bh=ZYgxak8mp9uuayccXxDfxePP9MKe90MOzFAAtT/YEu8=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=T+awaodi3zd80yGIEI0Ujb3p3EXb9VsYZLTmjfY6MHcmH+QgBBzkPFPqUTvVFq+rI5ScYcBqsd4wMkr69hO6atig8EC3GMjjqF0JraR3wzGxYkgREG52zOc2NtRRJ6S9wQ89MvvNZsaxbgoX/2jR1fboT6QIF3sjeMdLQgHVY3w= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1705014351; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=nre+shovu/WLBZBHRJKq/lUv6xUA3/V/QhiGmFWsGhI=; b=XMQUxAXHXVk3omZ7QWR3B8+oZvxrVGeoJQ7sFnUqY+/NzCyyrQreNacB+jk2f6JlKdrMY6 +9IllBgvuv6xyJ+9x1W2JAtKUeRFKsH36ERNQCjd2DtSVuIrHPmbxTWVe8sHnfkUHjH7k2 qV4VASId9UkOJq/O4GJDivH4VRzT1fo= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-183-ppPsJptZM5iWpNhyJqlODg-1; Thu, 11 Jan 2024 18:05:49 -0500 X-MC-Unique: ppPsJptZM5iWpNhyJqlODg-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 56D6C185A780; Thu, 11 Jan 2024 23:05:49 +0000 (UTC) Received: from localhost (unknown [10.42.28.185]) by smtp.corp.redhat.com (Postfix) with ESMTP id 1F83E1C0652C; Thu, 11 Jan 2024 23:05:49 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [PATCH] libstdc++: Fix non-portable results from 64-bit std::subtract_with_carry_engine [PR107466] Date: Thu, 11 Jan 2024 23:04:29 +0000 Message-ID: <20240111230548.623208-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.7 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-13.1 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE 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.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1787837175936432658 X-GMAIL-MSGID: 1787837175936432658 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(-) 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(); +}