1 2 /********************************bit_mask.c************************************ 3 4 Author: Henry M. Tufo III 5 6 e-mail: hmt@cs.brown.edu 7 8 snail-mail: 9 Division of Applied Mathematics 10 Brown University 11 Providence, RI 02912 12 13 Last Modification: 14 11.21.97 15 *********************************bit_mask.c***********************************/ 16 #include <../src/ksp/pc/impls/tfs/tfs.h> 17 18 /*********************************bit_mask.c***********************************/ 19 PetscErrorCode PCTFS_bm_to_proc(char *ptr, PetscInt p_mask, PetscInt *msg_list) 20 { 21 PetscInt i, tmp; 22 23 PetscFunctionBegin; 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 PetscFunctionReturn(0); 57 } 58 59 /*********************************bit_mask.c***********************************/ 60 PetscInt PCTFS_ct_bits(char *ptr, PetscInt n) 61 { 62 PetscInt i, tmp=0; 63 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 } 77 78 /*********************************bit_mask.c***********************************/ 79 PetscInt PCTFS_div_ceil(PetscInt numer, PetscInt denom) 80 { 81 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); 82 return(PetscCeilInt(numer,denom)); 83 } 84 85 /*********************************bit_mask.c***********************************/ 86 PetscInt PCTFS_len_bit_mask(PetscInt num_items) 87 { 88 PetscInt rt_val, tmp; 89 90 PetscCheck(num_items>=0,PETSC_COMM_SELF,PETSC_ERR_PLIB,"Value Sent To PCTFS_len_bit_mask() Must be >= 0!"); 91 92 rt_val = PetscCeilInt(num_items,BYTE); 93 /* make multiple of sizeof PetscInt */ 94 if ((tmp=rt_val%sizeof(PetscInt))) rt_val+=(sizeof(PetscInt)-tmp); 95 return(rt_val); 96 } 97 98 /*********************************bit_mask.c***********************************/ 99 PetscErrorCode PCTFS_set_bit_mask(PetscInt *bm, PetscInt len, PetscInt val) 100 { 101 PetscInt i, offset; 102 char mask = 1; 103 char *cptr; 104 105 PetscFunctionBegin; 106 PetscCheck(PCTFS_len_bit_mask(val)<=len,PETSC_COMM_SELF,PETSC_ERR_PLIB,"The Bit Mask Isn't That Large!"); 107 108 cptr = (char*) bm; 109 110 offset = len/sizeof(PetscInt); 111 for (i=0; i<offset; i++) { 112 *bm=0; 113 bm++; 114 } 115 116 offset = val%BYTE; 117 for (i=0;i<offset;i++) { 118 mask <<= 1; 119 } 120 121 offset = len - val/BYTE - 1; 122 cptr[offset] = mask; 123 PetscFunctionReturn(0); 124 } 125 126 /*********************************bit_mask.c***********************************/ 127 PetscInt PCTFS_len_buf(PetscInt item_size, PetscInt num_items) 128 { 129 PetscInt rt_val, tmp; 130 131 rt_val = item_size * num_items; 132 133 /* double precision align for now ... consider page later */ 134 if ((tmp = (rt_val%(PetscInt)sizeof(double)))) rt_val += (sizeof(double) - tmp); 135 return(rt_val); 136 } 137 138