Skip to content

Commit

Permalink
Optimisation of floating point specialisations of etl::cumulative_mov…
Browse files Browse the repository at this point in the history
…ing_average.
  • Loading branch information
jwellbelove committed Jul 7, 2019
1 parent 9fbbb5c commit f395981
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 21 deletions.
31 changes: 11 additions & 20 deletions include/etl/cumulative_moving_average.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
}

Expand All @@ -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;
}

//*************************************************************************
Expand All @@ -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.
};

//***************************************************************************
Expand All @@ -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)
{
}

Expand All @@ -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));
}

//*************************************************************************
Expand All @@ -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;
}

//*************************************************************************
Expand All @@ -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.
};
}

Expand Down
2 changes: 1 addition & 1 deletion include/etl/version.h
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
4 changes: 4 additions & 0 deletions support/Release notes.txt
Original file line number Diff line number Diff line change
@@ -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.
Expand Down

0 comments on commit f395981

Please sign in to comment.