Apply clang-format to source tree
[alexxy/gromacs.git] / src / gromacs / simd / impl_x86_mic / impl_x86_mic_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_MIC_SIMD4_FLOAT_H
37 #define GMX_SIMD_IMPL_X86_MIC_SIMD4_FLOAT_H
38
39 #include "config.h"
40
41 #include <cassert>
42
43 #include <immintrin.h>
44
45 #include "gromacs/utility/basedefinitions.h"
46
47 #include "impl_x86_mic_simd_float.h"
48
49 namespace gmx
50 {
51
52 class Simd4Float
53 {
54 public:
55     Simd4Float() {}
56
57     Simd4Float(float f) : simdInternal_(_mm512_set1_ps(f)) {}
58
59     // Internal utility constructor to simplify return statements
60     Simd4Float(__m512 simd) : simdInternal_(simd) {}
61
62     __m512 simdInternal_;
63 };
64
65 class Simd4FBool
66 {
67 public:
68     Simd4FBool() {}
69
70     // Internal utility constructor to simplify return statements
71     Simd4FBool(__mmask16 simd) : simdInternal_(simd) {}
72
73     __mmask16 simdInternal_;
74 };
75
76 static inline Simd4Float gmx_simdcall load4(const float* m)
77 {
78     assert(size_t(m) % 16 == 0);
79     return { _mm512_mask_extload_ps(_mm512_undefined_ps(), _mm512_int2mask(0xF), m,
80                                     _MM_UPCONV_PS_NONE, _MM_BROADCAST_4X16, _MM_HINT_NONE) };
81 }
82
83 static inline void gmx_simdcall store4(float* m, Simd4Float a)
84 {
85     assert(size_t(m) % 16 == 0);
86     _mm512_mask_packstorelo_ps(m, _mm512_int2mask(0xF), a.simdInternal_);
87 }
88
89 static inline Simd4Float gmx_simdcall load4U(const float* m)
90 {
91     return { _mm512_mask_loadunpackhi_ps(
92             _mm512_mask_loadunpacklo_ps(_mm512_undefined_ps(), _mm512_int2mask(0xF), m),
93             _mm512_int2mask(0xF), m + 16) };
94 }
95
96 static inline void gmx_simdcall store4U(float* m, Simd4Float a)
97 {
98     _mm512_mask_packstorelo_ps(m, _mm512_int2mask(0xF), a.simdInternal_);
99     _mm512_mask_packstorehi_ps(m + 16, _mm512_int2mask(0xF), a.simdInternal_);
100 }
101
102 static inline Simd4Float gmx_simdcall simd4SetZeroF()
103 {
104     return { _mm512_setzero_ps() };
105 }
106
107 static inline Simd4Float gmx_simdcall operator&(Simd4Float a, Simd4Float b)
108 {
109     return { _mm512_castsi512_ps(_mm512_mask_and_epi32(_mm512_undefined_epi32(), _mm512_int2mask(0xF),
110                                                        _mm512_castps_si512(a.simdInternal_),
111                                                        _mm512_castps_si512(b.simdInternal_))) };
112 }
113
114 static inline Simd4Float gmx_simdcall andNot(Simd4Float a, Simd4Float b)
115 {
116     return { _mm512_castsi512_ps(_mm512_mask_andnot_epi32(
117             _mm512_undefined_epi32(), _mm512_int2mask(0xF), _mm512_castps_si512(a.simdInternal_),
118             _mm512_castps_si512(b.simdInternal_))) };
119 }
120
121 static inline Simd4Float gmx_simdcall operator|(Simd4Float a, Simd4Float b)
122 {
123     return { _mm512_castsi512_ps(_mm512_mask_or_epi32(_mm512_undefined_epi32(), _mm512_int2mask(0xF),
124                                                       _mm512_castps_si512(a.simdInternal_),
125                                                       _mm512_castps_si512(b.simdInternal_))) };
126 }
127
128 static inline Simd4Float gmx_simdcall operator^(Simd4Float a, Simd4Float b)
129 {
130     return { _mm512_castsi512_ps(_mm512_mask_xor_epi32(_mm512_undefined_epi32(), _mm512_int2mask(0xF),
131                                                        _mm512_castps_si512(a.simdInternal_),
132                                                        _mm512_castps_si512(b.simdInternal_))) };
133 }
134
135 static inline Simd4Float gmx_simdcall operator+(Simd4Float a, Simd4Float b)
136 {
137     return { _mm512_mask_add_ps(_mm512_undefined_ps(), _mm512_int2mask(0xF), a.simdInternal_,
138                                 b.simdInternal_) };
139 }
140
141 static inline Simd4Float gmx_simdcall operator-(Simd4Float a, Simd4Float b)
142 {
143     return { _mm512_mask_sub_ps(_mm512_undefined_ps(), _mm512_int2mask(0xF), a.simdInternal_,
144                                 b.simdInternal_) };
145 }
146
147 static inline Simd4Float gmx_simdcall operator-(Simd4Float x)
148 {
149     return { _mm512_mask_addn_ps(_mm512_undefined_ps(), _mm512_int2mask(0xF), x.simdInternal_,
150                                  _mm512_setzero_ps()) };
151 }
152
153 static inline Simd4Float gmx_simdcall operator*(Simd4Float a, Simd4Float b)
154 {
155     return { _mm512_mask_mul_ps(_mm512_undefined_ps(), _mm512_int2mask(0xF), a.simdInternal_,
156                                 b.simdInternal_) };
157 }
158
159 static inline Simd4Float gmx_simdcall fma(Simd4Float a, Simd4Float b, Simd4Float c)
160 {
161     return { _mm512_mask_fmadd_ps(a.simdInternal_, _mm512_int2mask(0xF), b.simdInternal_, c.simdInternal_) };
162 }
163
164 static inline Simd4Float gmx_simdcall fms(Simd4Float a, Simd4Float b, Simd4Float c)
165 {
166     return { _mm512_mask_fmsub_ps(a.simdInternal_, _mm512_int2mask(0xF), b.simdInternal_, c.simdInternal_) };
167 }
168
169 static inline Simd4Float gmx_simdcall fnma(Simd4Float a, Simd4Float b, Simd4Float c)
170 {
171     return { _mm512_mask_fnmadd_ps(a.simdInternal_, _mm512_int2mask(0xF), b.simdInternal_, c.simdInternal_) };
172 }
173
174 static inline Simd4Float gmx_simdcall fnms(Simd4Float a, Simd4Float b, Simd4Float c)
175 {
176     return { _mm512_mask_fnmsub_ps(a.simdInternal_, _mm512_int2mask(0xF), b.simdInternal_, c.simdInternal_) };
177 }
178
179 static inline Simd4Float gmx_simdcall rsqrt(Simd4Float x)
180 {
181     return { _mm512_mask_rsqrt23_ps(_mm512_undefined_ps(), _mm512_int2mask(0xF), x.simdInternal_) };
182 }
183
184 static inline Simd4Float gmx_simdcall abs(Simd4Float x)
185 {
186     return { _mm512_castsi512_ps(_mm512_mask_andnot_epi32(
187             _mm512_undefined_epi32(), _mm512_int2mask(0xF),
188             _mm512_castps_si512(_mm512_set1_ps(GMX_FLOAT_NEGZERO)), _mm512_castps_si512(x.simdInternal_))) };
189 }
190
191 static inline Simd4Float gmx_simdcall max(Simd4Float a, Simd4Float b)
192 {
193     return { _mm512_mask_gmax_ps(_mm512_undefined_ps(), _mm512_int2mask(0xF), a.simdInternal_,
194                                  b.simdInternal_) };
195 }
196
197 static inline Simd4Float gmx_simdcall min(Simd4Float a, Simd4Float b)
198 {
199     return { _mm512_mask_gmin_ps(_mm512_undefined_ps(), _mm512_int2mask(0xF), a.simdInternal_,
200                                  b.simdInternal_) };
201 }
202
203 static inline Simd4Float gmx_simdcall round(Simd4Float x)
204 {
205     return { _mm512_mask_round_ps(_mm512_undefined_ps(), _mm512_int2mask(0xF), x.simdInternal_,
206                                   _MM_FROUND_TO_NEAREST_INT, _MM_EXPADJ_NONE) };
207 }
208
209 static inline Simd4Float gmx_simdcall trunc(Simd4Float x)
210 {
211     return { _mm512_mask_round_ps(_mm512_undefined_ps(), _mm512_int2mask(0xF), x.simdInternal_,
212                                   _MM_FROUND_TO_ZERO, _MM_EXPADJ_NONE) };
213 }
214
215 static inline float gmx_simdcall dotProduct(Simd4Float a, Simd4Float b)
216 {
217     __m512 x = _mm512_mask_mul_ps(_mm512_setzero_ps(), _mm512_int2mask(0x7), a.simdInternal_,
218                                   b.simdInternal_);
219     x        = _mm512_add_ps(x, _mm512_swizzle_ps(x, _MM_SWIZ_REG_BADC));
220     x        = _mm512_add_ps(x, _mm512_swizzle_ps(x, _MM_SWIZ_REG_CDAB));
221     float f;
222     _mm512_mask_packstorelo_ps(&f, _mm512_mask2int(0x1), x);
223     return f;
224 }
225
226 static inline void gmx_simdcall transpose(Simd4Float* v0, Simd4Float* v1, Simd4Float* v2, Simd4Float* v3)
227 {
228     v0->simdInternal_ = _mm512_mask_permute4f128_ps(v0->simdInternal_, _mm512_int2mask(0x00F0),
229                                                     v1->simdInternal_, _MM_PERM_AAAA);
230     v2->simdInternal_ = _mm512_mask_permute4f128_ps(v2->simdInternal_, _mm512_int2mask(0x00F0),
231                                                     v3->simdInternal_, _MM_PERM_AAAA);
232     v0->simdInternal_ = _mm512_mask_permute4f128_ps(v0->simdInternal_, _mm512_int2mask(0xFF00),
233                                                     v2->simdInternal_, _MM_PERM_BABA);
234     v0->simdInternal_ = _mm512_castsi512_ps(_mm512_permutevar_epi32(
235             _mm512_set_epi32(15, 11, 7, 3, 14, 10, 6, 2, 13, 9, 5, 1, 12, 8, 4, 0),
236             _mm512_castps_si512(v0->simdInternal_)));
237     v1->simdInternal_ = _mm512_mask_permute4f128_ps(_mm512_setzero_ps(), _mm512_int2mask(0x000F),
238                                                     v0->simdInternal_, _MM_PERM_BBBB);
239     v2->simdInternal_ = _mm512_mask_permute4f128_ps(_mm512_setzero_ps(), _mm512_int2mask(0x000F),
240                                                     v0->simdInternal_, _MM_PERM_CCCC);
241     v3->simdInternal_ = _mm512_mask_permute4f128_ps(_mm512_setzero_ps(), _mm512_int2mask(0x000F),
242                                                     v0->simdInternal_, _MM_PERM_DDDD);
243 }
244
245 // Picky, picky, picky:
246 // icc-16 complains about "Illegal value of immediate argument to intrinsic"
247 // unless we use
248 // 1) Ordered-quiet for ==
249 // 2) Unordered-quiet for !=
250 // 3) Ordered-signaling for < and <=
251
252 static inline Simd4FBool gmx_simdcall operator==(Simd4Float a, Simd4Float b)
253 {
254     return { _mm512_mask_cmp_ps_mask(_mm512_int2mask(0xF), a.simdInternal_, b.simdInternal_, _CMP_EQ_OQ) };
255 }
256
257 static inline Simd4FBool gmx_simdcall operator!=(Simd4Float a, Simd4Float b)
258 {
259     return { _mm512_mask_cmp_ps_mask(_mm512_int2mask(0xF), a.simdInternal_, b.simdInternal_, _CMP_NEQ_UQ) };
260 }
261
262 static inline Simd4FBool gmx_simdcall operator<(Simd4Float a, Simd4Float b)
263 {
264     return { _mm512_mask_cmp_ps_mask(_mm512_int2mask(0xF), a.simdInternal_, b.simdInternal_, _CMP_LT_OS) };
265 }
266
267 static inline Simd4FBool gmx_simdcall operator<=(Simd4Float a, Simd4Float b)
268 {
269     return { _mm512_mask_cmp_ps_mask(_mm512_int2mask(0xF), a.simdInternal_, b.simdInternal_, _CMP_LE_OS) };
270 }
271
272 static inline Simd4FBool gmx_simdcall operator&&(Simd4FBool a, Simd4FBool b)
273 {
274     return { _mm512_kand(a.simdInternal_, b.simdInternal_) };
275 }
276
277 static inline Simd4FBool gmx_simdcall operator||(Simd4FBool a, Simd4FBool b)
278 {
279     return { _mm512_kor(a.simdInternal_, b.simdInternal_) };
280 }
281
282 static inline bool gmx_simdcall anyTrue(Simd4FBool a)
283 {
284     return (_mm512_mask2int(a.simdInternal_) & 0xF) != 0;
285 }
286
287 static inline Simd4Float gmx_simdcall selectByMask(Simd4Float a, Simd4FBool m)
288 {
289     return { _mm512_mask_mov_ps(_mm512_setzero_ps(), m.simdInternal_, a.simdInternal_) };
290 }
291
292 static inline Simd4Float gmx_simdcall selectByNotMask(Simd4Float a, Simd4FBool m)
293 {
294     return { _mm512_mask_mov_ps(_mm512_setzero_ps(), _mm512_knot(m.simdInternal_), a.simdInternal_) };
295 }
296
297 static inline Simd4Float gmx_simdcall blend(Simd4Float a, Simd4Float b, Simd4FBool sel)
298 {
299     return { _mm512_mask_blend_ps(sel.simdInternal_, a.simdInternal_, b.simdInternal_) };
300 }
301
302 static inline float gmx_simdcall reduce(Simd4Float a)
303 {
304     __m512 x = a.simdInternal_;
305     x        = _mm512_add_ps(x, _mm512_swizzle_ps(x, _MM_SWIZ_REG_BADC));
306     x        = _mm512_add_ps(x, _mm512_swizzle_ps(x, _MM_SWIZ_REG_CDAB));
307     float f;
308     _mm512_mask_packstorelo_ps(&f, _mm512_mask2int(0x1), x);
309     return f;
310 }
311
312 } // namespace gmx
313
314 #endif // GMX_SIMD_IMPL_X86_MIC_SIMD4_FLOAT_H