Declare __STDC_*_MACROS in gmxpre.h
[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, 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 /* Information about integer data type sizes */
48 #include <limits.h>
49 #include <stdint.h>
50 #ifndef _MSC_VER
51 #include <inttypes.h>
52 #endif
53
54 /*! \brief
55  * Boolean type for use in \Gromacs C code.
56  *
57  * There is no standard size for 'bool' in C++, so when
58  * we previously defined it to int for C code the data types
59  * (and structs) would have different size depending on your compiler,
60  * both at \Gromacs build time and when you use the library.
61  * The only way around this is to NOT assume anything about the C++ type,
62  * so we cannot use the name 'bool' in our C code anymore.
63  */
64 typedef int gmx_bool;
65
66 #ifndef FALSE
67 /** False value for ::gmx_bool. */
68 #  define FALSE   0
69 #endif
70 #ifndef TRUE
71 /** True value for ::gmx_bool. */
72 #  define TRUE    1
73 #endif
74 /** Number of gmx_bool values. */
75 #define BOOL_NR 2
76
77 /*! \name Fixed-width integer types
78  *
79  * These types and macros provide the equivalent of 32- and 64-bit integer
80  * types from C99 headers `stdint.h` and `inttypes.h`.  These headers are also
81  * there in C++11.  The types and macros from here should be used instead of
82  * `int32_t` etc.
83  * MSVC doesn't support these before Visual Studio 2013.
84  */
85 /*! \{ */
86 #ifdef _MSC_VER
87 typedef __int32 gmx_int32_t;
88 #define GMX_PRId32 "I32d"
89 #define GMX_SCNd32 "I32d"
90
91 typedef __int64 gmx_int64_t;
92 #define GMX_PRId64 "I64d"
93 #define GMX_SCNd64 "I64d"
94
95 typedef unsigned __int32 gmx_uint32_t;
96 #define GMX_PRIu32 "I32u"
97 #define GMX_SCNu32 "I32u"
98
99 typedef unsigned __int64 gmx_uint64_t;
100 #define GMX_PRIu64 "I64u"
101 #define GMX_SCNu64 "I64u"
102 #else
103 typedef int32_t gmx_int32_t;
104 #define GMX_PRId32 PRId32
105 #define GMX_SCNd32 SCNd32
106
107 typedef int64_t gmx_int64_t;
108 #define GMX_PRId64 PRId64
109 #define GMX_SCNd64 SCNd64
110
111 typedef uint32_t gmx_uint32_t;
112 #define GMX_PRIu32 PRIu32
113 #define GMX_SCNu32 SCNu32
114
115 typedef uint64_t gmx_uint64_t;
116 #define GMX_PRIu64 PRIu64
117 #define GMX_SCNu64 SCNu64
118 #endif
119
120 #define GMX_INT32_MAX INT32_MAX
121 #define GMX_INT32_MIN INT32_MIN
122
123 #define GMX_INT64_MAX INT64_MAX
124 #define GMX_INT64_MIN INT64_MIN
125
126 #define GMX_UINT32_MAX UINT32_MAX
127 #define GMX_UINT32_MIN UINT32_MIN
128
129 #define GMX_UINT64_MAX UINT64_MAX
130 #define GMX_UINT64_MIN UINT64_MIN
131 /*! \} */
132
133 /*! \def gmx_inline
134  * \brief
135  * Keyword to use in C code instead of C99 `inline`.
136  *
137  * Some of the C compilers we support do not recognize the C99 keyword
138  * `inline`.  This macro should be used in C code and in shared C/C++ headers
139  * to indicate a function is inlined.
140  * C++ code should use plain `inline`, as that is already in C++98.
141  */
142 #if !defined __cplusplus && _MSC_VER
143 #define gmx_inline __inline
144 #else
145 /* C++ or C99 */
146 #define gmx_inline inline
147 #endif
148
149 /* ICC, GCC, MSVC, Pathscale, PGI, XLC support __restrict.
150  * Any other compiler can be added here. */
151 /*! \brief
152  * Keyword to use in instead of C99 `restrict`.
153  *
154  * We cannot use `restrict` because it is only in C99, but not in C++.
155  * This macro should instead be used to allow easily supporting different
156  * compilers.
157  */
158 #define gmx_restrict __restrict
159
160 /*! \def gmx_cxx_const
161  * \brief
162  * Keyword to work around C/C++ differences in possible const keyword usage.
163  *
164  * Some functions that do not modify their input parameters cannot declare
165  * those parameters as `const` and compile warning/error-free on both C and C++
166  * compilers because of differences in `const` semantics.  This macro can be
167  * used in cases where C++ allows `const`, but C does not like it, to make the
168  * same declaration work for both.
169  */
170 #ifdef __cplusplus
171 #define gmx_cxx_const const
172 #else
173 #define gmx_cxx_const
174 #endif
175
176 /*! \def gmx_unused
177  * \brief
178  * Attribute to suppress compiler warnings about unused function parameters.
179  *
180  * This attribute suppresses compiler warnings about unused function arguments
181  * by marking them as possibly unused.  Some arguments are unused but
182  * have to be retained to preserve a function signature
183  * that must match that of another function.
184  * Some arguments are only used in *some* conditional compilation code paths
185  * (e.g. MPI).
186  */
187 #ifndef gmx_unused
188 #ifdef __GNUC__
189 /* GCC, clang, and some ICC pretending to be GCC */
190 #  define gmx_unused __attribute__ ((unused))
191 #elif (defined(__INTEL_COMPILER) || defined(__ECC)) && !defined(_MSC_VER)
192 /* ICC on *nix */
193 #  define gmx_unused __attribute__ ((unused))
194 #elif defined(__PGI)
195 /* Portland group compilers */
196 #  define gmx_unused __attribute__ ((unused))
197 #elif defined _MSC_VER
198 /* MSVC */
199 #  define gmx_unused /*@unused@*/
200 #elif defined(__xlC__)
201 /* IBM */
202 #  define gmx_unused __attribute__ ((unused))
203 #else
204 #  define gmx_unused
205 #endif
206 #endif
207
208 #ifndef __has_feature
209 /** For compatibility with non-clang compilers. */
210 #define __has_feature(x) 0
211 #endif
212
213 /*! \def gmx_noreturn
214  * \brief
215  * Indicate that a function is not expected to return.
216  *
217  */
218 #ifndef gmx_noreturn
219 #if defined(__GNUC__) || __has_feature(attribute_analyzer_noreturn)
220 #define gmx_noreturn __attribute__((noreturn))
221 #elif defined (_MSC_VER)
222 #define gmx_noreturn __declspec(noreturn)
223 #else
224 #define gmx_noreturn
225 #endif
226 #endif
227
228
229 #endif