Blender  V3.3
bitmap.c
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 
10 #include <limits.h>
11 #include <string.h>
12 
13 #include "BLI_bitmap.h"
14 #include "BLI_math_bits.h"
15 #include "BLI_utildefines.h"
16 
17 void BLI_bitmap_set_all(BLI_bitmap *bitmap, bool set, size_t bits)
18 {
19  memset(bitmap, set ? UCHAR_MAX : 0, BLI_BITMAP_SIZE(bits));
20 }
21 
22 void BLI_bitmap_flip_all(BLI_bitmap *bitmap, size_t bits)
23 {
24  size_t blocks_num = _BITMAP_NUM_BLOCKS(bits);
25  for (size_t i = 0; i < blocks_num; i++) {
26  bitmap[i] ^= ~(BLI_bitmap)0;
27  }
28 }
29 
30 void BLI_bitmap_copy_all(BLI_bitmap *dst, const BLI_bitmap *src, size_t bits)
31 {
32  memcpy(dst, src, BLI_BITMAP_SIZE(bits));
33 }
34 
35 void BLI_bitmap_and_all(BLI_bitmap *dst, const BLI_bitmap *src, size_t bits)
36 {
37  size_t blocks_num = _BITMAP_NUM_BLOCKS(bits);
38  for (size_t i = 0; i < blocks_num; i++) {
39  dst[i] &= src[i];
40  }
41 }
42 
43 void BLI_bitmap_or_all(BLI_bitmap *dst, const BLI_bitmap *src, size_t bits)
44 {
45  size_t blocks_num = _BITMAP_NUM_BLOCKS(bits);
46  for (size_t i = 0; i < blocks_num; i++) {
47  dst[i] |= src[i];
48  }
49 }
50 
51 int BLI_bitmap_find_first_unset(const BLI_bitmap *bitmap, const size_t bits)
52 {
53  const size_t blocks_num = _BITMAP_NUM_BLOCKS(bits);
54  int result = -1;
55  /* Skip over completely set blocks. */
56  int index = 0;
57  while (index < blocks_num && bitmap[index] == ~0u) {
58  index++;
59  }
60  if (index < blocks_num) {
61  /* Found a partially used block: find the lowest unused bit. */
62  const uint m = ~bitmap[index];
63  BLI_assert(m != 0);
64  const uint bit_index = bitscan_forward_uint(m);
65  result = bit_index + (index << _BITMAP_POWER);
66  }
67  return result;
68 }
#define BLI_assert(a)
Definition: BLI_assert.h:46
#define _BITMAP_NUM_BLOCKS(_num)
Definition: BLI_bitmap.h:30
#define BLI_BITMAP_SIZE(_num)
Definition: BLI_bitmap.h:35
#define _BITMAP_POWER
Definition: BLI_bitmap.h:23
unsigned int BLI_bitmap
Definition: BLI_bitmap.h:16
MINLINE unsigned int bitscan_forward_uint(unsigned int a)
unsigned int uint
Definition: BLI_sys_types.h:67
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
int BLI_bitmap_find_first_unset(const BLI_bitmap *bitmap, const size_t bits)
Definition: bitmap.c:51
void BLI_bitmap_copy_all(BLI_bitmap *dst, const BLI_bitmap *src, size_t bits)
Definition: bitmap.c:30
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
SyclQueue void void * src