0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-11-12 04:51:08 +01:00
construct/include/ircd/math/mean.h

75 lines
1.5 KiB
C
Raw Normal View History

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
{
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
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
}
namespace ircd
{
using math::mean;
}
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
{
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
{
const R dp
(
simd::lane_cast<R>(block)
);
2021-03-02 18:58:24 +01:00
acc += dp;
});
2021-03-02 18:58:24 +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;
}
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
{
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;
}