Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 11 additions & 17 deletions Sofa/framework/Type/src/sofa/type/Vec.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include <cmath>
#include <array>
#include <cassert>
#include <numeric>

#define EQUALITY_THRESHOLD 1e-6

Expand Down Expand Up @@ -257,8 +258,7 @@ class Vec
// assign one value to all elements
constexpr void assign(const ValueType& value) noexcept
{
for (size_type i = 0; i < N; i++)
elems[i] = value;
std::fill_n(this->elems.data(), N, value);
}

/// Sets every element to 0.
Expand Down Expand Up @@ -472,10 +472,7 @@ class Vec
/// Squared norm.
constexpr ValueType norm2() const noexcept
{
ValueType r = this->elems[0]*this->elems[0];
for (Size i=1; i<N; i++)
r += this->elems[i]*this->elems[i];
return r;
return std::inner_product(elems.begin(), elems.end(), elems.begin(), ValueType{});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can ask the question of the performances...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can ask the question of the performances...

Usually people (and IA...) advise to use stl functions because it may help the compiler more (vectorization and stuff). But of course it depends of the implementation of the libstl itself... I will try to do some bench tho

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://quick-bench.com/q/WGEPcgHll4O2cAl8Ms-VoyRBn7I

At -O3, all config (clang/gcc & libstdc++/libc++) give same result. But will less optimization the loop is faster.
Should I roll back the implementation ?

}

/// Euclidean norm.
Expand Down Expand Up @@ -583,10 +580,7 @@ class Vec
/// sum of all elements of the vector
constexpr ValueType sum() const noexcept
{
ValueType sum = ValueType(0.0);
for (Size i=0; i<N; i++)
sum += this->elems[i];
return sum;
return std::accumulate(elems.begin(), elems.end(), ValueType{});
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}


Expand All @@ -597,8 +591,8 @@ class Vec
{
if constexpr (std::is_floating_point_v<ValueType>)
{
return std::equal(this->elems.begin(), this->elems.end(), b.elems.begin(),
[](auto x, auto y) { return std::abs(x - y) < EQUALITY_THRESHOLD; });
constexpr auto equalTest = [](auto x, auto y) { return std::abs(x - y) < EQUALITY_THRESHOLD; };
return std::equal(this->elems.begin(), this->elems.end(), b.elems.begin(), equalTest );
}
else
{
Expand Down Expand Up @@ -680,19 +674,19 @@ class Vec
return elems.end();
}

constexpr reference front()
constexpr reference front() noexcept
{
return elems[0];
}
constexpr const_reference front() const
constexpr const_reference front() const noexcept
{
return elems[0];
}
constexpr reference back()
constexpr reference back() noexcept
{
return elems[N - 1];
}
constexpr const_reference back() const
constexpr const_reference back() const noexcept
{
return elems[N - 1];
}
Expand All @@ -717,7 +711,7 @@ class VecNoInit : public Vec<N,real>
{}

constexpr VecNoInit(Vec<N,real>&& v) noexcept
: Vec<N,real>(v)
: Vec<N,real>(std::move(v))
{}

using Vec<N,real>::Vec;
Expand Down
24 changes: 9 additions & 15 deletions Sofa/framework/Type/src/sofa/type/fixed_array_algorithms.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,25 @@
******************************************************************************/
#pragma once

namespace sofa::type::pairwise
{
#include <algorithm>

/// @brief clamp a single value. This function should be removed when std::clamp will be available
template<class T>
const T& stdclamp( const T& v, const T& lo, const T& hi )
namespace sofa::type::pairwise
{
assert( !(hi < lo) );
return (v < lo) ? lo : (hi < v) ? hi : v;
}

/// @brief clamp all the values of a fixed_array to be within a given interval.
template<class T, class TT=typename T::value_type, size_t TN=T::static_size>
T clamp(const T& in, const TT& minValue, const TT& maxValue)
template<class T, size_t TN=T::static_size>
T clamp(const T& in, const typename T::value_type& minValue, const typename T::value_type& maxValue)
{
T result {};
for(typename T::size_type i=0; i < typename T::size_type(TN); ++i)
{
result[i] = stdclamp(in[i], minValue, maxValue);
result[i] = std::clamp(in[i], minValue, maxValue);
}
return result;
}

/// @brief pairwise add of two fixed_array
template<class T, class TT=typename T::value_type, size_t TN=T::static_size>
template<class T, size_t TN=T::static_size>
constexpr T operator+(const T& l, const T& r)
{
T result {};
Expand All @@ -57,7 +51,7 @@ constexpr T operator+(const T& l, const T& r)
}

/// @brief pairwise subtract of two fixed_array
template<class T, class TT=typename T::value_type, size_t TN=T::static_size>
template<class T, size_t TN=T::static_size>
constexpr T operator-(const T& l, const T& r)
{
T result {};
Expand All @@ -69,7 +63,7 @@ constexpr T operator-(const T& l, const T& r)
}

/// @brief multiply from l the r components.
template<class T, class TT=typename T::value_type, size_t TN=T::static_size>
template<class T, size_t TN=T::static_size>
T operator*(const T& r, const typename T::value_type& f)
{
T result {};
Expand All @@ -81,7 +75,7 @@ T operator*(const T& r, const typename T::value_type& f)
}

/// @brief multiply from l the r components.
template<class T, class TT=typename T::value_type, size_t TN=T::static_size>
template<class T, size_t TN=T::static_size>
T operator/(const T& r, const typename T::value_type& f)
{
T result {};
Expand Down
Loading
Loading