Parallel Programming Guide for HP-UX Systems

Troubleshooting
Compiler assumptions
Chapter 9202
The Fortran code below contains an example of a loop in which the
induction variable may be replaced by the compiler:
ICONST = 64
ITOT = 0
DO IND = 1,N
IPACK = (IND*1024)*ICONST**2
IF(IPACK .LE. (N/2)*1024*ICONST**2)
> ITOT = ITOT + IPACK
.
.
.
ENDDO
END
Executing this loop using IND as the induction variable with a stride of 1
would be extremely inefficient. Therefore, the compiler picks IPACK as
the induction variable and uses the amount by which it increases on each
iteration, 1024*64
2
or 2
22
, as the stride.
The trip count (N in the example), or just trip, is the number of times the
loop executes, and the start value is the initial value of the induction
variable.
Linear test replacement, a standard optimization at levels +O2 and
above, normally does not cause problems. However, when the loop stride
is very large a large trip count can cause the loop limit value
(start+((trip-1)*stride)) to overflow.
In the code above, the induction variable is a 4-byte integer, which
occupies 32 bits in memory. That means if start+((trip-1)*stride)
(1+((N-1)*2
22
)) is greater than 2
31
-1, the value overflows into the sign bit
and is treated as a negative number. If the stride value is negative, the
absolute value of start+((trip-1)*stride) must be not exceed 2
31
. When a
loop has a positive stride and the trip count overflows, the loop stops
executing when the overflow occurs because the limit becomes
negative—assuming a positive stride—and the termination test fails.
Because the largest allowable value for start+((trip-1)*stride) is 2
31
-1,
the start value is 1, and the stride is 2
22
, the maximum trip count for the
loop is found.
The stride, trip, and start values for a loop must satisfy the following
inequality: