This commit is contained in:
veclavtlica
2026-09-11 02:21:21 +03:00
parent 2e890fb5b7
commit adfae10938
17 changed files with 949 additions and 119 deletions
+30
View File
@@ -0,0 +1,30 @@
#ifndef COM_BITS_H
#define COM_BITS_H
#include "def.h"
#include <assert.h>
#include <stdint.h>
/* How many bits needed to represent a given number */
/* Alternitive semntic: nearest upper power of two */
static inline int com_bits_needed(uint32_t v) {
#ifdef COM_DEF_COMPILE_MODERN
/* Use intrinsic, required for fast sqrt impl */
return v == 0 ? 1 : 32 - __builtin_clz(v);
#else
#endif
if (v == 0)
return 1;
v--;
v |= v >> 1;
v |= v >> 2;
v |= v >> 4;
v |= v >> 8;
v |= v >> 16;
v++;
return v;
}
#endif