When compiling the following code with -O2 or -Os, the if clause at the end of
STRING_hash_code is optimized away, yielding in a negative result
  if(R<0){
    R=~R;
  }

With -O1 everything works as expected (positive result)

#include <stdint.h>
#include <stdio.h>
#include <string.h>

typedef struct {unsigned char* storage;int32_t count;int32_t capacity;} STRING;

int main(int argc, char *argv[]) {
  STRING x;
 
x.storage="HASHED_DICTIONARY[WEAK_REFERENCE[ANY_HASHED_DICTIONARY_NODE],STRING]";
  x.count=strlen(x.storage);
  x.capacity=x.count+1;
  printf("%d\n",STRING_hash_code(&x));
}

int32_t STRING_hash_code(STRING* C){
  int32_t R=0;
  int32_t i=0;
  int32_t j=0;
  j=C->count;
  i=1;
  while (j>0)
    {
      R=5*R+C->storage[i-1];
      i++;
      j--;
    }
  if(R<0){
    R=~R;
  }
  return R;
}


-- 
           Summary: Optimizer too aggressive
           Product: gcc
           Version: 4.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: frederic dot merizen at gmail dot com
 GCC build triplet: i686-pc-linux-gnu
  GCC host triplet: i686-pc-linux-gnu
GCC target triplet: i686-pc-linux-gnu


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

Reply via email to