https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91373
Bug ID: 91373 Summary: gcc6.2.0: ((U32)((U16 * U16)) >> 31) cannot always get correct result with gcc -O2 Product: gcc Version: 6.2.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c Assignee: unassigned at gcc dot gnu.org Reporter: qiang.fu at verisilicon dot com Target Milestone: --- Created attachment 46676 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=46676&action=edit main.c Hi, I met an issue with gcc6.2.0: ((U16 * U16) >> 31) cannot always get expected result for gcc optimization switch '-O2'. e.g. (63139 * 36032) >> 30 = 0x2 // right (63139 * 36032) >> 31 = 0x0 // wrong, should be 0x1. I create a test case for it, see the attachement(main.c). It passed on gcc 4.6.3, but failed with gcc6.2.0 + release build(gcc -O2) gcc -O2 main.c -o test ./test 63139 36032 data1: 0x879a1e40, data1 >> 31: 1, data1 >> 30: 2 data2: 0x879a1e40, data2 >> 31: 0, data1 >> 30: 2 This test passed if build with -O0 or -O1 with gcc6.2.0 GCC version: 6.2.0 * gcc -v Using built-in specs. COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=/usr/local/libexec/gcc/x86_64-pc-linux-gnu/6.2.0/lto-wrapper Target: x86_64-pc-linux-gnu Configured with: ../configure -enable-checking=release -enable-languages=c,c++ -disable-multilib Thread model: posix gcc version 6.2.0 (GCC)