From f3959810cb1097cd7c119595060bcff6e02fd8b3 Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Sun, 7 Jul 2019 16:35:34 +0100 Subject: [PATCH] Optimisation of floating point specialisations of etl::cumulative_moving_average. --- include/etl/cumulative_moving_average.h | 31 +++++++++---------------- include/etl/version.h | 2 +- support/Release notes.txt | 4 ++++ 3 files changed, 16 insertions(+), 21 deletions(-) diff --git a/include/etl/cumulative_moving_average.h b/include/etl/cumulative_moving_average.h index 95e476cb..df90d3d2 100644 --- a/include/etl/cumulative_moving_average.h +++ b/include/etl/cumulative_moving_average.h @@ -202,9 +202,8 @@ namespace etl /// \param initial_value The initial value for the average. //************************************************************************* cumulative_moving_average(const T initial_value) - : samples(T(SAMPLE_SIZE_)), - samples_plus_1(T(SAMPLE_SIZE_ + 1U)), - average(initial_value) + : reciprocal_samples_plus_1(T(1.0) / T(SAMPLE_SIZE_ + 1U)) + , average(initial_value) { } @@ -223,9 +222,7 @@ namespace etl //************************************************************************* void add(const T new_value) { - average *= samples; - average += new_value; - average /= samples_plus_1; + average += (new_value - average) * reciprocal_samples_plus_1; } //************************************************************************* @@ -239,9 +236,8 @@ namespace etl private: - const T samples; ///< The sample size to average over. - const T samples_plus_1; ///< One greater than the sample size. - T average; ///< The current cumulative average. + const T reciprocal_samples_plus_1; ///< Reciprocal of one greater than the sample size. + T average; ///< The current cumulative average. }; //*************************************************************************** @@ -260,9 +256,8 @@ namespace etl /// \param initial_value The initial value for the average. //************************************************************************* cumulative_moving_average(const T initial_value, const size_t sample_size) - : samples(T(sample_size)), - samples_plus_1(T(sample_size + 1U)), - average(initial_value) + : reciprocal_samples_plus_1(T(1.0) / T(sample_size + 1U)) + , average(initial_value) { } @@ -281,8 +276,7 @@ namespace etl //************************************************************************* void set_sample_size(const size_t sample_size) { - samples = T(sample_size); - samples_plus_1 = samples + T(1); + reciprocal_samples_plus_1 = T(1.0) / (T(sample_size) + T(1)); } //************************************************************************* @@ -291,9 +285,7 @@ namespace etl //************************************************************************* void add(const T new_value) { - average *= samples; - average += new_value; - average /= samples_plus_1; + average += (new_value - average) * reciprocal_samples_plus_1; } //************************************************************************* @@ -307,9 +299,8 @@ namespace etl private: - T samples; ///< The sample size to average over. - T samples_plus_1; ///< One greater than the sample size. - T average; ///< The current cumulative average. + T reciprocal_samples_plus_1; ///< Reciprocal of one greater than the sample size. + T average; ///< The current cumulative average. }; } diff --git a/include/etl/version.h b/include/etl/version.h index 92a421e8..3edc3888 100644 --- a/include/etl/version.h +++ b/include/etl/version.h @@ -39,7 +39,7 @@ SOFTWARE. #define ETL_VERSION_MAJOR 14 #define ETL_VERSION_MINOR 28 -#define ETL_VERSION_PATCH 0 +#define ETL_VERSION_PATCH 1 #define ETL_VERSION ETL_STRINGIFY(ETL_VERSION_MAJOR) ETL_STRINGIFY(ETL_VERSION_MINOR) ETL_STRINGIFY(ETL_VERSION_PATCH) #define ETL_VERSION_W ETL_WIDE_STRING(ETL_CONCAT(ETL_CONCAT(ETL_VERSION_MAJOR, ETL_VERSION_MINOR), ETL_VERSION_PATCH)) diff --git a/support/Release notes.txt b/support/Release notes.txt index 671c522a..1db05040 100644 --- a/support/Release notes.txt +++ b/support/Release notes.txt @@ -1,3 +1,7 @@ +=============================================================================== +14.28.1 +Optimisation of floating point specialisations of etl::cumulative_moving_average. + =============================================================================== 14.28.0 Added runtime sample size specialisations to etl::cumulative_moving_average.