Bug 24819 – Optimizer changes result of float calculations on 32-bit

Status
RESOLVED
Resolution
FIXED
Severity
major
Priority
P3
Component
dmd
Product
D
Version
D2
Platform
x86
OS
Windows
Creation time
2024-10-15T13:21:15Z
Last change time
2024-11-16T23:27:16Z
Keywords
backend, industry, pull
Assigned to
No Owner
Creator
Dennis

Comments

Comment #0 by dkorpel — 2024-10-15T13:21:15Z
On 32-bit, dmd's optimizer produces a different result for the following code: ``` void main() { double[4] y; cubic_roots(-3.016658e-04, 1.042930e-02, 1.427929e-01, -2.600958e-02, y); writeln(y[1 .. $]); } void cubic_roots(double a, double b, double c, double d, ref double[4] x) { immutable ovfl = 1E+6; immutable half = 0.5; immutable e3 = 0.3333333333333; immutable pi3 = 1.04719755; int i, z; double a1, b1, c1, df, disc, d1, f, p3, q, r, step, u, u2, y, ay; a1 = abs(a); b1 = abs(b); c1 = abs(c); d1 = abs(d); if (max(max(b1, c1), d1) < a1 * ovfl) { b1 = b / a * e3; c1 = c / a; d1 = d / a; q = c1 * e3 - (b1) ^^ 2; r = (b1) ^^ 2 * b1 + (d1 - b1 * c1) * half; disc = (q) ^^ 2 * q + (r) ^^ 2; if (disc <= 0) { u = sqrt(abs(q)); if (r < 0) u = -u; if (r != 0) { p3 = atan(sqrt(-disc) / abs(r)) * e3; u2 = u + u; x[1] = -u2 * cos(p3) - b1; x[2] = u2 * cos(pi3 - p3) - b1; x[3] = u2 * cos(pi3 + p3) - b1; } } } for ({z = 1; immutable max_z_expression = 3; } z <= max_z_expression; z++) for ({i = 1; immutable max_i_expression = 3; } i <= max_i_expression; i++) { y = x[i]; ay = a * y; f = ((ay + b) * y + c) * y + d; df = (3 * ay + 2 * b) * y + c; step = 0; x[i] = y - step; } } ``` Expected result: [45.0395, -10.6469, 0.1798] Actual result (with dmd -m32 -O): [45.6174, -5.52252, -5.52252] I'll reduce it further later.
Comment #1 by dkorpel — 2024-10-15T13:52:29Z
Reduced: ``` import core.stdc.stdio; pragma(inline, true) double sqrt(double x) { static import core.math; return core.math.sqrt(x); } void main() { double q = -1.0; double r = q + 0.1; double result = sqrt(-r); printf("%f\n", result); } ```
Comment #2 by bugzilla — 2024-10-24T04:35:21Z
The problem shows up with this optimization: in localize(): Moved equation x(3) = (r(1) ^ -9223372036854775808LL ); el:0x2ade880 cnt=0 = TYdouble 0x2ade6c0 0x2ade810 el:0x2ade6c0 cnt=0 var TYlong x el:0x2ade810 cnt=0 ^ TYlong 0x2ade7a0 0x2ade000 el:0x2ade7a0 cnt=0 var TYlong r el:0x2ade000 cnt=0 const TYlong -9223372036854775808LL replacing: el:0x2ade9d0 cnt=0 var TYdouble x which fouls up the x87 code generation.
Comment #3 by dlang-bot — 2024-10-24T04:45:56Z
@WalterBright created dlang/dmd pull request #17023 "fix bugzilla Issue 24819 - Optimizer changes result of float calculat…" fixing this issue: - fix bugzilla Issue 24819 - Optimizer changes result of float calculations on 32-bit https://github.com/dlang/dmd/pull/17023
Comment #4 by dlang-bot — 2024-10-24T11:01:27Z
dlang/dmd pull request #17023 "fix bugzilla Issue 24819 - Optimizer changes result of float calculat…" was merged into master: - 06a4bff4640e7ffaf329288ec253d2cd961e0a64 by Walter Bright: fix bugzilla Issue 24819 - Optimizer changes result of float calculations on 32-bit https://github.com/dlang/dmd/pull/17023
Comment #5 by dlang-bot — 2024-10-24T23:47:47Z
dlang/dmd pull request #17028 "fix bugzilla Issue 24819 - Optimizer changes result of float calculat…" was merged into stable: - 6a53ff6520c1904134572d39ebceb1cdcd2dd662 by Walter Bright: fix bugzilla Issue 24819 - Optimizer changes result of float calculations on 32-bit (#17023) (cherry picked from commit 88d1e8fc37428b873f59d87f8dff1f40fbd3e7a3) https://github.com/dlang/dmd/pull/17028
Comment #6 by dlang-bot — 2024-11-16T23:27:16Z
dlang/dmd pull request #17069 "Merge stable" was merged into master: - 735193cc36db6b2fa2c50270fae3414316323d9b by Walter Bright: fix bugzilla Issue 24819 - Optimizer changes result of float calculations on 32-bit (#17023) (cherry picked from commit 88d1e8fc37428b873f59d87f8dff1f40fbd3e7a3) https://github.com/dlang/dmd/pull/17069