xref: /petsc/src/ksp/pc/impls/tfs/bitmask.c (revision 58d68138c660dfb4e9f5b03334792cd4f2ffd7cc)
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   PetscInt i, tmp;
21 
22   PetscFunctionBegin;
23   if (msg_list) {
24     /* low to high */
25     ptr += (p_mask - 1);
26     for (i = p_mask - 1; i >= 0; i--) {
27       tmp = BYTE * (p_mask - i - 1);
28       if (*ptr & BIT_0) {
29         *msg_list = tmp;
30         msg_list++;
31       }
32       if (*ptr & BIT_1) {
33         *msg_list = tmp + 1;
34         msg_list++;
35       }
36       if (*ptr & BIT_2) {
37         *msg_list = tmp + 2;
38         msg_list++;
39       }
40       if (*ptr & BIT_3) {
41         *msg_list = tmp + 3;
42         msg_list++;
43       }
44       if (*ptr & BIT_4) {
45         *msg_list = tmp + 4;
46         msg_list++;
47       }
48       if (*ptr & BIT_5) {
49         *msg_list = tmp + 5;
50         msg_list++;
51       }
52       if (*ptr & BIT_6) {
53         *msg_list = tmp + 6;
54         msg_list++;
55       }
56       if (*ptr & BIT_7) {
57         *msg_list = tmp + 7;
58         msg_list++;
59       }
60       ptr--;
61     }
62   }
63   PetscFunctionReturn(0);
64 }
65 
66 /*********************************bit_mask.c***********************************/
67 PetscInt PCTFS_ct_bits(char *ptr, PetscInt n) {
68   PetscInt i, tmp = 0;
69 
70   for (i = 0; i < n; i++) {
71     if (*ptr & 128) tmp++;
72     if (*ptr & 64) tmp++;
73     if (*ptr & 32) tmp++;
74     if (*ptr & 16) tmp++;
75     if (*ptr & 8) tmp++;
76     if (*ptr & 4) tmp++;
77     if (*ptr & 2) tmp++;
78     if (*ptr & 1) tmp++;
79     ptr++;
80   }
81   return (tmp);
82 }
83 
84 /*********************************bit_mask.c***********************************/
85 PetscInt PCTFS_div_ceil(PetscInt numer, PetscInt denom) {
86   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);
87   return (PetscCeilInt(numer, denom));
88 }
89 
90 /*********************************bit_mask.c***********************************/
91 PetscInt PCTFS_len_bit_mask(PetscInt num_items) {
92   PetscInt rt_val, tmp;
93 
94   PetscCheck(num_items >= 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Value Sent To PCTFS_len_bit_mask() Must be >= 0!");
95 
96   rt_val = PetscCeilInt(num_items, BYTE);
97   /* make multiple of sizeof PetscInt */
98   if ((tmp = rt_val % sizeof(PetscInt))) rt_val += (sizeof(PetscInt) - tmp);
99   return (rt_val);
100 }
101 
102 /*********************************bit_mask.c***********************************/
103 PetscErrorCode PCTFS_set_bit_mask(PetscInt *bm, PetscInt len, PetscInt val) {
104   PetscInt i, offset;
105   char     mask = 1;
106   char    *cptr;
107 
108   PetscFunctionBegin;
109   PetscCheck(PCTFS_len_bit_mask(val) <= len, PETSC_COMM_SELF, PETSC_ERR_PLIB, "The Bit Mask Isn't That Large!");
110 
111   cptr = (char *)bm;
112 
113   offset = len / sizeof(PetscInt);
114   for (i = 0; i < offset; i++) {
115     *bm = 0;
116     bm++;
117   }
118 
119   offset = val % BYTE;
120   for (i = 0; i < offset; i++) { mask <<= 1; }
121 
122   offset       = len - val / BYTE - 1;
123   cptr[offset] = mask;
124   PetscFunctionReturn(0);
125 }
126 
127 /*********************************bit_mask.c***********************************/
128 PetscInt PCTFS_len_buf(PetscInt item_size, PetscInt num_items) {
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