Actual source code: bitmask.c
petsc-3.6.1 2015-08-06
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;
66: for (i=0;i<n;i++) {
67: if (*ptr&128) tmp++;
68: if (*ptr&64) tmp++;
69: if (*ptr&32) tmp++;
70: if (*ptr&16) tmp++;
71: if (*ptr&8) tmp++;
72: if (*ptr&4) tmp++;
73: if (*ptr&2) tmp++;
74: if (*ptr&1) tmp++;
75: ptr++;
76: }
78: return(tmp);
79: }
81: /*********************************bit_mask.c***********************************/
82: PetscInt PCTFS_div_ceil(PetscInt numer, PetscInt denom)
83: {
84: PetscInt rt_val;
86: if ((numer<0)||(denom<=0)) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"PCTFS_div_ceil() :: numer=%D ! >=0, denom=%D ! >0",numer,denom);
88: /* if integer division remainder then increment */
89: rt_val = numer/denom;
90: if (numer%denom) rt_val++;
91: return(rt_val);
92: }
94: /*********************************bit_mask.c***********************************/
95: PetscInt PCTFS_len_bit_mask(PetscInt num_items)
96: {
97: PetscInt rt_val, tmp;
99: if (num_items<0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Value Sent To PCTFS_len_bit_mask() Must be >= 0!");
101: /* mod BYTE ceiling function */
102: rt_val = num_items/BYTE;
103: if (num_items%BYTE) rt_val++;
104: /* make mults of sizeof int */
105: if ((tmp=rt_val%sizeof(PetscInt))) rt_val+=(sizeof(PetscInt)-tmp);
106: return(rt_val);
107: }
109: /*********************************bit_mask.c***********************************/
110: PetscErrorCode PCTFS_set_bit_mask(PetscInt *bm, PetscInt len, PetscInt val)
111: {
112: PetscInt i, offset;
113: char mask = 1;
114: char *cptr;
116: if (PCTFS_len_bit_mask(val)>len) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"The Bit Mask Isn't That Large!");
118: cptr = (char*) bm;
120: offset = len/sizeof(PetscInt);
121: for (i=0; i<offset; i++) {
122: *bm=0;
123: bm++;
124: }
126: offset = val%BYTE;
127: for (i=0;i<offset;i++) {
128: mask <<= 1;
129: }
131: offset = len - val/BYTE - 1;
132: cptr[offset] = mask;
133: return(0);
134: }
136: /*********************************bit_mask.c***********************************/
137: PetscInt PCTFS_len_buf(PetscInt item_size, PetscInt num_items)
138: {
139: PetscInt rt_val, tmp;
142: rt_val = item_size * num_items;
144: /* double precision align for now ... consider page later */
145: if ((tmp = (rt_val%(PetscInt)sizeof(double)))) rt_val += (sizeof(double) - tmp);
146: return(rt_val);
147: }