8182d7269200c2c526c38ea474c8d070a189822c
[alexxy/gromacs.git] / src / gromacs / utility / bitmask.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2014,2018, 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  * \ingroup module_utility
42  */
43
44 #ifndef GMX_MDLIB_BITMASK_H
45 #define GMX_MDLIB_BITMASK_H
46
47 #include "config.h" /* for GMX_MAX_OPENMP_THREADS */
48
49 #include <string.h>
50
51 #include <array>
52
53 #include "gromacs/utility/basedefinitions.h"
54
55 /*! \brief Size of bitmask. Has to be 32 or multiple of 64. */
56 #ifndef BITMASK_SIZE
57 #define BITMASK_SIZE GMX_OPENMP_MAX_THREADS
58 #endif
59
60 #if BITMASK_SIZE != 32 && BITMASK_SIZE%64 != 0
61 #error BITMASK_SIZE has to be 32 or a multiple of 64.
62 #endif
63
64 #if BITMASK_SIZE <= 64 || defined DOXYGEN
65 #if BITMASK_SIZE == 32
66 typedef uint32_t gmx_bitmask_t;
67 #else
68 typedef uint64_t gmx_bitmask_t; /**< bitmask type */
69 #endif
70
71 /*! \brief Initialize all bits to 0 */
72 inline static void bitmask_clear(gmx_bitmask_t* m)
73 {
74     *m = 0;
75 }
76
77 /*! \brief Set bit at position b to 1. */
78 inline static void bitmask_set_bit(gmx_bitmask_t* m, int b)
79 {
80     *m |= gmx_bitmask_t(1) << b;
81 }
82
83 /*! \brief Initialize all bits: bit b to 1, others to 0 */
84 inline static void bitmask_init_bit(gmx_bitmask_t* m, int b)
85 {
86     *m = gmx_bitmask_t(1) << b;
87 }
88
89 /*! \brief Initialize all bits: all bits below b to 1, others to 0 */
90 inline static void bitmask_init_low_bits(gmx_bitmask_t* m, int b)
91 {
92     *m = (gmx_bitmask_t(1) << b) - 1;
93 }
94
95 /*! \brief Test if bit b is set */
96 inline static bool bitmask_is_set(gmx_bitmask_t m, int b)
97 {
98     return (m & (gmx_bitmask_t(1) << b)) != 0;
99 }
100
101 /*! \brief Test if both bitmasks have no common bits enabled */
102 inline static bool bitmask_is_disjoint(gmx_bitmask_t a, gmx_bitmask_t b)
103 {
104     return (a & b) == 0u;
105 }
106
107 /*! \brief Test if both bitmasks are equal */
108 inline static bool bitmask_is_equal(gmx_bitmask_t a, gmx_bitmask_t b)
109 {
110     return a == b;
111 }
112
113 /*! \brief Test if bitmask has no enabled bits */
114 inline static bool bitmask_is_zero(gmx_bitmask_t m)
115 {
116     return m == 0u;
117 }
118
119 /*! \brief Set all bits enabled in either mask and write into a */
120 inline static void bitmask_union(gmx_bitmask_t* a, gmx_bitmask_t b)
121 {
122     *a |= b;
123 }
124 #else
125 #define BITMASK_ALEN (BITMASK_SIZE/64)
126 using gmx_bitmask_t = std::array<uint64_t, BITMASK_ALEN>;
127
128 inline static void bitmask_clear(gmx_bitmask_t* m)
129 {
130     m->fill(0);
131 }
132
133 inline static void bitmask_set_bit(gmx_bitmask_t* m, int b)
134 {
135     (*m)[b/64] |= (static_cast<uint64_t>(1) << (b%64));
136 }
137
138 inline static void bitmask_init_bit(gmx_bitmask_t* m, int b)
139 {
140     bitmask_clear(m);
141     (*m)[b/64] = (static_cast<uint64_t>(1) << (b%64));
142 }
143
144 inline static void bitmask_init_low_bits(gmx_bitmask_t* m, int b)
145 {
146     memset(m->data(), 255, b/64*8);
147     (*m)[b/64] = (static_cast<uint64_t>(1) << (b%64)) - 1;
148     memset(&(m->data())[b/64+1], 0, (BITMASK_ALEN-b/64-1)*8);
149 }
150
151 inline static bool bitmask_is_set(gmx_bitmask_t m, int b)
152 {
153     return (m[b/64] & (static_cast<uint64_t>(1) << (b%64))) != 0;
154 }
155
156 inline static bool bitmask_is_disjoint(gmx_bitmask_t a, gmx_bitmask_t b)
157 {
158     int      i;
159     bool     r = true;
160     for (i = 0; i < BITMASK_ALEN; i++)
161     {
162         r = r && ((a[i] & b[i]) == 0u);
163     }
164     return r;
165 }
166
167 inline static bool bitmask_is_equal(gmx_bitmask_t a, gmx_bitmask_t b)
168 {
169     int      i;
170     bool     r = true;
171     for (i = 0; i < BITMASK_ALEN; i++)
172     {
173         r = r && (a[i] == b[i]);
174     }
175     return r;
176 }
177
178 inline static bool bitmask_is_zero(gmx_bitmask_t m)
179 {
180     int      i;
181     bool     r = true;
182     for (i = 0; i < BITMASK_ALEN; i++)
183     {
184         r = r && (m[i] == 0u);
185     }
186     return r;
187 }
188
189 inline static void bitmask_union(gmx_bitmask_t* a, gmx_bitmask_t b)
190 {
191     int i;
192     for (i = 0; i < BITMASK_ALEN; i++)
193     {
194         (*a)[i] |= b[i];
195     }
196 }
197 #endif
198
199 #endif