SIMD acceleration for LINCS
[alexxy/gromacs.git] / src / gromacs / pbcutil / pbc-simd.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2015, 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.
8  *
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.
13  *
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.
18  *
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.
23  *
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.
31  *
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.
34  */
35 /*! \libinternal \file
36  *
37  * \brief This file contains a definition, declaration and inline function
38  * for SIMD accelerated PBC calculations.
39  *
40  * \author Berk Hess <hess@kth.se>
41  * \inlibraryapi
42  * \ingroup module_pbcutil
43  */
44
45 #ifndef GMX_PBCUTIL_PBC_SIMD_H
46 #define GMX_PBCUTIL_PBC_SIMD_H
47
48 #include "config.h"
49
50 #include "gromacs/pbcutil/pbc.h"
51 #include "gromacs/simd/simd.h"
52 #include "gromacs/utility/fatalerror.h"
53
54 #ifdef __cplusplus
55 extern "C" {
56 #endif
57
58 /*! \cond INTERNAL */
59
60 /*! \brief Structure containing the PBC setup for SIMD PBC calculations.
61  *
62  * Without SIMD this is a dummy struct, so it can be declared and passed.
63  * This can avoid some ifdef'ing.
64  */
65 typedef struct {
66 #ifdef GMX_SIMD_HAVE_REAL
67     gmx_simd_real_t inv_bzz; /**< 1/box[ZZ][ZZ] */
68     gmx_simd_real_t inv_byy; /**< 1/box[YY][YY] */
69     gmx_simd_real_t inv_bxx; /**< 1/box[XX][XX] */
70     gmx_simd_real_t bzx;     /**< box[ZZ][XX] */
71     gmx_simd_real_t bzy;     /**< box[ZZ][YY] */
72     gmx_simd_real_t bzz;     /**< box[ZZ][ZZ] */
73     gmx_simd_real_t byx;     /**< box[YY][XX] */
74     gmx_simd_real_t byy;     /**< box[YY][YY] */
75     gmx_simd_real_t bxx;     /**< bo[XX][XX] */
76 #else
77     int             dum;     /**< Dummy variable to avoid empty struct */
78 #endif
79 } pbc_simd_t;
80
81 /*! \endcond */
82
83 /*! \brief Set the SIMD PBC data from a normal t_pbc struct.
84  *
85  * NULL can be passed for \p pbc, then no PBC will be used.
86  */
87 void set_pbc_simd(const t_pbc *pbc,
88                   pbc_simd_t  *pbc_simd);
89
90 #if defined GMX_SIMD_HAVE_REAL
91
92 /*! \brief Correct SIMD distance vector *dx,*dy,*dz for PBC using SIMD.
93  *
94  * For rectangular boxes all returned distance vectors are the shortest.
95  * For triclinic boxes only distances up to half the smallest box diagonal
96  * element are guaranteed to be the shortest. This means that distances from
97  * 0.5/sqrt(2) times a box vector length (e.g. for a rhombic dodecahedron)
98  * can use a more distant periodic image.
99  * Note that this routine always does PBC arithmetic, even for dimensions
100  * without PBC. But on modern processors the overhead of this, often called,
101  * routine should be low. On e.g. Intel Haswell/Broadwell it takes 8 cycles.
102  */
103 static gmx_inline void gmx_simdcall
104 pbc_correct_dx_simd(gmx_simd_real_t  *dx,
105                     gmx_simd_real_t  *dy,
106                     gmx_simd_real_t  *dz,
107                     const pbc_simd_t *pbc)
108 {
109     gmx_simd_real_t shz, shy, shx;
110
111 #if defined _MSC_VER && _MSC_VER < 1700
112     /* The caller side should make sure we never end up here.
113      * TODO Black-list _MSC_VER < 1700 when it's old enough, so we can rid
114      * of this code complication.
115      */
116     gmx_incons("pbc_correct_dx_simd was called for code compiled with MSVC 2010 or older, which produces incorrect code (probably corrupts memory) and therefore this function should not have been called");
117 #endif
118
119     shz = gmx_simd_round_r(gmx_simd_mul_r(*dz, pbc->inv_bzz));
120     *dx = gmx_simd_fnmadd_r(shz, pbc->bzx, *dx);
121     *dy = gmx_simd_fnmadd_r(shz, pbc->bzy, *dy);
122     *dz = gmx_simd_fnmadd_r(shz, pbc->bzz, *dz);
123
124     shy = gmx_simd_round_r(gmx_simd_mul_r(*dy, pbc->inv_byy));
125     *dx = gmx_simd_fnmadd_r(shy, pbc->byx, *dx);
126     *dy = gmx_simd_fnmadd_r(shy, pbc->byy, *dy);
127
128     shx = gmx_simd_round_r(gmx_simd_mul_r(*dx, pbc->inv_bxx));
129     *dx = gmx_simd_fnmadd_r(shx, pbc->bxx, *dx);
130 }
131
132 #endif /* GMX_SIMD_HAVE_REAL */
133
134 #ifdef __cplusplus
135 }
136 #endif
137
138 #endif