Skip to content

double 4x4 matrix inversion fails with -Ofast optimization flag (g++)

Summary

4x4 double matrix inversion fails when using -Ofast compilation flag and g++. Strange signs appear in front of some output values.

Environment

  • Operating System : Linux
  • Architecture : x86_64
  • Eigen Version : 3.4.0
  • Compiler Version : G++10.3.0
  • Compile Flags : -Ofast -march=native -std=c++17
  • Vector Extension : AVX

Minimal Example

main.cpp: 

#include <stdio.h>
#include <Eigen/Core>
#include <Eigen/Geometry>
#include <Eigen/Dense>

void foo(const Eigen::Matrix4d& u, Eigen::Matrix4d& w)
{
  EIGEN_ASM_COMMENT("begin");
  w = u.inverse();
  EIGEN_ASM_COMMENT("end");
}

void dumpEigenMatrix4d(const char *name, const Eigen::Matrix4d &mtx) {
    printf("%s:\n", name);
    printf("%lf %lf %lf %lf\n", mtx(0, 0), mtx(0, 1), mtx(0, 2), mtx(0, 3));
    printf("%lf %lf %lf %lf\n", mtx(1, 0), mtx(1, 1), mtx(1, 2), mtx(1, 3));
    printf("%lf %lf %lf %lf\n", mtx(2, 0), mtx(2, 1), mtx(2, 2), mtx(2, 3));
    printf("%lf %lf %lf %lf\n", mtx(3, 0), mtx(3, 1), mtx(3, 2), mtx(3, 3));
}

int main(int argc, char *argv[]) {
    static Eigen::Matrix4d mtx = (Eigen::Matrix4d() << 0.652997, -0.041407, -0.756228, -5.549290, -0.061985, 0.992233, -0.107853, -0.556586,0.754819, 0.117303, 0.645359, -6.319764,0.000000, 0.000000,  0.000000, 1.000000).finished();
    Eigen::Matrix4d mtx2;
    foo(mtx,mtx2);
    dumpEigenMatrix4d("fyy",mtx2);
    return 0;
}

Steps to reproduce

test:

does not work:

g++ -I<path_to_eigen3> -std=c++17 -march=native -Ofast main.cpp -o main.test1

output:

0.652998 0.061985 0.754820 -8.359456

-0.041407 -0.992232 0.117302 -1.063807

-0.756227 0.107853 0.645358 0.178041

-0.000000 -0.000000 -0.000000 -1.000000

works:

g++ -I<path_to_eigen3> -std=c++17 -march=native -O3 main.cpp -o main.test2

output:

0.652998 -0.061985 0.754820 8.359456

-0.041407 0.992232 0.117302 1.063807

-0.756227 -0.107853 0.645358 -0.178041

0.000000 -0.000000 -0.000000 1.000000

What is the current bug behavior?

Incorrect signs appear to output matrix.

What is the expected correct behavior?

See example above.

Relevant logs

Warning Messages

Benchmark scripts and results

Anything else that might help

  • Have a plan to fix this issue.
Edited by Tommi Tykkälä