public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug other/26474]  New: compiling 'long long' math with optimization gives bad results
@ 2006-02-26 17:19 eran dot nissenhaus at mobileye dot com
  2006-02-26 18:39 ` [Bug other/26474] " pinskia at gcc dot gnu dot org
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: eran dot nissenhaus at mobileye dot com @ 2006-02-26 17:19 UTC (permalink / raw)
  To: gcc-bugs

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


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2006-02-27 13:28 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-02-26 17:19 [Bug other/26474] New: compiling 'long long' math with optimization gives bad results eran dot nissenhaus at mobileye dot com
2006-02-26 18:39 ` [Bug other/26474] " pinskia at gcc dot gnu dot org
2006-02-27  8:35 ` eran dot nissenhaus at mobileye dot com
2006-02-27 11:48 ` [Bug target/26474] " rguenth at gcc dot gnu dot org
2006-02-27 12:03 ` rguenth at gcc dot gnu dot org
2006-02-27 13:48 ` rguenth at gcc dot gnu dot org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).