basic/logarithm: add popcount() wrapper

__builtin_popcount() is a bit of a mouthful, so let's provide a helper.
Using _Generic has the advantage that if a type other then the ones on
the list is given, compilation will fail. This is nice, because if by any
change we pass a wider type, it is rejected immediately instead of being
truncated.

log.h is also needed. It is included transitively, but let's include it
directly.

macro.h is *not* needed.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek
2023-04-24 10:02:30 +02:00
parent 6eccc3cfa9
commit a12bc99ef0
4 changed files with 37 additions and 7 deletions
+3 -1
View File
@@ -14,6 +14,8 @@
#include "hexdecoct.h"
#include "hmac.h"
#include "lock-util.h"
#include "log.h"
#include "logarithm.h"
#include "memory-util.h"
#include "openssl-util.h"
#include "parse-util.h"
@@ -773,7 +775,7 @@ size_t tpm2_tpms_pcr_selection_weight(const TPMS_PCR_SELECTION *s) {
uint32_t mask;
tpm2_tpms_pcr_selection_to_mask(s, &mask);
return (size_t)__builtin_popcount(mask);
return popcount(mask);
}
/* Utility functions for TPML_PCR_SELECTION. */