Optimizing a+b-a-b etc.

Robert Corbett sun!Eng!Robert.Corbett
Sun May 10 16:24:19 PDT 1992


> From validgh!uunet!idiap.ch!tmbaSun.COM Sun May 10 13:33:36 1992
> Date: Sun, 10 May 92 21:37:02 +0200
> Subject: Re:  Optimizing a+b-a-b etc.
> Cc: validgh!numeric-interestaEng
>  
> But, please don't spoil C for the "rest of us", who don't want to have
> to hand-optimize every floating point expression just because some
> esoteric uses of floating point have ursurped the standard 
> notation for floating point.
> 
> 					Thomas.

Your plea comes years too late.  The ANSI and ISO C Standards
place tight restrictions on regrouping of expressions.  A
compiler that does the optimization you describe cannot be part
of a conforming implementation.

The restrictions are spelled out in Section 5.1.2.3 of
ISO/IEC 9899-1990.  The key sentences from that section
are

    The semantic descriptions in this International
    Standard describe the behavior of an abstract
    machine in which issues of optimization are
    irrelevant.

    In the abstract machine, all expressions are
    evaluated as specified by the semantics.

and

    The least requirement on a conforming implementation
    are:

    . . .

   -At program termination, all data written into files
    shall be identical to the result that execution of
    the program according to the abstract would have
    produced.

The Examples portion adds the following explanation

    To illustrate the grouping behavior of expressions,
    in the following fragment

	int a, b;
	/* . . . */
	a = a + 32760 + b + 5;

    the expression statement behaves exactly the same as

	a = (((a + 32760) + b) + 5);

    due to the associativity and precedence of these operators.
    Thus, the result of the sum "(a + 32760)" is next added to
    b, and that result is then added to 5 which results in the
    value assigned to a.  On a machine in which overflows
    produce an exception and in which the range of values
    representable by an int is [-32768, +32767], the
    implementation cannot rewrite this expression as

	a = ((a + b) + 32765);

    since if the values for a and b were, respectively, -32754
    and -15, the sum a + b would produce an exception while the
    original expression would not; nor can the expression be
    rewritten either as

	a = ((a + 32765) + b);

    or

	a = (a + (b + 32765));

    since if the values for a and b might have been, respectively,
    4 and 8 or -17 and 12.  However on a machine in which the
    results of overflows are reversible, the above expression
    statement can be rewritten by the implementation in any of
    the above ways because the same result will occur.

The rules that apply to expressions also apply to floating-point
expressions.  Thus, a conforming implementation cannot regroup a
floating-point expression in a way that changes data written to
a file.

Note that the rules for Standard C are even more restrictive than
those for Standard FORTRAN.  FORTRAN allows expressions, including
floating-point expressions, to be regrouped provided parentheses
are respected.

					Yours truly,
					Bob Corbett



More information about the Numeric-interest mailing list