From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 8BD8A3857023 for ; Mon, 24 Apr 2023 16:23:46 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 8BD8A3857023 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1682353426; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=By5E55kp6sLjzR7bmgxy9nJ0XTYklVb6BJkg6pQTOhI=; b=aimJ/rfO9xIx4O/FRWFYfy0f0JIRccOi7kS5QuANynPhODzeElIMbWCJY8qQq9w3rhFt5O lLaHtjiR7CDbv0BzKsAODzWX7uHaV/rlRelKVr6OMN1/ISD8NwAbyTRV5bm70GSePbhFv+ TM8FANWS4ol2aVYJxdNY9A6ELm4IKrw= Received: from mail-qv1-f70.google.com (mail-qv1-f70.google.com [209.85.219.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-1-Tq2D-vyVMUmbW-t02J8lWw-1; Mon, 24 Apr 2023 12:23:45 -0400 X-MC-Unique: Tq2D-vyVMUmbW-t02J8lWw-1 Received: by mail-qv1-f70.google.com with SMTP id 6a1803df08f44-5e7223aab07so27600336d6.0 for ; Mon, 24 Apr 2023 09:23:45 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682353424; x=1684945424; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=By5E55kp6sLjzR7bmgxy9nJ0XTYklVb6BJkg6pQTOhI=; b=enMbia3UnQ7adIm9ajmu9JS0A5Yc9s/ggWuPXH5yWk08plWRKyM0zmlICXqCytG/vb rUEp6Nv0jTP8QyQXbCJaOIxp3SnOte6ErWxHL7iRfFBjUppRndJmA1YIc6IhAUm7qAnH uomz0jfY6PHhftOEJtG2kfobToMuHgMj0whvzibFYDMVxQXdCdzbfuBOZ+t3QSB5QlSN xldI7beYkC1Smm28j6+u40qT13myp5GXn1qHkwWX9Vnmnw7xm2mEuLUHYPhrDddTlC3n mn7+BLL5IPOFHIRnxpNNQyH4YbG6QDDAUjYeEz077ArDLsoZAr+7HkQ5ezY2b56YJoKX PTcw== X-Gm-Message-State: AAQBX9ckSLdy6ZPs8oe6fFdwyc1x44b5dVjzC+mwyG5eXV+3L8WbU8RD TPjDxM7Zsi/NmZ09XXW0nuW1nPY5TL/kXeKgcLQROTnGztvYncM41oibcMp5Ok7qqai9ze96iNU zR+F6BHq6sggxVQo6nPVicplHwZjAtUnJCL8O/qnTwMatAO41GYlt69E0LwfznOBt4z0PyM0INf k= X-Received: by 2002:ad4:5be8:0:b0:5f3:deca:ead with SMTP id k8-20020ad45be8000000b005f3deca0eadmr19860599qvc.28.1682353424198; Mon, 24 Apr 2023 09:23:44 -0700 (PDT) X-Google-Smtp-Source: AKy350aelvnLvzMJJuMYrgFxxhTXKdDE5PPQOKZxUw32fxWPHlYQl5MAFIS2HQ1wjutX78ofJIMFGw== X-Received: by 2002:ad4:5be8:0:b0:5f3:deca:ead with SMTP id k8-20020ad45be8000000b005f3deca0eadmr19860569qvc.28.1682353423783; Mon, 24 Apr 2023 09:23:43 -0700 (PDT) Received: from localhost.localdomain (ool-457670bb.dyn.optonline.net. [69.118.112.187]) by smtp.gmail.com with ESMTPSA id i7-20020a056214030700b005eedb5cebd1sm3378186qvu.130.2023.04.24.09.23.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 24 Apr 2023 09:23:43 -0700 (PDT) From: Patrick Palka To: gcc-patches@gcc.gnu.org Cc: libstdc++@gcc.gnu.org, Patrick Palka Subject: [PATCH] libstdc++: Fix __max_diff_type::operator>>= for negative values Date: Mon, 24 Apr 2023 12:23:37 -0400 Message-ID: <20230424162337.104065-1-ppalka@redhat.com> X-Mailer: git-send-email 2.40.0.374.g7580f92ffa MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true X-Spam-Status: No, score=-14.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_H2,SPF_HELO_NONE,SPF_NONE,TXREP,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: This patch fixes sign bit propagation when right-shifting a negative __max_diff_type value by more than one, a bug which our test coverage failed to uncover until r14-159-g03cebd304955a6 fixed the front end's 'signed typedef-name' handling (which is a non-standard extension to the language grammar). Tested on x86_64-pc-linux-gnu, does this look OK for trunk/13.2/12/11? libstdc++-v3/ChangeLog: * include/bits/max_size_type.h (max_diff_type::operator>>=): Fix propagation of sign bit. * testsuite/std/ranges/iota/max_size_type.cc: Avoid using 'signed typedef-name'. Add compile-time tests for right-shifting a negative __max_diff_type value by more than one. --- libstdc++-v3/include/bits/max_size_type.h | 3 ++- .../testsuite/std/ranges/iota/max_size_type.cc | 11 +++++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/libstdc++-v3/include/bits/max_size_type.h b/libstdc++-v3/include/bits/max_size_type.h index 92b8168d02f..4796135d073 100644 --- a/libstdc++-v3/include/bits/max_size_type.h +++ b/libstdc++-v3/include/bits/max_size_type.h @@ -560,7 +560,8 @@ namespace ranges // Arithmetic right shift. const auto __msb = _M_rep._M_msb; _M_rep >>= __r._M_rep; - _M_rep._M_msb |= __msb; + if (__msb) + _M_rep |= ~(__max_size_type(-1) >> __r._M_rep); return *this; } diff --git a/libstdc++-v3/testsuite/std/ranges/iota/max_size_type.cc b/libstdc++-v3/testsuite/std/ranges/iota/max_size_type.cc index 06114c22cae..985acd5a803 100644 --- a/libstdc++-v3/testsuite/std/ranges/iota/max_size_type.cc +++ b/libstdc++-v3/testsuite/std/ranges/iota/max_size_type.cc @@ -26,6 +26,11 @@ using max_size_t = std::ranges::__detail::__max_size_type; using max_diff_t = std::ranges::__detail::__max_diff_type; using rep_t = max_size_t::__rep; +#if __SIZEOF_INT128__ +using signed_rep_t = signed __int128; +#else +using signed_rep_t = long long; +#endif static_assert(sizeof(max_size_t) == sizeof(max_diff_t)); @@ -54,6 +59,8 @@ test01() static_assert(max_diff_t(3) % -2 == 1); static_assert(max_diff_t(-3) << 1 == -6); static_assert(max_diff_t(-3) >> 1 == -2); + static_assert(max_diff_t(-3) >> 2 == -1); + static_assert(max_diff_t(-3) >> 10 == -1); static_assert(max_diff_t(3) >> 1 == 1); static_assert(max_diff_t(3) >> 2 == 0); @@ -188,7 +195,7 @@ template void test02() { - using hw_type = std::conditional_t; + using hw_type = std::conditional_t; using max_type = std::conditional_t; using shorten_type = std::conditional_t; const int hw_type_bit_size = sizeof(hw_type) * __CHAR_BIT__; @@ -246,7 +253,7 @@ template void test03() { - using hw_type = std::conditional_t; + using hw_type = std::conditional_t; using max_type = std::conditional_t; using base_type = std::conditional_t; constexpr int hw_type_bit_size = sizeof(hw_type) * __CHAR_BIT__; -- 2.40.0.374.g7580f92ffa