public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
From: David Brown <david.brown@hesbynett.no>
To: Aldy Hernandez <aldyh@redhat.com>,
	Richard Biener <richard.guenther@gmail.com>
Cc: GCC Mailing List <gcc@gcc.gnu.org>
Subject: Re: Can gcc.dg/torture/pr67828.c be an infinite loop?
Date: Fri, 24 Sep 2021 13:30:48 +0200	[thread overview]
Message-ID: <7e81cf0f-e173-cd54-6da7-f18bbe0833cd@hesbynett.no> (raw)
In-Reply-To: <3d8e40f3-20e8-f265-85d8-d667aa65052a@redhat.com>

On 24/09/2021 10:59, Aldy Hernandez via Gcc wrote:
> 
> 
> On 9/24/21 10:08 AM, Richard Biener wrote:
>> On Fri, Sep 24, 2021 at 10:04 AM Aldy Hernandez via Gcc
>> <gcc@gcc.gnu.org> wrote:
>>>
>>> Is this correct, or did I miss something?
>>
>> Yes, 'c' will wrap to negative SHORT_MIN and terminate the loop via
>> the c>=0 test.
> 
> Huh, so SHORT_MAX + 1 = SHORT_MIN?  I thought that was an overflow, and
> therefore undefined.
> 

C and C++ don't do arithmetic on "short" (or "char").  They are
immediately promoted to "int" (or "unsigned int", as appropriate).  So
if short is smaller than int, the code behaviour is well defined (as
Richard described below).  If short is the same size as int (such as on
the 16-bit mspgcc port of gcc), however, then SHORT_MAX + 1 /would/ be
an overflow and the compiler can assume it does not happen - thus giving
you an infinite loop.

With more common 32-bit int and 16-bit short, the loop should execute
32768 times.

(At least, that is my understanding.)

> 
>>
>> Mind c++ is really (short)(((int)c)++) and signed integer truncation
>> is implementation
>> defined.
>>
>> Richard.
>>
>>> Aldy
>>>
>>
> 
> 


  reply	other threads:[~2021-09-24 11:30 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-24  8:03 Aldy Hernandez
2021-09-24  8:08 ` Richard Biener
2021-09-24  8:59   ` Aldy Hernandez
2021-09-24 11:30     ` David Brown [this message]
2021-09-24  9:29 ` Andrew Pinski
2021-09-24  9:35   ` Aldy Hernandez
2021-09-24  9:38     ` Andrew Pinski
2021-09-24 10:49       ` Aldy Hernandez
2021-09-24 11:33       ` David Brown
2021-09-24 11:08   ` Richard Earnshaw
2021-09-24 11:37 ` David Brown
2021-09-24 11:41   ` Aldy Hernandez

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=7e81cf0f-e173-cd54-6da7-f18bbe0833cd@hesbynett.no \
    --to=david.brown@hesbynett.no \
    --cc=aldyh@redhat.com \
    --cc=gcc@gcc.gnu.org \
    --cc=richard.guenther@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).