Implement changes for CMake policy 0068
[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, 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 /*! \brief
53  * Boolean type for use in \Gromacs C code.
54  *
55  * There is no standard size for 'bool' in C++, so when
56  * we previously defined it to int for C code the data types
57  * (and structs) would have different size depending on your compiler,
58  * both at \Gromacs build time and when you use the library.
59  * The only way around this is to NOT assume anything about the C++ type,
60  * so we cannot use the name 'bool' in our C code anymore.
61  */
62 typedef int gmx_bool;
63
64 #ifndef FALSE
65 /** False value for ::gmx_bool. */
66 #  define FALSE   0
67 #endif
68 #ifndef TRUE
69 /** True value for ::gmx_bool. */
70 #  define TRUE    1
71 #endif
72 /** Number of gmx_bool values. */
73 #define BOOL_NR 2
74
75 /*! \name Fixed-width integer types
76  *
77  * These types and macros provide the equivalent of 32- and 64-bit integer
78  * types from C99 headers `stdint.h` and `inttypes.h`.  These headers are also
79  * there in C++11.  The types and macros from here should be used instead of
80  * `int32_t` etc.
81  *
82  * (MSVC 2015 still doesn't support the format strings.)
83  */
84 /*! \{ */
85 typedef int32_t gmx_int32_t;
86 typedef int64_t gmx_int64_t;
87 typedef uint32_t gmx_uint32_t;
88 typedef uint64_t gmx_uint64_t;
89
90 #ifdef _MSC_VER
91 #define GMX_PRId32 "I32d"
92 #define GMX_SCNd32 "I32d"
93
94 #define GMX_PRId64 "I64d"
95 #define GMX_SCNd64 "I64d"
96
97 #define GMX_PRIu32 "I32u"
98 #define GMX_SCNu32 "I32u"
99
100 #define GMX_PRIu64 "I64u"
101 #define GMX_SCNu64 "I64u"
102 #else
103 #define GMX_PRId32 PRId32
104 #define GMX_SCNd32 SCNd32
105
106 #define GMX_PRId64 PRId64
107 #define GMX_SCNd64 SCNd64
108
109 #define GMX_PRIu32 PRIu32
110 #define GMX_SCNu32 SCNu32
111
112 #define GMX_PRIu64 PRIu64
113 #define GMX_SCNu64 SCNu64
114 #endif
115
116 #define GMX_INT32_MAX INT32_MAX
117 #define GMX_INT32_MIN INT32_MIN
118
119 #define GMX_INT64_MAX INT64_MAX
120 #define GMX_INT64_MIN INT64_MIN
121
122 #define GMX_UINT32_MAX UINT32_MAX
123 #define GMX_UINT32_MIN UINT32_MIN
124
125 #define GMX_UINT64_MAX UINT64_MAX
126 #define GMX_UINT64_MIN UINT64_MIN
127 /*! \} */
128
129 /*! \def gmx_inline
130  * \brief
131  * Keyword to use in C code instead of C99 `inline`.
132  *
133  * Some of the C compilers we support do not recognize the C99 keyword
134  * `inline`.  This macro should be used in C code and in shared C/C++ headers
135  * to indicate a function is inlined.
136  * C++ code should use plain `inline`, as that is already in C++98.
137  */
138 #if !defined __cplusplus && defined _MSC_VER
139 #define gmx_inline __inline
140 #else
141 /* C++ or C99 */
142 #define gmx_inline inline
143 #endif
144
145 /* ICC, GCC, MSVC, Pathscale, PGI, XLC support __restrict.
146  * Any other compiler can be added here. */
147 /*! \brief
148  * Keyword to use in instead of C99 `restrict`.
149  *
150  * We cannot use `restrict` because it is only in C99, but not in C++.
151  * This macro should instead be used to allow easily supporting different
152  * compilers.
153  */
154 #define gmx_restrict __restrict
155
156 /*! \def GMX_CXX11_COMPILATION
157  * \brief
158  * Defined to 1 when compiling as C++11.
159  *
160  * While \Gromacs only supports C++11 compilation, there are some parts of the
161  * code that are compiled with other tools than the actual C++ compiler, and
162  * these may not support C++11.  Most notable such case is all of CUDA code
163  * (with CUDA versions older than 6.5), but other types of kernels might also
164  * have similar limitations in the future.
165  *
166  * The define is intended for conditional compilation in low-level headers that
167  * need to support inclusion from such non-C++11 files, but get significant
168  * benefit (e.g., for correctness checking or more convenient use) from C++11.
169  * It should only be used for features that do not influence the ABI of the
170  * header; e.g., static_asserts or additional helper methods.
171  */
172 #if defined __cplusplus && __cplusplus >= 201103L
173 #    define GMX_CXX11_COMPILATION 1
174 #else
175 #    define GMX_CXX11_COMPILATION 0
176 #endif
177
178 /*! \def gmx_unused
179  * \brief
180  * Attribute to suppress compiler warnings about unused function parameters.
181  *
182  * This attribute suppresses compiler warnings about unused function arguments
183  * by marking them as possibly unused.  Some arguments are unused but
184  * have to be retained to preserve a function signature
185  * that must match that of another function.
186  * Some arguments are only used in *some* conditional compilation code paths
187  * (e.g. MPI).
188  */
189 #ifndef gmx_unused
190 #ifdef __GNUC__
191 /* GCC, clang, and some ICC pretending to be GCC */
192 #  define gmx_unused __attribute__ ((unused))
193 #elif (defined(__INTEL_COMPILER) || defined(__ECC)) && !defined(_MSC_VER)
194 /* ICC on *nix */
195 #  define gmx_unused __attribute__ ((unused))
196 #elif defined(__PGI)
197 /* Portland group compilers */
198 #  define gmx_unused __attribute__ ((unused))
199 #elif defined _MSC_VER
200 /* MSVC */
201 #  define gmx_unused /*@unused@*/
202 #elif defined(__xlC__)
203 /* IBM */
204 #  define gmx_unused __attribute__ ((unused))
205 #else
206 #  define gmx_unused
207 #endif
208 #endif
209
210 #ifndef __has_feature
211 /** For compatibility with non-clang compilers. */
212 #define __has_feature(x) 0
213 #endif
214
215 /*! \def gmx_noreturn
216  * \brief
217  * Indicate that a function is not expected to return.
218  */
219 #ifndef gmx_noreturn
220 #if defined(__GNUC__) || __has_feature(attribute_analyzer_noreturn)
221 #define gmx_noreturn __attribute__((noreturn))
222 #elif defined (_MSC_VER)
223 #define gmx_noreturn __declspec(noreturn)
224 #else
225 #define gmx_noreturn
226 #endif
227 #endif
228
229 /*! \brief
230  * Macro to explicitly ignore an unused value.
231  *
232  * \ingroup module_utility
233  */
234 #define GMX_UNUSED_VALUE(value) (void)value
235
236 #ifdef __cplusplus
237 namespace gmx
238 {
239 namespace internal
240 {
241 /*! \cond internal */
242 /*! \internal \brief
243  * Helper for ignoring values in macros.
244  *
245  * \ingroup module_utility
246  */
247 template <typename T>
248 static inline void ignoreValueHelper(const T &)
249 {
250 }
251 //! \endcond
252 }   // namespace internal
253 }   // namespace gmx
254
255 /*! \brief
256  * Macro to explicitly ignore a return value of a call.
257  *
258  * Mainly meant for ignoring values of functions declared with
259  * `__attribute__((warn_unused_return))`.  Makes it easy to find those places if
260  * they need to be fixed, and document the intent in cases where the return
261  * value really can be ignored.  It also makes it easy to adapt the approach so
262  * that they don't produce warnings.  A cast to void doesn't remove the warning
263  * in gcc, while adding a dummy variable can cause warnings about an unused
264  * variable.
265  *
266  * \ingroup module_utility
267  */
268 #define GMX_IGNORE_RETURN_VALUE(call) \
269         ::gmx::internal::ignoreValueHelper(call)
270 #endif
271
272 #endif