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