cc optimizer bug (arithmetic shifts).

Arndt Jonasson arndt at zyx.ZYX.SE
Fri Feb 8 00:23:35 AEST 1991


The version of SunOS is 4.1.

-------------------- start of script --------------------
% cat mc.c
#include <stdio.h>

main()
{
   int i;

   for (i=0; i<8; i++)
      test(i<<29);
}

test(j)
int j;
{
   printf("%d\n", j);
}
% cc -o mc mc.c
% mc
0
536870912
1073741824
1610612736
-2147483648
-1610612736
-1073741824
-536870912
% cc -O -o mc mc.c
% mc
0
% uname -srm
SunOS 4.1 sun4c
% 
-------------------- end of script --------------------

What seems to happen is that the optimizer sees a chance to avoid
shifting 'i' all the time, and therefore keeps 'i' shifted left 29
steps, modifying the index arithmetic accordingly. It's basically a
good idea, but it only works if the maximum shifted value of 'i'
doesn't overflow, which it does here.
-- 
Arndt Jonasson, ZYX AB, Styrmansgatan 6, 114 54 Stockholm, Sweden
email address:   arndt at zyx.SE   or      <backbone>!mcsun!sunic!zyx!arndt



More information about the Alt.sys.sun mailing list