Skip to content

Wrong code for -O1 with loops fabs and floating point math #4997

@veelo

Description

@veelo

This code:

import std.math : abs;

void main()
{
    double[2] v = [-1, 0];
    double maxip = 0;
    for (int d = 0; d < 2; d++)
    {
        double[2] w = [0, 1];
        w[d] = 1;
        double ip = v[0] * w[0] + v[1] * w[1];
        if (abs(ip) > abs(maxip))
            maxip = ip;
    }
    assert(maxip < 0);
}

succeeds when compiled without options, which is expected. When compiled with -O1 the assertion trips.

LDC - the LLVM D compiler (1.41.0):
  based on DMD v2.111.0 and LLVM 20.1.5
  built with LDC - the LLVM D compiler (1.41.0)
  Default target: x86_64-pc-windows-msvc
  Host CPU: znver5

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions