Apply clang-format to source tree
[alexxy/gromacs.git] / src / gromacs / simd / impl_x86_avx_256 / impl_x86_avx_256_simd4_float.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2014,2015,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
36 #ifndef GMX_SIMD_IMPL_X86_AVX_256_SIMD4_FLOAT_H
37 #define GMX_SIMD_IMPL_X86_AVX_256_SIMD4_FLOAT_H
38
39 #include "config.h"
40
41 #include <cassert>
42 #include <cstddef>
43
44 #include <immintrin.h>
45
46 namespace gmx
47 {
48
49 class Simd4Float
50 {
51 public:
52     Simd4Float() {}
53
54     Simd4Float(float f) : simdInternal_(_mm_set1_ps(f)) {}
55
56     // Internal utility constructor to simplify return statements
57     Simd4Float(__m128 simd) : simdInternal_(simd) {}
58
59     __m128 simdInternal_;
60 };
61
62 class Simd4FBool
63 {
64 public:
65     Simd4FBool() {}
66
67     //! \brief Construct from scalar bool
68     Simd4FBool(bool b) : simdInternal_(_mm_castsi128_ps(_mm_set1_epi32(b ? 0xFFFFFFFF : 0))) {}
69
70     // Internal utility constructor to simplify return statements
71     Simd4FBool(__m128 simd) : simdInternal_(simd) {}
72
73     __m128 simdInternal_;
74 };
75
76 static inline Simd4Float gmx_simdcall load4(const float* m)
77 {
78     assert(std::size_t(m) % 16 == 0);
79     return { _mm_load_ps(m) };
80 }
81
82 static inline void gmx_simdcall store4(float* m, Simd4Float a)
83 {
84     assert(std::size_t(m) % 16 == 0);
85     _mm_store_ps(m, a.simdInternal_);
86 }
87
88 static inline Simd4Float gmx_simdcall load4U(const float* m)
89 {
90     return { _mm_loadu_ps(m) };
91 }
92
93 static inline void gmx_simdcall store4U(float* m, Simd4Float a)
94 {
95     _mm_storeu_ps(m, a.simdInternal_);
96 }
97
98 static inline Simd4Float gmx_simdcall simd4SetZeroF()
99 {
100     return { _mm_setzero_ps() };
101 }
102
103 static inline Simd4Float gmx_simdcall operator&(Simd4Float a, Simd4Float b)
104 {
105     return { _mm_and_ps(a.simdInternal_, b.simdInternal_) };
106 }
107
108 static inline Simd4Float gmx_simdcall andNot(Simd4Float a, Simd4Float b)
109 {
110     return { _mm_andnot_ps(a.simdInternal_, b.simdInternal_) };
111 }
112
113 static inline Simd4Float gmx_simdcall operator|(Simd4Float a, Simd4Float b)
114 {
115     return { _mm_or_ps(a.simdInternal_, b.simdInternal_) };
116 }
117
118 static inline Simd4Float gmx_simdcall operator^(Simd4Float a, Simd4Float b)
119 {
120     return { _mm_xor_ps(a.simdInternal_, b.simdInternal_) };
121 }
122
123 static inline Simd4Float gmx_simdcall operator+(Simd4Float a, Simd4Float b)
124 {
125     return { _mm_add_ps(a.simdInternal_, b.simdInternal_) };
126 }
127
128 static inline Simd4Float gmx_simdcall operator-(Simd4Float a, Simd4Float b)
129 {
130     return { _mm_sub_ps(a.simdInternal_, b.simdInternal_) };
131 }
132
133 static inline Simd4Float gmx_simdcall operator-(Simd4Float x)
134 {
135     return { _mm_xor_ps(x.simdInternal_, _mm_set1_ps(GMX_FLOAT_NEGZERO)) };
136 }
137
138 static inline Simd4Float gmx_simdcall operator*(Simd4Float a, Simd4Float b)
139 {
140     return { _mm_mul_ps(a.simdInternal_, b.simdInternal_) };
141 }
142
143 // Override for AVX2 and higher
144 #if GMX_SIMD_X86_AVX_256
145 static inline Simd4Float gmx_simdcall fma(Simd4Float a, Simd4Float b, Simd4Float c)
146 {
147     return { _mm_add_ps(_mm_mul_ps(a.simdInternal_, b.simdInternal_), c.simdInternal_) };
148 }
149
150 static inline Simd4Float gmx_simdcall fms(Simd4Float a, Simd4Float b, Simd4Float c)
151 {
152     return { _mm_sub_ps(_mm_mul_ps(a.simdInternal_, b.simdInternal_), c.simdInternal_) };
153 }
154
155 static inline Simd4Float gmx_simdcall fnma(Simd4Float a, Simd4Float b, Simd4Float c)
156 {
157     return { _mm_sub_ps(c.simdInternal_, _mm_mul_ps(a.simdInternal_, b.simdInternal_)) };
158 }
159
160 static inline Simd4Float gmx_simdcall fnms(Simd4Float a, Simd4Float b, Simd4Float c)
161 {
162     return { _mm_sub_ps(_mm_setzero_ps(),
163                         _mm_add_ps(_mm_mul_ps(a.simdInternal_, b.simdInternal_), c.simdInternal_)) };
164 }
165 #endif
166
167 static inline Simd4Float gmx_simdcall rsqrt(Simd4Float x)
168 {
169     return { _mm_rsqrt_ps(x.simdInternal_) };
170 }
171
172 static inline Simd4Float gmx_simdcall abs(Simd4Float x)
173 {
174     return { _mm_andnot_ps(_mm_set1_ps(GMX_FLOAT_NEGZERO), x.simdInternal_) };
175 }
176
177 static inline Simd4Float gmx_simdcall max(Simd4Float a, Simd4Float b)
178 {
179     return { _mm_max_ps(a.simdInternal_, b.simdInternal_) };
180 }
181
182 static inline Simd4Float gmx_simdcall min(Simd4Float a, Simd4Float b)
183 {
184     return { _mm_min_ps(a.simdInternal_, b.simdInternal_) };
185 }
186
187 static inline Simd4Float gmx_simdcall round(Simd4Float x)
188 {
189     return { _mm_round_ps(x.simdInternal_, _MM_FROUND_NINT) };
190 }
191
192 static inline Simd4Float gmx_simdcall trunc(Simd4Float x)
193 {
194     return { _mm_round_ps(x.simdInternal_, _MM_FROUND_TRUNC) };
195 }
196
197 static inline float gmx_simdcall dotProduct(Simd4Float a, Simd4Float b)
198 {
199     __m128 c, d;
200     c = _mm_mul_ps(a.simdInternal_, b.simdInternal_);
201     d = _mm_add_ps(c, _mm_permute_ps(c, _MM_SHUFFLE(2, 1, 2, 1)));
202     d = _mm_add_ps(d, _mm_permute_ps(c, _MM_SHUFFLE(3, 2, 3, 2)));
203     return *reinterpret_cast<float*>(&d);
204 }
205
206 static inline void gmx_simdcall transpose(Simd4Float* v0, Simd4Float* v1, Simd4Float* v2, Simd4Float* v3)
207 {
208     _MM_TRANSPOSE4_PS(v0->simdInternal_, v1->simdInternal_, v2->simdInternal_, v3->simdInternal_);
209 }
210
211 static inline Simd4FBool gmx_simdcall operator==(Simd4Float a, Simd4Float b)
212 {
213     return { _mm_cmp_ps(a.simdInternal_, b.simdInternal_, _CMP_EQ_OQ) };
214 }
215
216 static inline Simd4FBool gmx_simdcall operator!=(Simd4Float a, Simd4Float b)
217 {
218     return { _mm_cmp_ps(a.simdInternal_, b.simdInternal_, _CMP_NEQ_OQ) };
219 }
220
221 static inline Simd4FBool gmx_simdcall operator<(Simd4Float a, Simd4Float b)
222 {
223     return { _mm_cmp_ps(a.simdInternal_, b.simdInternal_, _CMP_LT_OQ) };
224 }
225
226 static inline Simd4FBool gmx_simdcall operator<=(Simd4Float a, Simd4Float b)
227 {
228     return { _mm_cmp_ps(a.simdInternal_, b.simdInternal_, _CMP_LE_OQ) };
229 }
230
231 static inline Simd4FBool gmx_simdcall operator&&(Simd4FBool a, Simd4FBool b)
232 {
233     return { _mm_and_ps(a.simdInternal_, b.simdInternal_) };
234 }
235
236 static inline Simd4FBool gmx_simdcall operator||(Simd4FBool a, Simd4FBool b)
237 {
238     return { _mm_or_ps(a.simdInternal_, b.simdInternal_) };
239 }
240
241 static inline bool gmx_simdcall anyTrue(Simd4FBool a)
242 {
243     return _mm_movemask_ps(a.simdInternal_) != 0;
244 }
245
246 static inline Simd4Float gmx_simdcall selectByMask(Simd4Float a, Simd4FBool mask)
247 {
248     return { _mm_and_ps(a.simdInternal_, mask.simdInternal_) };
249 }
250
251 static inline Simd4Float gmx_simdcall selectByNotMask(Simd4Float a, Simd4FBool mask)
252 {
253     return { _mm_andnot_ps(mask.simdInternal_, a.simdInternal_) };
254 }
255
256 static inline Simd4Float gmx_simdcall blend(Simd4Float a, Simd4Float b, Simd4FBool sel)
257 {
258     return { _mm_blendv_ps(a.simdInternal_, b.simdInternal_, sel.simdInternal_) };
259 }
260
261 static inline float gmx_simdcall reduce(Simd4Float a)
262 {
263     __m128 b;
264     b = _mm_add_ps(a.simdInternal_, _mm_permute_ps(a.simdInternal_, _MM_SHUFFLE(1, 0, 3, 2)));
265     b = _mm_add_ss(b, _mm_permute_ps(b, _MM_SHUFFLE(0, 3, 2, 1)));
266     return *reinterpret_cast<float*>(&b);
267 }
268
269 } // namespace gmx
270
271 #endif // GMX_SIMD_IMPL_X86_AVX_256_SIMD4_FLOAT_H