Hello,

Tested with two compilers:
1. powerpc-7400-linux-gnu-g++ version 3.3.4
2. powerpc-linux-g++ version 3.3.2

the following code gives correct results when compiled with no optimization:
"Add siy: -3252329895"

Compiling with any of the optimization levels gives bad results:
"Add siy: 39697343065"

it looks like a sign-extension problem.
turning on the #define works-around the problem in a mysterious way.

Regrds,
Eran.

#include <cstdio>

void add(long* yy, long* xx, int n);

int main() {
  const int N = 10;
  long yy[N] = {-5,-1,-2,-3,-4,-5,-6,-7,-8,-9};
  long xx[N] = {-1,-2,-3,-4,-5,-6,-7,-8,-9,10};

  add(yy, xx, N);

  return 0;
}


void add(long* yy, long* xx, int n) {
  long long siy=0;

  for(int i = 0; i < n; i++) {
    long y = yy[i];
#if 0 // workaround
    long ipy = -(1<<30)/y;
    long long iy = ipy;
    iy = -iy;
#else
    long iy = (1<<30)/y;
#endif
    siy  += iy;
  }
  printf("Add siy: %lld \n",siy);
}


-- 
           Summary: compiling 'long long' math with optimization gives bad
                    results
           Product: gcc
           Version: 3.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: other
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: eran dot nissenhaus at mobileye dot com
 GCC build triplet: 3.2.1


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=26474

Reply via email to