diff options
Diffstat (limited to 'core')
-rw-r--r-- | core/typedefs.h | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/core/typedefs.h b/core/typedefs.h index 2472e5fcd9..f0c32f2c95 100644 --- a/core/typedefs.h +++ b/core/typedefs.h @@ -193,6 +193,20 @@ static inline unsigned int nearest_shift(unsigned int p_number) { return 0; } +// constexpr function to find the floored log2 of a number +template <typename T> +constexpr T floor_log2(T x) { + return x < 2 ? x : 1 + floor_log2(x >> 1); +} + +// Get the number of bits needed to represent the number. +// IE, if you pass in 8, you will get 4. +// If you want to know how many bits are needed to store 8 values however, pass in (8 - 1). +template <typename T> +constexpr T get_num_bits(T x) { + return floor_log2(x); +} + // Swap 16, 32 and 64 bits value for endianness. #if defined(__GNUC__) #define BSWAP16(x) __builtin_bswap16(x) |