e1e944084d1ba957d25283e289c1055b3953ef0c
[alexxy/gromacs.git] / src / gromacs / utility / basedefinitions.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5  * Copyright (c) 2001-2004, The GROMACS development team.
6  * Copyright (c) 2013,2014,2015,2016,2017,2018, by the GROMACS development team, led by
7  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
8  * and including many others, as listed in the AUTHORS file in the
9  * top-level source directory and at http://www.gromacs.org.
10  *
11  * GROMACS is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public License
13  * as published by the Free Software Foundation; either version 2.1
14  * of the License, or (at your option) any later version.
15  *
16  * GROMACS is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with GROMACS; if not, see
23  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
24  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
25  *
26  * If you want to redistribute modifications to GROMACS, please
27  * consider that scientific software is very special. Version
28  * control is crucial - bugs must be traceable. We will be happy to
29  * consider code for inclusion in the official distribution, but
30  * derived work must not be called official GROMACS. Details are found
31  * in the README & COPYING files - if they are missing, get the
32  * official version at http://www.gromacs.org.
33  *
34  * To help us fund GROMACS development, we humbly ask that you cite
35  * the research papers on the package. Check out http://www.gromacs.org.
36  */
37 /*! \file
38  * \brief
39  * Basic types and macros used throughout \Gromacs.
40  *
41  * \inpublicapi
42  * \ingroup module_utility
43  */
44 #ifndef GMX_UTILITY_BASEDEFINITIONS_H
45 #define GMX_UTILITY_BASEDEFINITIONS_H
46
47 #include <stdint.h>
48 #ifndef _MSC_VER
49 #include <inttypes.h>
50 #endif
51
52 #if defined __cplusplus
53 #include <cstddef>
54 #else
55 #include <stdbool.h>
56 #endif
57
58 //! Identical to bool
59 typedef bool gmx_bool;
60
61 #ifndef FALSE
62 /** False value for ::gmx_bool. */
63 #  define FALSE   false
64 #endif
65 #ifndef TRUE
66 /** True value for ::gmx_bool. */
67 #  define TRUE    true
68 #endif
69 /** Number of gmx_bool values. */
70 #define BOOL_NR 2
71
72 #if defined __cplusplus
73 namespace gmx
74 {
75 /*! \brief Integer type for indexing into arrays or vectors
76  *
77  * Same as ptrdiff_t.
78  */
79 using index = std::ptrdiff_t;
80 }
81 #endif
82
83 /*! \name Fixed-width integer types
84  *
85  * These types and macros provide the equivalent of 32- and 64-bit integer
86  * types from C99 headers `stdint.h` and `inttypes.h`.  These headers are also
87  * there in C++11.  The types and macros from here should be used instead of
88  * `int32_t` etc.
89  *
90  * (MSVC 2015 still doesn't support the format strings.)
91  */
92 /*! \{ */
93 typedef int32_t gmx_int32_t;
94 typedef int64_t gmx_int64_t;
95 typedef uint32_t gmx_uint32_t;
96 typedef uint64_t gmx_uint64_t;
97
98 #ifdef _MSC_VER
99 #define GMX_PRId32 "I32d"
100 #define GMX_SCNd32 "I32d"
101
102 #define GMX_PRId64 "I64d"
103 #define GMX_SCNd64 "I64d"
104
105 #define GMX_PRIu32 "I32u"
106 #define GMX_SCNu32 "I32u"
107
108 #define GMX_PRIu64 "I64u"
109 #define GMX_SCNu64 "I64u"
110 #else
111 #define GMX_PRId32 PRId32
112 #define GMX_SCNd32 SCNd32
113
114 #define GMX_PRId64 PRId64
115 #define GMX_SCNd64 SCNd64
116
117 #define GMX_PRIu32 PRIu32
118 #define GMX_SCNu32 SCNu32
119
120 #define GMX_PRIu64 PRIu64
121 #define GMX_SCNu64 SCNu64
122 #endif
123
124 #define GMX_INT32_MAX INT32_MAX
125 #define GMX_INT32_MIN INT32_MIN
126
127 #define GMX_INT64_MAX INT64_MAX
128 #define GMX_INT64_MIN INT64_MIN
129
130 #define GMX_UINT32_MAX UINT32_MAX
131 #define GMX_UINT32_MIN UINT32_MIN
132
133 #define GMX_UINT64_MAX UINT64_MAX
134 #define GMX_UINT64_MIN UINT64_MIN
135 /*! \} */
136
137 /* ICC, GCC, MSVC, Pathscale, PGI, XLC support __restrict.
138  * Any other compiler can be added here. */
139 /*! \brief
140  * Keyword to use in instead of C99 `restrict`.
141  *
142  * We cannot use `restrict` because it is only in C99, but not in C++.
143  * This macro should instead be used to allow easily supporting different
144  * compilers.
145  */
146 #define gmx_restrict __restrict
147
148 /*! \def GMX_CXX11_COMPILATION
149  * \brief
150  * Defined to 1 when compiling as C++11.
151  *
152  * While \Gromacs only supports C++11 compilation, there are some parts of the
153  * code that are compiled with other tools than the actual C++ compiler, and
154  * these may not support C++11.  Most notable such case is all of CUDA code
155  * (with CUDA versions older than 6.5), but other types of kernels might also
156  * have similar limitations in the future.
157  *
158  * The define is intended for conditional compilation in low-level headers that
159  * need to support inclusion from such non-C++11 files, but get significant
160  * benefit (e.g., for correctness checking or more convenient use) from C++11.
161  * It should only be used for features that do not influence the ABI of the
162  * header; e.g., static_asserts or additional helper methods.
163  */
164 #if defined __cplusplus && __cplusplus >= 201103L
165 #    define GMX_CXX11_COMPILATION 1
166 #else
167 #    define GMX_CXX11_COMPILATION 0
168 #endif
169
170 /*! \def gmx_unused
171  * \brief
172  * Attribute to suppress compiler warnings about unused function parameters.
173  *
174  * This attribute suppresses compiler warnings about unused function arguments
175  * by marking them as possibly unused.  Some arguments are unused but
176  * have to be retained to preserve a function signature
177  * that must match that of another function.
178  * Some arguments are only used in *some* conditional compilation code paths
179  * (e.g. MPI).
180  */
181 #ifndef gmx_unused
182 #ifdef __GNUC__
183 /* GCC, clang, and some ICC pretending to be GCC */
184 #  define gmx_unused __attribute__ ((unused))
185 #elif (defined(__INTEL_COMPILER) || defined(__ECC)) && !defined(_MSC_VER)
186 /* ICC on *nix */
187 #  define gmx_unused __attribute__ ((unused))
188 #elif defined(__PGI)
189 /* Portland group compilers */
190 #  define gmx_unused __attribute__ ((unused))
191 #elif defined _MSC_VER
192 /* MSVC */
193 #  define gmx_unused /*@unused@*/
194 #elif defined(__xlC__)
195 /* IBM */
196 #  define gmx_unused __attribute__ ((unused))
197 #else
198 #  define gmx_unused
199 #endif
200 #endif
201
202 /*! \brief Attribute to explicitly indicate that a parameter or
203  * locally scoped variable is used just in debug mode.
204  *
205  * \ingroup module_utility
206  */
207 #ifdef NDEBUG
208 #define gmx_used_in_debug gmx_unused
209 #else
210 #define gmx_used_in_debug
211 #endif
212
213 #ifndef __has_feature
214 /** For compatibility with non-clang compilers. */
215 #define __has_feature(x) 0
216 #endif
217
218 /*! \brief
219  * Macro to explicitly ignore an unused value.
220  *
221  * \ingroup module_utility
222  *
223  * \todo Deprecated - use gmx_unused
224  */
225 #define GMX_UNUSED_VALUE(value) (void)value
226
227 #ifdef __cplusplus
228 namespace gmx
229 {
230 namespace internal
231 {
232 /*! \cond internal */
233 /*! \internal \brief
234  * Helper for ignoring values in macros.
235  *
236  * \ingroup module_utility
237  */
238 template <typename T>
239 static inline void ignoreValueHelper(const T &)
240 {
241 }
242 //! \endcond
243 }   // namespace internal
244 }   // namespace gmx
245
246 /*! \brief
247  * Macro to explicitly ignore a return value of a call.
248  *
249  * Mainly meant for ignoring values of functions declared with
250  * `__attribute__((warn_unused_return))`.  Makes it easy to find those places if
251  * they need to be fixed, and document the intent in cases where the return
252  * value really can be ignored.  It also makes it easy to adapt the approach so
253  * that they don't produce warnings.  A cast to void doesn't remove the warning
254  * in gcc, while adding a dummy variable can cause warnings about an unused
255  * variable.
256  *
257  * \ingroup module_utility
258  */
259 #define GMX_IGNORE_RETURN_VALUE(call) \
260         ::gmx::internal::ignoreValueHelper(call)
261 #endif
262
263 #endif