Fix malformed CUDA version macro check
[alexxy/gromacs.git] / include / maths.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  * check out http://www.gromacs.org for more information.
7  * Copyright (c) 2012,2013, by the GROMACS development team, led by
8  * David van der Spoel, Berk Hess, Erik Lindahl, and including many
9  * others, as listed in the AUTHORS file in the top-level source
10  * 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
39 #ifndef _maths_h
40 #define _maths_h
41
42 #include <math.h>
43 #include "visibility.h"
44 #include "types/simple.h"
45 #include "typedefs.h"
46
47 #ifdef __cplusplus
48 extern "C" {
49 #endif
50
51 #ifndef M_PI
52 #define M_PI        3.14159265358979323846
53 #endif
54
55 #ifndef M_PI_2
56 #define M_PI_2      1.57079632679489661923
57 #endif
58
59 #ifndef M_2PI
60 #define M_2PI       6.28318530717958647692
61 #endif
62
63 #ifndef M_SQRT2
64 #define M_SQRT2 sqrt(2.0)
65 #endif
66
67 #ifndef M_1_PI
68 #define M_1_PI      0.31830988618379067154
69 #endif
70
71 #ifndef M_FLOAT_1_SQRTPI /* used in CUDA kernels */
72 /* 1.0 / sqrt(M_PI) */
73 #define M_FLOAT_1_SQRTPI 0.564189583547756f
74 #endif
75
76 #ifndef M_1_SQRTPI
77 /* 1.0 / sqrt(M_PI) */
78 #define M_1_SQRTPI 0.564189583547756
79 #endif
80
81 #ifndef M_2_SQRTPI
82 /* 2.0 / sqrt(M_PI) */
83 #define M_2_SQRTPI  1.128379167095513
84 #endif
85
86 /* Suzuki-Yoshida Constants, for n=3 and n=5, for symplectic integration  */
87 /* for n=1, w0 = 1 */
88 /* for n=3, w0 = w2 = 1/(2-2^-(1/3)), w1 = 1-2*w0 */
89 /* for n=5, w0 = w1 = w3 = w4 = 1/(4-4^-(1/3)), w1 = 1-4*w0 */
90
91 #define MAX_SUZUKI_YOSHIDA_NUM 5
92 #define SUZUKI_YOSHIDA_NUM  5
93
94 static const double  sy_const_1[] = { 1. };
95 static const double  sy_const_3[] = { 0.828981543588751, -0.657963087177502, 0.828981543588751 };
96 static const double  sy_const_5[] = { 0.2967324292201065, 0.2967324292201065, -0.186929716880426, 0.2967324292201065, 0.2967324292201065 };
97
98 static const double* sy_const[] = {
99     NULL,
100     sy_const_1,
101     NULL,
102     sy_const_3,
103     NULL,
104     sy_const_5
105 };
106
107 /*
108    static const double sy_const[MAX_SUZUKI_YOSHIDA_NUM+1][MAX_SUZUKI_YOSHIDA_NUM+1] = {
109     {},
110     {1},
111     {},
112     {0.828981543588751,-0.657963087177502,0.828981543588751},
113     {},
114     {0.2967324292201065,0.2967324292201065,-0.186929716880426,0.2967324292201065,0.2967324292201065}
115    };*/
116
117 GMX_LIBGMX_EXPORT
118 int     gmx_nint(real a);
119 real    sign(real x, real y);
120
121 real    cuberoot (real a);
122 GMX_LIBGMX_EXPORT
123 double  gmx_erfd(double x);
124 GMX_LIBGMX_EXPORT
125 double  gmx_erfcd(double x);
126 GMX_LIBGMX_EXPORT
127 float   gmx_erff(float x);
128 GMX_LIBGMX_EXPORT
129 float   gmx_erfcf(float x);
130 #ifdef GMX_DOUBLE
131 #define gmx_erf(x)   gmx_erfd(x)
132 #define gmx_erfc(x)  gmx_erfcd(x)
133 #else
134 #define gmx_erf(x)   gmx_erff(x)
135 #define gmx_erfc(x)  gmx_erfcf(x)
136 #endif
137
138 GMX_LIBGMX_EXPORT
139 gmx_bool gmx_isfinite(real x);
140
141 /*! \brief Check if two numbers are within a tolerance
142  *
143  *  This routine checks if the relative difference between two numbers is
144  *  approximately within the given tolerance, defined as
145  *  fabs(f1-f2)<=tolerance*fabs(f1+f2).
146  *
147  *  To check if two floating-point numbers are almost identical, use this routine
148  *  with the tolerance GMX_REAL_EPS, or GMX_DOUBLE_EPS if the check should be
149  *  done in double regardless of Gromacs precision.
150  *
151  *  To check if two algorithms produce similar results you will normally need
152  *  to relax the tolerance significantly since many operations (e.g. summation)
153  *  accumulate floating point errors.
154  *
155  *  \param f1  First number to compare
156  *  \param f2  Second number to compare
157  *  \param tol Tolerance to use
158  *
159  *  \return 1 if the relative difference is within tolerance, 0 if not.
160  */
161 static int
162 gmx_within_tol(double   f1,
163                double   f2,
164                double   tol)
165 {
166     /* The or-equal is important - otherwise we return false if f1==f2==0 */
167     if (fabs(f1-f2) <= tol*0.5*(fabs(f1)+fabs(f2)) )
168     {
169         return 1;
170     }
171     else
172     {
173         return 0;
174     }
175 }
176
177
178
179 /**
180  * Check if a number is smaller than some preset safe minimum
181  * value, currently defined as GMX_REAL_MIN/GMX_REAL_EPS.
182  *
183  * If a number is smaller than this value we risk numerical overflow
184  * if any number larger than 1.0/GMX_REAL_EPS is divided by it.
185  *
186  * \return 1  if 'almost' numerically zero, 0 otherwise.
187  */
188 static int
189 gmx_numzero(double a)
190 {
191     return gmx_within_tol(a, 0.0, GMX_REAL_MIN/GMX_REAL_EPS);
192 }
193
194
195 static real
196 gmx_log2(real x)
197 {
198     const real iclog2 = 1.0/log( 2.0 );
199
200     return log( x ) * iclog2;
201 }
202
203 /*! /brief Multiply two large ints
204  *
205  *  Returns true when overflow did not occur.
206  */
207 GMX_LIBGMX_EXPORT
208 gmx_bool
209 check_int_multiply_for_overflow(gmx_large_int_t  a,
210                                 gmx_large_int_t  b,
211                                 gmx_large_int_t *result);
212
213 static int gmx_greatest_common_divisor(int p, int q)
214 {
215     int tmp;
216     while (q != 0)
217     {
218         tmp = q;
219         q = p % q;
220         p = tmp;
221     }
222     return p;
223 }
224
225 #ifdef __cplusplus
226 }
227 #endif
228
229 #endif  /* _maths_h */