[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Bug#429657: gcc-4.1: optimisation bug abs() in nested if



Package: gcc-4.1
Version: 4.1.1-21
Severity: normal


abuse-sdl-0.7.0 has a gun aiming problem due to a mis-optimisation.
attached is a canned example. 

(compile with -01 and -02 and compare the differences)


-- System Information:
Debian Release: 4.0
  APT prefers stable
  APT policy: (990, 'stable'), (500, 'testing')
Architecture: i386 (i686)
Shell:  /bin/sh linked to /bin/bash
Kernel: Linux 2.6.18-4-686
Locale: LANG=en_NZ, LC_CTYPE=en_NZ (charmap=ISO-8859-1)

Versions of packages gcc-4.1 depends on:
ii  binutils               2.17cvs20070426-8 The GNU assembler, linker and bina
ii  cpp-4.1                4.1.1-21          The GNU C preprocessor
ii  gcc-4.1-base           4.1.1-21          The GNU Compiler Collection (base 
ii  libc6                  2.5-9+b1          GNU C Library: Shared libraries
ii  libgcc1                1:4.1.1-21        GCC support library
ii  libssp0                4.1.1-21          GCC stack smashing protection libr

Versions of packages gcc-4.1 recommends:
ii  libc6-dev                     2.5-9+b1   GNU C Library: Development Librari
pn  libmudflap0-dev               <none>     (no description available)

-- no debconf information
// example of GCC 4.1 optimisation bug 
// compile with -01 and -02 and compare the differences

// licence is  GPL2 - based on code taken from abuse-sdl

// gcc -o bad  example.c -O2 -lm
// gcc -o good example.c -O1 -lm


#include <stdio.h>
#include <math.h>

unsigned short atan_table[1662]={
    0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0, 
    0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0, 
    0,   0,   0,   0,   0,   0,   0,   1,   2,   3,   4,   5, 
    6,   6,   7,   8,   9,   9,  10,  11,  11,  12,  12,  13, 
   13,  14,  14,  15,  15,  16,  16,  17,  17,  18,  18,  18, 
   19,  19,  19,  20,  20,  20,  21,  21,  21,  22,  22,  22, 
   23,  23,  23,  23,  24,  24,  24,  24,  25,  25,  25,  25, 
   25,  26,  26,  26,  26,  26,  27,  27,  27,  27,  27,  28, 
   28,  28,  28,  28,  28,  28,  29,  29,  29,  29,  29,  29, 
   29,  30,  30,  30,  30,  30,  30,  30,  30,  31,  31,  31, 
   31,  31,  31,  31,  31,  31,  32,  32,  32,  32,  32,  32, 
   32,  32,  32,  32,  32,  33,  33,  33,  33,  33,  33,  33, 
   33,  33,  33,  33,  33,  33,  34,  34,  34,  34,  34,  34, 
   34,  34,  34,  34,  34,  34,  34,  34,  34,  35,  35,  35, 
   35,  35,  35,  35,  35,  35,  35,  35,  35,  35,  35,  35, 
   35,  35,  35,  35,  36,  36,  36,  36,  36,  36,  36,  36, 
   36,  36,  36,  36,  36,  36,  36,  36,  36,  36,  36,  36, 
   36,  36,  36,  37,  37,  37,  37,  37,  37,  37,  37,  37, 
   37,  37,  37,  37,  37,  37,  37,  37,  37,  37,  37,  37, 
   37,  37,  37,  37,  37,  37,  37,  37,  37,  38,  38,  38, 
   38,  38,  38,  38,  38,  38,  38,  38,  38,  38,  38,  38, 
   38,  38,  38,  38,  38,  38,  38,  38,  38,  38,  38,  38, 
   38,  38,  38,  38,  38,  38,  38,  38,  38,  38,  38,  38, 
   39,  39,  39,  39,  39,  39,  39,  39,  39,  39,  39,  39, 
   39,  39,  39,  39,  39,  39,  39,  39,  39,  39,  39,  39, 
   39,  39,  39,  39,  39,  39,  39,  39,  39,  39,  39,  39, 
   39,  39,  39,  39,  39,  39,  39,  39,  39,  39,  39,  39, 
   39,  39,  39,  39,  39,  39,  39,  39,  40,  40,  40,  40, 
   40,  40,  40,  40,  40,  40,  40,  40,  40,  40,  40,  40, 
   40,  40,  40,  40,  40,  40,  40,  40,  40,  40,  40,  40, 
   40,  40,  40,  40,  40,  40,  40,  40,  40,  40,  40,  40, 
   40,  40,  40,  40,  40,  40,  40,  40,  40,  40,  40,  40, 
   40,  40,  40,  40,  40,  40,  40,  40,  40,  40,  40,  40, 
   40,  40,  40,  40,  40,  40,  40,  40,  40,  40,  40,  40, 
   40,  40,  40,  40,  40,  40,  40,  41,  41,  41,  41,  41, 
   41,  41,  41,  41,  41,  41,  41,  41,  41,  41,  41,  41, 
   41,  41,  41,  41,  41,  41,  41,  41,  41,  41,  41,  41, 
   41,  41,  41,  41,  41,  41,  41,  41,  41,  41,  41,  41, 
   41,  41,  41,  41,  41,  41,  41,  41,  41,  41,  41,  41, 
   41,  41,  41,  41,  41,  41,  41,  41,  41,  41,  41,  41, 
   41,  41,  41,  41,  41,  41,  41,  41,  41,  41,  41,  41, 
   41,  41,  41,  41,  41,  41,  41,  41,  41,  41,  41,  41, 
   41,  41,  41,  41,  41,  41,  41,  41,  41,  41,  41,  41, 
   41,  41,  41,  41,  41,  41,  41,  41,  41,  41,  41,  41, 
   41,  41,  41,  41,  41,  41,  41,  41,  41,  41,  41,  41, 
   41,  41,  41,  41,  41,  41,  41,  41,  41,  41,  41,  41, 
   41,  41,  42,  42,  42,  42,  42,  42,  42,  42,  42,  42, 
   42,  42,  42,  42,  42,  42,  42,  42,  42,  42,  42,  42, 
   42,  42,  42,  42,  42,  42,  42,  42,  42,  42,  42,  42, 
   42,  42,  42,  42,  42,  42,  42,  42,  42,  42,  42,  42, 
   42,  42,  42,  42,  42,  42,  42,  42,  42,  42,  42,  42, 
   42,  42,  42,  42,  42,  42,  42,  42,  42,  42,  42,  42, 
   42,  42,  42,  42,  42,  42,  42,  42,  42,  42,  42,  42, 
   42,  42,  42,  42,  42,  42,  42,  42,  42,  42,  42,  42, 
   42,  42,  42,  42,  42,  42,  42,  42,  42,  42,  42,  42, 
   42,  42,  42,  42,  42,  42,  42,  42,  42,  42,  42,  42, 
   42,  42,  42,  42,  42,  42,  42,  42,  42,  42,  42,  42, 
   42,  42,  42,  42,  42,  42,  42,  42,  42,  42,  42,  42, 
   42,  42,  42,  42,  42,  42,  42,  42,  42,  42,  42,  42, 
   42,  42,  42,  42,  42,  42,  42,  42,  42,  42,  42,  42, 
   42,  42,  42,  42,  42,  42,  42,  42,  42,  42,  42,  42, 
   42,  42,  42,  42,  42,  42,  42,  42,  42,  42,  42,  42, 
   42,  42,  42,  42,  42,  42,  42,  42,  42,  42,  42,  42, 
   42,  42,  42,  42,  42,  42,  42,  42,  42,  42,  42,  42, 
   42,  42,  42,  42,  42,  42,  42,  42,  42,  42,  42,  42, 
   42,  42,  42,  42,  42,  42,  42,  42,  42,  42,  42,  42, 
   42,  42,  42,  42,  42,  42,  42,  42,  42,  42,  42,  42, 
   42,  42,  42,  42,  42,  42,  42,  42,  42,  42,  42,  42, 
   42,  42,  42,  42,  42,  42,  42,  42,  42,  42,  42,  42, 
   42,  42,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43, 
   43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43, 
   43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43, 
   43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43, 
   43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43, 
   43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43, 
   43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43, 
   43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43, 
   43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43, 
   43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43, 
   43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43, 
   43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43, 
   43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43, 
   43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43, 
   43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43, 
   43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43, 
   43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43, 
   43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43, 
   43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43, 
   43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43, 
   43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43, 
   43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43, 
   43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43, 
   43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43, 
   43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43, 
   43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43, 
   43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43, 
   43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43, 
   43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43, 
   43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43, 
   43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43, 
   43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43, 
   43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43, 
   43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43, 
   43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43, 
   43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43, 
   43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43, 
   43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43, 
   43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43, 
   43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43, 
   43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43, 
   43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43, 
   43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43, 
   43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43, 
   43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43, 
   43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43, 
   43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43, 
   43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43, 
   43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43, 
   43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43, 
   43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43, 
   43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43, 
   43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43, 
   43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43, 
   43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43, 
   43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43, 
   43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43, 
   43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43, 
   43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43, 
   43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43, 
   43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43, 
   43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43, 
   43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43, 
   43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43, 
   43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43, 
   43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43, 
   43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43, 
   43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43, 
   43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43,  43, 
   43,  43,  43,  44,  44,  44};

#define TBS (1662)
long lisp_atan2(long dy, long dx)
{
  if (dy==0)
  {
    if (dx>0) return 0;
    else return 180;
  } else if (dx==0)
  {
    if (dy>0) return 90;
    else return 270;
  } else
  {
    if (dx>0)
    {      
      if (dy>0)
      {
	if (abs(dx)>abs(dy))
	{
	  printf("case1:");	   
	  long a=dx*29/dy;
	  if (a>=TBS) return 0;
	  else return 45-atan_table[a];
	}
	else 
	{
	  printf("case2:");
	  long a=dy*29/dx;
	  if (a>=TBS) return 90;
	  else return 45+atan_table[a];
	}
      } else
      {
	if (abs(dx)>abs(dy))
	{
	  printf("case3:");
	  long a=dx*29/abs(dy);
	  if (a>=TBS)
	    return 0;
	  else
	    return 315+atan_table[a];
	}
	else
	{
	  printf("case4:");
	  long a=abs(dy)*29/dx;
	  if (a>=TBS)
	    return 260;
	  else
	    return 315-atan_table[a];
	}
      } 
    } else
    {
      if (dy>0)
      {
	if (abs(dx)>abs(dy))
	{
	  printf("case5:");
	  long a=-dx*29/dy;
	  if (a>=TBS)
	    return 135+45;
	  else
	    return 135+atan_table[a];
	}
	else 
	{
	  printf("case6:");
	  long a=dy*29/-dx;
	  if (a>=TBS)
	    return 135-45;
	  else
	    return 135-atan_table[a];
	}
      } else
      {
	if (abs(dx)>abs(dy))
	{
	  printf("case7:");
	  long a=-dx*29/abs(dy);
	  if (a>=TBS)
	    return 225-45;
	  else return 225-atan_table[a];
	}
	else 
	{
	  printf("case8:");
	  long a=abs(dy)*29/abs(dx);
	  if (a>=TBS)
	    return 225+45;	  
	  else return 225+atan_table[a];
	}
      } 
    }
  }  
}


int main()
{
   double angle;

   for( angle=M_PI/32; angle < M_PI * 2 ; angle+=M_PI/16)
     {
	double s=sin(angle) , c=cos(angle);
	
        long theta = lisp_atan2(s*100,c*100);
	
	printf ( " a=%4.0f theta=%4d %s\n",angle/M_PI * 180,theta, 
		 floor(0.5+angle/M_PI * 180 -theta)  ? "ERROR" : "ok");
     }
  return 0 ;
}

Reply to: