[Issue 24819] New: Optimizer changes result of float calculations on 32-bit

d-bugmail at puremagic.com d-bugmail at puremagic.com
Tue Oct 15 13:21:15 UTC 2024


https://issues.dlang.org/show_bug.cgi?id=24819

          Issue ID: 24819
           Summary: Optimizer changes result of float calculations on
                    32-bit
           Product: D
           Version: D2
          Hardware: x86
                OS: Windows
            Status: NEW
          Keywords: backend, industry
          Severity: major
          Priority: P3
         Component: dmd
          Assignee: nobody at puremagic.com
          Reporter: dkorpel at live.nl

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.

--


More information about the Digitalmars-d-bugs mailing list