From 3a5fddbf9740e37e7008b05b86fab976af9481e6 Mon Sep 17 00:00:00 2001 From: f4exb Date: Sat, 27 Mar 2021 05:34:05 +0100 Subject: [PATCH] DATV: leandvb: use bitset to calculate Hamming weight of 32 and 64 bit integers --- plugins/channelrx/demoddatv/leansdr/math.cpp | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/plugins/channelrx/demoddatv/leansdr/math.cpp b/plugins/channelrx/demoddatv/leansdr/math.cpp index afb21be40..60b2e1f17 100644 --- a/plugins/channelrx/demoddatv/leansdr/math.cpp +++ b/plugins/channelrx/demoddatv/leansdr/math.cpp @@ -1,4 +1,5 @@ #include "math.h" +#include namespace leansdr { @@ -16,12 +17,20 @@ int hamming_weight(uint16_t x) int hamming_weight(uint32_t x) { - return hamming_weight((uint16_t)x) + hamming_weight((uint16_t)(x >> 16)); + return std::bitset<32>(x).count(); + // x = x - ((x >> 1) & 0x55555555); + // x = (x & 0x33333333) + ((x >> 2) & 0x33333333); + // return (((x + (x >> 4)) & 0x0F0F0F0F) * 0x01010101) >> 24; +// return hamming_weight((uint16_t)x) + hamming_weight((uint16_t)(x >> 16)); } int hamming_weight(uint64_t x) { - return hamming_weight((uint32_t)x) + hamming_weight((uint32_t)(x >> 32)); + return std::bitset<64>(x).count(); + // x = x - ((x >> 1) & 0x5555555555555555); + // x = (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333); + // return (((x + (x >> 4)) & 0x0F0F0F0F0F0F0F0F) * 0x0101010101010101) >> 56; +// return hamming_weight((uint32_t)x) + hamming_weight((uint32_t)(x >> 32)); } unsigned char parity(uint8_t x)