Narrow-down Intel-specific #ifdef's
[alexxy/gromacs.git] / api / legacy / include / gromacs / math / utilities.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.
7  * Copyright (c) 2018,2019,2020, by the GROMACS development team, led by
8  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
9  * and including many others, as listed in the AUTHORS file in the
10  * top-level source directory and at http://www.gromacs.org.
11  *
12  * GROMACS is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU Lesser General Public License
14  * as published by the Free Software Foundation; either version 2.1
15  * of the License, or (at your option) any later version.
16  *
17  * GROMACS is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20  * Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public
23  * License along with GROMACS; if not, see
24  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
26  *
27  * If you want to redistribute modifications to GROMACS, please
28  * consider that scientific software is very special. Version
29  * control is crucial - bugs must be traceable. We will be happy to
30  * consider code for inclusion in the official distribution, but
31  * derived work must not be called official GROMACS. Details are found
32  * in the README & COPYING files - if they are missing, get the
33  * official version at http://www.gromacs.org.
34  *
35  * To help us fund GROMACS development, we humbly ask that you cite
36  * the research papers on the package. Check out http://www.gromacs.org.
37  */
38 #ifndef GMX_MATH_UTILITIES_H
39 #define GMX_MATH_UTILITIES_H
40
41 #include <limits.h>
42
43 #include <cmath>
44
45 #include "gromacs/utility/basedefinitions.h"
46 #include "gromacs/utility/real.h"
47
48 #ifndef M_PI
49 #    define M_PI 3.14159265358979323846
50 #endif
51
52 #ifndef M_PI_2
53 #    define M_PI_2 1.57079632679489661923
54 #endif
55
56 #ifndef M_2PI
57 #    define M_2PI 6.28318530717958647692
58 #endif
59
60 #ifndef M_SQRT2
61 #    define M_SQRT2 sqrt(2.0)
62 #endif
63
64 #ifndef M_1_PI
65 #    define M_1_PI 0.31830988618379067154
66 #endif
67
68 #ifndef M_FLOAT_1_SQRTPI /* used in GPU kernels */
69 /* 1.0 / sqrt(M_PI) */
70 #    define M_FLOAT_1_SQRTPI 0.564189583547756f
71 #endif
72
73 #ifndef M_1_SQRTPI
74 /* 1.0 / sqrt(M_PI) */
75 #    define M_1_SQRTPI 0.564189583547756
76 #endif
77
78 #ifndef M_2_SQRTPI
79 /* 2.0 / sqrt(M_PI) */
80 #    define M_2_SQRTPI 1.128379167095513
81 #endif
82
83 /*! \brief Enum to select safe or highly unsafe (faster) math functions.
84  *
85  *  Normally all the Gromacs math functions should apply reasonable care with
86  *  input arguments. While we do not necessarily adhere strictly to IEEE
87  *  (in particular not for arguments that might result in NaN, inf, etc.), the
88  *  functions should return reasonable values or e.g. clamp results to zero.
89  *
90  *  However, in a few cases where we are extremely performance-sensitive it
91  *  makes sense to forego these checks too in cases where we know the exact
92  *  properties if the input data, and we really need to save every cycle we can.
93  *
94  *  This class is typically used as a template parameter to such calls to enable
95  *  the caller to select the level of aggressiveness. We should always use the
96  *  safe alternative as the default value, and document carefully what might
97  *  happen with the unsafe alternative.
98  */
99 enum class MathOptimization
100 {
101     Safe,  //!< Don't do unsafe optimizations. This should always be default.
102     Unsafe //!< Allow optimizations that can be VERY dangerous for general code.
103 };
104
105 /*! \brief Check if two numbers are within a tolerance
106  *
107  *  This routine checks if the relative difference between two numbers is
108  *  approximately within the given tolerance, defined as
109  *  fabs(f1-f2)<=tolerance*fabs(f1+f2).
110  *
111  *  To check if two floating-point numbers are almost identical, use this routine
112  *  with the tolerance GMX_REAL_EPS, or GMX_DOUBLE_EPS if the check should be
113  *  done in double regardless of Gromacs precision.
114  *
115  *  To check if two algorithms produce similar results you will normally need
116  *  to relax the tolerance significantly since many operations (e.g. summation)
117  *  accumulate floating point errors.
118  *
119  *  \param f1  First number to compare
120  *  \param f2  Second number to compare
121  *  \param tol Tolerance to use
122  *
123  *  \return 1 if the relative difference is within tolerance, 0 if not.
124  */
125 bool gmx_within_tol(double f1, double f2, double tol);
126
127 /*!
128  * \brief Check if a number is smaller than some preset safe minimum
129  * value, currently defined as GMX_REAL_MIN/GMX_REAL_EPS.
130  *
131  * If a number is smaller than this value we risk numerical overflow
132  * if any number larger than 1.0/GMX_REAL_EPS is divided by it.
133  *
134  * \return True if 'almost' numerically zero, false otherwise.
135  */
136 bool gmx_numzero(double a);
137
138 /*! \brief Multiply two large ints
139  *
140  * \return False iff overflow occurred
141  */
142 gmx_bool check_int_multiply_for_overflow(int64_t a, int64_t b, int64_t* result);
143
144 /*! \brief Enable floating-point exceptions if supported on OS
145  *
146  * Enables division-by-zero, invalid value, and overflow.
147  *
148  * \returns 0 if successful in enabling exceptions, anything else in case of failure/unsupported OS.
149  */
150 int gmx_feenableexcept();
151
152 /*! \brief Disable floating-point exceptions if supported on OS
153  *
154  * Disables division-by-zero, invalid value, and overflow.
155  *
156  * \returns 0 if successful in disabling exceptions, anything else in case of failure/unsupported OS.
157  */
158 int gmx_fedisableexcept();
159
160 #endif