From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 008E73858C60; Tue, 13 Feb 2024 00:00:15 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 008E73858C60 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1707782416; bh=f0ytKt+WVL+pgDSBftd23Qh8tN4QfLo+pHDIhJSCMUQ=; h=From:To:Subject:Date:From; b=fByPhrtSF8RNs0nWE7j76vxiJ/Ks8fhV25dXcQCeyJJ+rfDof74g7KPLbR4Vb/6Ol t1+iwLc/UdDOtKkr4MLDW5nJ2uBbEw8nxPmuJLlD06gyLGnLK/LtxLKOcF2YPneb2w TMPZdM1tBGSpT6jHQCycuHj2nhDyyMxjjgOTI9sI= From: "noobie-iv at mail dot ru" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/113896] New: Assigning array elements in the wrong order after floating point optimization Date: Tue, 13 Feb 2024 00:00:15 +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: 12.3.1 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: noobie-iv at mail dot ru 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 attachments.created 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=3D113896 Bug ID: 113896 Summary: Assigning array elements in the wrong order after floating point optimization Product: gcc Version: 12.3.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: noobie-iv at mail dot ru Target Milestone: --- Created attachment 57404 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=3D57404&action=3Dedit Test program and build_and_run script /* file: f.cpp */ extern double a1; // 1.0 extern double a2; // 1.0 void f(double K[2], bool b) { double A[] =3D { b ? a1 : a2, 0, 0, 0 }; double sum{}; for(double a : A) sum +=3D a; for(double& a : A) a /=3D sum; if (b) { K[0] =3D A[0]; // 1.0 K[1] =3D A[1]; // 0.0 } else { K[0] =3D A[0] + A[1]; } } /* file: main.cpp */ #include double a1 =3D 1.0; double a2 =3D 1.0; void f(double K[2], bool b); int main() { double K[2]{}; f(K, true); std::cout << K[0] << "\t" << K[1] << "\n"; } Bug: Returns different results when compiled with different optimization settings: g++ -O2 -ffast-math f.cpp main.cpp -o good g++ -O3 -ffast-math f.cpp main.cpp -o bad ./good outputs "1 0" ./bad outputs "0 1" The bug is reproduced in systems: * Fedora-37-1.7 x64 with latest gcc autoupdate (12.3.1 20230508) * Debian-12.2.0 x64 with gcc-12.3.1 build from sources In the gcc-12 branch: * Last good commit: d127348d7711e148e5ddd205a8c3409b37fae64c (12.2.1 202210= 17) * First bad commit: fe7d74313736b8e1c30812bc49419f419bdf1c53 (12.2.1 202210= 17) * Last tested bad commit: 4ced4ca95001f1583623c801c9c3642224a2c4f0 (12.3.1 20240210) In the gcc-13 branch - There is no bug: * Last tested good commit: c891d8dc23e1a46ad9f3e757d09e57b500d40044 (13.2.0) In the gcc-14 branch - There is no bug: * Last tested good commit: cff174fabd6c980c09aee95db1d9d5c22421761f (14.0.1 20240210) Note: Function f() is a simplified version of the XSpline::linearCombinationFor() function from the scantailor-experimental project: https://github.com/ImageProcessing-ElectronicPublications/scantailor-experi= mental/blob/main/src/math/XSpline.cpp Now the bug has been temporarily resolved by declaring array A volatile.=