https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106440
Bug ID: 106440 Summary: on Mac M1 chip undefined behavior of double arithmetic Product: gcc Version: 12.1.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: target Assignee: unassigned at gcc dot gnu.org Reporter: yaobig97 at gmail dot com Target Milestone: --- Created attachment 53351 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=53351&action=edit the preprocessed file (*.i*) that triggers the bug Here are the infos you want for reporting bugs: %% gcc-12 -v %% Using built-in specs. COLLECT_GCC=gcc-12 COLLECT_LTO_WRAPPER=/opt/homebrew/Cellar/gcc@12/12.1.0_1/bin/../libexec/gcc/aarch64-apple-darwin21/12/lto-wrapper Target: aarch64-apple-darwin21 Configured with: ../configure --prefix=/opt/homebrew/opt/gcc@12 --libdir=/opt/homebrew/opt/gcc@12/lib/gcc/12 --disable-nls --enable-checking=release --with-gcc-major-version-only --enable-languages=c,c++,objc,obj-c++,fortran --program-suffix=-12 --with-gmp=/opt/homebrew/opt/gmp --with-mpfr=/opt/homebrew/opt/mpfr --with-mpc=/opt/homebrew/opt/libmpc --with-isl=/opt/homebrew/opt/isl --with-zstd=/opt/homebrew/opt/zstd --with-pkgversion='Homebrew GCC 12.1.0_1' --with-bugurl=https://github.com/Homebrew/homebrew-core/issues --with-system-zlib --build=aarch64-apple-darwin21 --with-native-system-header-dir=/usr/include --with-sysroot=/Library/Developer/CommandLineTools/SDKs/MacOSX12.sdk Thread model: posix Supported LTO compression algorithms: zlib zstd gcc version 12.1.0 (Homebrew GCC 12.1.0_1) %% the complete command line that triggers the bug %% g++-12 -O2 -Wall -Wextra test.cpp %% the compiler output (error messages, warnings, etc.) %% No error. %% the preprocessed file (*.i*) that triggers the bug %% See the attachment. Here are the infos which are said not needed but pretty helpful from my side: %% code %% #include <iostream> #include <vector> using namespace std; struct P { double x, y; P operator -(const P &a) const { return P{x - a.x, y - a.y}; } double cross(const P &b) const { return x * b.y - y * b.x; } }; int main() { const double A = 3'000'000'000; vector<P> as{P{A, 1 + A}, P{0, 1}}; auto c = as[0]; auto b = as[1]; cout << c.x << " " << c.y << endl; cout << (P{1, 2} - as[0]).cross(as[1] - as[0]) << endl; cout << (P{1, 2} - c).cross(b - c) << endl; return 0; } %% output %% 3e+09 3e+09 512 0 %% Description %% I do not see why the second line and the third line in the output can be different. I do not think that the code itself is wrong but the behavior really seems to be undefined: remove cout << c.x << " " << c.y << endl; and output numbers are both 0. The code works perfectly under gcc on x86 and also clang on ARM (Mac M1 chip) so I suppose that the error comes from gcc on ARM (Mac M1 chip)