Blender  V2.93
BLI_bitmap.h
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2012 by Nicholas Bishop
17  * All rights reserved.
18  */
19 
20 #pragma once
21 
26 #include "BLI_utildefines.h"
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31 
32 typedef unsigned int BLI_bitmap;
33 
34 /* warning: the bitmap does not keep track of its own size or check
35  * for out-of-bounds access */
36 
37 /* internal use */
38 /* 2^5 = 32 (bits) */
39 #define _BITMAP_POWER 5
40 /* 0b11111 */
41 #define _BITMAP_MASK 31
42 
43 /* number of blocks needed to hold '_tot' bits */
44 #define _BITMAP_NUM_BLOCKS(_tot) (((_tot) >> _BITMAP_POWER) + 1)
45 
46 /* size (in bytes) used to hold '_tot' bits */
47 #define BLI_BITMAP_SIZE(_tot) ((size_t)(_BITMAP_NUM_BLOCKS(_tot)) * sizeof(BLI_bitmap))
48 
49 /* allocate memory for a bitmap with '_tot' bits; free with MEM_freeN() */
50 #define BLI_BITMAP_NEW(_tot, _alloc_string) \
51  ((BLI_bitmap *)MEM_callocN(BLI_BITMAP_SIZE(_tot), _alloc_string))
52 
53 /* allocate a bitmap on the stack */
54 #define BLI_BITMAP_NEW_ALLOCA(_tot) \
55  ((BLI_bitmap *)memset(alloca(BLI_BITMAP_SIZE(_tot)), 0, BLI_BITMAP_SIZE(_tot)))
56 
57 /* Allocate using given MemArena */
58 #define BLI_BITMAP_NEW_MEMARENA(_mem, _tot) \
59  (CHECK_TYPE_INLINE(_mem, MemArena *), \
60  ((BLI_bitmap *)BLI_memarena_calloc(_mem, BLI_BITMAP_SIZE(_tot))))
61 
62 /* get the value of a single bit at '_index' */
63 #define BLI_BITMAP_TEST(_bitmap, _index) \
64  (CHECK_TYPE_ANY(_bitmap, BLI_bitmap *, const BLI_bitmap *), \
65  ((_bitmap)[(_index) >> _BITMAP_POWER] & (1u << ((_index)&_BITMAP_MASK))))
66 
67 #define BLI_BITMAP_TEST_AND_SET_ATOMIC(_bitmap, _index) \
68  (CHECK_TYPE_ANY(_bitmap, BLI_bitmap *, const BLI_bitmap *), \
69  (atomic_fetch_and_or_uint32((uint32_t *)&(_bitmap)[(_index) >> _BITMAP_POWER], \
70  (1u << ((_index)&_BITMAP_MASK))) & \
71  (1u << ((_index)&_BITMAP_MASK))))
72 
73 #define BLI_BITMAP_TEST_BOOL(_bitmap, _index) \
74  (CHECK_TYPE_ANY(_bitmap, BLI_bitmap *, const BLI_bitmap *), \
75  (BLI_BITMAP_TEST(_bitmap, _index) != 0))
76 
77 /* set the value of a single bit at '_index' */
78 #define BLI_BITMAP_ENABLE(_bitmap, _index) \
79  (CHECK_TYPE_ANY(_bitmap, BLI_bitmap *, const BLI_bitmap *), \
80  ((_bitmap)[(_index) >> _BITMAP_POWER] |= (1u << ((_index)&_BITMAP_MASK))))
81 
82 /* clear the value of a single bit at '_index' */
83 #define BLI_BITMAP_DISABLE(_bitmap, _index) \
84  (CHECK_TYPE_ANY(_bitmap, BLI_bitmap *, const BLI_bitmap *), \
85  ((_bitmap)[(_index) >> _BITMAP_POWER] &= ~(1u << ((_index)&_BITMAP_MASK))))
86 
87 /* flip the value of a single bit at '_index' */
88 #define BLI_BITMAP_FLIP(_bitmap, _index) \
89  (CHECK_TYPE_ANY(_bitmap, BLI_bitmap *, const BLI_bitmap *), \
90  ((_bitmap)[(_index) >> _BITMAP_POWER] ^= (1u << ((_index)&_BITMAP_MASK))))
91 
92 /* set or clear the value of a single bit at '_index' */
93 #define BLI_BITMAP_SET(_bitmap, _index, _set) \
94  { \
95  CHECK_TYPE(_bitmap, BLI_bitmap *); \
96  if (_set) { \
97  BLI_BITMAP_ENABLE(_bitmap, _index); \
98  } \
99  else { \
100  BLI_BITMAP_DISABLE(_bitmap, _index); \
101  } \
102  } \
103  (void)0
104 
105 /* resize bitmap to have space for '_tot' bits */
106 #define BLI_BITMAP_RESIZE(_bitmap, _tot) \
107  { \
108  CHECK_TYPE(_bitmap, BLI_bitmap *); \
109  (_bitmap) = MEM_recallocN(_bitmap, BLI_BITMAP_SIZE(_tot)); \
110  } \
111  (void)0
112 
113 void BLI_bitmap_set_all(BLI_bitmap *bitmap, bool set, size_t bits);
114 void BLI_bitmap_flip_all(BLI_bitmap *bitmap, size_t bits);
115 void BLI_bitmap_copy_all(BLI_bitmap *dst, const BLI_bitmap *src, size_t bits);
116 void BLI_bitmap_and_all(BLI_bitmap *dst, const BLI_bitmap *src, size_t bits);
117 void BLI_bitmap_or_all(BLI_bitmap *dst, const BLI_bitmap *src, size_t bits);
118 
119 #ifdef __cplusplus
120 }
121 #endif
void BLI_bitmap_or_all(BLI_bitmap *dst, const BLI_bitmap *src, size_t bits)
Definition: bitmap.c:63
void BLI_bitmap_and_all(BLI_bitmap *dst, const BLI_bitmap *src, size_t bits)
Definition: bitmap.c:54
void BLI_bitmap_copy_all(BLI_bitmap *dst, const BLI_bitmap *src, size_t bits)
Definition: bitmap.c:48
void BLI_bitmap_set_all(BLI_bitmap *bitmap, bool set, size_t bits)
Definition: bitmap.c:33
void BLI_bitmap_flip_all(BLI_bitmap *bitmap, size_t bits)
Definition: bitmap.c:39
unsigned int BLI_bitmap
Definition: BLI_bitmap.h:32