https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82192
Bug ID: 82192
Summary: gcc produces incorrect code with -O2 and bit-field
Product: gcc
Version: 8.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: tree-optimization
Assignee: unassigned at gcc dot gnu.org
Reporter: vsevolod.livinskij at frtk dot ru
Target Milestone: ---
gcc produces incorrect code with -O2 and higher and unsigned int : 13 bit-field
>$ g++ -O2 driver.cpp func.cpp ; ./a.out
6930
>$ g++ -O0 driver.cpp func.cpp ; ./a.out
0
>$ cat init.h
extern const unsigned long int a;
struct struct_t {
unsigned int memb : 13;
};
extern struct_t b;
>$ cat driver.cpp
#include <stdio.h>
#include "init.h"
const unsigned long int a = 10798855141994013410UL;
struct_t b;
extern void foo ();
int main () {
foo ();
printf("%llu\n", b.memb);
return 0;
}
>$ cat func.cpp
#include "init.h"
void foo() {
b.memb = unsigned(a) >> (7227976781724269559 | a & ~3739384568U) -
7227976781724531672;
}
>$ g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/gcc-dev/bin-trunk/libexec/gcc/x86_64-pc-linux-gnu/8.0.0/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: /gcc-dev/trunk/configure --prefix=/gcc-dev/bin-trunk
--disable-bootstrap
Thread model: posix
gcc version 8.0.0 20170912 (Rev. 252003) (GCC)