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