Message ID | Yr6hObntpK6Fo9bj@tucnak |
---|---|
State | New |
Headers |
Return-Path: <gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org> X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id CEDE53858D39 for <patchwork@sourceware.org>; Fri, 1 Jul 2022 07:25:18 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org CEDE53858D39 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1656660318; bh=kaRzeJhW74ntGnnm2IxJVKfy9QniMr0ZzpfIgX2JGwI=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=r+e/jQov7YVNKQiv5pDkm83+nFBrJRFWYMzVCsuqJ0Tmkzi8sx9aqIIF4gd8p4ZlM kNsyxjfbjIMD2WUw+21ehDqkdAG2kcRw1CFYykZ3WYyEcese6hddgPSlbc7V+MHDVe GgMR31ad3sb3tq4kgfTu0O7h7nB9K8ALIrSI7l/4= 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 B47C73858D39 for <gcc-patches@gcc.gnu.org>; Fri, 1 Jul 2022 07:24:49 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org B47C73858D39 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-21-LbOg7afAM5WzsxB8RkOFkA-1; Fri, 01 Jul 2022 03:24:46 -0400 X-MC-Unique: LbOg7afAM5WzsxB8RkOFkA-1 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 01328185A79C; Fri, 1 Jul 2022 07:24:46 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.192.30]) by smtp.corp.redhat.com (Postfix) with ESMTPS id B4D4D416154; Fri, 1 Jul 2022 07:24:45 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.17.1/8.17.1) with ESMTPS id 2617Ogtn1648723 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Fri, 1 Jul 2022 09:24:43 +0200 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 2617OfCV1648722; Fri, 1 Jul 2022 09:24:41 +0200 Date: Fri, 1 Jul 2022 09:24:41 +0200 To: Richard Sandiford <richard.sandiford@arm.com>, Richard Biener <rguenther@suse.de> Subject: [PATCH] wide-int: Fix up wi::shifted_mask [PR106144] Message-ID: <Yr6hObntpK6Fo9bj@tucnak> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.85 on 10.11.54.10 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-Spam-Status: No, score=-4.1 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_LOW, 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.29 Precedence: list List-Id: Gcc-patches mailing list <gcc-patches.gcc.gnu.org> List-Unsubscribe: <https://gcc.gnu.org/mailman/options/gcc-patches>, <mailto:gcc-patches-request@gcc.gnu.org?subject=unsubscribe> List-Archive: <https://gcc.gnu.org/pipermail/gcc-patches/> List-Post: <mailto:gcc-patches@gcc.gnu.org> List-Help: <mailto:gcc-patches-request@gcc.gnu.org?subject=help> List-Subscribe: <https://gcc.gnu.org/mailman/listinfo/gcc-patches>, <mailto:gcc-patches-request@gcc.gnu.org?subject=subscribe> From: Jakub Jelinek via Gcc-patches <gcc-patches@gcc.gnu.org> Reply-To: Jakub Jelinek <jakub@redhat.com> Cc: gcc-patches@gcc.gnu.org Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" <gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org> |
Series |
wide-int: Fix up wi::shifted_mask [PR106144]
|
|
Commit Message
Jakub Jelinek
July 1, 2022, 7:24 a.m. UTC
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 <jakub@redhat.com> 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. Jakub
Comments
Jakub Jelinek <jakub@redhat.com> 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 <jakub@redhat.com> > > 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 <widest_int> (); > 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
--- 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 <widest_int> (); 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