bitmap 2: bit_set, bit_get, bit_test, bit_sub2_up, bitmap, bitmap_set, bitmap_get
This commit is contained in:
parent
a3da87ad75
commit
0820443a7d
4 changed files with 47 additions and 2 deletions
|
|
@ -16,6 +16,7 @@ set(SOURCES
|
|||
bitmap.c
|
||||
)
|
||||
|
||||
add_compile_definitions(CU_64BIT)
|
||||
add_library(cutil STATIC ${SOURCES} ${HEADERS})
|
||||
|
||||
add_executable(test test.c)
|
||||
|
|
|
|||
12
bitmap.h
12
bitmap.h
|
|
@ -3,7 +3,17 @@
|
|||
|
||||
#include "cutypes.h"
|
||||
|
||||
#define bit_align2(v, p) (((v>>p)+!!(v&((1<<p)-1)))<<p)
|
||||
#define bit_set(v, p, b) (v)=(b?(v)|(1<<(p)):(v)^(1<<(p)))
|
||||
#define bit_get(v, p) ((v>>(p))&1)
|
||||
#define bit_test(v, p) (!!(v&(1<<(p))))
|
||||
#define bit_sub2_up(v, p) ((v>>(p))+!!(v&((1<<(p))-1)))
|
||||
#define bit_align2(v, p) (bit_sub2_up(v, (p))<<(p))
|
||||
uint bit_log2(uint64_t value);
|
||||
|
||||
#define bitmap(name, s) uword name[bit_sub2_up(s, CU_WORD_POW2)] = {0}
|
||||
#define bitmap_set(m, p, b) \
|
||||
bit_set((m)[p>>CU_WORD_POW2], p&((1<<CU_WORD_POW2)-1), b)
|
||||
#define bitmap_get(m, p) \
|
||||
bit_get((m)[p>>CU_WORD_POW2], p&((1<<CU_WORD_POW2)-1))
|
||||
|
||||
#endif
|
||||
20
cutypes.h
20
cutypes.h
|
|
@ -4,7 +4,25 @@
|
|||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
typedef unsigned int uint;
|
||||
#if (!defined(CU_64BIT) && !defined(CU_32BIT))
|
||||
# define CU_64BIT
|
||||
#endif
|
||||
|
||||
#if defined(CU_64BIT)
|
||||
#define CU_WORD_BITS 64
|
||||
#define CU_WORD_POW2 6
|
||||
typedef uint64_t uword;
|
||||
typedef int64_t iword;
|
||||
#elif defined(CU_32BIT)
|
||||
#define CU_WORD_BITS 32
|
||||
#define CU_WORD_POW2 5
|
||||
typedef uint32_t uword;
|
||||
typedef int32_t iword;
|
||||
#else
|
||||
# warning "define CU_32BIT or CU_64BIT"
|
||||
#endif
|
||||
|
||||
typedef uint32_t uint;
|
||||
#define CU_UINT_SIZE sizeof(uint)
|
||||
|
||||
typedef void* cu_ptr;
|
||||
|
|
|
|||
16
test.c
16
test.c
|
|
@ -52,8 +52,24 @@ int main()
|
|||
array.size, array.count, array.align);
|
||||
array_clear(&array);
|
||||
|
||||
printf("[cutypes]\n");
|
||||
printf("uword\t%u\n", sizeof(uword) * 8);
|
||||
printf("uint\t%u\n", sizeof(uint) * 8);
|
||||
|
||||
printf("[bitmap]\n");
|
||||
printf("bit_align2\t%u\n", bit_align2(354, 3));
|
||||
printf("bit_log2\t%u\n", bit_log2(34));
|
||||
num = 5;
|
||||
bit_set(num, 1, 1);
|
||||
printf("bit_get\t%u\n", bit_get(num, 0));
|
||||
printf("num\t%u\n", num);
|
||||
bitmap(m, 128);
|
||||
bitmap_set(m, 0, 1);
|
||||
bitmap_set(m, 5, 1);
|
||||
bitmap_set(m, 64, 1);
|
||||
printf("bitmap\t%u\t%u\n", m[0], m[1]);
|
||||
printf("bitmap\t%u\t%u\n", bitmap_get(m, 0), bitmap_get(m, 64));
|
||||
bitmap_set(m, 5, 0);
|
||||
printf("bitmap\t%u\n", bitmap_get(m, 5));
|
||||
return 0;
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue