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.133.124]) by sourceware.org (Postfix) with ESMTPS id E17213858412 for ; Mon, 24 Apr 2023 16:40:10 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E17213858412 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=1682354410; 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: in-reply-to:in-reply-to:references:references; bh=OasFn9bzQQGf7+C9jgvAKakjGmvWLVTPaG56AkdxcKA=; b=EidQgXYYzvubxbxGl0XF9Oy+omYFPhfMNm0xg80mPQ36+NcLqxrTjGH80kgBkhCYX/+KdH 6tKLbL2q8U/nqmdNJ0TFB7u5oAku7ZJRNQ0Rt7lJSHLh+dzefaS36N2ZmHtdOq8Gbzub+e e80WeYT1MQN9wxrIihfs/SaCUMLxUKA= Received: from mail-lj1-f197.google.com (mail-lj1-f197.google.com [209.85.208.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-558-frZtPp1wOKGvxqiTZK-WEQ-1; Mon, 24 Apr 2023 12:40:09 -0400 X-MC-Unique: frZtPp1wOKGvxqiTZK-WEQ-1 Received: by mail-lj1-f197.google.com with SMTP id 38308e7fff4ca-2a8b52c112fso16425751fa.0 for ; Mon, 24 Apr 2023 09:40:08 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682354407; x=1684946407; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=OasFn9bzQQGf7+C9jgvAKakjGmvWLVTPaG56AkdxcKA=; b=TgWafMdZX2ojY0Gkb+CSNjYcNNG2umv/VemoHixlIK4LQxzXxcvoyUjybmaYc0/1Y6 HJ7iflsqQWe5nYaHmKfEqm/DuTV6gkck8ZOgqr5H44vjcO1vHqshSLlLG+NJvER6RpKl OSD4M6i7D1ciju1Rg2lO6+2Zg1JszptPHAye00frZVoi83lOocfYwmAq19W2MSazXW+7 /WAavz2ximK2oT+vq2bhtg5cMFMHminiaGl9CiE5ZbnYuKrhTiH2cqbtuQ8dA23UvfJQ 9LCAXQ9OAjWFlIq7SdJowYIHA/xfHsr3tfLCLRp8fQKFfHhB0+S5Iar87fg8TqC+v8iw VxaQ== X-Gm-Message-State: AAQBX9dyumJUP8pvidQj3sYkKMcG4jecgmj9FcawpbsngEt19MkSYwjz AwBZLKrOQ49S+UglftKBf+yNrnTbgr6c5nL3oESiA05g7TRpSBIwbh0YlFC+p/9/tOiDta7KvHP qx0TW5l4Mm5F7UZUMDzInAYczEibLXhOFJg== X-Received: by 2002:a2e:920b:0:b0:2a8:d08e:b7dc with SMTP id k11-20020a2e920b000000b002a8d08eb7dcmr2590088ljg.28.1682354407713; Mon, 24 Apr 2023 09:40:07 -0700 (PDT) X-Google-Smtp-Source: AKy350bI9+zd7JasWubEyoffgk8n2PEs8ilfv4L0H4W7UxetMedU33FO+R8S4g8qbZ07bsvRP04SjilVM+LIHeoGPG0= X-Received: by 2002:a2e:920b:0:b0:2a8:d08e:b7dc with SMTP id k11-20020a2e920b000000b002a8d08eb7dcmr2590082ljg.28.1682354407429; Mon, 24 Apr 2023 09:40:07 -0700 (PDT) MIME-Version: 1.0 References: <20230424162337.104065-1-ppalka@redhat.com> In-Reply-To: <20230424162337.104065-1-ppalka@redhat.com> From: Jonathan Wakely Date: Mon, 24 Apr 2023 17:39:56 +0100 Message-ID: Subject: Re: [PATCH] libstdc++: Fix __max_diff_type::operator>>= for negative values To: Patrick Palka Cc: gcc-patches@gcc.gnu.org, libstdc++@gcc.gnu.org X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-12.3 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=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On Mon, 24 Apr 2023 at 17:24, Patrick Palka via Libstdc++ wrote: > > 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? Yes, OK for all (but gcc-13 only after 13.1 is released of course). > > 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 >