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 19 /*********************************bit_mask.c***********************************/ 20 PetscErrorCode bm_to_proc( char *ptr, PetscInt p_mask, PetscInt *msg_list) 21 { 22 PetscInt i, tmp; 23 24 PetscFunctionBegin; 25 if (msg_list) 26 { 27 /* low to high */ 28 ptr+=(p_mask-1); 29 for (i=p_mask-1;i>=0;i--) 30 { 31 tmp = BYTE*(p_mask-i-1); 32 if (*ptr&BIT_0) 33 {*msg_list = tmp; msg_list++;} 34 if (*ptr&BIT_1) 35 {*msg_list = tmp+1; msg_list++;} 36 if (*ptr&BIT_2) 37 {*msg_list = tmp+2; msg_list++;} 38 if (*ptr&BIT_3) 39 {*msg_list = tmp+3; msg_list++;} 40 if (*ptr&BIT_4) 41 {*msg_list = tmp+4; msg_list++;} 42 if (*ptr&BIT_5) 43 {*msg_list = tmp+5; msg_list++;} 44 if (*ptr&BIT_6) 45 {*msg_list = tmp+6; msg_list++;} 46 if (*ptr&BIT_7) 47 {*msg_list = tmp+7; msg_list++;} 48 ptr --; 49 } 50 } 51 PetscFunctionReturn(0); 52 } 53 54 /*********************************bit_mask.c***********************************/ 55 PetscInt ct_bits( char *ptr, PetscInt n) 56 { 57 PetscInt i, tmp=0; 58 59 PetscFunctionBegin; 60 for(i=0;i<n;i++) 61 { 62 if (*ptr&128) {tmp++;} 63 if (*ptr&64) {tmp++;} 64 if (*ptr&32) {tmp++;} 65 if (*ptr&16) {tmp++;} 66 if (*ptr&8) {tmp++;} 67 if (*ptr&4) {tmp++;} 68 if (*ptr&2) {tmp++;} 69 if (*ptr&1) {tmp++;} 70 ptr++; 71 } 72 73 return(tmp); 74 } 75 76 /*********************************bit_mask.c***********************************/ 77 PetscInt 78 div_ceil( PetscInt numer, PetscInt denom) 79 { 80 PetscInt rt_val; 81 82 if ((numer<0)||(denom<=0)) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"div_ceil() :: numer=%D ! >=0, denom=%D ! >0",numer,denom); 83 84 /* if integer division remainder then increment */ 85 rt_val = numer/denom; 86 if (numer%denom) 87 {rt_val++;} 88 89 return(rt_val); 90 } 91 92 /*********************************bit_mask.c***********************************/ 93 PetscInt 94 len_bit_mask( PetscInt num_items) 95 { 96 PetscInt rt_val, tmp; 97 98 if (num_items<0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Value Sent To len_bit_mask() Must be >= 0!"); 99 100 /* mod BYTE ceiling function */ 101 rt_val = num_items/BYTE; 102 if (num_items%BYTE) 103 {rt_val++;} 104 105 /* make mults of sizeof int */ 106 if ((tmp=rt_val%sizeof(PetscInt))) 107 {rt_val+=(sizeof(PetscInt)-tmp);} 108 109 return(rt_val); 110 } 111 112 /*********************************bit_mask.c***********************************/ 113 PetscErrorCode set_bit_mask( PetscInt *bm, PetscInt len, PetscInt val) 114 { 115 PetscInt i, offset; 116 char mask = 1; 117 char *cptr; 118 119 if (len_bit_mask(val)>len) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"The Bit Mask Isn't That Large!"); 120 121 cptr = (char *) bm; 122 123 offset = len/sizeof(PetscInt); 124 for (i=0;i<offset;i++) 125 {*bm=0; bm++;} 126 127 offset = val%BYTE; 128 for (i=0;i<offset;i++) 129 {mask <<= 1;} 130 131 offset = len - val/BYTE - 1; 132 cptr[offset] = mask; 133 PetscFunctionReturn(0); 134 } 135 136 /*********************************bit_mask.c***********************************/ 137 PetscInt len_buf(PetscInt item_size, PetscInt num_items) 138 { 139 PetscInt rt_val, tmp; 140 141 PetscFunctionBegin; 142 rt_val = item_size * num_items; 143 144 /* double precision align for now ... consider page later */ 145 if ((tmp = (rt_val%(PetscInt)sizeof(double)))) 146 {rt_val += (sizeof(double) - tmp);} 147 148 return(rt_val); 149 } 150 151 152 153