From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2153) id 973023858C33; Sat, 30 Jul 2022 09:34:40 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 973023858C33 MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jakub Jelinek To: gcc-cvs@gcc.gnu.org Subject: [gcc r12-8643] wide-int: Fix up wi::shifted_mask [PR106144] X-Act-Checkin: gcc X-Git-Author: Jakub Jelinek X-Git-Refname: refs/heads/releases/gcc-12 X-Git-Oldrev: 2339ee2c89af80270840e04fc0906acea765489f X-Git-Newrev: 527dccb33e54ffe49fb1507fc4539968f48a9d12 Message-Id: <20220730093440.973023858C33@sourceware.org> Date: Sat, 30 Jul 2022 09:34:40 +0000 (GMT) X-BeenThere: gcc-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 30 Jul 2022 09:34:40 -0000 https://gcc.gnu.org/g:527dccb33e54ffe49fb1507fc4539968f48a9d12 commit r12-8643-g527dccb33e54ffe49fb1507fc4539968f48a9d12 Author: Jakub Jelinek Date: Fri Jul 1 11:17:41 2022 +0200 wide-int: Fix up wi::shifted_mask [PR106144] 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. 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. (cherry picked from commit e52592073f6df3d7a3acd9f0436dcc32a8b7493d) Diff: --- gcc/wide-int.cc | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/gcc/wide-int.cc b/gcc/wide-int.cc index f61b9fefc13..1fee0788554 100644 --- a/gcc/wide-int.cc +++ b/gcc/wide-int.cc @@ -842,6 +842,13 @@ wi::shifted_mask (HOST_WIDE_INT *val, unsigned int start, unsigned int width, 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; @@ -853,7 +860,7 @@ wi::shifted_mask (HOST_WIDE_INT *val, unsigned int start, unsigned int width, HOST_WIDE_INT block = (HOST_WIDE_INT_1U << shift) - 1; val[i++] = negate ? ~block : block; } - else if (end < prec) + else val[i++] = negate ? -1 : 0; return i; @@ -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