From e1dfa8c62b31661d249f3d0f6a0395b01a2e21f9 Mon Sep 17 00:00:00 2001
From: Nicholas Corgan <n.corgan@gmail.com>
Date: Fri, 7 May 2021 14:10:19 -0500
Subject: blocks: use VOLK popcnt implementations for count_bits

* Changed count_bits64 input parameter type to uint64_t

Signed-off-by: Nicholas Corgan <n.corgan@gmail.com>
---
 gr-blocks/lib/count_bits.cc | 22 ++++++++++------------
 1 file changed, 10 insertions(+), 12 deletions(-)

(limited to 'gr-blocks/lib/count_bits.cc')

diff --git a/gr-blocks/lib/count_bits.cc b/gr-blocks/lib/count_bits.cc
index b0953428d4..f60f71ad05 100644
--- a/gr-blocks/lib/count_bits.cc
+++ b/gr-blocks/lib/count_bits.cc
@@ -10,11 +10,7 @@
 
 #include <gnuradio/blocks/count_bits.h>
 
-/*
- * these are slow and obvious.  If you need something faster, fix these
- *
- * Can probably replace with VOLK's popcount
- */
+#include <volk/volk.h>
 
 namespace gr {
 namespace blocks {
@@ -45,16 +41,18 @@ unsigned int count_bits16(unsigned int x)
 
 unsigned int count_bits32(unsigned int x)
 {
-    unsigned res = (x & 0x55555555) + ((x >> 1) & 0x55555555);
-    res = (res & 0x33333333) + ((res >> 2) & 0x33333333);
-    res = (res & 0x0F0F0F0F) + ((res >> 4) & 0x0F0F0F0F);
-    res = (res & 0x00FF00FF) + ((res >> 8) & 0x00FF00FF);
-    return (res & 0x0000FFFF) + ((res >> 16) & 0x0000FFFF);
+    unsigned res = 0;
+    volk_32u_popcnt(&res, x);
+
+    return res;
 }
 
-unsigned int count_bits64(unsigned long long x)
+unsigned int count_bits64(uint64_t x)
 {
-    return count_bits32((x >> 32) & 0xffffffff) + count_bits32(x & 0xffffffff);
+    uint64_t res_as_u64 = 0;
+    volk_64u_popcnt(&res_as_u64, x);
+
+    return static_cast<unsigned int>(res_as_u64);
 }
 
 } /* namespace blocks */
-- 
cgit v1.2.3