2021-03-02 18:58:24 +01:00
|
|
|
// Matrix Construct
|
|
|
|
//
|
|
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
|
|
|
// Copyright (C) 2016-2021 Jason Volk <jason@zemos.net>
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice is present in all copies. The
|
|
|
|
// full license for this software is available in the LICENSE file.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
#define HAVE_IRCD_MATH_MEAN_H
|
|
|
|
|
|
|
|
namespace ircd::math
|
|
|
|
{
|
2021-03-16 04:37:31 +01:00
|
|
|
template<class T,
|
|
|
|
class R = T>
|
|
|
|
typename std::enable_if<!simd::is<T>(), R>::type
|
|
|
|
mean(const vector_view<const T>);
|
2021-03-02 18:58:24 +01:00
|
|
|
|
2021-03-16 04:37:31 +01:00
|
|
|
template<class T,
|
|
|
|
class R = T>
|
|
|
|
typename std::enable_if<simd::is<T>(), simd::lane_type<R>>::type
|
|
|
|
mean(const vector_view<const T>);
|
2021-03-02 18:58:24 +01:00
|
|
|
}
|
|
|
|
|
2021-03-16 21:31:53 +01:00
|
|
|
namespace ircd
|
|
|
|
{
|
|
|
|
using math::mean;
|
|
|
|
}
|
|
|
|
|
2021-03-16 04:37:31 +01:00
|
|
|
template<class T,
|
|
|
|
class R>
|
|
|
|
inline typename std::enable_if<ircd::simd::is<T>(), ircd::simd::lane_type<R>>::type
|
|
|
|
ircd::math::mean(const vector_view<const T> a)
|
2021-03-02 18:58:24 +01:00
|
|
|
{
|
2021-03-16 04:37:31 +01:00
|
|
|
R acc {0};
|
|
|
|
simd::for_each(a.data(), u64x2{0, a.size()}, [&acc]
|
|
|
|
(const auto block, const auto mask)
|
2021-03-02 18:58:24 +01:00
|
|
|
{
|
2021-03-16 04:37:31 +01:00
|
|
|
const R dp
|
|
|
|
(
|
|
|
|
simd::lane_cast<R>(block)
|
|
|
|
);
|
2021-03-02 18:58:24 +01:00
|
|
|
|
2021-03-16 04:37:31 +01:00
|
|
|
acc += dp;
|
|
|
|
});
|
2021-03-02 18:58:24 +01:00
|
|
|
|
2021-03-16 04:37:31 +01:00
|
|
|
auto num(acc[0]);
|
|
|
|
for(uint i(1); i < simd::lanes<T>(); ++i)
|
|
|
|
num += acc[i];
|
|
|
|
|
|
|
|
const auto den
|
2021-03-02 18:58:24 +01:00
|
|
|
{
|
|
|
|
a.size() * simd::lanes<T>()
|
|
|
|
};
|
|
|
|
|
|
|
|
num /= den;
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
2021-03-16 04:37:31 +01:00
|
|
|
template<class T,
|
|
|
|
class R>
|
|
|
|
inline typename std::enable_if<!ircd::simd::is<T>(), R>::type
|
|
|
|
ircd::math::mean(const vector_view<const T> a)
|
2021-03-02 18:58:24 +01:00
|
|
|
{
|
2021-03-16 04:37:31 +01:00
|
|
|
R ret{0};
|
2021-03-02 18:58:24 +01:00
|
|
|
size_t i{0};
|
|
|
|
while(i < a.size())
|
|
|
|
ret += a[i++];
|
|
|
|
|
|
|
|
ret /= i;
|
|
|
|
return ret;
|
|
|
|
}
|