https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108720
Bug ID: 108720 Summary: INT_MIN * -1 is incorrect Product: gcc Version: 11.3.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c Assignee: unassigned at gcc dot gnu.org Reporter: amk7r at mtmail dot mtsu.edu Target Milestone: --- Created attachment 54434 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=54434&action=edit Attached is a file with the source code (.c) and the preprocessed file (.i) Version of GCC: 11.3.0 System type: Ubuntu 22.04.1 LTS Options given when GCC was configured/built: ../src/configure -v --with-pkgversion='Ubuntu 11.3.0-1ubuntu1~22.04' --with-bugurl=file:///usr/share/doc/gcc-11/README.Bugs --enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++,m2 --prefix=/usr --with-gcc-major-version-only --program-suffix=-11 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --enable-bootstrap --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-plugin --enable-default-pie --with-system-zlib --enable-libphobos-checking=release --with-target-system-zlib=auto --enable-objc-gc=auto --enable-multiarch --disable-werror --enable-cet --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-offload-targets=nvptx-none=/build/gcc-11-xKiWfi/gcc-11-11.3.0/debian/tmp-nvptx/usr,amdgcn-amdhsa=/build/gcc-11-xKiWfi/gcc-11-11.3.0/debian/tmp-gcn/usr --without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu --with-build-config=bootstrap-lto-lean --enable-link-serialization=2 Complete command line that triggers the bug: gcc err_file.c The compiler output (error messages, warnings, ect.): n/a; there were no errors or warnings Expected behavior: The program should print out the value of y, and then print out “true 4” because -INT_MIN is equal to INT_MIN, which is less than 0. Actual behavior: The program prints out the value of y, and then prints out “true 2.” Case 1: y>= 0. "true 1" should not be printed because y is INT_MIN. -> Test Pass Case 2: (x-1)>= 0. "true 2" should not be printed because x is INT_MIN. -> Test Fail o INT_MIN * -1 = INT_MIN. Case 3: (x-1)>= zero. "true 3" should not be printed because x is INT_MIN. -> Test Pass o INT_MIN * -1 = INT_MIN and int zero has 0 Case 4: (x-1)< 0. "true 4" should be printed because x is INT_MIN. o INT_MIN * -1 = INT_MIN and INT_MIN is less than 0 -> Test Fail More information: If you translate the C code into assembly code using the command “gcc -s err_file.c,” and check the .L2 block: In .L2: The assembly code should be: #equivalent c code cmpl $0, -8(%rbp) #because it has the result of x * -1 if ((x*-1)>=0) js .L3 #instead of jg .L3 Explanation: * x *-1 is INT_MIN. * “jg” instruction will skip the statement in “if block” if (x*-1) or INT_MIN is greater than 0. * Since, (x*-1) or INT_MIN is not greater than 0, the statement in the “if block” is printed (“true 2”). * But “true 2” should only be printed if (x*-1) or INT_MIN is greater than or equal to 0 (check c code line 9). * In this case, doing “js .L3” would make more sense and will give the expected result.