802b0e02b9bb834c947d19bacac2f73a5c9bf793
[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
49 #if defined __cplusplus
50 #include <cinttypes>
51 #include <cstddef>
52 #else
53 #include <stdbool.h>
54 #endif
55
56 //! Identical to bool
57 typedef bool gmx_bool;
58
59 #ifndef FALSE
60 /** False value for ::gmx_bool. */
61 #  define FALSE   false
62 #endif
63 #ifndef TRUE
64 /** True value for ::gmx_bool. */
65 #  define TRUE    true
66 #endif
67 /** Number of gmx_bool values. */
68 #define BOOL_NR 2
69
70 #if defined __cplusplus
71 namespace gmx
72 {
73 /*! \brief Integer type for indexing into arrays or vectors
74  *
75  * Same as ptrdiff_t.
76  */
77 using index = std::ptrdiff_t;
78 }
79 #endif
80
81 /* ICC, GCC, MSVC, Pathscale, PGI, XLC support __restrict.
82  * Any other compiler can be added here. */
83 /*! \brief
84  * Keyword to use in instead of C99 `restrict`.
85  *
86  * We cannot use `restrict` because it is only in C99, but not in C++.
87  * This macro should instead be used to allow easily supporting different
88  * compilers.
89  */
90 #define gmx_restrict __restrict
91
92 /*! \def GMX_CXX11_COMPILATION
93  * \brief
94  * Defined to 1 when compiling as C++11.
95  *
96  * While \Gromacs only supports C++11 compilation, there are some parts of the
97  * code that are compiled with other tools than the actual C++ compiler, and
98  * these may not support C++11.  Most notable such case is all of CUDA code
99  * (with CUDA versions older than 6.5), but other types of kernels might also
100  * have similar limitations in the future.
101  *
102  * The define is intended for conditional compilation in low-level headers that
103  * need to support inclusion from such non-C++11 files, but get significant
104  * benefit (e.g., for correctness checking or more convenient use) from C++11.
105  * It should only be used for features that do not influence the ABI of the
106  * header; e.g., static_asserts or additional helper methods.
107  */
108 #if defined __cplusplus && __cplusplus >= 201103L
109 #    define GMX_CXX11_COMPILATION 1
110 #else
111 #    define GMX_CXX11_COMPILATION 0
112 #endif
113
114 /*! \def gmx_unused
115  * \brief
116  * Attribute to suppress compiler warnings about unused function parameters.
117  *
118  * This attribute suppresses compiler warnings about unused function arguments
119  * by marking them as possibly unused.  Some arguments are unused but
120  * have to be retained to preserve a function signature
121  * that must match that of another function.
122  * Some arguments are only used in *some* conditional compilation code paths
123  * (e.g. MPI).
124  */
125 #ifndef gmx_unused
126 #ifdef __GNUC__
127 /* GCC, clang, and some ICC pretending to be GCC */
128 #  define gmx_unused __attribute__ ((unused))
129 #elif (defined(__INTEL_COMPILER) || defined(__ECC)) && !defined(_MSC_VER)
130 /* ICC on *nix */
131 #  define gmx_unused __attribute__ ((unused))
132 #elif defined(__PGI)
133 /* Portland group compilers */
134 #  define gmx_unused __attribute__ ((unused))
135 #elif defined _MSC_VER
136 /* MSVC */
137 #  define gmx_unused /*@unused@*/
138 #elif defined(__xlC__)
139 /* IBM */
140 #  define gmx_unused __attribute__ ((unused))
141 #else
142 #  define gmx_unused
143 #endif
144 #endif
145
146 /*! \brief Attribute to explicitly indicate that a parameter or
147  * locally scoped variable is used just in debug mode.
148  *
149  * \ingroup module_utility
150  */
151 #ifdef NDEBUG
152 #define gmx_used_in_debug gmx_unused
153 #else
154 #define gmx_used_in_debug
155 #endif
156
157 #ifndef __has_feature
158 /** For compatibility with non-clang compilers. */
159 #define __has_feature(x) 0
160 #endif
161
162 /*! \brief
163  * Macro to explicitly ignore an unused value.
164  *
165  * \ingroup module_utility
166  *
167  * \todo Deprecated - use gmx_unused
168  */
169 #define GMX_UNUSED_VALUE(value) (void)value
170
171 #ifdef __cplusplus
172 namespace gmx
173 {
174 namespace internal
175 {
176 /*! \cond internal */
177 /*! \internal \brief
178  * Helper for ignoring values in macros.
179  *
180  * \ingroup module_utility
181  */
182 template <typename T>
183 static inline void ignoreValueHelper(const T &)
184 {
185 }
186 //! \endcond
187 }   // namespace internal
188 }   // namespace gmx
189
190 /*! \brief
191  * Macro to explicitly ignore a return value of a call.
192  *
193  * Mainly meant for ignoring values of functions declared with
194  * `__attribute__((warn_unused_return))`.  Makes it easy to find those places if
195  * they need to be fixed, and document the intent in cases where the return
196  * value really can be ignored.  It also makes it easy to adapt the approach so
197  * that they don't produce warnings.  A cast to void doesn't remove the warning
198  * in gcc, while adding a dummy variable can cause warnings about an unused
199  * variable.
200  *
201  * \ingroup module_utility
202  */
203 #define GMX_IGNORE_RETURN_VALUE(call) \
204         ::gmx::internal::ignoreValueHelper(call)
205 #endif
206
207 #endif