From patchwork Wed Jul 22 05:56:49 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Eggert X-Patchwork-Id: 7793 Received: (qmail 106754 invoked by alias); 22 Jul 2015 05:56:55 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 106743 invoked by uid 89); 22 Jul 2015 05:56:54 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.0 required=5.0 tests=AWL, BAYES_00, RP_MATCHES_RCVD, SPF_PASS autolearn=ham version=3.3.2 X-HELO: zimbra.cs.ucla.edu Message-ID: <55AF30A1.2030402@cs.ucla.edu> Date: Tue, 21 Jul 2015 22:56:49 -0700 From: Paul Eggert User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.8.0 MIME-Version: 1.0 To: Roland McGrath , sellcey@imgtec.com CC: Jeff Law , GNU C Library Subject: Re: Another GLIBC build error with GCC6 References: <1437496279.19674.126.camel@ubuntu-sellcey> <1437507995.19674.136.camel@ubuntu-sellcey> <55AEA71B.2020405@redhat.com> <1437509796.19674.138.camel@ubuntu-sellcey> <55AEB7FF.1000405@redhat.com> <1437518197.19674.152.camel@ubuntu-sellcey> <20150721224637.1E0102C3B32@topped-with-meat.com> In-Reply-To: <20150721224637.1E0102C3B32@topped-with-meat.com> Roland McGrath wrote: > the left shift doesn't even lose any bits, because the high > bit is already zero (and it's a constant, so the compiler knows that). But it does lose a bit. It loses a zero bit, which can't be recovered the way that a non-overflowing signed left shift is recovered by shifting right the same amount. > that's the whole point of the thing: to sign-extend the 31-bit value > to 32 bits. I don't think it should complain about 0x7fffffff << 1. Such complaints are useful for compilers that take advantage of C's overflow rules to generate efficient-but-weird code when you violate the rules (something that describes GCC more and more nowadays...). Anyway, if I understand this one correctly it should be easy enough to fix portably. Something like the attached patch, say. (I haven't tested it.) From 64586c5448f6f5537bd36bb5f271324790055c52 Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Tue, 21 Jul 2015 22:50:29 -0700 Subject: [PATCH] Port the 0x7efe...feff pattern to GCC 6. See Steve Ellcey's bug report in: https://sourceware.org/ml/libc-alpha/2015-07/msg00673.html * string/memrchr.c (MEMRCHR): * string/rawmemchr.c (RAWMEMCHR): * string/strchr.c (strchr): * string/strchrnul.c (STRCHRNUL): Rewrite code to avoid issues with signed shift overflow. --- ChangeLog | 11 +++++++++++ string/memrchr.c | 11 ++--------- string/rawmemchr.c | 11 ++--------- string/strchr.c | 9 ++------- string/strchrnul.c | 9 ++------- 5 files changed, 19 insertions(+), 32 deletions(-) diff --git a/ChangeLog b/ChangeLog index 652d968..9f87cd9 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,14 @@ +2015-07-21 Paul Eggert + + Port the 0x7efe...feff pattern to GCC 6. + See Steve Ellcey's bug report in: + https://sourceware.org/ml/libc-alpha/2015-07/msg00673.html + * string/memrchr.c (MEMRCHR): + * string/rawmemchr.c (RAWMEMCHR): + * string/strchr.c (strchr): + * string/strchrnul.c (STRCHRNUL): + Rewrite code to avoid issues with signed shift overflow. + 2015-07-22 Mike Frysinger * sysdeps/unix/sysv/linux/ia64/bits/msq.h: Change sys/types.h include diff --git a/string/memrchr.c b/string/memrchr.c index 0c8fd84..86cd5b9 100644 --- a/string/memrchr.c +++ b/string/memrchr.c @@ -96,15 +96,8 @@ MEMRCHR The 1-bits make sure that carries propagate to the next 0-bit. The 0-bits provide holes for carries to fall into. */ - - if (sizeof (longword) != 4 && sizeof (longword) != 8) - abort (); - -#if LONG_MAX <= LONG_MAX_32_BITS - magic_bits = 0x7efefeff; -#else - magic_bits = ((unsigned long int) 0x7efefefe << 32) | 0xfefefeff; -#endif + magic_bits = -1; + magic_bits = magic_bits / 0xff * 0xfe << 1 >> 1 | 1; /* Set up a longword, each of whose bytes is C. */ charmask = c | (c << 8); diff --git a/string/rawmemchr.c b/string/rawmemchr.c index 05b22be..228ca9d 100644 --- a/string/rawmemchr.c +++ b/string/rawmemchr.c @@ -86,15 +86,8 @@ RAWMEMCHR (s, c_in) The 1-bits make sure that carries propagate to the next 0-bit. The 0-bits provide holes for carries to fall into. */ - - if (sizeof (longword) != 4 && sizeof (longword) != 8) - abort (); - -#if LONG_MAX <= LONG_MAX_32_BITS - magic_bits = 0x7efefeff; -#else - magic_bits = ((unsigned long int) 0x7efefefe << 32) | 0xfefefeff; -#endif + magic_bits = -1; + magic_bits = magic_bits / 0xff * 0xfe << 1 >> 1 | 1; /* Set up a longword, each of whose bytes is C. */ charmask = c | (c << 8); diff --git a/string/strchr.c b/string/strchr.c index 5f90075..f13b2b3 100644 --- a/string/strchr.c +++ b/string/strchr.c @@ -60,13 +60,8 @@ strchr (const char *s, int c_in) The 1-bits make sure that carries propagate to the next 0-bit. The 0-bits provide holes for carries to fall into. */ - switch (sizeof (longword)) - { - case 4: magic_bits = 0x7efefeffL; break; - case 8: magic_bits = ((0x7efefefeL << 16) << 16) | 0xfefefeffL; break; - default: - abort (); - } + magic_bits = -1; + magic_bits = magic_bits / 0xff * 0xfe << 1 >> 1 | 1; /* Set up a longword, each of whose bytes is C. */ charmask = c | (c << 8); diff --git a/string/strchrnul.c b/string/strchrnul.c index 2678f1d..daf0b3f 100644 --- a/string/strchrnul.c +++ b/string/strchrnul.c @@ -66,13 +66,8 @@ STRCHRNUL (s, c_in) The 1-bits make sure that carries propagate to the next 0-bit. The 0-bits provide holes for carries to fall into. */ - switch (sizeof (longword)) - { - case 4: magic_bits = 0x7efefeffL; break; - case 8: magic_bits = ((0x7efefefeL << 16) << 16) | 0xfefefeffL; break; - default: - abort (); - } + magic_bits = -1; + magic_bits = magic_bits / 0xff * 0xfe << 1 >> 1 | 1; /* Set up a longword, each of whose bytes is C. */ charmask = c | (c << 8); -- 2.1.0