// Matrix Construct // // Copyright (C) Matrix Construct Developers, Authors & Contributors // Copyright (C) 2016-2021 Jason Volk // // 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 typename std::enable_if(), R>::type mean(const vector_view); template typename std::enable_if(), simd::lane_type>::type mean(const vector_view); } namespace ircd { using math::mean; } template inline typename std::enable_if(), ircd::simd::lane_type>::type ircd::math::mean(const vector_view a) { R acc {0}; simd::for_each(a.data(), u64x2{0, a.size()}, [&acc] (const auto block, const auto mask) { const R dp ( simd::lane_cast(block) ); acc += dp; }); auto num(acc[0]); for(uint i(1); i < simd::lanes(); ++i) num += acc[i]; const auto den { a.size() * simd::lanes() }; num /= den; return num; } template inline typename std::enable_if(), R>::type ircd::math::mean(const vector_view a) { R ret{0}; size_t i{0}; while(i < a.size()) ret += a[i++]; ret /= i; return ret; }