diff options
author | Cyrille Bagard <nocbos@gmail.com> | 2018-06-19 20:23:30 (GMT) |
---|---|---|
committer | Cyrille Bagard <nocbos@gmail.com> | 2018-06-19 20:27:43 (GMT) |
commit | 72b1c890ed19ebd7780e634d4874d12a619f259e (patch) | |
tree | 0d2e2f5e76ba9102a6671bdb2f7ab6b2e14c8d9c /src/common/bits.c | |
parent | ceeba88cafc4c7d2c625e53fb175b763e480f6ba (diff) |
Extended the bitfield operations and their Python bindings.
Diffstat (limited to 'src/common/bits.c')
-rw-r--r-- | src/common/bits.c | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/src/common/bits.c b/src/common/bits.c index 5533293..cdcef85 100644 --- a/src/common/bits.c +++ b/src/common/bits.c @@ -30,6 +30,9 @@ #include <string.h> +#include "asm.h" + + /* Champ de bits simple */ struct _bitfield_t @@ -549,3 +552,50 @@ bool test_all_in_bit_field(const bitfield_t *field, size_t first, size_t count) return result; } + + +/****************************************************************************** +* * +* Paramètres : field = champ de bits à consulter. * +* * +* Description : Détermine le nombre de bits à 1 dans un champ. * +* * +* Retour : Valeur positive ou nulle. * +* * +* Remarques : - * +* * +******************************************************************************/ + +size_t popcount_for_bit_field(const bitfield_t *field) +{ + size_t result; /* Quantité à renvoyer */ + size_t remaining; /* Nombre de bits restants */ + size_t i; /* Boucle de parcours */ + unsigned long value; /* Valeur masquée à traiter */ + + result = 0; + + remaining = field->length; + + for (i = 0; i < field->requested; i++) + { + value = field->bits[i]; + + if (remaining < (8 * sizeof(unsigned long))) + value &= 1 << (remaining - 1); + +#if __WORDSIZE == 64 + result += popcount_64(value); +#elif __WORDSIZE == 32 + result += popcount_32(value); +#else +# error "Unkown word size" +#endif + + remaining -= 8 * sizeof(unsigned long); + + } + + return result; + +} |