Hi, Here is a short summary of the problem. In the upgrading of gcc from 3.3 to 4.4.3, my hash unit test failed. I did some investigation and wrote a simple program, which is attached, to reproduce this problem. I use command below to compile this program: g++ -g -pipe -O9 -funroll-loops -ffast-math -DNDEBUG -march=pentiumpro -DLINUX -fpic -DGEOTARGETING -D_NG_POSIX_THREAD -D_RWCONFIG_${RWDBGNUM}_$RWLIBVERSION -DRW_MULTI_THREAD -DRW_NO_XMSG -DRW_POSIX_D10_THREADS -D_REENTRANT -Wall -Werror -Wno-sign-compare -Wno-write-strings -Wno-strict-aliasing -Wno-unused-result abs_error.cc and the result shows me "hash<0" is false. If "-O9" is removed from the command line, "hash<0" is true. I disassembly the obj file by objdump (objdump -Sl a.out), and below is digest of the optimized result: ... 80484f1: b8 49 5e 86 da mov $0xda865e49,%eax 80484f6: 89 e5 mov %esp,%ebp 80484f8: 83 e4 f0 and $0xfffffff0,%esp 80484fb: 53 push %ebx 80484fc: e8 4c 00 00 00 call 804854d <__i686.get_pc_thunk.bx> 8048501: 81 c3 f3 1a 00 00 add $0x1af3,%ebx 8048507: 83 ec 1c sub $0x1c,%esp 804850a: 89 44 24 08 mov %eax,0x8(%esp) 804850e: c7 04 24 01 00 00 00 movl $0x1,(%esp) 8048515: 8d 8b ec e6 ff ff lea -0x1914(%ebx),%ecx 804851b: 89 4c 24 04 mov %ecx,0x4(%esp) 804851f: e8 dc fe ff ff call 8048400 <__printf_chk@plt> 8048524: 8d 83 f7 e6 ff ff lea -0x1909(%ebx),%eax 804852a: ba 49 5e 86 da mov $0xda865e49,%edx 804852f: 89 54 24 08 mov %edx,0x8(%esp) 8048533: 89 44 24 04 mov %eax,0x4(%esp) 8048537: c7 04 24 01 00 00 00 movl $0x1,(%esp) 804853e: e8 bd fe ff ff call 8048400 <__printf_chk@plt> ... Seems "hash" and "result" both assigned 0xda865e49 (-628728247 in oct) directly, for the optimizations. I am not sure whether it is a problem in optimizations of the compiler, so I am not sure whether it is correct for me to assign bug on this component. And, because I am not familiar with i86 assembly, just read some tutorial to understand a bit of the asm code, there must be some problems in the investigations. Please let me know what do you think of this issue. Sure, if it is my problem or there is a duplication of this bug, please let me know and close the bug freely. And, because it is almost impossible for our project to move back to gcc 3.3 or try one more upgrade in short period, would you share me some way for us to walk around this problem? (Sure, to make sure other code won't face this problem any more) Many thanks for your help!
Because I can't find the attachment in the thread, add the text below for debugging easily. ================================================== #include <stdio.h> #include <string.h> int main() { const char *str = "1234567"; int hash = 17; for (int i = 0; i < strlen(str); ++i) { hash = 37 * hash + str[i]; } printf("hash = %d, %d\n", hash, hash < 0); int result = (hash < 0) ? (-hash) : hash; printf("result = %d\n", result); return 0; } ===================================================
overflow for signed integer is undefined. Use unsigned integers if you want defined wrapping behavior.