This is the mail archive of the
fortran@gcc.gnu.org
mailing list for the GNU Fortran project.
Re: Fortran loops
- From: William Clodius <williamclodius at gmail dot com>
- To: fortran at gcc dot gnu dot org
- Date: Mon, 27 Jun 2016 23:23:55 +0200
- Subject: Re: Fortran loops
- Authentication-results: sourceware.org; auth=none
- References: <6662B213-476B-4624-808D-2AC0DB33E152 at lps dot ens dot fr> <53F372D1-D7CD-4E96-BC09-F90650BC4826 at gmail dot com>
I am slightly surprised you special case to avoid calculating the iteration count for increments of +/-1. The iteration count is easiest to determine for these two increments.
> On Jun 27, 2016, at 10:22 PM, FX <fxcoudert@gmail.com> wrote:
>
>> 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.
>
> In general, DO loops in Fortran are more messy than in C, because they are not defined in terms of a condition but instead the standard clearly calls for the calculation of an iteration count which is then decremented.
>
> Dominique is right that the code is invalid, because on the last iteration of the DO execution cycle, the value of I is is incremented from HUGE(I) and thus overflowing. However, as a quality of implementation, I think it would be nice for this code which has clear meaning not to give rise to undefined behavior, because the value of I is not used.
>
> Generally in the Fortran front-end we implement DO loops with an iteration count, as the standard specifies them. However, the case of integer DO loops with Â1 increments is handled as a special case in gfc_trans_simple_do() [trans-stmt.c], without an actual count.
>
> FX