From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by sourceware.org (Postfix) with ESMTP id A8E283858D39 for ; Fri, 1 Jul 2022 09:11:12 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org A8E283858D39 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 6A7D01063; Fri, 1 Jul 2022 02:11:12 -0700 (PDT) Received: from localhost (e121540-lin.manchester.arm.com [10.32.98.37]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 8A2793F66F; Fri, 1 Jul 2022 02:11:11 -0700 (PDT) From: Richard Sandiford To: Jakub Jelinek Mail-Followup-To: Jakub Jelinek , Richard Biener , gcc-patches@gcc.gnu.org, richard.sandiford@arm.com Cc: Richard Biener , gcc-patches@gcc.gnu.org Subject: Re: [PATCH] wide-int: Fix up wi::shifted_mask [PR106144] References: Date: Fri, 01 Jul 2022 10:11:02 +0100 In-Reply-To: (Jakub Jelinek's message of "Fri, 1 Jul 2022 09:24:41 +0200") Message-ID: User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.3 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain X-Spam-Status: No, score=-50.1 required=5.0 tests=BAYES_00, KAM_DMARC_NONE, KAM_DMARC_STATUS, KAM_LAZY_DOMAIN_SECURITY, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=no 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-List-Received-Date: Fri, 01 Jul 2022 09:11:14 -0000 Jakub Jelinek writes: > Hi! > > As the following self-test testcase shows, wi::shifted_mask sometimes > doesn't create canonicalized wide_ints, which then fail to compare equal > to canonicalized wide_ints with the same value. > In particular, wi::mask (128, false, 128) gives { -1 } with len 1 and prec 128, > while wi::shifted_mask (0, 128, false, 128) gives { -1, -1 } with len 2 > and prec 128. > The problem is that the code is written with the assumption that there are > 3 bit blocks (or 2 if start is 0), but doesn't consider the possibility > where there are 2 bit blocks (or 1 if start is 0) where the highest block > isn't present. In that case, there is the optional block of negate ? 0 : -1 > elts, followed by just one elt (either one from the if (shift) or just > negate ? -1 : 0) and the rest is implicit sign-extension. > Only if end < prec there is 1 or more bits above it that have different bit > value and so we need to emit all the elts till end and then one more elt. > > if (end == prec) would work too, because we have: > if (width > prec - start) > width = prec - start; > unsigned int end = start + width; > so end is guaranteed to be end <= prec, dunno what is preferred. > > Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? > > 2022-07-01 Jakub Jelinek > > PR middle-end/106144 > * wide-int.cc (wi::shifted_mask): If end >= prec, return right after > emitting element for shift or if shift is 0 first element after start. > (wide_int_cc_tests): Add tests for equivalency of wi::mask and > wi::shifted_mask with 0 start. OK, thanks, but could you also remove the "end < prec" condition from: else if (end < prec) val[i++] = negate ? -1 : 0; Richard > --- gcc/wide-int.cc.jj 2022-01-11 23:11:23.592273263 +0100 > +++ gcc/wide-int.cc 2022-06-30 20:41:25.506292687 +0200 > @@ -842,6 +842,13 @@ wi::shifted_mask (HOST_WIDE_INT *val, un > val[i++] = negate ? block : ~block; > } > > + if (end >= prec) > + { > + if (!shift) > + val[i++] = negate ? 0 : -1; > + return i; > + } > + > while (i < end / HOST_BITS_PER_WIDE_INT) > /* 1111111 */ > val[i++] = negate ? 0 : -1; > @@ -2583,6 +2590,10 @@ wide_int_cc_tests () > run_all_wide_int_tests (); > test_overflow (); > test_round_for_mask (); > + ASSERT_EQ (wi::mask (128, false, 128), > + wi::shifted_mask (0, 128, false, 128)); > + ASSERT_EQ (wi::mask (128, true, 128), > + wi::shifted_mask (0, 128, true, 128)); > } > > } // namespace selftest > > Jakub