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 E322C3858D35 for ; Mon, 24 Apr 2023 16:29:05 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E322C3858D35 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=1682353745; 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=q+3zbtV0337tuGDMTQrFtFY0jQz6H0VohCZR7qDAouE=; b=WEWANvrfLO14UIcvq9jzWQgy0CyEUWUASI2kV62S67KMiJThftqlmWofmR31+7yeFQv81Q 19QBJQZJ9EVoFzSjDo462OMKsUgyOcBuf9NiyIfvsb0Y3Zlz20RrHAZPecoGF9HBOHRJhy u9tkhkU/c1qvZiBuLsDK5UnXUT4zZdo= Received: from mail-qk1-f199.google.com (mail-qk1-f199.google.com [209.85.222.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-164-iVoipq3hMrOyNwcZ_sooGw-1; Mon, 24 Apr 2023 12:28:59 -0400 X-MC-Unique: iVoipq3hMrOyNwcZ_sooGw-1 Received: by mail-qk1-f199.google.com with SMTP id af79cd13be357-74d885db8c1so299913385a.0 for ; Mon, 24 Apr 2023 09:28:59 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682353738; x=1684945738; h=mime-version:references:message-id:in-reply-to:subject:cc:to:date :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=q+3zbtV0337tuGDMTQrFtFY0jQz6H0VohCZR7qDAouE=; b=Fkm+6cC8tfkuIFbK3mqRR4cW5x8z79cYtNIjSwddt/xzUuMAPaPhkGBR53UTnfsUil w7+jHNl22Ed37IzBHShWgvAB5kRj5KEYnGvFz/zOqdx0p1Z132vh4L6K1jeNZYTirNEU OL6WSb1P+0rElRpOtvOMMx2ZPwHZQu1L2/srvPHwhKUnRYrit/sSC/liAqOqa/D/s8Qh E5wzeynA5qyqzYqZwxMlijqhOqtYmMKrlOqRaqoE64GezQRde9ctx5wO5tA2hmZF4PtV +xZrzZse0EK2ZGElJrg70JMnNG1wkqtzSaPiLERu2IypgqAzbBM0sLVXNJ2dj2tkkq4q t7TQ== X-Gm-Message-State: AAQBX9cALzmpPp14AaoB6h0keUXswhKbjRwXFOgU5NmDoVEz+iXV8QAj +FHEfYOSbynWhXj5aLUKtFoFWEEwGjY0l5QSQR2PVejLnEOO4X7r4mVFljzwh1IfT4AfjO0elEs KOrH6xwK5WCpuoxlYPVExHneLwg== X-Received: by 2002:a05:6214:529e:b0:5e8:11a0:ae17 with SMTP id kj30-20020a056214529e00b005e811a0ae17mr19423426qvb.43.1682353738504; Mon, 24 Apr 2023 09:28:58 -0700 (PDT) X-Google-Smtp-Source: AKy350bJC6T3ems5dlBIBWEwm4lT/7aTN5oB/ONQvDocv2celoxqepM4myyPPUpaFCgJ7MQovsLaGQ== X-Received: by 2002:a05:6214:529e:b0:5e8:11a0:ae17 with SMTP id kj30-20020a056214529e00b005e811a0ae17mr19423404qvb.43.1682353738170; Mon, 24 Apr 2023 09:28:58 -0700 (PDT) Received: from [192.168.1.130] (ool-457670bb.dyn.optonline.net. [69.118.112.187]) by smtp.gmail.com with ESMTPSA id dp12-20020a05620a2b4c00b0074df4765056sm3676727qkb.104.2023.04.24.09.28.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 24 Apr 2023 09:28:57 -0700 (PDT) From: Patrick Palka X-Google-Original-From: Patrick Palka Date: Mon, 24 Apr 2023 12:28:57 -0400 (EDT) To: Patrick Palka cc: gcc-patches@gcc.gnu.org, libstdc++@gcc.gnu.org Subject: Re: [PATCH] libstdc++: Fix __max_diff_type::operator>>= for negative values In-Reply-To: <20230424162337.104065-1-ppalka@redhat.com> Message-ID: <3183a20d-ecb8-3ad8-50cc-91ebf14e2d38@idea> References: <20230424162337.104065-1-ppalka@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=US-ASCII X-Spam-Status: No, score=-14.2 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: On Mon, 24 Apr 2023, Patrick Palka 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? > > 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; Whoops, I suppose this 'signed' qualifier is redundant so consider it removed. > +#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 > >