COMP: DiagonalMatrix: avoid clang's over-eager optimisation (fixes #2650)

This commit is contained in:
Kutalmis Bercin
2022-12-06 09:54:11 +00:00
parent 3ab182b1ae
commit b50850de56
2 changed files with 11 additions and 15 deletions

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2019-2020 OpenCFD Ltd. Copyright (C) 2019-2022 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -74,11 +74,9 @@ void Foam::DiagonalMatrix<Type>::invert()
{ {
for (Type& val : *this) for (Type& val : *this)
{ {
if (mag(val) < VSMALL) // If mag(val)<VSMALL, leave untouched
{ // forcing of val=Zero sometimes confuses compiler
val = Zero; if (mag(val) > VSMALL)
}
else
{ {
val = Type(1)/val; val = Type(1)/val;
} }
@ -149,21 +147,19 @@ namespace Foam
// * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
//- Return the matrix inverse as a DiagonalMatrix if no elem is equal to zero //- Return the matrix inverse as a DiagonalMatrix
template<class Type> template<class Type>
DiagonalMatrix<Type> inv(const DiagonalMatrix<Type>& mat) DiagonalMatrix<Type> inv(const DiagonalMatrix<Type>& mat)
{ {
DiagonalMatrix<Type> Ainv(mat.size()); // Construct with fall-back value conditional calculation
// of invert to avoid over-eager compiler optimisation
DiagonalMatrix<Type> Ainv(mat.size(), Zero);
Type* iter = Ainv.begin(); Type* iter = Ainv.begin();
for (const Type& val : mat) for (const Type& val : mat)
{ {
if (mag(val) < VSMALL) if (mag(val) > VSMALL)
{
*iter = Zero;
}
else
{ {
*iter = Type(1)/val; *iter = Type(1)/val;
} }

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2019-2020 OpenCFD Ltd. Copyright (C) 2019-2022 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -94,7 +94,7 @@ public:
// Member Functions // Member Functions
//- Return the matrix inverse into itself if no elem is equal to zero //- Return the matrix inverse into itself
void invert(); void invert();
//- Return a sort permutation labelList according to //- Return a sort permutation labelList according to