Actual source code: bitmask.c


  2: /********************************bit_mask.c************************************

  4: Author: Henry M. Tufo III

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

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

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

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

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

 59: /*********************************bit_mask.c***********************************/
 60: PetscInt PCTFS_ct_bits(char *ptr, PetscInt n)
 61: {
 62:   PetscInt i, tmp=0;

 64:   for (i=0;i<n;i++) {
 65:     if (*ptr&128) tmp++;
 66:     if (*ptr&64)  tmp++;
 67:     if (*ptr&32)  tmp++;
 68:     if (*ptr&16)  tmp++;
 69:     if (*ptr&8)   tmp++;
 70:     if (*ptr&4)   tmp++;
 71:     if (*ptr&2)   tmp++;
 72:     if (*ptr&1)   tmp++;
 73:     ptr++;
 74:   }
 75:   return(tmp);
 76: }

 78: /*********************************bit_mask.c***********************************/
 79: PetscInt PCTFS_div_ceil(PetscInt numer,  PetscInt denom)
 80: {
 81:   PetscInt rt_val;

 83:   if ((numer<0)||(denom<=0)) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"PCTFS_div_ceil() :: numer=%D ! >=0, denom=%D ! >0",numer,denom);

 85:   /* if integer division remainder then increment */
 86:   rt_val = numer/denom;
 87:   if (numer%denom) rt_val++;
 88:   return(rt_val);
 89: }

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

 96:   if (num_items<0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Value Sent To PCTFS_len_bit_mask() Must be >= 0!");

 98:   /* mod BYTE ceiling function */
 99:   rt_val = num_items/BYTE;
100:   if (num_items%BYTE) rt_val++;
101:   /* make mults of sizeof int */
102:   if ((tmp=rt_val%sizeof(PetscInt))) rt_val+=(sizeof(PetscInt)-tmp);
103:   return(rt_val);
104: }

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

114:   if (PCTFS_len_bit_mask(val)>len) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"The Bit Mask Isn't That Large!");

116:   cptr = (char*) bm;

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

124:   offset = val%BYTE;
125:   for (i=0;i<offset;i++) {
126:     mask <<= 1;
127:   }

129:   offset       = len - val/BYTE - 1;
130:   cptr[offset] = mask;
131:   return(0);
132: }

134: /*********************************bit_mask.c***********************************/
135: PetscInt PCTFS_len_buf(PetscInt item_size, PetscInt num_items)
136: {
137:   PetscInt rt_val, tmp;

139:   rt_val = item_size * num_items;

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