Merge branch release-2018
[alexxy/gromacs.git] / src / gromacs / simd / tests / simd4.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2014,2015,2017,2018, 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 "simd4.h"
38
39 #include "gromacs/simd/simd.h"
40 #include "gromacs/utility/basedefinitions.h"
41
42 #include "data.h"
43
44 #if GMX_SIMD
45
46 namespace gmx
47 {
48 namespace test
49 {
50
51 /*! \cond internal */
52 /*! \addtogroup module_simd */
53 /*! \{ */
54
55 #if GMX_SIMD4_HAVE_REAL
56
57 const Simd4Real rSimd4_c0c1c2   = setSimd4RealFrom3R( c0, c1, c2);
58 const Simd4Real rSimd4_c3c4c5   = setSimd4RealFrom3R( c3, c4, c5);
59 const Simd4Real rSimd4_c6c7c8   = setSimd4RealFrom3R( c6, c7, c8);
60 const Simd4Real rSimd4_c3c0c4   = setSimd4RealFrom3R( c3, c0, c4);
61 const Simd4Real rSimd4_c4c6c8   = setSimd4RealFrom3R( c4, c6, c8);
62 const Simd4Real rSimd4_c7c2c3   = setSimd4RealFrom3R( c7, c2, c3);
63 const Simd4Real rSimd4_m0m1m2   = setSimd4RealFrom3R(-c0, -c1, -c2);
64 const Simd4Real rSimd4_m3m0m4   = setSimd4RealFrom3R(-c3, -c0, -c4);
65 const Simd4Real rSimd4_2p25     = setSimd4RealFrom1R(2.25);
66 const Simd4Real rSimd4_3p75     = setSimd4RealFrom1R(3.75);
67 const Simd4Real rSimd4_m2p25    = setSimd4RealFrom1R(-2.25);
68 const Simd4Real rSimd4_m3p75    = setSimd4RealFrom1R(-3.75);
69
70 #if GMX_SIMD_HAVE_LOGICAL
71 // The numbers below all have exponent (2^0), which will not change with AND/OR operations.
72 // We also leave the last part of the mantissa as zeros, to avoid rounding issues in the compiler
73 #if GMX_DOUBLE
74 const Simd4Real rSimd4_logicalA         = setSimd4RealFrom1R(1.3333333332557231188); // mantissa 01010101010101010101010101010101
75 const Simd4Real rSimd4_logicalB         = setSimd4RealFrom1R(1.7999999998137354851); // mantissa 11001100110011001100110011001100
76 const Simd4Real rSimd4_logicalResultAnd = setSimd4RealFrom1R(1.266666666604578495);  // mantissa 01000100010001000100010001000100
77 const Simd4Real rSimd4_logicalResultOr  = setSimd4RealFrom1R(1.8666666664648801088); // mantissa 11011101110111011101110111011101
78 #else                                                                                // GMX_DOUBLE
79 const Simd4Real rSimd4_logicalA         = setSimd4RealFrom1R(1.3333282470703125);    // mantissa 0101010101010101
80 const Simd4Real rSimd4_logicalB         = setSimd4RealFrom1R(1.79998779296875);      // mantissa 1100110011001100
81 const Simd4Real rSimd4_logicalResultAnd = setSimd4RealFrom1R(1.26666259765625);      // mantissa 0100010001000100
82 const Simd4Real rSimd4_logicalResultOr  = setSimd4RealFrom1R(1.8666534423828125);    // mantissa 1101110111011101
83 #endif                                                                               // GMX_DOUBLE
84 #endif                                                                               // GMX_SIMD_HAVE_LOGICAL
85
86 ::std::vector<real>
87 simd4Real2Vector(const Simd4Real simd4)
88 {
89     alignas(GMX_SIMD_ALIGNMENT) real  mem[GMX_SIMD4_WIDTH];
90
91     store4(mem, simd4);
92     std::vector<real>   v(mem, mem+GMX_SIMD4_WIDTH);
93
94     return v;
95 }
96
97 Simd4Real
98 vector2Simd4Real(const std::vector<real> &v)
99 {
100     alignas(GMX_SIMD_ALIGNMENT) real  mem[GMX_SIMD4_WIDTH];
101
102     for (int i = 0; i < GMX_SIMD4_WIDTH; i++)
103     {
104         mem[i] = v[i % v.size()];  // repeat vector contents to fill simd width
105     }
106     return load4(mem);
107 }
108
109 Simd4Real
110 setSimd4RealFrom3R(real r0, real r1, real r2)
111 {
112     std::vector<real> v(3);
113     v[0] = r0;
114     v[1] = r1;
115     v[2] = r2;
116     return vector2Simd4Real(v);
117 }
118
119 Simd4Real
120 setSimd4RealFrom1R(real value)
121 {
122     std::vector<real> v(GMX_SIMD4_WIDTH);
123     for (int i = 0; i < GMX_SIMD4_WIDTH; i++)
124     {
125         v[i] = value;
126     }
127     return vector2Simd4Real(v);
128 }
129
130 testing::AssertionResult
131 Simd4Test::compareSimd4RealUlp(const char *  refExpr,     const char *  tstExpr,
132                                const Simd4Real ref, const Simd4Real tst)
133 {
134     return compareVectorRealUlp(refExpr, tstExpr, simd4Real2Vector(ref), simd4Real2Vector(tst));
135 }
136
137 testing::AssertionResult
138 Simd4Test::compareSimd4RealEq(const char * refExpr, const char * tstExpr,
139                               const Simd4Real ref, const Simd4Real tst)
140 {
141     return compareVectorEq(refExpr, tstExpr, simd4Real2Vector(ref), simd4Real2Vector(tst));
142 }
143
144 #endif  // GMX_SIMD4_HAVE_REAL
145
146 /*! \} */
147 /*! \endcond */
148
149 }      // namespace
150 }      // namespace
151
152 #endif // GMX_SIMD