Blender  V3.3
BLI_bitmap.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2012 by Nicholas Bishop. All rights reserved. */
3 
4 #pragma once
5 
10 #include "BLI_utildefines.h"
11 
12 #ifdef __cplusplus
13 extern "C" {
14 #endif
15 
16 typedef unsigned int BLI_bitmap;
17 
18 /* WARNING: the bitmap does not keep track of its own size or check
19  * for out-of-bounds access */
20 
21 /* internal use */
22 /* 2^5 = 32 (bits) */
23 #define _BITMAP_POWER 5
24 /* 0b11111 */
25 #define _BITMAP_MASK 31
26 
30 #define _BITMAP_NUM_BLOCKS(_num) (((_num) + _BITMAP_MASK) >> _BITMAP_POWER)
31 
35 #define BLI_BITMAP_SIZE(_num) ((size_t)(_BITMAP_NUM_BLOCKS(_num)) * sizeof(BLI_bitmap))
36 
40 #define BLI_BITMAP_NEW(_num, _alloc_string) \
41  ((BLI_bitmap *)MEM_callocN(BLI_BITMAP_SIZE(_num), _alloc_string))
42 
46 #define BLI_BITMAP_NEW_ALLOCA(_num) \
47  ((BLI_bitmap *)memset(alloca(BLI_BITMAP_SIZE(_num)), 0, BLI_BITMAP_SIZE(_num)))
48 
52 #define BLI_BITMAP_NEW_MEMARENA(_mem, _num) \
53  (CHECK_TYPE_INLINE(_mem, MemArena *), \
54  ((BLI_bitmap *)BLI_memarena_calloc(_mem, BLI_BITMAP_SIZE(_num))))
55 
59 #define BLI_BITMAP_DECLARE(_name, _num) BLI_bitmap _name[_BITMAP_NUM_BLOCKS(_num)] = {}
60 
64 #define BLI_BITMAP_TEST(_bitmap, _index) \
65  (CHECK_TYPE_ANY(_bitmap, BLI_bitmap *, const BLI_bitmap *), \
66  ((_bitmap)[(_index) >> _BITMAP_POWER] & (1u << ((_index)&_BITMAP_MASK))))
67 
68 #define BLI_BITMAP_TEST_AND_SET_ATOMIC(_bitmap, _index) \
69  (CHECK_TYPE_ANY(_bitmap, BLI_bitmap *, const BLI_bitmap *), \
70  (atomic_fetch_and_or_uint32((uint32_t *)&(_bitmap)[(_index) >> _BITMAP_POWER], \
71  (1u << ((_index)&_BITMAP_MASK))) & \
72  (1u << ((_index)&_BITMAP_MASK))))
73 
74 #define BLI_BITMAP_TEST_BOOL(_bitmap, _index) \
75  (CHECK_TYPE_ANY(_bitmap, BLI_bitmap *, const BLI_bitmap *), \
76  (BLI_BITMAP_TEST(_bitmap, _index) != 0))
77 
81 #define BLI_BITMAP_ENABLE(_bitmap, _index) \
82  (CHECK_TYPE_ANY(_bitmap, BLI_bitmap *, const BLI_bitmap *), \
83  ((_bitmap)[(_index) >> _BITMAP_POWER] |= (1u << ((_index)&_BITMAP_MASK))))
84 
88 #define BLI_BITMAP_DISABLE(_bitmap, _index) \
89  (CHECK_TYPE_ANY(_bitmap, BLI_bitmap *, const BLI_bitmap *), \
90  ((_bitmap)[(_index) >> _BITMAP_POWER] &= ~(1u << ((_index)&_BITMAP_MASK))))
91 
95 #define BLI_BITMAP_FLIP(_bitmap, _index) \
96  (CHECK_TYPE_ANY(_bitmap, BLI_bitmap *, const BLI_bitmap *), \
97  ((_bitmap)[(_index) >> _BITMAP_POWER] ^= (1u << ((_index)&_BITMAP_MASK))))
98 
102 #define BLI_BITMAP_SET(_bitmap, _index, _set) \
103  { \
104  CHECK_TYPE(_bitmap, BLI_bitmap *); \
105  if (_set) { \
106  BLI_BITMAP_ENABLE(_bitmap, _index); \
107  } \
108  else { \
109  BLI_BITMAP_DISABLE(_bitmap, _index); \
110  } \
111  } \
112  (void)0
113 
117 #define BLI_BITMAP_RESIZE(_bitmap, _num) \
118  { \
119  CHECK_TYPE(_bitmap, BLI_bitmap *); \
120  (_bitmap) = MEM_recallocN(_bitmap, BLI_BITMAP_SIZE(_num)); \
121  } \
122  (void)0
123 
127 void BLI_bitmap_set_all(BLI_bitmap *bitmap, bool set, size_t bits);
131 void BLI_bitmap_flip_all(BLI_bitmap *bitmap, size_t bits);
135 void BLI_bitmap_copy_all(BLI_bitmap *dst, const BLI_bitmap *src, size_t bits);
139 void BLI_bitmap_and_all(BLI_bitmap *dst, const BLI_bitmap *src, size_t bits);
143 void BLI_bitmap_or_all(BLI_bitmap *dst, const BLI_bitmap *src, size_t bits);
144 
149 int BLI_bitmap_find_first_unset(const BLI_bitmap *bitmap, size_t bits);
150 
151 #ifdef __cplusplus
152 }
153 #endif
void BLI_bitmap_or_all(BLI_bitmap *dst, const BLI_bitmap *src, size_t bits)
Definition: bitmap.c:43
void BLI_bitmap_and_all(BLI_bitmap *dst, const BLI_bitmap *src, size_t bits)
Definition: bitmap.c:35
void BLI_bitmap_copy_all(BLI_bitmap *dst, const BLI_bitmap *src, size_t bits)
Definition: bitmap.c:30
int BLI_bitmap_find_first_unset(const BLI_bitmap *bitmap, size_t bits)
Definition: bitmap.c:51
void BLI_bitmap_set_all(BLI_bitmap *bitmap, bool set, size_t bits)
Definition: bitmap.c:17
void BLI_bitmap_flip_all(BLI_bitmap *bitmap, size_t bits)
Definition: bitmap.c:22
unsigned int BLI_bitmap
Definition: BLI_bitmap.h:16
SyclQueue void void * src