diff --git a/sprout/bit/clz.hpp b/sprout/bit/clz.hpp index 376c72c9..19e04a34 100644 --- a/sprout/bit/clz.hpp +++ b/sprout/bit/clz.hpp @@ -8,6 +8,7 @@ #ifndef SPROUT_BIT_CLZ_HPP #define SPROUT_BIT_CLZ_HPP +#include #include #include #include diff --git a/sprout/bit/ctz.hpp b/sprout/bit/ctz.hpp index b4b000c0..12f1e853 100644 --- a/sprout/bit/ctz.hpp +++ b/sprout/bit/ctz.hpp @@ -8,6 +8,7 @@ #ifndef SPROUT_BIT_CTZ_HPP #define SPROUT_BIT_CTZ_HPP +#include #include #include #include diff --git a/sprout/bit/parity.hpp b/sprout/bit/parity.hpp index 7a3985f7..344ab11a 100644 --- a/sprout/bit/parity.hpp +++ b/sprout/bit/parity.hpp @@ -8,10 +8,12 @@ #ifndef SPROUT_BIT_PARITY_HPP #define SPROUT_BIT_PARITY_HPP +#include #include #include #include -#include +//#include +#include namespace sprout { namespace detail { @@ -29,10 +31,62 @@ namespace sprout { return __builtin_parityll(x); } # endif +// template +// inline SPROUT_CONSTEXPR int +// parity(Integral x) { +// return sprout::popcount(x) & 1; +// } + template + inline SPROUT_CONSTEXPR typename std::enable_if< + (N == 1), + Integral + >::type + parity_bytes(Integral x) SPROUT_NOEXCEPT { + return x; + } + template + inline SPROUT_CONSTEXPR typename std::enable_if< + (N > 1), + Integral + >::type + parity_bytes(Integral x) SPROUT_NOEXCEPT { + return sprout::detail::parity_bytes( + x ^ sprout::shlr(x, N / 2 * CHAR_BIT) + ); + } + template + inline SPROUT_CONSTEXPR Integral + parity_bytes(Integral x) SPROUT_NOEXCEPT { + return sprout::detail::parity_bytes(x); + } + template + inline SPROUT_CONSTEXPR Integral + parity_bits4(Integral x) SPROUT_NOEXCEPT { + return x ^ sprout::shlr(x, 4); + } + template + inline SPROUT_CONSTEXPR Integral + parity_bits2(Integral x) SPROUT_NOEXCEPT { + return sprout::detail::parity_bits4( + x ^ sprout::shlr(x, 2) + ); + } + template + inline SPROUT_CONSTEXPR Integral + parity_bits1(Integral x) SPROUT_NOEXCEPT { + return sprout::detail::parity_bits2( + x ^ sprout::shlr(x, 1) + ); + } + template + inline SPROUT_CONSTEXPR Integral + parity_bits(Integral x) SPROUT_NOEXCEPT { + return sprout::detail::parity_bits1(x); + } template inline SPROUT_CONSTEXPR int parity(Integral x) { - return sprout::popcount(x) & 1; + return static_cast(sprout::detail::parity_bytes(sprout::detail::parity_bits(x))) & 1; } } // namespace detail //