From 8a10ca93e5341bd6d5d95d108518c7b8af358d66 Mon Sep 17 00:00:00 2001 From: Michael Fabian 'Xaymar' Dirks Date: Tue, 8 Jun 2021 07:54:41 +0200 Subject: [PATCH] util: Add Pascal Triangle and integer power --- source/util/utility.hpp | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/source/util/utility.hpp b/source/util/utility.hpp index 19c3b07a..fdb39db8 100644 --- a/source/util/utility.hpp +++ b/source/util/utility.hpp @@ -22,6 +22,7 @@ #include #include #include +#include extern "C" { #ifdef _MSC_VER @@ -105,6 +106,19 @@ namespace streamfx::util { std::pair size_from_string(std::string text, bool allowSquare = true); namespace math { + template + inline T pow(T base, T exp) + { + T res = 1; + while (exp) { + if (exp & 1) + res *= base; + exp >>= 1; + base *= base; + } + return res; + } + // Proven by tests to be the fastest implementation on Intel and AMD CPUs. // Ranking: log10, loop < bitscan < pow // loop and log10 trade blows, usually almost identical. @@ -167,6 +181,18 @@ namespace streamfx::util { && (target < (value + std::numeric_limits::epsilon())); } + template + inline std::vector pascal_triangle(size_t n) + { + std::vector line; + line.push_back(1); + for (uint64_t k = 0; k < n; k++) { + T v = static_cast(line.at(k) * static_cast(n - k) / static_cast(k + 1)); + line.push_back(v); + } + return line; + } + template inline T gaussian(T x, T o /*, T u = 0*/) {