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 PetscByte PetscBTMask_Internal(PetscCount index)
     17: {
     18:   return (PetscByte)(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 PetscByte 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)] &= (PetscByte)~PetscBTMask_Internal(index);
     69:   PetscFunctionReturn(PETSC_SUCCESS);
     70: }

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

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

     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;

     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);