Date: 2026-05-01T06:11:54+00:00

Actual source code: bitmask.c

      1: /********************************bit_mask.c************************************

      3: Author: Henry M. Tufo III

      5: e-mail: hmt@cs.brown.edu

      7: snail-mail:
      8: Division of Applied Mathematics
      9: Brown University
     10: Providence, RI 02912

     12: Last Modification:
     13: 11.21.97
     14: *********************************bit_mask.c***********************************/
     15: #include <../src/ksp/pc/impls/tfs/tfs.h>

     17: /*********************************bit_mask.c***********************************/
     18: PetscErrorCode PCTFS_bm_to_proc(char *ptr, PetscInt p_mask, PetscInt *msg_list)
     19: {
     20:   PetscInt i, tmp;

     22:   PetscFunctionBegin;
     23:   if (msg_list) {
     24:     /* low to high */
     25:     ptr += (p_mask - 1);
     26:     for (i = p_mask - 1; i >= 0; i--) {
     27:       tmp = BYTE * (p_mask - i - 1);
     28:       if (*ptr & BIT_0) {
     29:         *msg_list = tmp;
     30:         msg_list++;
     31:       }
     32:       if (*ptr & BIT_1) {
     33:         *msg_list = tmp + 1;
     34:         msg_list++;
     35:       }
     36:       if (*ptr & BIT_2) {
     37:         *msg_list = tmp + 2;
     38:         msg_list++;
     39:       }
     40:       if (*ptr & BIT_3) {
     41:         *msg_list = tmp + 3;
     42:         msg_list++;
     43:       }
     44:       if (*ptr & BIT_4) {
     45:         *msg_list = tmp + 4;
     46:         msg_list++;
     47:       }
     48:       if (*ptr & BIT_5) {
     49:         *msg_list = tmp + 5;
     50:         msg_list++;
     51:       }
     52:       if (*ptr & BIT_6) {
     53:         *msg_list = tmp + 6;
     54:         msg_list++;
     55:       }
     56:       if (*ptr & BIT_7) {
     57:         *msg_list = tmp + 7;
     58:         msg_list++;
     59:       }
     60:       ptr--;
     61:     }
     62:   }
     63:   PetscFunctionReturn(PETSC_SUCCESS);
     64: }

     66: /*********************************bit_mask.c***********************************/
     67: PetscInt PCTFS_ct_bits(char *ptr, PetscInt n)
     68: {
     69:   PetscInt i, tmp = 0;

     71:   for (i = 0; i < n; i++) {
     72:     if (*ptr & 128) tmp++;
     73:     if (*ptr & 64) tmp++;
     74:     if (*ptr & 32) tmp++;
     75:     if (*ptr & 16) tmp++;
     76:     if (*ptr & 8) tmp++;
     77:     if (*ptr & 4) tmp++;
     78:     if (*ptr & 2) tmp++;
     79:     if (*ptr & 1) tmp++;
     80:     ptr++;
     81:   }
     82:   return tmp;
     83: }

     85: /*********************************bit_mask.c***********************************/
     86: PetscInt PCTFS_div_ceil(PetscInt numer, PetscInt denom)
     87: {
     88:   if ((numer < 0) || (denom <= 0)) SETERRABORT(PETSC_COMM_SELF, PETSC_ERR_PLIB, "PCTFS_div_ceil() :: numer=%" PetscInt_FMT " ! >=0, denom=%" PetscInt_FMT " ! >0", numer, denom);
     89:   return PetscCeilInt(numer, denom);
     90: }

     92: /*********************************bit_mask.c***********************************/
     93: PetscInt PCTFS_len_bit_mask(PetscInt num_items)
     94: {
     95:   PetscInt rt_val, tmp;

     97:   PetscCheck(num_items >= 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Value Sent To PCTFS_len_bit_mask() Must be >= 0!");

     99:   rt_val = PetscCeilInt(num_items, BYTE);
    100:   /* make multiple of sizeof PetscInt */
    101:   if ((tmp = rt_val % sizeof(PetscInt))) rt_val += (sizeof(PetscInt) - tmp);
    102:   return rt_val;
    103: }

    105: /*********************************bit_mask.c***********************************/
    106: PetscErrorCode PCTFS_set_bit_mask(PetscInt *bm, PetscInt len, PetscInt val)
    107: {
    108:   PetscInt i, offset;
    109:   char     mask = 1;
    110:   char    *cptr;

    112:   PetscFunctionBegin;
    113:   PetscCheck(PCTFS_len_bit_mask(val) <= len, PETSC_COMM_SELF, PETSC_ERR_PLIB, "The Bit Mask Isn't That Large!");

    115:   cptr = (char *)bm;

    117:   offset = len / sizeof(PetscInt);
    118:   for (i = 0; i < offset; i++) {
    119:     *bm = 0;
    120:     bm++;
    121:   }

    123:   offset = val % BYTE;
    124:   for (i = 0; i < offset; i++) mask <<= 1;

    126:   offset       = len - val / BYTE - 1;
    127:   cptr[offset] = mask;
    128:   PetscFunctionReturn(PETSC_SUCCESS);
    129: }

    131: /*********************************bit_mask.c***********************************/
    132: PetscInt PCTFS_len_buf(PetscInt item_size, PetscInt num_items)
    133: {
    134:   PetscInt rt_val, tmp;

    136:   rt_val = item_size * num_items;

    138:   /*  double precision align for now ... consider page later */
    139:   if ((tmp = (rt_val % (PetscInt)sizeof(double)))) rt_val += (sizeof(double) - tmp);
    140:   return rt_val;
    141: }