From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 4BB413858D3C; Wed, 29 Nov 2023 06:05:25 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4BB413858D3C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1701237925; bh=z1nVS0gmg/eAdCjW5b/VYUfQLgbIbHeT77EkDETKooM=; h=From:To:Subject:Date:From; b=UMQ6M8j88hXz1xuDhNGU9qNEcvoLg74fhkRvub/wGadDw2vZGXRb8MwjrUsugKCY4 TzRr10miOkCVXco1/S4gBcoRdKE565cyJ5MUAekefr/gBuVNBuoKxRSL/dd9MAMUZm KPg1Qvm9BlSzpGmsR4xLWfyliP36pkNOyzx4dHnk= From: "guminb at ajou dot ac.kr" To: gcc-bugs@gcc.gnu.org Subject: [Bug c/112758] New: Inconsistent Bitwise AND Operation Result between int and long long int on Different Optimization Levels in GCC Trunk Date: Wed, 29 Nov 2023 06:05:24 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c X-Bugzilla-Version: 14.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: guminb at ajou dot ac.kr X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter target_milestone Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D112758 Bug ID: 112758 Summary: Inconsistent Bitwise AND Operation Result between int and long long int on Different Optimization Levels in GCC Trunk Product: gcc Version: 14.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c Assignee: unassigned at gcc dot gnu.org Reporter: guminb at ajou dot ac.kr Target Milestone: --- Dear GCC Development Team, I would like to report an inconsistency observed in the GCC RISC-V 64 versi= on 14.0.0 when compiling code involving bitwise AND operations between an `int` and a `long long int` variable under different optimization levels. The iss= ue appears when both operands are negative, with results varying significantly between non-optimized and optimized compilations. The proof of concept (PoC) code provided below demonstrates this issue. The code performs a bitwise AND operation between a 32-bit integer with its high bit set (`globalVar`) and a 64-bit long long integer (`localVar`), both containing negative values. The expected result of the operation seems to differ based on the optimization level used during compilation. PoC Code: ```c #include int globalVar =3D 0x80000000; // 32-bit int with high bit set int main () { long long int localVar =3D 0xffFF00ffffffffff; // 64-bit long long int printf("localVar: 0x%llx\\n", localVar); printf("globalVar: 0x%llx\\n", (long long int)globalVar); printf("Result: 0x%llx\\n", ((localVar) & ((long long int) (globalVar))= )); return 0; } ``` Observed Results: - With **`O0`** optimization, the result of the bitwise AND operation is as expected (**`0xffff00ff80000000`**). - With **`O1`**, **`O2`**, **`O3`**, **`Os`**, **`Oz`** optimizations, the result changes to **`0x80000000`**. Assembly Output: The assembly output for -O0 and -O1 can be viewed at the following Compiler Explorer link: https://godbolt.org/z/fb33vWT7o - The **`O0`** output shows the expected behavior with explicit casting and= AND operation. - The **`O1`** output, however, omits the casting and AND operation, leadin= g to an unexpected result. I suspect this might be related to how the compiler handles casting or the bitwise operation under different optimization levels. This inconsistency c= ould potentially lead to unintended behavior in applications that rely on such operations, especially when negative values are involved. I appreciate your attention to this matter and look forward to any insights= or potential solutions you might provide. Best regards, [Gyumin Baek]=