diff --git a/source/simple/support.hpp b/source/simple/support.hpp index e8b6ac47228f7a44932a33e7f293e353da767bec..5fc0b4902c759914eb297ec09ebe9a903b5e0f16 100644 --- a/source/simple/support.hpp +++ b/source/simple/support.hpp @@ -3,7 +3,7 @@ #include "support/array.hpp" #include "support/array_operators.hpp" #include "support/array_utils.hpp" -#include "support/binary.hpp" +#include "support/bits.hpp" #include "support/carcdr.hpp" #include "support/enum_flags_operators.hpp" #include "support/enum.hpp" diff --git a/source/simple/support/binary.hpp b/source/simple/support/binary.hpp index 014302dd3cfd4a8dd3b0a4c17f935c5e94e6396d..feddb42f8992d2b6361ce2610eb9c9cf9fb84b1d 100644 --- a/source/simple/support/binary.hpp +++ b/source/simple/support/binary.hpp @@ -1,67 +1,5 @@ #ifndef SIMPLE_SUPPORT_BINARY_HPP #define SIMPLE_SUPPORT_BINARY_HPP - -#include <type_traits> -#include <cassert> -#include <climits> - -#if !defined __GNUC__ -#define SIMPLE_SUPPORT_BINARY_DISABLE_INTRINSICS -#endif - -#if defined SIMPLE_SUPPORT_BINARY_DISABLE_INTRINSICS -#include <bitset> -#endif - -namespace simple { namespace support -{ - - template <typename Int, std::enable_if_t<std::is_integral_v<Int>>* = nullptr> - constexpr int count_trailing_zeros(Int in) noexcept - { - assert(in && "Input must not be zero."); - constexpr auto size = sizeof(Int); -#if !defined SIMPLE_SUPPORT_BINARY_DISABLE_INTRINSICS - if constexpr (size == sizeof(unsigned int)) - return __builtin_ctz(in); - if constexpr (size == sizeof(unsigned long)) - return __builtin_ctzl(in); - else - return __builtin_ctzll(in); -#else - const std::bitset<size * CHAR_BIT> bin(in); - int count = -1; - while(!bin[++count]); - return count; -#endif - } - - template <typename Int, std::enable_if_t<std::is_integral_v<Int>>* = nullptr> - constexpr int count_ones(Int in) noexcept - { - constexpr auto size = sizeof(Int); -#if !defined SIMPLE_SUPPORT_BINARY_DISABLE_INTRINSICS - if constexpr (size == sizeof(unsigned int)) - return __builtin_popcount(in); - if constexpr (size == sizeof(unsigned long)) - return __builtin_popcountl(in); - else - return __builtin_popcountll(in); -#else - const std::bitset<size * CHAR_BIT> bin(in); - int count = 0; - for(size_t i = 0; i < bin.size() ; ++i) - count += bin[i]; - return count; -#endif - } - - template <typename T> - constexpr std::size_t bit_count(const T&) noexcept - { - return sizeof(T) * CHAR_BIT; - } - -}} // namespace simple::support - +#warning simple/support/binary.hpp is deprecated, use bits.hpp instead +#include "bits.hpp" #endif /* end of include guard */ diff --git a/source/simple/support/bits.hpp b/source/simple/support/bits.hpp new file mode 100644 index 0000000000000000000000000000000000000000..eda1cd8d90bacc08cdcdba6d8b5c6dd9aa712754 --- /dev/null +++ b/source/simple/support/bits.hpp @@ -0,0 +1,68 @@ +#ifndef SIMPLE_SUPPORT_BITS_HPP +#define SIMPLE_SUPPORT_BITS_HPP + +#include <type_traits> +#include <cassert> +#include <climits> + +#if !defined __GNUC__ +#define SIMPLE_SUPPORT_BITS_DISABLE_INTRINSICS +#endif + +#if defined SIMPLE_SUPPORT_BITS_DISABLE_INTRINSICS +#include <bitset> +#endif + +namespace simple { namespace support +{ + + template <typename Int, std::enable_if_t<std::is_integral_v<Int>>* = nullptr> + constexpr int count_trailing_zeros(Int in) noexcept + { + assert(in && "Input must not be zero."); + constexpr auto size = sizeof(Int); +#if !defined SIMPLE_SUPPORT_BITS_DISABLE_INTRINSICS + if constexpr (size == sizeof(unsigned int)) + return __builtin_ctz(in); + if constexpr (size == sizeof(unsigned long)) + return __builtin_ctzl(in); + else + return __builtin_ctzll(in); +#else + const std::bitset<size * CHAR_BIT> bin(in); + int count = -1; + while(!bin[++count]); + return count; +#endif + } + + template <typename Int, std::enable_if_t<std::is_integral_v<Int>>* = nullptr> + constexpr int count_ones(Int in) noexcept + { + constexpr auto size = sizeof(Int); +#if !defined SIMPLE_SUPPORT_BITS_DISABLE_INTRINSICS + if constexpr (size == sizeof(unsigned int)) + return __builtin_popcount(in); + if constexpr (size == sizeof(unsigned long)) + return __builtin_popcountl(in); + else + return __builtin_popcountll(in); +#else + const std::bitset<size * CHAR_BIT> bin(in); + int count = 0; + for(size_t i = 0; i < bin.size() ; ++i) + count += bin[i]; + return count; +#endif + } + + template <typename T> + constexpr std::size_t bit_count(const T&) noexcept + { + return sizeof(T) * CHAR_BIT; + } + +}} // namespace simple::support + + +#endif /* end of include guard */ diff --git a/unit_tests/binary.cpp b/unit_tests/binary.cpp index 5c0af9b52860b4dec6f7e9c52b0ad9a6243f0b9f..c88d8a297185424d609e75ad95b57efa6ad6b6f3 100644 --- a/unit_tests/binary.cpp +++ b/unit_tests/binary.cpp @@ -1,4 +1,4 @@ -#include "simple/support/binary.hpp" +#include "simple/support/bits.hpp" #include <climits> using namespace simple::support; diff --git a/unit_tests/binary_non_intrinsic.cpp b/unit_tests/binary_non_intrinsic.cpp index b2449f07fd7fb6111f564077af789db0d6d855cf..a995af41e402a44e016c023da984df9ae2fcc577 100644 --- a/unit_tests/binary_non_intrinsic.cpp +++ b/unit_tests/binary_non_intrinsic.cpp @@ -1,2 +1,2 @@ -#define SIMPLE_SUPPORT_BINARY_DISABLE_INTRINSICS +#define SIMPLE_SUPPORT_BITS_DISABLE_INTRINSICS #include "binary.cpp"