4b9c812513327131e896e6212e6af33f1f82a29a
[alexxy/gromacs.git] / src / gromacs / mdlib / bitmask.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2014, by the GROMACS development team, led by
5  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6  * and including many others, as listed in the AUTHORS file in the
7  * top-level source directory and at http://www.gromacs.org.
8  *
9  * GROMACS is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public License
11  * as published by the Free Software Foundation; either version 2.1
12  * of the License, or (at your option) any later version.
13  *
14  * GROMACS 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 GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with GROMACS; if not, see
21  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
23  *
24  * If you want to redistribute modifications to GROMACS, please
25  * consider that scientific software is very special. Version
26  * control is crucial - bugs must be traceable. We will be happy to
27  * consider code for inclusion in the official distribution, but
28  * derived work must not be called official GROMACS. Details are found
29  * in the README & COPYING files - if they are missing, get the
30  * official version at http://www.gromacs.org.
31  *
32  * To help us fund GROMACS development, we humbly ask that you cite
33  * the research papers on the package. Check out http://www.gromacs.org.
34  */
35 /*! \libinternal \file
36  * \brief
37  * Declares gmx_bitmask_t and associated functions
38  *
39  * \author Roland Schulz <roland@utk.edu>
40  * \inlibraryapi
41  */
42
43 #ifndef GMX_MDLIB_BITMASK_H
44 #define GMX_MDLIB_BITMASK_H
45
46 #include "config.h" /* for GMX_MAX_OPENMP_THREADS */
47
48 #include <string.h>
49
50 #include "gromacs/utility/basedefinitions.h"
51
52 /*! \brief Size of bitmask. Has to be 32 or multiple of 64. */
53 #ifndef BITMASK_SIZE
54 #define BITMASK_SIZE GMX_OPENMP_MAX_THREADS
55 #endif
56
57 #if BITMASK_SIZE != 32 && BITMASK_SIZE%64 != 0
58 #error BITMASK_SIZE has to be 32 or a multiple of 64.
59 #endif
60
61 #if BITMASK_SIZE <= 64 || defined DOXYGEN
62 #if BITMASK_SIZE == 32
63 typedef gmx_uint32_t gmx_bitmask_t;
64 #else
65 typedef gmx_uint64_t gmx_bitmask_t; /**< bitmask type */
66 #endif
67
68 /*! \brief Initialize all bits to 0 */
69 gmx_inline static void bitmask_clear(gmx_bitmask_t* m)
70 {
71     *m = 0;
72 }
73
74 /*! \brief Set bit at position b to 1. */
75 gmx_inline static void bitmask_set_bit(gmx_bitmask_t* m, int b)
76 {
77     *m |= ((gmx_bitmask_t)1 << b);
78 }
79
80 /*! \brief Initialize all bits: bit b to 1, others to 0 */
81 gmx_inline static void bitmask_init_bit(gmx_bitmask_t* m, int b)
82 {
83     *m = ((gmx_bitmask_t)1 << b);
84 }
85
86 /*! \brief Initialize all bits: all bits below b to 1, others to 0 */
87 gmx_inline static void bitmask_init_low_bits(gmx_bitmask_t* m, int b)
88 {
89     *m = ((gmx_bitmask_t)1 << b) - 1;
90 }
91
92 /*! \brief Test if bit b is set */
93 gmx_inline static gmx_bool bitmask_is_set(gmx_bitmask_t m, int b)
94 {
95     return (m & ((gmx_bitmask_t)1 << b)) != 0;
96 }
97
98 /*! \brief Test if both bitmasks have no common bits enabled */
99 gmx_inline static gmx_bool bitmask_is_disjoint(gmx_bitmask_t a, gmx_bitmask_t b)
100 {
101     return !(a & b);
102 }
103
104 /*! \brief Test if both bitmasks are equal */
105 gmx_inline static gmx_bool bitmask_is_equal(gmx_bitmask_t a, gmx_bitmask_t b)
106 {
107     return a == b;
108 }
109
110 /*! \brief Test if bitmask has no enabled bits */
111 gmx_inline static gmx_bool bitmask_is_zero(gmx_bitmask_t m)
112 {
113     return !m;
114 }
115
116 /*! \brief Set all bits enabled in either mask and write into a */
117 gmx_inline static void bitmask_union(gmx_bitmask_t* a, gmx_bitmask_t b)
118 {
119     *a |= b;
120 }
121 #else
122 #define BITMASK_ALEN (BITMASK_SIZE/64)
123 typedef gmx_uint64_t gmx_bitmask_t[BITMASK_ALEN];
124
125 gmx_inline static void bitmask_clear(gmx_bitmask_t* m)
126 {
127     memset(*m, 0, BITMASK_SIZE/8);
128 }
129
130 gmx_inline static void bitmask_set_bit(gmx_bitmask_t* m, int b)
131 {
132     (*m)[b/64] |= ((gmx_uint64_t)1 << (b%64));
133 }
134
135 gmx_inline static void bitmask_init_bit(gmx_bitmask_t* m, int b)
136 {
137     bitmask_clear(m);
138     (*m)[b/64] = ((gmx_uint64_t)1 << (b%64));
139 }
140
141 gmx_inline static void bitmask_init_low_bits(gmx_bitmask_t* m, int b)
142 {
143     memset(*m, 255, b/64*8);
144     (*m)[b/64] = ((gmx_uint64_t)1 << (b%64)) - 1;
145     memset(&(*m)[b/64+1], 0, (BITMASK_ALEN-b/64-1)*8);
146 }
147
148 gmx_inline static gmx_bool bitmask_is_set(gmx_bitmask_t m, int b)
149 {
150     return (m[b/64] & ((gmx_uint64_t)1 << (b%64))) != 0;
151 }
152
153 gmx_inline static gmx_bool bitmask_is_disjoint(gmx_bitmask_t a, gmx_bitmask_t b)
154 {
155     int      i;
156     gmx_bool r = 1;
157     for (i = 0; i < BITMASK_ALEN; i++)
158     {
159         r = r && !(a[i] & b[i]);
160     }
161     return r;
162 }
163
164 gmx_inline static gmx_bool bitmask_is_equal(gmx_bitmask_t a, gmx_bitmask_t b)
165 {
166     int      i;
167     gmx_bool r = 1;
168     for (i = 0; i < BITMASK_ALEN; i++)
169     {
170         r = r && (a[i] == b[i]);
171     }
172     return r;
173 }
174
175 gmx_inline static gmx_bool bitmask_is_zero(gmx_bitmask_t m)
176 {
177     int      i;
178     gmx_bool r = 1;
179     for (i = 0; i < BITMASK_ALEN; i++)
180     {
181         r = r && !m[i];
182     }
183     return r;
184 }
185
186 gmx_inline static void bitmask_union(gmx_bitmask_t* a, gmx_bitmask_t b)
187 {
188     int i;
189     for (i = 0; i < BITMASK_ALEN; i++)
190     {
191         (*a)[i] |= b[i];
192     }
193 }
194 #endif
195
196 #endif