1 /* Copyright (c) 2012, 2021, Oracle and/or its affiliates.
2 
3    This program is free software; you can redistribute it and/or modify
4    it under the terms of the GNU General Public License, version 2.0,
5    as published by the Free Software Foundation.
6 
7    This program is also distributed with certain software (including
8    but not limited to OpenSSL) that is licensed under separate terms,
9    as designated in a particular file or component or in included license
10    documentation.  The authors of MySQL hereby grant you an additional
11    permission to link the program and your derivative works with the
12    separately licensed software that they have included with MySQL.
13 
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License, version 2.0, for more details.
18 
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */
22 
23 #include <assert.h>
24 #include <stdlib.h>
25 
26 #include "xcom_vp.h"
27 #include "task_debug.h"
28 
new_bit_set(uint32_t bits)29 bit_set *new_bit_set(uint32_t bits)
30 {
31   bit_set *bs = malloc(sizeof(*bs));
32   bs->bits.bits_len = howmany_words(bits, MASK_BITS);
33   bs->bits.bits_val = malloc(bs->bits.bits_len * sizeof(*bs->bits.bits_val));
34   BIT_ZERO(bs);
35   return bs;
36 }
37 
clone_bit_set(bit_set * orig)38 bit_set *clone_bit_set(bit_set *orig)
39 {
40   if(!orig)return orig;
41   {
42     bit_set *bs = malloc(sizeof(*bs));
43     bs->bits.bits_len = orig->bits.bits_len;
44     bs->bits.bits_val = malloc(bs->bits.bits_len * sizeof(*bs->bits.bits_val));
45     memcpy(bs->bits.bits_val, orig->bits.bits_val, bs->bits.bits_len * sizeof(*bs->bits.bits_val));
46     return bs;
47   }
48 }
49 
free_bit_set(bit_set * bs)50 void free_bit_set(bit_set *bs)
51 {
52   free(bs->bits.bits_val);
53   free(bs);
54 }
55 /* purecov: begin deadcode */
dbg_bit_set(bit_set * bs)56 void dbg_bit_set(bit_set *bs)
57 {
58   unsigned int i = 0;
59   GET_GOUT;
60   for(i = 0; i < bs->bits.bits_len * sizeof(*bs->bits.bits_val) * BITS_PER_BYTE; i++){
61     NPUT(BIT_ISSET(i,bs),d);
62   }
63   PRINT_GOUT;
64   FREE_GOUT;
65 }
66 
bit_set_or(bit_set * x,bit_set const * y)67 void bit_set_or(bit_set *x, bit_set const *y)
68 {
69   unsigned int i = 0;
70   assert(x->bits.bits_len == y->bits.bits_len);
71   for(i = 0; i < x->bits.bits_len ; i++){
72     x->bits.bits_val[i] |= y->bits.bits_val[i];
73   }
74 }
75 
76 #if 0
77 void bit_set_and(bit_set *x, bit_set const *y)
78 {
79   unsigned int i = 0;
80   assert(x->bits.bits_len == y->bits.bits_len);
81   for(i = 0; i < x->bits.bits_len ; i++){
82     x->bits.bits_val[i] &= y->bits.bits_val[i];
83   }
84 }
85 #endif
86 
87 #if 0
88 void bit_set_xor(bit_set *x, bit_set const *y)
89 {
90   unsigned int i = 0;
91   assert(x->bits.bits_len == y->bits.bits_len);
92   for(i = 0; i < x->bits.bits_len ; i++){
93     x->bits.bits_val[i] ^= y->bits.bits_val[i];
94   }
95 }
96 #endif
97 
98 #if 0
99 void bit_set_not(bit_set *x)
100 {
101   unsigned int i = 0;
102   for(i = 0; i < x->bits.bits_len ; i++){
103     x->bits.bits_val[i] = ~x->bits.bits_val[i];
104   }
105 }
106 #endif
107 
108 /* Debug a bit set */
dbg_bitset(bit_set const * p,u_int nodes)109 char *dbg_bitset(bit_set const *p, u_int nodes)
110 {
111   u_int i = 0;
112   GET_NEW_GOUT;
113   if (!p) {
114     STRLIT("p == 0 ");
115   }
116   else
117   {
118     STRLIT("{");
119     for (i = 0; i < nodes; i++) {
120       NPUT(BIT_ISSET(i, p),d);
121     }
122     STRLIT("} ");
123   }
124   RET_GOUT;
125 }
126 
127 #ifdef ETEST
main()128 int main()
129 {
130   bit_set *bs = new_bit_set(64);
131   BIT_SET(16,bs); XDBG("%X"NEWLINE,bs->bits.bits_val[0]);
132   dbg_bit_set(bs);
133   BIT_CLR(16,bs); XDBG("%X"NEWLINE,bs->bits.bits_val[0]);
134   dbg_bit_set(bs);
135   BIT_XOR(16,bs); XDBG("%X"NEWLINE,bs->bits.bits_val[0]);
136   dbg_bit_set(bs);
137   BIT_SET(33,bs); XDBG("%X"NEWLINE,bs->bits.bits_val[1]);
138   dbg_bit_set(bs);
139   BIT_CLR(33,bs); XDBG("%X"NEWLINE,bs->bits.bits_val[1]);
140   dbg_bit_set(bs);
141   BIT_XOR(33,bs); XDBG("%X"NEWLINE,bs->bits.bits_val[1]);
142   dbg_bit_set(bs);
143   bit_set_not(bs);
144   dbg_bit_set(bs);
145   return 0;
146 }
147 #endif
148 /* purecov: end */