aboutsummaryrefslogtreecommitdiffstats
path: root/chilbert
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2018-08-12 22:27:55 +0200
committerDavid Robillard <d@drobilla.net>2018-09-29 14:46:27 +0200
commitc28ab4461fd7868cff200283cc4cf6aded0f331f (patch)
tree012e9f873a6e5f10192ca9ee1ae8c3c6f859c036 /chilbert
parent50145430b5fb08802bc22a6ae06351a11a091c60 (diff)
downloadchilbert-c28ab4461fd7868cff200283cc4cf6aded0f331f.tar.gz
chilbert-c28ab4461fd7868cff200283cc4cf6aded0f331f.tar.bz2
chilbert-c28ab4461fd7868cff200283cc4cf6aded0f331f.zip
Add pop count operations
Diffstat (limited to 'chilbert')
-rw-r--r--chilbert/BigBitVec.hpp10
-rw-r--r--chilbert/FixBitVec.hpp3
-rw-r--r--chilbert/Operations.hpp18
3 files changed, 31 insertions, 0 deletions
diff --git a/chilbert/BigBitVec.hpp b/chilbert/BigBitVec.hpp
index 83b8a5b..8f8dc6d 100644
--- a/chilbert/BigBitVec.hpp
+++ b/chilbert/BigBitVec.hpp
@@ -422,6 +422,16 @@ public:
return 0;
}
+ /// Return the number of set bits
+ size_t count() const
+ {
+ size_t c = 0;
+ for (size_t i = 0; i < m_iRacks; ++i) {
+ c += static_cast<size_t>(pop_count(m_pcRacks[i]));
+ }
+ return c;
+ }
+
/// Flip all bits (one's complement)
CBigBitVec& flip()
{
diff --git a/chilbert/FixBitVec.hpp b/chilbert/FixBitVec.hpp
index e47b868..bba25d9 100644
--- a/chilbert/FixBitVec.hpp
+++ b/chilbert/FixBitVec.hpp
@@ -224,6 +224,9 @@ public:
return static_cast<size_t>(chilbert::ffs(m_rack));
}
+ /// Return the number of set bits
+ size_t count() const { return static_cast<size_t>(pop_count(m_rack)); }
+
/// Flip all bits (one's complement)
CFixBitVec& flip()
{
diff --git a/chilbert/Operations.hpp b/chilbert/Operations.hpp
index b2e4344..5781e6c 100644
--- a/chilbert/Operations.hpp
+++ b/chilbert/Operations.hpp
@@ -90,6 +90,24 @@ setBit(T& field, const BitsetIndex<T> index, const bool value)
/// Return 1 + the index of the least significant 1-bit of `field`, or zero
template <typename T>
+int pop_count(const T& field);
+
+template <>
+int
+pop_count<unsigned long>(const unsigned long& field)
+{
+ return __builtin_popcountl(field);
+}
+
+template <>
+int
+pop_count<unsigned long long>(const unsigned long long& field)
+{
+ return __builtin_popcountll(field);
+}
+
+/// Return 1 + the index of the least significant 1-bit of `field`, or zero
+template <typename T>
int ffs(const T field);
template <>