Actual source code: petscbt.h

  1: #pragma once

  3: #include <petscsystypes.h>
  4: #include <petscviewertypes.h>
  5: #include <petscstring.h>

  7: /* SUBMANSEC = Sys */

  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 */
 11: static inline size_t PetscBTIndex_Internal(PetscCount index)
 12: {
 13:   return (size_t)index / PETSC_BITS_PER_BYTE;
 14: }

 16: static inline char PetscBTMask_Internal(PetscCount index)
 17: {
 18:   return (char)(1 << index % PETSC_BITS_PER_BYTE);
 19: }

 21: static inline size_t PetscBTLength(PetscCount m)
 22: {
 23:   return (size_t)m / PETSC_BITS_PER_BYTE + 1;
 24: }

 26: static inline PetscErrorCode PetscBTMemzero(PetscCount m, PetscBT array)
 27: {
 28:   return PetscArrayzero(array, PetscBTLength(m));
 29: }

 31: static inline PetscErrorCode PetscBTDestroy(PetscBT *array)
 32: {
 33:   return (*array) ? PetscFree(*array) : PETSC_SUCCESS;
 34: }

 36: static inline PetscErrorCode PetscBTCreate(PetscCount m, PetscBT *array)
 37: {
 38:   return PetscCalloc1(PetscBTLength(m), array);
 39: }

 41: static inline PetscErrorCode PetscBTCopy(PetscBT dest, PetscCount m, PetscBT source)
 42: {
 43:   return PetscArraycpy(dest, source, PetscBTLength(m));
 44: }

 46: static inline char PetscBTLookup(PetscBT array, PetscCount index)
 47: {
 48:   return array[PetscBTIndex_Internal(index)] & PetscBTMask_Internal(index);
 49: }

 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: }

 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: }

 65: static inline PetscErrorCode PetscBTClear(PetscBT array, PetscCount index)
 66: {
 67:   PetscFunctionBegin;
 68:   array[PetscBTIndex_Internal(index)] &= (char)~PetscBTMask_Internal(index);
 69:   PetscFunctionReturn(PETSC_SUCCESS);
 70: }

 72: static inline char PetscBTLookupSet(PetscBT array, PetscCount index)
 73: {
 74:   const char ret = PetscBTLookup(array, index);
 75:   PetscCallContinue(PetscBTSet(array, index));
 76:   return ret;
 77: }

 79: static inline char PetscBTLookupClear(PetscBT array, PetscCount index)
 80: {
 81:   const char ret = PetscBTLookup(array, index);
 82:   PetscCallContinue(PetscBTClear(array, index));
 83:   return ret;
 84: }

 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:     unsigned char       byte = (unsigned char)array[j];
 91:     const unsigned char c1   = 0x55;
 92:     const unsigned char c2   = 0x33;
 93:     const unsigned char c4   = 0x0F;

 95:     byte -= (byte >> 1) & c1;
 96:     byte = ((byte >> 2) & c2) + (byte & c2);
 97:     cnt += (byte + (byte >> 4)) & c4;
 98:   }
 99:   return cnt;
100: }

102: PETSC_EXTERN PetscErrorCode PetscBTView(PetscCount, const PetscBT, PetscViewer);