COMP: circumvent aggressive compiler branch optimization (clang-17)

- it seems that both sides of the ternary are evaluated despite
  the divide-by-zero protection. Use volatile to force the compiler
  to use in-order evaluation.
This commit is contained in:
Mark Olesen
2023-10-23 15:19:41 +02:00
parent a7bb8edbad
commit cfa6da5953
10 changed files with 31 additions and 19 deletions

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011 OpenFOAM Foundation
Copyright (C) 2017-2022 OpenCFD Ltd.
Copyright (C) 2017-2023 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -417,10 +417,9 @@ inline Foam::vector Foam::edge::unitVec(const UList<point>& pts) const
}
#endif
const vector v = (pts[second()] - pts[first()]);
const scalar s(Foam::mag(v));
return s < ROOTVSMALL ? Zero : v/s;
vector v = (pts[second()] - pts[first()]);
(void) v.normalise(ROOTVSMALL);
return v;
}

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011 OpenFOAM Foundation
Copyright (C) 2017-2022 OpenCFD Ltd.
Copyright (C) 2017-2023 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -103,9 +103,9 @@ inline Foam::pointField Foam::face::points(const UList<point>& pts) const
inline Foam::vector Foam::face::unitNormal(const UList<point>& p) const
{
const vector n(areaNormal(p));
const scalar s(Foam::mag(n));
return s < ROOTVSMALL ? Zero : n/s;
vector n(areaNormal(p));
(void) n.normalise(ROOTVSMALL);
return n;
}

View File

@ -129,6 +129,10 @@ template<class Point, class PointRef>
inline Point Foam::line<Point, PointRef>::unitVec() const
{
const Point v = (b_ - a_);
#ifdef __clang__
volatile // Use volatile to avoid aggressive branch optimization
#endif
const scalar s(::Foam::mag(v));
return s < ROOTVSMALL ? Zero : v/s;

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2017 OpenFOAM Foundation
Copyright (C) 2018-2022 OpenCFD Ltd.
Copyright (C) 2018-2023 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -175,9 +175,9 @@ inline Foam::vector Foam::triangle<Point, PointRef>::unitNormal
const Point& p2
)
{
const vector n(areaNormal(p0, p1, p2));
const scalar s(::Foam::mag(n));
return s < ROOTVSMALL ? Zero : n/s;
vector n(areaNormal(p0, p1, p2));
(void) n.normalise(ROOTVSMALL);
return n;
}

View File

@ -120,6 +120,9 @@ inline Foam::scalar Foam::Vector<Cmpt>::dist(const Vector<Cmpt>& v2) const
template<class Cmpt>
inline Foam::Vector<Cmpt>& Foam::Vector<Cmpt>::normalise(const scalar tol)
{
#ifdef __clang__
volatile // Use volatile to avoid aggressive branch optimization
#endif
const scalar s = this->mag();
if (s < tol)

View File

@ -490,6 +490,9 @@ inline VectorSpace<Form, Cmpt, Ncmpts> normalised
const VectorSpace<Form, Cmpt, Ncmpts>& vs
)
{
#ifdef __clang__
volatile // Use volatile to avoid aggressive branch optimization
#endif
const scalar s(mag(vs));
return s < ROOTVSMALL ? Zero : vs/s;
}

View File

@ -210,6 +210,7 @@ inline complex sqr(const complex& c)
//- sgn() https://en.wikipedia.org/wiki/Sign_function#Complex_signum
inline complex sign(const complex& c)
{
// TBD: Use volatile to avoid aggressive branch optimization
const scalar s(mag(c));
return s < ROOTVSMALL ? Zero : c/s;
}

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2019-2022 OpenCFD Ltd.
Copyright (C) 2019-2023 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -215,7 +215,7 @@ public:
//- Inplace normalise the quaternion by its magnitude
// For small magnitudes (less than ROOTVSMALL) set to zero.
inline quaternion& normalise();
inline quaternion& normalise(const scalar tol = ROOTVSMALL);
// Transform

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2019-2022 OpenCFD Ltd.
Copyright (C) 2019-2023 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -306,13 +306,14 @@ inline Foam::vector& Foam::quaternion::v() noexcept
}
inline Foam::quaternion& Foam::quaternion::normalise()
inline Foam::quaternion& Foam::quaternion::normalise(const scalar tol)
{
// TBD: Use volatile to avoid aggressive branch optimization
const scalar s(Foam::mag(*this));
if (s < ROOTVSMALL)
if (s < tol)
{
*this = Zero;
*this = Foam::zero{};
}
else
{

View File

@ -68,6 +68,7 @@ static inline vector areaInvDistSqrWeightedNormal
const vector& b
)
{
// TBD: Use volatile to avoid aggressive branch optimization
const scalar s(2*magSqr(a)*magSqr(b));
return s < ROOTVSMALL ? Zero : (a ^ b) / s;