[comp.unix.ultrix] Warning! Compiler Bug

chris@contex.UUCP (Chris Anderson) (11/22/89)

The following program, when compiled on the DecStation 3100,
produces d=0, when it should be d=1:

    unsigned a = 2, d;
    int b = 1, c = 1;

    d = (c >= (b - a + 1));

What happens is that the whole expression is promoted to unsigned as expected,
but then the comparison is changed to ">" and the +1 operation is
"optimized" out. (Note, this happens even without -O). Voila! Bad code.

Beware of unsigned IFs with +1 in them somewhere...

Chris Anderson     (contex!chris@uunet.uu.net)
Xyvision, Inc.    

mccoy@pixar.UUCP (Daniel McCoy) (11/23/89)

In article <473@contex.UUCP> chris@contex.UUCP (Chris Anderson) writes:
>The following program, when compiled on the DecStation 3100,
>produces d=0, when it should be d=1:
>
>    unsigned a = 2, d;
>    int b = 1, c = 1;
>
>    d = (c >= (b - a + 1));
 ...
>(Note, this happens even without -O). Voila! Bad code.

It is the optimizer.
The default optimization level is -O1
The program produces d=1 when compiled with -O0 to turn off all optimization.

Dan McCoy     Pixar       {ucbvax,sun}!pixar!mccoy