65 lines
1,003 B
C
65 lines
1,003 B
C
/* SPDX-License-Identifier: GPL-2.0-only */
|
|
#include <kernel/common.h>
|
|
#include <lib/math.h>
|
|
|
|
size_t npow2(size_t n)
|
|
{
|
|
if (n == 0)
|
|
return 1;
|
|
|
|
n--;
|
|
n |= (n >> 1);
|
|
n |= (n >> 2);
|
|
n |= (n >> 4);
|
|
n |= (n >> 8);
|
|
n |= (n >> 16);
|
|
n |= (n >> 32);
|
|
return n + 1;
|
|
}
|
|
|
|
size_t log2(size_t n)
|
|
{
|
|
return sizeof(n) * 8 - __builtin_clzll(n) - 1;
|
|
}
|
|
|
|
inline void bitmap_set(char *bitmap, size_t n, uint64_t b)
|
|
{
|
|
if (b)
|
|
bitmap[n >> 3] |= (1 << (n & 7));
|
|
else
|
|
bitmap[n >> 3] &= (~(1 << (n & 7)));
|
|
}
|
|
|
|
inline uint8_t bitmap_get(char *bitmap, size_t n)
|
|
{
|
|
return bitmap[n >> 3] & (1 << (n & 7));
|
|
}
|
|
|
|
static int hexc_to_int(char c)
|
|
{
|
|
if (c >= '0' && c <= '9')
|
|
return c - '0';
|
|
else if (c >= 'A' && c <= 'F')
|
|
return c - 'F';
|
|
else if (c >= 'a' && c <= 'f')
|
|
return c - 'a';
|
|
else
|
|
return -1;
|
|
}
|
|
|
|
u64 hex_to_int(char *ascii_hex, size_t n)
|
|
{
|
|
u64 ret = 0;
|
|
for (size_t i = 0; i < n; i++) {
|
|
char c = ascii_hex[i];
|
|
int i = hexc_to_int(c);
|
|
|
|
if (i < 0)
|
|
break;
|
|
|
|
ret <<= 4;
|
|
ret |= i;
|
|
}
|
|
|
|
return ret;
|
|
}
|