2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2015,2016,2018,2019, by the GROMACS development team, led by
5 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6 * and including many others, as listed in the AUTHORS file in the
7 * top-level source directory and at http://www.gromacs.org.
9 * GROMACS is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1
12 * of the License, or (at your option) any later version.
14 * GROMACS is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with GROMACS; if not, see
21 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * If you want to redistribute modifications to GROMACS, please
25 * consider that scientific software is very special. Version
26 * control is crucial - bugs must be traceable. We will be happy to
27 * consider code for inclusion in the official distribution, but
28 * derived work must not be called official GROMACS. Details are found
29 * in the README & COPYING files - if they are missing, get the
30 * official version at http://www.gromacs.org.
32 * To help us fund GROMACS development, we humbly ask that you cite
33 * the research papers on the package. Check out http://www.gromacs.org.
37 * Declares simple math functions
39 * \author Erik Lindahl <erik.lindahl@gmail.com>
41 * \ingroup module_math
43 #ifndef GMX_MATH_FUNCTIONS_H
44 #define GMX_MATH_FUNCTIONS_H
49 #include "gromacs/utility/gmxassert.h"
50 #include "gromacs/utility/real.h"
55 /*! \brief Evaluate log2(n) for integer n statically at compile time.
57 * Use as staticLog2<n>::value, where n must be a positive integer.
58 * Negative n will be reinterpreted as the corresponding unsigned integer,
59 * and you will get a compile-time error if n==0.
60 * The calculation is done by recursively dividing n by 2 (until it is 1),
61 * and incrementing the result by 1 in each step.
63 * \tparam n Value to recursively calculate log2(n) for
65 template<std::uint64_t n>
68 static const int value = StaticLog2<n / 2>::value
69 + 1; //!< Variable value used for recursive static calculation of Log2(int)
72 /*! \brief Specialization of StaticLog2<n> for n==1.
74 * This specialization provides the final value in the recursion; never
75 * call it directly, but use StaticLog2<n>::value.
80 static const int value = 0; //!< Base value for recursive static calculation of Log2(int)
83 /*! \brief Specialization of StaticLog2<n> for n==0.
85 * This specialization should never actually be used since log2(0) is
86 * negative infinity. However, since Log2() is often used to calculate the number
87 * of bits needed for a number, we might be using the value 0 with a conditional
88 * statement around the logarithm. Depending on the compiler the expansion of
89 * the template can occur before the conditional statement, so to avoid infinite
90 * recursion we need a specialization for the case n==0.
95 static const int value = -1; //!< Base value for recursive static calculation of Log2(int)
99 /*! \brief Compute floor of logarithm to base 2, 32 bit signed argument
101 * \param x 32-bit signed argument
105 * \note This version of the overloaded function will assert that x is
108 unsigned int log2I(std::int32_t x);
110 /*! \brief Compute floor of logarithm to base 2, 64 bit signed argument
112 * \param x 64-bit signed argument
116 * \note This version of the overloaded function will assert that x is
119 unsigned int log2I(std::int64_t x);
121 /*! \brief Compute floor of logarithm to base 2, 32 bit unsigned argument
123 * \param x 32-bit unsigned argument
127 * \note This version of the overloaded function uses unsigned arguments to
128 * be able to handle arguments using all 32 bits.
130 unsigned int log2I(std::uint32_t x);
132 /*! \brief Compute floor of logarithm to base 2, 64 bit unsigned argument
134 * \param x 64-bit unsigned argument
138 * \note This version of the overloaded function uses unsigned arguments to
139 * be able to handle arguments using all 64 bits.
141 unsigned int log2I(std::uint64_t x);
143 /*! \brief Find greatest common divisor of two numbers
145 * \param p First number, positive
146 * \param q Second number, positive
148 * \return Greatest common divisor of p and q
150 std::int64_t greatestCommonDivisor(std::int64_t p, std::int64_t q);
153 /*! \brief Calculate 1.0/sqrt(x) in single precision
155 * \param x Positive value to calculate inverse square root for
157 * For now this is implemented with std::sqrt(x) since gcc seems to do a
158 * decent job optimizing it. However, we might decide to use instrinsics
159 * or compiler-specific functions in the future.
161 * \return 1.0/sqrt(x)
163 static inline float invsqrt(float x)
165 return 1.0F / std::sqrt(x);
168 /*! \brief Calculate 1.0/sqrt(x) in double precision, but single range
170 * \param x Positive value to calculate inverse square root for, must be
171 * in the input domain valid for single precision.
173 * For now this is implemented with std::sqrt(x). However, we might
174 * decide to use instrinsics or compiler-specific functions in the future, and
175 * then we want to have the freedom to do the first step in single precision.
177 * \return 1.0/sqrt(x)
179 static inline double invsqrt(double x)
181 return 1.0 / std::sqrt(x);
184 /*! \brief Calculate 1.0/sqrt(x) for integer x in double precision.
186 * \param x Positive value to calculate inverse square root for.
188 * \return 1.0/sqrt(x)
190 static inline double invsqrt(int x)
192 return invsqrt(static_cast<double>(x));
195 /*! \brief Calculate inverse cube root of x in single precision
201 * This routine is typically faster than using std::pow().
203 static inline float invcbrt(float x)
205 return 1.0F / std::cbrt(x);
208 /*! \brief Calculate inverse sixth root of x in double precision
214 * This routine is typically faster than using std::pow().
216 static inline double invcbrt(double x)
218 return 1.0 / std::cbrt(x);
221 /*! \brief Calculate inverse sixth root of integer x in double precision
227 * This routine is typically faster than using std::pow().
229 static inline double invcbrt(int x)
231 return 1.0 / std::cbrt(x);
234 /*! \brief Calculate sixth root of x in single precision.
236 * \param x Argument, must be greater than or equal to zero.
240 * This routine is typically faster than using std::pow().
242 static inline float sixthroot(float x)
244 return std::sqrt(std::cbrt(x));
247 /*! \brief Calculate sixth root of x in double precision.
249 * \param x Argument, must be greater than or equal to zero.
253 * This routine is typically faster than using std::pow().
255 static inline double sixthroot(double x)
257 return std::sqrt(std::cbrt(x));
260 /*! \brief Calculate sixth root of integer x, return double.
262 * \param x Argument, must be greater than or equal to zero.
266 * This routine is typically faster than using std::pow().
268 static inline double sixthroot(int x)
270 return std::sqrt(std::cbrt(x));
273 /*! \brief Calculate inverse sixth root of x in single precision
275 * \param x Argument, must be greater than zero.
279 * This routine is typically faster than using std::pow().
281 static inline float invsixthroot(float x)
283 return invsqrt(std::cbrt(x));
286 /*! \brief Calculate inverse sixth root of x in double precision
288 * \param x Argument, must be greater than zero.
292 * This routine is typically faster than using std::pow().
294 static inline double invsixthroot(double x)
296 return invsqrt(std::cbrt(x));
299 /*! \brief Calculate inverse sixth root of integer x in double precision
301 * \param x Argument, must be greater than zero.
305 * This routine is typically faster than using std::pow().
307 static inline double invsixthroot(int x)
309 return invsqrt(std::cbrt(x));
312 /*! \brief calculate x^2
314 * \tparam T Type of argument and return value
325 /*! \brief calculate x^3
327 * \tparam T Type of argument and return value
335 return x * square(x);
338 /*! \brief calculate x^4
340 * \tparam T Type of argument and return value
348 return square(square(x));
351 /*! \brief calculate x^5
353 * \tparam T Type of argument and return value
361 return x * power4(x);
364 /*! \brief calculate x^6
366 * \tparam T Type of argument and return value
374 return square(power3(x));
377 /*! \brief calculate x^12
379 * \tparam T Type of argument and return value
387 return square(power6(x));
390 /*! \brief Maclaurin series for sinh(x)/x.
392 * Used for NH chains and MTTK pressure control.
393 * Here, we compute it to 10th order, which might be an overkill.
394 * 8th is probably enough, but it's not very much more expensive.
396 static inline real series_sinhx(real x)
403 * (1 + (x2 / 42.0_real) * (1 + (x2 / 72.0_real) * (1 + (x2 / 110.0_real))))));
406 /*! \brief Inverse error function, double precision.
408 * \param x Argument, should be in the range -1.0 < x < 1.0
410 * \return The inverse of the error function if the argument is inside the
411 * range, +/- infinity if it is exactly 1.0 or -1.0, and NaN otherwise.
413 double erfinv(double x);
415 /*! \brief Inverse error function, single precision.
417 * \param x Argument, should be in the range -1.0 < x < 1.0
419 * \return The inverse of the error function if the argument is inside the
420 * range, +/- infinity if it is exactly 1.0 or -1.0, and NaN otherwise.
422 float erfinv(float x);
424 /*! \brief Exact integer division, 32bit.
426 * \param a dividend. Function asserts that it is a multiple of divisor
429 * \return quotient of division
431 constexpr int32_t exactDiv(int32_t a, int32_t b)
433 return GMX_ASSERT(a % b == 0, "exactDiv called with non-divisible arguments"), a / b;
436 //! Exact integer division, 64bit.
437 constexpr int64_t exactDiv(int64_t a, int64_t b)
439 return GMX_ASSERT(a % b == 0, "exactDiv called with non-divisible arguments"), a / b;
442 /*! \brief Round float to int
444 * Rounding behavior is round to nearest. Rounding of halfway cases is implemention defined
445 * (either halway to even or halway away from zero).
447 /* Implementation details: It is assumed that FE_TONEAREST is default and not changed by anyone.
448 * Currently the implementation is using rint(f) because 1) on all known HW that is faster than
449 * lround and 2) some compilers (e.g. clang (#22944) and icc) don't optimize (l)lrint(f) well.
450 * GCC(>=4.7) optimizes (l)lrint(f) well but with "-fno-math-errno -funsafe-math-optimizations"
451 * rint(f) is optimized as well. This avoids using intrinsics.
452 * rint(f) followed by float/double to int/int64 conversion produces the same result as directly
453 * rounding to int/int64.
455 static inline int roundToInt(float x)
457 return static_cast<int>(rintf(x));
459 //! Round double to int
460 static inline int roundToInt(double x)
462 return static_cast<int>(rint(x));
464 //! Round float to int64_t
465 static inline int64_t roundToInt64(float x)
467 return static_cast<int>(rintf(x));
469 //! Round double to int64_t
470 static inline int64_t roundToInt64(double x)
472 return static_cast<int>(rint(x));
478 #endif // GMX_MATH_FUNCTIONS_H