public inbox for fortran@gcc.gnu.org
 help / color / mirror / Atom feed
From: Adam Hirst <ahirst@cedint.upm.es>
To: fortran@gcc.gnu.org
Subject: Re: Fortran loops
Date: Mon, 27 Jun 2016 19:43:00 -0000	[thread overview]
Message-ID: <577181D9.1070807@cedint.upm.es> (raw)
In-Reply-To: <6662B213-476B-4624-808D-2AC0DB33E152@lps.ens.fr>

I don't know how the compiler handles this exactly, but my "educated
guess" would be that it depends on whether the loop is unrolled and on
whether the incrementing of the loop index variable occurs before or
after the check at the "start" of each loop iteration.

If "at" (i.e. just before, while still inside) the end of the "last"
iteration the value is huge(x), and it then decides to add one (causing
it to overflow to a -ve number), and THEN compare to huge(x) to see
whether the loop is over, it ought then go on forever infinitely
repeating the entire span of the integer type.

But I'm not an "informed" person. :)

~ Adam

On 27/06/16 21:36, Dominique d'Humières wrote:
> IIRC this has already been discussed in the past (gfortran.dg/do_1.f90). 
> The Fortran standard requires that the variable i is equal to array(2)+1 on the loop exit, which causes an overflow if array(2) is equal to huge(i). 
> So the code is invalid when b(2) = huge(x), but I don’t see why the overflow of the exit value should lead to an infinite loop.
> Dominique
>> On Jun 27, 2016, at 6:56 PM, Jan Hubicka <hubicka@ucw.cz> wrote:
>>
>> Hello,
>> I have a Fortran question.  The following program loops infinitly when compiled
>> with ifort -O0, while it does the expected number of iterations with gfortran.
>> The issue is that we do produce quite ineffective code to deal with
>> side cases like this.
>>
>> is the program bellow valid and expected to terminate?  If so I guess we want
>> to have it as a testcase.
>>
>> Thanks,
>> Honza
>>
>> program test_program
>> integer(4) :: b(2), a(22), x
>> integer(8) :: suma
>>
>> b(1) = huge(x)-10
>> b(2) = huge(x)
>>
>> call test2(b, suma)
>> print *,suma
>>
>> end program test_program
>> function test2(array, s)
>>  integer(4) :: i, block(9), array(2)
>>  integer (8) :: s
>>  s = 1
>>
>>  do i = array(1), array(2)
>>      s = s + 1
>>  end do
>>
>> end function test2
>>
> 

-- 
Using GPG? Add my key, and respond to me with it enabled!
Also, feel free to verify my key with me online or in person!

Not using GPG? You should, it's easy!
https://www.enigmail.net/documentation/quickstart.php

  reply	other threads:[~2016-06-27 19:43 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-06-27 19:37 Dominique d'Humières
2016-06-27 19:43 ` Adam Hirst [this message]
2016-06-27 20:05 ` Dominique d'Humières
2016-06-27 20:22 ` FX
2016-06-27 21:25   ` William Clodius
2016-06-28 12:10 ` Jan Hubicka
2016-06-28 13:50   ` Richard Biener
2016-06-28 13:56     ` Jan Hubicka
2016-06-30 11:17       ` [PATCH, RFC] Introduce -ffast-do-loop flag Martin Liška
2016-06-30 11:43         ` Richard Biener
2016-06-30 15:10           ` Martin Liška
2016-06-30 19:10         ` FX
2016-06-30 20:23           ` Mikael Morin
2016-07-01 12:30             ` Martin Liška
  -- strict thread matches above, loose matches on Subject: below --
2016-06-27 16:56 Fortran loops Jan Hubicka
2016-06-27 17:45 ` William Clodius
2016-06-27 17:55   ` Adam Hirst
2016-06-27 21:14     ` William Clodius
2016-06-28  7:36 ` Anton Shterenlikht

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=577181D9.1070807@cedint.upm.es \
    --to=ahirst@cedint.upm.es \
    --cc=fortran@gcc.gnu.org \
    /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).