From 9c01fd7d781ba07e6b952287daa493f934e031e9 Mon Sep 17 00:00:00 2001 From: namark <namark@disroot.org> Date: Wed, 16 Sep 2020 02:17:31 +0400 Subject: [PATCH] Renamed binary.hpp to bits.hpp --- source/simple/support.hpp | 2 +- source/simple/support/binary.hpp | 66 +--------------------------- source/simple/support/bits.hpp | 68 +++++++++++++++++++++++++++++ unit_tests/binary.cpp | 2 +- unit_tests/binary_non_intrinsic.cpp | 2 +- 5 files changed, 73 insertions(+), 67 deletions(-) create mode 100644 source/simple/support/bits.hpp diff --git a/source/simple/support.hpp b/source/simple/support.hpp index e8b6ac4..5fc0b49 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 014302d..feddb42 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 0000000..eda1cd8 --- /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 5c0af9b..c88d8a2 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 b2449f0..a995af4 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" -- GitLab