79ff7783d87e0a6557ddccc062d0c82cf4355aec
[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, 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 "config.h"
38
39 #include "simd4.h"
40
41 namespace gmx
42 {
43 namespace test
44 {
45
46 /*! \cond internal */
47 /*! \addtogroup module_simd */
48 /*! \{ */
49
50 #ifdef GMX_SIMD4_HAVE_REAL
51
52 const gmx_simd4_real_t rSimd4_1_2_3    = setSimd4RealFrom3R(1, 2, 3);
53 const gmx_simd4_real_t rSimd4_4_5_6    = setSimd4RealFrom3R(4, 5, 6);
54 const gmx_simd4_real_t rSimd4_7_8_9    = setSimd4RealFrom3R(7, 8, 9);
55 const gmx_simd4_real_t rSimd4_5_7_9    = setSimd4RealFrom3R(5, 7, 9);
56 const gmx_simd4_real_t rSimd4_m1_m2_m3 = setSimd4RealFrom3R(-1, -2, -3);
57 const gmx_simd4_real_t rSimd4_3_1_4    = setSimd4RealFrom3R(3, 1, 4);
58 const gmx_simd4_real_t rSimd4_m3_m1_m4 = setSimd4RealFrom3R(-3, -1, -4);
59 const gmx_simd4_real_t rSimd4_2p25     = setSimd4RealFrom1R(2.25);
60 const gmx_simd4_real_t rSimd4_3p75     = setSimd4RealFrom1R(3.75);
61 const gmx_simd4_real_t rSimd4_m2p25    = setSimd4RealFrom1R(-2.25);
62 const gmx_simd4_real_t rSimd4_m3p75    = setSimd4RealFrom1R(-3.75);
63 const gmx_simd4_real_t rSimd4_Exp      = setSimd4RealFrom3R( 1.4055235171027452623914516e+18,
64                                                              5.3057102734253445623914516e-13,
65                                                              -2.1057102745623934534514516e+16);
66 #    if (defined GMX_SIMD_HAVE_DOUBLE) && (defined GMX_DOUBLE)
67 // Make sure we also test exponents outside single precision when we use double
68 const gmx_simd4_real_t  rSimd_ExpDouble = setSimd4RealFrom3R( 6.287393598732017379054414e+176,
69                                                               8.794495252903116023030553e-140,
70                                                               -3.637060701570496477655022e+202);
71 #    endif
72
73 ::std::vector<real>
74 simd4Real2Vector(const gmx_simd4_real_t simd4)
75 {
76     real                mem[GMX_SIMD4_WIDTH*2];
77     real *              p = gmx_simd4_align_r(mem);
78
79     gmx_simd4_store_r(p, simd4);
80     std::vector<real>   v(p, p+GMX_SIMD4_WIDTH);
81
82     return v;
83 }
84
85 gmx_simd4_real_t
86 vector2Simd4Real(const std::vector<real> &v)
87 {
88     real                mem[GMX_SIMD4_WIDTH*2];
89     real *              p = gmx_simd4_align_r(mem);
90
91     for (int i = 0; i < GMX_SIMD4_WIDTH; i++)
92     {
93         p[i] = v[i % v.size()];  // repeat vector contents to fill simd width
94     }
95     return gmx_simd4_load_r(p);
96 }
97
98 gmx_simd4_real_t
99 setSimd4RealFrom3R(real r0, real r1, real r2)
100 {
101     std::vector<real> v(3);
102     v[0] = r0;
103     v[1] = r1;
104     v[2] = r2;
105     return vector2Simd4Real(v);
106 }
107
108 gmx_simd4_real_t
109 setSimd4RealFrom1R(real value)
110 {
111     std::vector<real> v(GMX_SIMD4_WIDTH);
112     for (int i = 0; i < GMX_SIMD4_WIDTH; i++)
113     {
114         v[i] = value;
115     }
116     return vector2Simd4Real(v);
117 }
118
119 testing::AssertionResult
120 Simd4Test::compareSimd4RealUlp(const char *  refExpr,     const char *  tstExpr,
121                                const gmx_simd4_real_t ref, const gmx_simd4_real_t tst)
122 {
123     return compareVectorRealUlp(refExpr, tstExpr, simd4Real2Vector(ref), simd4Real2Vector(tst));
124 }
125
126 testing::AssertionResult
127 Simd4Test::compareSimd4RealEq(const char * refExpr, const char * tstExpr,
128                               const gmx_simd4_real_t ref, const gmx_simd4_real_t tst)
129 {
130     return compareVectorEq(refExpr, tstExpr, simd4Real2Vector(ref), simd4Real2Vector(tst));
131 }
132
133 #endif  // GMX_SIMD4_HAVE_REAL
134
135 /*! \} */
136 /*! \endcond */
137
138 }      // namespace
139 }      // namespace