Fixes for Intel and container build fixes
[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,2019,2020, 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 #include <cinttypes>
50 #include <cstddef>
51
52 //! Identical to bool
53 typedef bool gmx_bool;
54
55 #ifndef FALSE
56 /** False value for ::gmx_bool. */
57 #    define FALSE false
58 #endif
59 #ifndef TRUE
60 /** True value for ::gmx_bool. */
61 #    define TRUE true
62 #endif
63 /** Number of gmx_bool values. */
64 #define BOOL_NR 2
65
66 namespace gmx
67 {
68 /*! \brief Integer type for indexing into arrays or vectors
69  *
70  * Same as ptrdiff_t.
71  */
72 using index = std::ptrdiff_t;
73
74 //! Return signed size of container
75 template<typename T>
76 index ssize(const T& t)
77 {
78     return t.size();
79 }
80 } // namespace gmx
81
82 /* ICC, GCC, MSVC, Pathscale, PGI, XLC support __restrict.
83  * Any other compiler can be added here. */
84 /*! \brief
85  * Keyword to use in instead of C99 `restrict`.
86  *
87  * We cannot use `restrict` because it is only in C99, but not in C++.
88  * This macro should instead be used to allow easily supporting different
89  * compilers.
90  */
91 #define gmx_restrict __restrict
92
93 /*! \def gmx_unused
94  * \brief
95  * Attribute to suppress compiler warnings about unused function parameters.
96  *
97  * This attribute suppresses compiler warnings about unused function arguments
98  * by marking them as possibly unused.  Some arguments are unused but
99  * have to be retained to preserve a function signature
100  * that must match that of another function.
101  * Some arguments are only used in *some* conditional compilation code paths
102  * (e.g. MPI).
103  */
104 #ifndef gmx_unused
105 #    ifdef __GNUC__
106 /* GCC, clang, and some ICC pretending to be GCC */
107 #        define gmx_unused __attribute__((unused))
108 #    elif (defined(__INTEL_COMPILER) || defined(__ECC)) && !defined(_MSC_VER)
109 /* ICC on *nix */
110 #        define gmx_unused __attribute__((unused))
111 #    elif defined(__PGI)
112 /* Portland group compilers */
113 #        define gmx_unused __attribute__((unused))
114 #    elif defined _MSC_VER
115 /* MSVC */
116 #        define gmx_unused /*@unused@*/
117 #    elif defined(__xlC__)
118 /* IBM */
119 #        define gmx_unused __attribute__((unused))
120 #    else
121 #        define gmx_unused
122 #    endif
123 #endif
124
125 /*! \brief Attribute to explicitly indicate that a parameter or
126  * locally scoped variable is used just in debug mode.
127  *
128  * \ingroup module_utility
129  */
130 #ifdef NDEBUG
131 #    define gmx_used_in_debug gmx_unused
132 #else
133 #    define gmx_used_in_debug
134 #endif
135
136 #ifndef __has_feature
137 /** For compatibility with non-clang compilers. */
138 #    define __has_feature(x) 0
139 #endif
140
141 /*! \brief
142  * Macro to explicitly ignore an unused value.
143  *
144  * \ingroup module_utility
145  *
146  * \todo Deprecated - use gmx_unused
147  */
148 #define GMX_UNUSED_VALUE(value) (void)value
149
150 #ifdef __clang__
151 #    define DO_PRAGMA(x) _Pragma(#    x)
152 #    define CLANG_DIAGNOSTIC_IGNORE(warning) \
153         _Pragma("clang diagnostic push") DO_PRAGMA(clang diagnostic ignored #warning)
154 #    define CLANG_DIAGNOSTIC_RESET _Pragma("clang diagnostic pop")
155 #else
156 //! Ignore specified clang warning until CLANG_DIAGNOSTIC_RESET
157 #    define CLANG_DIAGNOSTIC_IGNORE(warning)
158 //! Reset all diagnostics to default
159 #    define CLANG_DIAGNOSTIC_RESET
160 #endif
161
162 #ifdef _MSC_VER
163 #    define MSVC_DIAGNOSTIC_IGNORE(id) __pragma(warning(push)) __pragma(warning(disable : id))
164 #    define MSVC_DIAGNOSTIC_RESET __pragma(warning(pop))
165 #else
166 //! Ignore specified MSVC warning until MSVC_DIAGNOSTIC_RESET
167 #    define MSVC_DIAGNOSTIC_IGNORE(warning)
168 //! Reset all diagnostics to default
169 #    define MSVC_DIAGNOSTIC_RESET
170 #endif
171
172 #ifdef __INTEL_COMPILER
173 // Ignore unused loop variable warning - it was used until the compiler removes the use!
174 #    define DO_PRAGMA(x) _Pragma(#    x)
175 #    define INTEL_DIAGNOSTIC_IGNORE(id) DO_PRAGMA(warning push) DO_PRAGMA(warning(disable : id))
176 #    define INTEL_DIAGNOSTIC_RESET DO_PRAGMA(warning pop)
177 #else
178 #    define INTEL_DIAGNOSTIC_IGNORE(id)
179 #    define INTEL_DIAGNOSTIC_RESET
180 #endif
181
182 namespace gmx
183 {
184 namespace internal
185 {
186 /*! \cond internal */
187 /*! \internal \brief
188  * Helper for ignoring values in macros.
189  *
190  * \ingroup module_utility
191  */
192 template<typename T>
193 static inline void ignoreValueHelper(const T& /*unused*/)
194 {
195 }
196 //! \endcond
197 } // namespace internal
198 } // namespace gmx
199
200 /*! \brief
201  * Macro to explicitly ignore a return value of a call.
202  *
203  * Mainly meant for ignoring values of functions declared with
204  * `__attribute__((warn_unused_return))`.  Makes it easy to find those places if
205  * they need to be fixed, and document the intent in cases where the return
206  * value really can be ignored.  It also makes it easy to adapt the approach so
207  * that they don't produce warnings.  A cast to void doesn't remove the warning
208  * in gcc, while adding a dummy variable can cause warnings about an unused
209  * variable.
210  *
211  * \ingroup module_utility
212  */
213 #define GMX_IGNORE_RETURN_VALUE(call) ::gmx::internal::ignoreValueHelper(call)
214
215 #endif