https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83283
Bug ID: 83283 Summary: Casting from boolean to unsigned char to enum returns incorrect results Product: gcc Version: 7.2.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: lukas.lorimer at snowflake dot net Target Milestone: --- Hello, it appears casting from boolean to unsigned char then to an enum returns incorrect results when the operation is vectorized. Below is a minimized example. * Removing the typedef and including `cstdint` also triggers the bug. * Running it with `-fsanitize=undefined` does not produce any warnings (but does stop the bug from occurring). * This appears to be a regression since g++ 6.2.1. // START main.ii # 1 "main.cpp" # 1 "/tmp/a//" # 1 "<built-in>" # 1 "<command-line>" # 1 "main.cpp" typedef unsigned char uint8_t; enum EN : uint8_t { X = 0, Y = 1 }; void __attribute__((noinline)) fn(EN *v, int size) { for (int i = 0; i < size; ++i) { const bool b = (v[i] == EN::Y); v[i] = static_cast<EN>(static_cast<uint8_t>(b)); } } int main() { constexpr int items = 32; EN vals[items] = {X}; vals[3] = Y; fn(vals, items); return vals[3]; } // END main.ii $ g++72 -g -O1 -ftree-loop-vectorize -Wall -Wextra main.cpp # No output $ ./a.out; echo $? # Expected output: 1 255 $ g++72 -v Using built-in specs. COLLECT_GCC=/usr/local/bin/g++72 COLLECT_LTO_WRAPPER=/opt/rh/devtoolset-7/root/usr/libexec/gcc/x86_64-redhat-linux/7/lto-wrapper Target: x86_64-redhat-linux Configured with: ../configure --enable-bootstrap --enable-languages=c,c++,fortran,lto --prefix=/opt/rh/devtoolset-7/root/usr --mandir=/opt/rh/devtoolset-7/root/usr/share/man --infodir=/opt/rh/devtoolset-7/root/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared --enable-threads=posix --enable-checking=release --enable-multilib --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-gcc-major-version-only --enable-plugin --with-linker-hash-style=gnu --enable-initfini-array --with-default-libstdcxx-abi=gcc4-compatible --with-isl=/builddir/build/BUILD/gcc-7.2.1-20170829/obj-x86_64-redhat-linux/isl-install --enable-libmpx --with-mpc=/builddir/build/BUILD/gcc-7.2.1-20170829/obj-x86_64-redhat-linux/mpc-install --with-tune=generic --with-arch_32=i686 --build=x86_64-redhat-linux Thread model: posix gcc version 7.2.1 20170829 (Red Hat 7.2.1-1) (GCC) The behaviour is correct if one uses: const uint8_t b = (v[i] == EN::Y); It appears the difference between the two is a single pand instruction which applies a mask to the result of some vectorized operations.