1
0
mirror of https://github.com/f4exb/sdrangel.git synced 2025-07-18 08:45:23 -04:00

DATV: leandvb: use bitset to calculate Hamming weight of 32 and 64 bit integers

This commit is contained in:
f4exb 2021-03-27 05:34:05 +01:00
parent 52d802baec
commit 3a5fddbf97

View File

@ -1,4 +1,5 @@
#include "math.h" #include "math.h"
#include <bitset>
namespace leansdr namespace leansdr
{ {
@ -16,12 +17,20 @@ int hamming_weight(uint16_t x)
int hamming_weight(uint32_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) 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) unsigned char parity(uint8_t x)