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