1 #pragma once
2
3 #include <petscsystypes.h>
4 #include <petscviewertypes.h>
5 #include <petscstring.h>
6
7 /* SUBMANSEC = Sys */
8
9 /* convert an index i to an index suitable for indexing a PetscBT, such that
10 * bt[PetscBTIndex(i)] returns the i'th value of the bt */
PetscBTIndex_Internal(PetscCount index)11 static inline size_t PetscBTIndex_Internal(PetscCount index)
12 {
13 return (size_t)index / PETSC_BITS_PER_BYTE;
14 }
15
PetscBTMask_Internal(PetscCount index)16 static inline PetscByte PetscBTMask_Internal(PetscCount index)
17 {
18 return (PetscByte)(1 << index % PETSC_BITS_PER_BYTE);
19 }
20
PetscBTLength(PetscCount m)21 static inline size_t PetscBTLength(PetscCount m)
22 {
23 return (size_t)m / PETSC_BITS_PER_BYTE + 1;
24 }
25
PetscBTMemzero(PetscCount m,PetscBT array)26 static inline PetscErrorCode PetscBTMemzero(PetscCount m, PetscBT array)
27 {
28 return PetscArrayzero(array, PetscBTLength(m));
29 }
30
PetscBTDestroy(PetscBT * array)31 static inline PetscErrorCode PetscBTDestroy(PetscBT *array)
32 {
33 return (*array) ? PetscFree(*array) : PETSC_SUCCESS;
34 }
35
PetscBTCreate(PetscCount m,PetscBT * array)36 static inline PetscErrorCode PetscBTCreate(PetscCount m, PetscBT *array)
37 {
38 return PetscCalloc1(PetscBTLength(m), array);
39 }
40
PetscBTCopy(PetscBT dest,PetscCount m,PetscBT source)41 static inline PetscErrorCode PetscBTCopy(PetscBT dest, PetscCount m, PetscBT source)
42 {
43 return PetscArraycpy(dest, source, PetscBTLength(m));
44 }
45
PetscBTLookup(PetscBT array,PetscCount index)46 static inline PetscByte PetscBTLookup(PetscBT array, PetscCount index)
47 {
48 return array[PetscBTIndex_Internal(index)] & PetscBTMask_Internal(index);
49 }
50
PetscBTSet(PetscBT array,PetscCount index)51 static inline PetscErrorCode PetscBTSet(PetscBT array, PetscCount index)
52 {
53 PetscFunctionBegin;
54 array[PetscBTIndex_Internal(index)] |= PetscBTMask_Internal(index);
55 PetscFunctionReturn(PETSC_SUCCESS);
56 }
57
PetscBTNegate(PetscBT array,PetscCount index)58 static inline PetscErrorCode PetscBTNegate(PetscBT array, PetscCount index)
59 {
60 PetscFunctionBegin;
61 array[PetscBTIndex_Internal(index)] ^= PetscBTMask_Internal(index);
62 PetscFunctionReturn(PETSC_SUCCESS);
63 }
64
PetscBTClear(PetscBT array,PetscCount index)65 static inline PetscErrorCode PetscBTClear(PetscBT array, PetscCount index)
66 {
67 PetscFunctionBegin;
68 array[PetscBTIndex_Internal(index)] &= (PetscByte)~PetscBTMask_Internal(index);
69 PetscFunctionReturn(PETSC_SUCCESS);
70 }
71
PetscBTLookupSet(PetscBT array,PetscCount index)72 static inline PetscByte PetscBTLookupSet(PetscBT array, PetscCount index)
73 {
74 const PetscByte ret = PetscBTLookup(array, index);
75 PetscCallContinue(PetscBTSet(array, index));
76 return ret;
77 }
78
PetscBTLookupClear(PetscBT array,PetscCount index)79 static inline PetscByte PetscBTLookupClear(PetscBT array, PetscCount index)
80 {
81 const PetscByte ret = PetscBTLookup(array, index);
82 PetscCallContinue(PetscBTClear(array, index));
83 return ret;
84 }
85
PetscBTCountSet(PetscBT array,PetscCount m)86 static inline PetscCount PetscBTCountSet(PetscBT array, PetscCount m)
87 {
88 PetscCount cnt = 0;
89 for (size_t j = 0; j < PetscBTLength(m); j++) {
90 PetscByte byte = array[j];
91 const PetscByte c1 = 0x55;
92 const PetscByte c2 = 0x33;
93 const PetscByte c4 = 0x0F;
94
95 byte -= (byte >> 1) & c1;
96 byte = ((byte >> 2) & c2) + (byte & c2);
97 cnt += (byte + (byte >> 4)) & c4;
98 }
99 return cnt;
100 }
101
102 PETSC_EXTERN PetscErrorCode PetscBTView(PetscCount, const PetscBT, PetscViewer);
103