Apply clang-format to source tree
[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,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.
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 =
75         setSimd4RealFrom1R(1.3333333332557231188); // mantissa 01010101010101010101010101010101
76 const Simd4Real rSimd4_logicalB =
77         setSimd4RealFrom1R(1.7999999998137354851); // mantissa 11001100110011001100110011001100
78 const Simd4Real rSimd4_logicalResultAnd =
79         setSimd4RealFrom1R(1.266666666604578495); // mantissa 01000100010001000100010001000100
80 const Simd4Real rSimd4_logicalResultOr =
81         setSimd4RealFrom1R(1.8666666664648801088); // mantissa 11011101110111011101110111011101
82 #            else                                  // GMX_DOUBLE
83 const Simd4Real rSimd4_logicalA = setSimd4RealFrom1R(1.3333282470703125); // mantissa 0101010101010101
84 const Simd4Real rSimd4_logicalB = setSimd4RealFrom1R(1.79998779296875); // mantissa 1100110011001100
85 const Simd4Real rSimd4_logicalResultAnd = setSimd4RealFrom1R(1.26666259765625); // mantissa 0100010001000100
86 const Simd4Real rSimd4_logicalResultOr = setSimd4RealFrom1R(1.8666534423828125); // mantissa 1101110111011101
87 #            endif                                 // GMX_DOUBLE
88 #        endif                                     // GMX_SIMD_HAVE_LOGICAL
89
90 ::std::vector<real> simd4Real2Vector(const Simd4Real simd4)
91 {
92     alignas(GMX_SIMD_ALIGNMENT) real mem[GMX_SIMD4_WIDTH];
93
94     store4(mem, simd4);
95     std::vector<real> v(mem, mem + GMX_SIMD4_WIDTH);
96
97     return v;
98 }
99
100 Simd4Real vector2Simd4Real(const std::vector<real>& v)
101 {
102     alignas(GMX_SIMD_ALIGNMENT) real mem[GMX_SIMD4_WIDTH];
103
104     for (int i = 0; i < GMX_SIMD4_WIDTH; i++)
105     {
106         mem[i] = v[i % v.size()]; // repeat vector contents to fill simd width
107     }
108     return load4(mem);
109 }
110
111 Simd4Real setSimd4RealFrom3R(real r0, real r1, real r2)
112 {
113     std::vector<real> v(3);
114     v[0] = r0;
115     v[1] = r1;
116     v[2] = r2;
117     return vector2Simd4Real(v);
118 }
119
120 Simd4Real 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 Simd4Test::compareSimd4RealUlp(const char*     refExpr,
131                                                         const char*     tstExpr,
132                                                         const Simd4Real ref,
133                                                         const Simd4Real tst)
134 {
135     return compareVectorRealUlp(refExpr, tstExpr, simd4Real2Vector(ref), simd4Real2Vector(tst));
136 }
137
138 testing::AssertionResult Simd4Test::compareSimd4RealEq(const char*     refExpr,
139                                                        const char*     tstExpr,
140                                                        const Simd4Real ref,
141                                                        const Simd4Real tst)
142 {
143     return compareVectorEq(refExpr, tstExpr, simd4Real2Vector(ref), simd4Real2Vector(tst));
144 }
145
146 #    endif // GMX_SIMD4_HAVE_REAL
147
148 /*! \} */
149 /*! \endcond */
150
151 } // namespace test
152 } // namespace gmx
153
154 #endif // GMX_SIMD