147493db016fffc0e422d02a7d3a09311ab385ec
[alexxy/gromacs.git] / src / gromacs / simd / tests / simd_vector_operations.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2014,2015,2017, 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 #include "gmxpre.h"
36
37 #include <cmath>
38
39 #include "gromacs/simd/simd.h"
40 #include "gromacs/simd/vector_operations.h"
41
42 #include "data.h"
43 #include "simd.h"
44
45 #if GMX_SIMD
46
47 namespace gmx
48 {
49 namespace test
50 {
51 namespace
52 {
53
54 /*! \cond internal */
55 /*! \addtogroup module_simd */
56 /*! \{ */
57
58 #if GMX_SIMD_HAVE_REAL
59
60 /*! \internal \brief Test fixture for vector operations tests (identical to the generic \ref SimdTest) */
61 typedef SimdTest SimdVectorOperationsTest;
62
63 TEST_F(SimdVectorOperationsTest, iprod)
64 {
65     SimdReal aX       = rSimd_c0c1c2;
66     SimdReal aY       = rSimd_c3c4c5;
67     SimdReal aZ       = rSimd_c6c7c8;
68     SimdReal bX       = rSimd_c3c0c4;
69     SimdReal bY       = rSimd_c4c6c8;
70     SimdReal bZ       = rSimd_c7c2c3;
71     SimdReal iprodRef = setSimdRealFrom3R(c0*c3 + c3*c4 + c6*c7,
72                                           c1*c0 + c4*c6 + c7*c2,
73                                           c2*c4 + c5*c8 + c8*c3);
74
75     setUlpTol(2);
76     GMX_EXPECT_SIMD_REAL_NEAR(iprodRef, iprod(aX, aY, aZ, bX, bY, bZ));
77 }
78
79 TEST_F(SimdVectorOperationsTest, norm2)
80 {
81     SimdReal simdX    = rSimd_c0c1c2;
82     SimdReal simdY    = rSimd_c3c4c5;
83     SimdReal simdZ    = rSimd_c6c7c8;
84     SimdReal norm2Ref = setSimdRealFrom3R(c0*c0 + c3*c3 + c6*c6,
85                                           c1*c1 + c4*c4 + c7*c7,
86                                           c2*c2 + c5*c5 + c8*c8);
87
88     setUlpTol(2);
89     GMX_EXPECT_SIMD_REAL_NEAR(norm2Ref, norm2(simdX, simdY, simdZ));
90 }
91
92 TEST_F(SimdVectorOperationsTest, cprod)
93 {
94     SimdReal aX    = rSimd_c0c1c2;
95     SimdReal aY    = rSimd_c3c4c5;
96     SimdReal aZ    = rSimd_c6c7c8;
97     SimdReal bX    = rSimd_c3c0c4;
98     SimdReal bY    = rSimd_c4c6c8;
99     SimdReal bZ    = rSimd_c7c2c3;
100     //The SIMD version might use FMA. If we don't force FMA for the reference value, the compiler is free to use FMA
101     //for either product. If the compiler uses FMA for one product and the SIMD version uses FMA for the other, the
102     //rounding error of each product adds up and the total possible ulp-error is 12.
103     SimdReal refcX = setSimdRealFrom3R( std::fma(-c6, c4, c3*c7), std::fma(-c7, c6, c4*c2), std::fma(-c8, c8, c5*c3));
104     SimdReal refcY = setSimdRealFrom3R( std::fma(-c0, c7, c6*c3), std::fma(-c1, c2, c7*c0), std::fma(-c2, c3, c8*c4));
105     SimdReal refcZ = setSimdRealFrom3R( std::fma(-c3, c3, c0*c4), std::fma(-c4, c0, c1*c6), std::fma(-c5, c4, c2*c8));
106     SimdReal cX, cY, cZ;
107
108     //The test assumes that cprod uses FMA on architectures which have FMA so that the compiler can't choose which
109     //product is computed with FMA.
110     cprod(aX, aY, aZ, bX, bY, bZ, &cX, &cY, &cZ);
111
112     //The test values cannot be computed without FMA for the case that SIMD has no FMA. Even if no explicit FMA were
113     //used, the compiler could choose to use FMA. This causes up to 6upl error because of the product is up to 6 times
114     //larger than the final result after the difference.
115     setUlpTol(GMX_SIMD_HAVE_FMA ? ulpTol_ : 6);
116
117     GMX_EXPECT_SIMD_REAL_NEAR(refcX, cX);
118     GMX_EXPECT_SIMD_REAL_NEAR(refcY, cY);
119     GMX_EXPECT_SIMD_REAL_NEAR(refcZ, cZ);
120 }
121
122 #endif      // GMX_SIMD_HAVE_REAL
123
124 /*! \} */
125 /*! \endcond */
126
127 }      // namespace
128 }      // namespace
129 }      // namespace
130
131 #endif // GMX_SIMD