public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "guminb at ajou dot ac.kr" <gcc-bugzilla@gcc.gnu.org>
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	[thread overview]
Message-ID: <bug-112758-4@http.gcc.gnu.org/bugzilla/> (raw)

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=112758

            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 version
14.0.0 when compiling code involving bitwise AND operations between an `int`
and a `long long int` variable under different optimization levels. The issue
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 <stdio.h>

int globalVar = 0x80000000; // 32-bit int with high bit set

int main () {
    long long int localVar = 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, leading 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 could
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]

             reply	other threads:[~2023-11-29  6:05 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-29  6:05 guminb at ajou dot ac.kr [this message]
2023-11-29 18:18 ` [Bug rtl-optimization/112758] [13/14 Regression] Inconsistent Bitwise AND Operation Result between int and long long int pinskia at gcc dot gnu.org
2023-12-08  2:57 ` guminb at ajou dot ac.kr
2023-12-08 17:01 ` jakub at gcc dot gnu.org
2023-12-08 17:37 ` segher at gcc dot gnu.org
2023-12-08 19:33 ` jakub at gcc dot gnu.org
2023-12-08 19:36 ` jakub at gcc dot gnu.org
2023-12-09  8:51 ` ebotcazou at gcc dot gnu.org
2023-12-09  9:11 ` jakub at gcc dot gnu.org
2023-12-09 11:00 ` ebotcazou at gcc dot gnu.org
2023-12-09 19:25 ` segher at gcc dot gnu.org
2023-12-09 22:06 ` ebotcazou at gcc dot gnu.org
2023-12-10 12:16 ` segher at gcc dot gnu.org
2023-12-13 22:06 ` pinskia at gcc dot gnu.org
2023-12-16 18:43 ` gkm at rivosinc dot com
2023-12-21 20:04 ` gkm at rivosinc dot com
2023-12-21 20:06 ` jakub at gcc dot gnu.org
2023-12-22 11:30 ` cvs-commit at gcc dot gnu.org
2024-03-02  0:37 ` cvs-commit at gcc dot gnu.org
2024-03-04  1:09 ` law at gcc dot gnu.org
2024-03-04 12:07 ` [Bug rtl-optimization/112758] [13 " jakub at gcc dot gnu.org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-112758-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).