From 4423a883e94eb6aecce58520c93df02c5b570977 Mon Sep 17 00:00:00 2001 From: Ludovic 'Archivist' Lagouardette Date: Mon, 5 Oct 2020 07:37:24 +0200 Subject: [PATCH] extended integer math with log2(uint8_t) --- include/gp/math/integer_math.hpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/include/gp/math/integer_math.hpp b/include/gp/math/integer_math.hpp index ea3e979..b1c8a73 100644 --- a/include/gp/math/integer_math.hpp +++ b/include/gp/math/integer_math.hpp @@ -13,6 +13,21 @@ namespace gp { Sean Eron Anderson seander@cs.stanford.edu **/ + template<> + constexpr size_t log2(uint8_t v) + { + constexpr int MultiplyDeBruijnBitPosition[8] = + { + 0, 5, 1, 6, 4, 3, 2, 7 + }; + + v |= v >> 1; + v |= v >> 2; + v |= v >> 4; + + return MultiplyDeBruijnBitPosition[(v * 29U) >> 5]; + } + template<> constexpr size_t log2(uint32_t v) {