Fix GMX_ALIGNMENT for MSVC 2012 and 2013
[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, 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  * MSVC doesn't support these before Visual Studio 2013.
82  */
83 /*! \{ */
84 #ifdef _MSC_VER
85 typedef __int32 gmx_int32_t;
86 #define GMX_PRId32 "I32d"
87 #define GMX_SCNd32 "I32d"
88
89 typedef __int64 gmx_int64_t;
90 #define GMX_PRId64 "I64d"
91 #define GMX_SCNd64 "I64d"
92
93 typedef unsigned __int32 gmx_uint32_t;
94 #define GMX_PRIu32 "I32u"
95 #define GMX_SCNu32 "I32u"
96
97 typedef unsigned __int64 gmx_uint64_t;
98 #define GMX_PRIu64 "I64u"
99 #define GMX_SCNu64 "I64u"
100 #else
101 typedef int32_t gmx_int32_t;
102 #define GMX_PRId32 PRId32
103 #define GMX_SCNd32 SCNd32
104
105 typedef int64_t gmx_int64_t;
106 #define GMX_PRId64 PRId64
107 #define GMX_SCNd64 SCNd64
108
109 typedef uint32_t gmx_uint32_t;
110 #define GMX_PRIu32 PRIu32
111 #define GMX_SCNu32 SCNu32
112
113 typedef uint64_t gmx_uint64_t;
114 #define GMX_PRIu64 PRIu64
115 #define GMX_SCNu64 SCNu64
116 #endif
117
118 #define GMX_INT32_MAX INT32_MAX
119 #define GMX_INT32_MIN INT32_MIN
120
121 #define GMX_INT64_MAX INT64_MAX
122 #define GMX_INT64_MIN INT64_MIN
123
124 #define GMX_UINT32_MAX UINT32_MAX
125 #define GMX_UINT32_MIN UINT32_MIN
126
127 #define GMX_UINT64_MAX UINT64_MAX
128 #define GMX_UINT64_MIN UINT64_MIN
129 /*! \} */
130
131 /*! \def gmx_inline
132  * \brief
133  * Keyword to use in C code instead of C99 `inline`.
134  *
135  * Some of the C compilers we support do not recognize the C99 keyword
136  * `inline`.  This macro should be used in C code and in shared C/C++ headers
137  * to indicate a function is inlined.
138  * C++ code should use plain `inline`, as that is already in C++98.
139  */
140 #if !defined __cplusplus && _MSC_VER
141 #define gmx_inline __inline
142 #else
143 /* C++ or C99 */
144 #define gmx_inline inline
145 #endif
146
147 /* ICC, GCC, MSVC, Pathscale, PGI, XLC support __restrict.
148  * Any other compiler can be added here. */
149 /*! \brief
150  * Keyword to use in instead of C99 `restrict`.
151  *
152  * We cannot use `restrict` because it is only in C99, but not in C++.
153  * This macro should instead be used to allow easily supporting different
154  * compilers.
155  */
156 #define gmx_restrict __restrict
157
158 /*! \def gmx_cxx_const
159  * \brief
160  * Keyword to work around C/C++ differences in possible const keyword usage.
161  *
162  * Some functions that do not modify their input parameters cannot declare
163  * those parameters as `const` and compile warning/error-free on both C and C++
164  * compilers because of differences in `const` semantics.  This macro can be
165  * used in cases where C++ allows `const`, but C does not like it, to make the
166  * same declaration work for both.
167  */
168 #ifdef __cplusplus
169 #define gmx_cxx_const const
170 #else
171 #define gmx_cxx_const
172 #endif
173
174 /*! \def gmx_unused
175  * \brief
176  * Attribute to suppress compiler warnings about unused function parameters.
177  *
178  * This attribute suppresses compiler warnings about unused function arguments
179  * by marking them as possibly unused.  Some arguments are unused but
180  * have to be retained to preserve a function signature
181  * that must match that of another function.
182  * Some arguments are only used in *some* conditional compilation code paths
183  * (e.g. MPI).
184  */
185 #ifndef gmx_unused
186 #ifdef __GNUC__
187 /* GCC, clang, and some ICC pretending to be GCC */
188 #  define gmx_unused __attribute__ ((unused))
189 #elif (defined(__INTEL_COMPILER) || defined(__ECC)) && !defined(_MSC_VER)
190 /* ICC on *nix */
191 #  define gmx_unused __attribute__ ((unused))
192 #elif defined(__PGI)
193 /* Portland group compilers */
194 #  define gmx_unused __attribute__ ((unused))
195 #elif defined _MSC_VER
196 /* MSVC */
197 #  define gmx_unused /*@unused@*/
198 #elif defined(__xlC__)
199 /* IBM */
200 #  define gmx_unused __attribute__ ((unused))
201 #else
202 #  define gmx_unused
203 #endif
204 #endif
205
206 #ifndef __has_feature
207 /** For compatibility with non-clang compilers. */
208 #define __has_feature(x) 0
209 #endif
210
211 /*! \def gmx_noreturn
212  * \brief
213  * Indicate that a function is not expected to return.
214  */
215 #ifndef gmx_noreturn
216 #if defined(__GNUC__) || __has_feature(attribute_analyzer_noreturn)
217 #define gmx_noreturn __attribute__((noreturn))
218 #elif defined (_MSC_VER)
219 #define gmx_noreturn __declspec(noreturn)
220 #else
221 #define gmx_noreturn
222 #endif
223 #endif
224
225 /*! \def GMX_ALIGNMENT
226  * \brief
227  * Supports aligned variables */
228
229 /*! \def GMX_ALIGNED(type, alignment)
230  * \brief
231  * Declare variable with data alignment
232  *
233  * \param[in] type       Type of variable
234  * \param[in] alignment  Alignment in multiples of type
235  *
236  * Typical usage:
237  * \code
238    GMX_ALIGNED(real, GMX_SIMD_REAL_WIDTH) buf[...];
239    \endcode
240  */
241 /* alignas(x) is not used even with GMX-CXX11 because it isn't in the list of
242    tested features and thus might not be supported.
243    MSVC before 2015 has align but doesn't support sizeof inside. */
244 #if defined(_MSC_VER) && (_MSC_VER > 1800 || defined(__ICL))
245 #  define GMX_ALIGNMENT 1
246 #  define GMX_ALIGNED(type, alignment) __declspec(align(alignment*sizeof(type))) type
247 #elif defined(__GNUC__) || defined(__clang__)
248 #  define GMX_ALIGNMENT 1
249 #  define GMX_ALIGNED(type, alignment) __attribute__ ((__aligned__(alignment*sizeof(type)))) type
250 #else
251 #  define GMX_ALIGNMENT 0
252 #  define GMX_ALIGNED(type, alignment)
253 #endif
254
255 /*! \brief
256  * Macro to explicitly ignore an unused value.
257  *
258  * \ingroup module_utility
259  */
260 #define GMX_UNUSED_VALUE(value) (void)value
261
262 #ifdef __cplusplus
263 namespace gmx
264 {
265 namespace internal
266 {
267 /*! \cond internal */
268 /*! \internal \brief
269  * Helper for ignoring values in macros.
270  *
271  * \ingroup module_utility
272  */
273 template <typename T>
274 static inline void ignoreValueHelper(const T &)
275 {
276 }
277 //! \endcond
278 }   // namespace internal
279 }   // namespace gmx
280
281 /*! \brief
282  * Macro to explicitly ignore a return value of a call.
283  *
284  * Mainly meant for ignoring values of functions declared with
285  * `__attribute__((warn_unused_return))`.  Makes it easy to find those places if
286  * they need to be fixed, and document the intent in cases where the return
287  * value really can be ignored.  It also makes it easy to adapt the approach so
288  * that they don't produce warnings.  A cast to void doesn't remove the warning
289  * in gcc, while adding a dummy variable can cause warnings about an unused
290  * variable.
291  *
292  * \ingroup module_utility
293  */
294 #define GMX_IGNORE_RETURN_VALUE(call) \
295         ::gmx::internal::ignoreValueHelper(call)
296 #endif
297
298 #endif