2 * This file is part of the GROMACS molecular simulation package.
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,2021, 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.
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.
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.
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.
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.
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.
38 #ifndef GMX_MATH_UTILITIES_H
39 #define GMX_MATH_UTILITIES_H
46 /*! \brief Enum to select safe or highly unsafe (faster) math functions.
48 * Normally all the Gromacs math functions should apply reasonable care with
49 * input arguments. While we do not necessarily adhere strictly to IEEE
50 * (in particular not for arguments that might result in NaN, inf, etc.), the
51 * functions should return reasonable values or e.g. clamp results to zero.
53 * However, in a few cases where we are extremely performance-sensitive it
54 * makes sense to forego these checks too in cases where we know the exact
55 * properties if the input data, and we really need to save every cycle we can.
57 * This class is typically used as a template parameter to such calls to enable
58 * the caller to select the level of aggressiveness. We should always use the
59 * safe alternative as the default value, and document carefully what might
60 * happen with the unsafe alternative.
62 enum class MathOptimization
64 Safe, //!< Don't do unsafe optimizations. This should always be default.
65 Unsafe //!< Allow optimizations that can be VERY dangerous for general code.
68 /*! \brief Check if two numbers are within a tolerance
70 * This routine checks if the relative difference between two numbers is
71 * approximately within the given tolerance, defined as
72 * fabs(f1-f2)<=tolerance*fabs(f1+f2).
74 * To check if two floating-point numbers are almost identical, use this routine
75 * with the tolerance GMX_REAL_EPS, or GMX_DOUBLE_EPS if the check should be
76 * done in double regardless of Gromacs precision.
78 * To check if two algorithms produce similar results you will normally need
79 * to relax the tolerance significantly since many operations (e.g. summation)
80 * accumulate floating point errors.
82 * \param f1 First number to compare
83 * \param f2 Second number to compare
84 * \param tol Tolerance to use
86 * \return 1 if the relative difference is within tolerance, 0 if not.
88 bool gmx_within_tol(double f1, double f2, double tol);
91 * \brief Check if a number is smaller than some preset safe minimum
92 * value, currently defined as GMX_REAL_MIN/GMX_REAL_EPS.
94 * If a number is smaller than this value we risk numerical overflow
95 * if any number larger than 1.0/GMX_REAL_EPS is divided by it.
97 * \return True if 'almost' numerically zero, false otherwise.
99 bool gmx_numzero(double a);
101 /*! \brief Multiply two large ints
103 * \return False iff overflow occurred
105 bool check_int_multiply_for_overflow(int64_t a, int64_t b, int64_t* result);
107 /*! \brief Enable floating-point exceptions if supported on OS
109 * Enables division-by-zero, invalid value, and overflow.
111 * \returns 0 if successful in enabling exceptions, anything else in case of failure/unsupported OS.
113 int gmx_feenableexcept();
115 /*! \brief Disable floating-point exceptions if supported on OS
117 * Disables division-by-zero, invalid value, and overflow.
119 * \returns 0 if successful in disabling exceptions, anything else in case of failure/unsupported OS.
121 int gmx_fedisableexcept();
123 /*! \brief Return true if the current build should enable floating-point exceptions by default.
125 * Currently, it returns true unless any of the following conditions are met:
127 * - SYCL build (Intel IGC, at least 1.0.5964, raises FP exceptions in JIT compilation),
128 * - - See https://github.com/intel/intel-graphics-compiler/issues/164
129 * - compilers with known buggy FP exception support (clang with any optimization)
130 * or suspected buggy FP exception support (gcc 7.* with optimization).
132 * Note that this function does not check whether the build/OS supports FP exceptions.
134 * \returns true if we should enable FP exceptions by default.
136 bool gmxShouldEnableFPExceptions();