smart compilers

Charley Wingate mangoe at umcp-cs.UUCP
Sat Dec 22 08:42:05 AEST 1984


In article <18397 at lanl.ARPA> jlg at lanl.ARPA writes:

>> However, the IBM H Level FORTRAN Programmer's Guide does warn that code
>> such as
>> 
>> 	DO 100 I = 1 TO 10
>> 	IF (Y .GT. 0) X(I) = SQRT(Y)
>>     100 CONTINUE
>> 
>> (with X appropriately declared as an array of course) can cause an error
>> because it tries to take the square root of a negative number.  The
>> optimizer compiles this as if it were
>> 
>> 	TEMP = SQRT(Y)
>> 	DO 100 I = 1 TO 10
>> 	IF (Y .GT. 0) X(I) = TEMP
>>     100 CONTINUE

>This particular example should be optimized as
>
>      IF ( Y.GT.0 ) TEMP=SQRT(Y)
>      DO 100 I = 1,10         !THERE ARE NO 'TO' DELIMITERS IN FORTRAN
>         X(I)=TEMP            !FOLLOW INDENTATION STANDARDS PLEASE
>100   CONTINUE

Wrong again.  A correct optimization would be

      If (Y.GT.0) Temp=Sqrt(Y)
      Do 100 I=1,10
         If (Y.GT.0) X(I)=Temp
  100 Continue

If Y is negative, X should be unchanged, according to the original program.
My feeling is that an optimizer should produce the same "action" on some
well-defined level as the original unoptimized code.  Well-defined in this
case should be equivalent to well-documented.

Charley Wingate   umcp-cs!mangoe



More information about the Comp.unix.wizards mailing list