Apply re-formatting to C++ in src/ tree.
[alexxy/gromacs.git] / src / gromacs / simd / impl_x86_mic / impl_x86_mic_simd4_double.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2014,2015,2017,2019,2020, 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_DOUBLE_H
37 #define GMX_SIMD_IMPL_X86_MIC_SIMD4_DOUBLE_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_double.h"
48
49 namespace gmx
50 {
51
52 class Simd4Double
53 {
54 public:
55     Simd4Double() {}
56
57     Simd4Double(double d) : simdInternal_(_mm512_set1_pd(d)) {}
58
59     // Internal utility constructor to simplify return statements
60     Simd4Double(__m512d simd) : simdInternal_(simd) {}
61
62     __m512d simdInternal_;
63 };
64
65 class Simd4DBool
66 {
67 public:
68     Simd4DBool() {}
69
70     // Internal utility constructor to simplify return statements
71     Simd4DBool(__mmask16 simd) : simdInternal_(simd) {}
72
73     __mmask16 simdInternal_;
74 };
75
76 static inline Simd4Double gmx_simdcall load4(const double* m)
77 {
78     assert(size_t(m) % 32 == 0);
79     return { _mm512_mask_extload_pd(
80             _mm512_undefined_pd(), _mm512_int2mask(0xF), m, _MM_UPCONV_PD_NONE, _MM_BROADCAST_4X8, _MM_HINT_NONE) };
81 }
82
83 static inline void gmx_simdcall store4(double* m, Simd4Double a)
84 {
85     assert(size_t(m) % 32 == 0);
86     _mm512_mask_packstorelo_pd(m, _mm512_int2mask(0xF), a.simdInternal_);
87 }
88
89 static inline Simd4Double gmx_simdcall load4U(const double* m)
90 {
91     return { _mm512_mask_loadunpackhi_pd(
92             _mm512_mask_loadunpacklo_pd(_mm512_undefined_pd(), _mm512_int2mask(0xF), m),
93             _mm512_int2mask(0xF),
94             m + 8) };
95 }
96
97 static inline void gmx_simdcall store4U(double* m, Simd4Double a)
98 {
99     _mm512_mask_packstorelo_pd(m, _mm512_int2mask(0xF), a.simdInternal_);
100     _mm512_mask_packstorehi_pd(m + 8, _mm512_int2mask(0xF), a.simdInternal_);
101 }
102
103 static inline Simd4Double gmx_simdcall simd4SetZeroD()
104 {
105     return { _mm512_setzero_pd() };
106 }
107
108 static inline Simd4Double gmx_simdcall operator&(Simd4Double a, Simd4Double b)
109 {
110     return { _mm512_castsi512_pd(_mm512_mask_and_epi32(_mm512_undefined_epi32(),
111                                                        _mm512_int2mask(0x00FF),
112                                                        _mm512_castpd_si512(a.simdInternal_),
113                                                        _mm512_castpd_si512(b.simdInternal_))) };
114 }
115
116 static inline Simd4Double gmx_simdcall andNot(Simd4Double a, Simd4Double b)
117 {
118     return { _mm512_castsi512_pd(_mm512_mask_andnot_epi32(_mm512_undefined_epi32(),
119                                                           _mm512_int2mask(0x00FF),
120                                                           _mm512_castpd_si512(a.simdInternal_),
121                                                           _mm512_castpd_si512(b.simdInternal_))) };
122 }
123
124 static inline Simd4Double gmx_simdcall operator|(Simd4Double a, Simd4Double b)
125 {
126     return { _mm512_castsi512_pd(_mm512_mask_or_epi32(_mm512_undefined_epi32(),
127                                                       _mm512_int2mask(0x00FF),
128                                                       _mm512_castpd_si512(a.simdInternal_),
129                                                       _mm512_castpd_si512(b.simdInternal_))) };
130 }
131
132 static inline Simd4Double gmx_simdcall operator^(Simd4Double a, Simd4Double b)
133 {
134     return { _mm512_castsi512_pd(_mm512_mask_xor_epi32(_mm512_undefined_epi32(),
135                                                        _mm512_int2mask(0x00FF),
136                                                        _mm512_castpd_si512(a.simdInternal_),
137                                                        _mm512_castpd_si512(b.simdInternal_))) };
138 }
139
140 static inline Simd4Double gmx_simdcall operator+(Simd4Double a, Simd4Double b)
141 {
142     return { _mm512_mask_add_pd(
143             _mm512_undefined_pd(), _mm512_int2mask(0xF), a.simdInternal_, b.simdInternal_) };
144 }
145
146 static inline Simd4Double gmx_simdcall operator-(Simd4Double a, Simd4Double b)
147 {
148     return { _mm512_mask_sub_pd(
149             _mm512_undefined_pd(), _mm512_int2mask(0xF), a.simdInternal_, b.simdInternal_) };
150 }
151
152 static inline Simd4Double gmx_simdcall operator-(Simd4Double x)
153 {
154     return { _mm512_mask_addn_pd(
155             _mm512_undefined_pd(), _mm512_int2mask(0xF), x.simdInternal_, _mm512_setzero_pd()) };
156 }
157
158 static inline Simd4Double gmx_simdcall operator*(Simd4Double a, Simd4Double b)
159 {
160     return { _mm512_mask_mul_pd(
161             _mm512_undefined_pd(), _mm512_int2mask(0xF), a.simdInternal_, b.simdInternal_) };
162 }
163
164 static inline Simd4Double gmx_simdcall fma(Simd4Double a, Simd4Double b, Simd4Double c)
165 {
166     return { _mm512_mask_fmadd_pd(a.simdInternal_, _mm512_int2mask(0xF), b.simdInternal_, c.simdInternal_) };
167 }
168
169 static inline Simd4Double gmx_simdcall fms(Simd4Double a, Simd4Double b, Simd4Double c)
170 {
171     return { _mm512_mask_fmsub_pd(a.simdInternal_, _mm512_int2mask(0xF), b.simdInternal_, c.simdInternal_) };
172 }
173
174 static inline Simd4Double gmx_simdcall fnma(Simd4Double a, Simd4Double b, Simd4Double c)
175 {
176     return { _mm512_mask_fnmadd_pd(a.simdInternal_, _mm512_int2mask(0xF), b.simdInternal_, c.simdInternal_) };
177 }
178
179 static inline Simd4Double gmx_simdcall fnms(Simd4Double a, Simd4Double b, Simd4Double c)
180 {
181     return { _mm512_mask_fnmsub_pd(a.simdInternal_, _mm512_int2mask(0xF), b.simdInternal_, c.simdInternal_) };
182 }
183
184 static inline Simd4Double gmx_simdcall rsqrt(Simd4Double x)
185 {
186     return { _mm512_mask_cvtpslo_pd(
187             _mm512_undefined_pd(),
188             _mm512_int2mask(0xF),
189             _mm512_mask_rsqrt23_ps(
190                     _mm512_undefined_ps(),
191                     _mm512_int2mask(0xF),
192                     _mm512_mask_cvtpd_pslo(_mm512_undefined_ps(), _mm512_int2mask(0xF), x.simdInternal_))) };
193 }
194
195 static inline Simd4Double gmx_simdcall abs(Simd4Double x)
196 {
197     return { _mm512_castsi512_pd(
198             _mm512_mask_andnot_epi32(_mm512_undefined_epi32(),
199                                      _mm512_int2mask(0x00FF),
200                                      _mm512_castpd_si512(_mm512_set1_pd(GMX_DOUBLE_NEGZERO)),
201                                      _mm512_castpd_si512(x.simdInternal_)))
202
203     };
204 }
205
206 static inline Simd4Double gmx_simdcall max(Simd4Double a, Simd4Double b)
207 {
208     return { _mm512_mask_gmax_pd(
209             _mm512_undefined_pd(), _mm512_int2mask(0xF), a.simdInternal_, b.simdInternal_) };
210 }
211
212 static inline Simd4Double gmx_simdcall min(Simd4Double a, Simd4Double b)
213 {
214     return { _mm512_mask_gmin_pd(
215             _mm512_undefined_pd(), _mm512_int2mask(0xF), a.simdInternal_, b.simdInternal_) };
216 }
217
218 static inline Simd4Double gmx_simdcall round(Simd4Double x)
219 {
220     return { _mm512_mask_roundfxpnt_adjust_pd(
221             _mm512_undefined_pd(), _mm512_int2mask(0xF), x.simdInternal_, _MM_FROUND_TO_NEAREST_INT, _MM_EXPADJ_NONE) };
222 }
223
224 static inline Simd4Double gmx_simdcall trunc(Simd4Double x)
225 {
226     return { _mm512_mask_roundfxpnt_adjust_pd(
227             _mm512_undefined_pd(), _mm512_int2mask(0xF), x.simdInternal_, _MM_FROUND_TO_ZERO, _MM_EXPADJ_NONE) };
228 }
229
230 static inline double gmx_simdcall dotProduct(Simd4Double a, Simd4Double b)
231 {
232     return _mm512_mask_reduce_add_pd(
233             _mm512_int2mask(7),
234             _mm512_mask_mul_pd(_mm512_undefined_pd(), _mm512_int2mask(7), a.simdInternal_, b.simdInternal_));
235 }
236
237 static inline void gmx_simdcall transpose(Simd4Double* v0, Simd4Double* v1, Simd4Double* v2, Simd4Double* v3)
238 {
239     __m512i t0 = _mm512_mask_permute4f128_epi32(_mm512_castpd_si512(v0->simdInternal_),
240                                                 0xFF00,
241                                                 _mm512_castpd_si512(v1->simdInternal_),
242                                                 _MM_PERM_BABA);
243     __m512i t1 = _mm512_mask_permute4f128_epi32(_mm512_castpd_si512(v2->simdInternal_),
244                                                 0xFF00,
245                                                 _mm512_castpd_si512(v3->simdInternal_),
246                                                 _MM_PERM_BABA);
247
248     t0 = _mm512_permutevar_epi32(
249             _mm512_set_epi32(15, 14, 7, 6, 13, 12, 5, 4, 11, 10, 3, 2, 9, 8, 1, 0), t0);
250     t1 = _mm512_permutevar_epi32(
251             _mm512_set_epi32(15, 14, 7, 6, 13, 12, 5, 4, 11, 10, 3, 2, 9, 8, 1, 0), t1);
252
253     v0->simdInternal_ = _mm512_mask_swizzle_pd(
254             _mm512_castsi512_pd(t0), _mm512_int2mask(0xCC), _mm512_castsi512_pd(t1), _MM_SWIZ_REG_BADC);
255     v1->simdInternal_ = _mm512_mask_swizzle_pd(
256             _mm512_castsi512_pd(t1), _mm512_int2mask(0x33), _mm512_castsi512_pd(t0), _MM_SWIZ_REG_BADC);
257
258     v2->simdInternal_ =
259             _mm512_castps_pd(_mm512_permute4f128_ps(_mm512_castpd_ps(v0->simdInternal_), _MM_PERM_DCDC));
260     v3->simdInternal_ =
261             _mm512_castps_pd(_mm512_permute4f128_ps(_mm512_castpd_ps(v1->simdInternal_), _MM_PERM_DCDC));
262 }
263
264 // Picky, picky, picky:
265 // icc-16 complains about "Illegal value of immediate argument to intrinsic"
266 // unless we use
267 // 1) Ordered-quiet for ==
268 // 2) Unordered-quiet for !=
269 // 3) Ordered-signaling for < and <=
270
271 static inline Simd4DBool gmx_simdcall operator==(Simd4Double a, Simd4Double b)
272 {
273     return { _mm512_mask_cmp_pd_mask(_mm512_int2mask(0xF), a.simdInternal_, b.simdInternal_, _CMP_EQ_OQ) };
274 }
275
276 static inline Simd4DBool gmx_simdcall operator!=(Simd4Double a, Simd4Double b)
277 {
278     return { _mm512_mask_cmp_pd_mask(_mm512_int2mask(0xF), a.simdInternal_, b.simdInternal_, _CMP_NEQ_UQ) };
279 }
280
281 static inline Simd4DBool gmx_simdcall operator<(Simd4Double a, Simd4Double b)
282 {
283     return { _mm512_mask_cmp_pd_mask(_mm512_int2mask(0xF), a.simdInternal_, b.simdInternal_, _CMP_LT_OS) };
284 }
285
286 static inline Simd4DBool gmx_simdcall operator<=(Simd4Double a, Simd4Double b)
287 {
288     return { _mm512_mask_cmp_pd_mask(_mm512_int2mask(0xF), a.simdInternal_, b.simdInternal_, _CMP_LE_OS) };
289 }
290
291 static inline Simd4DBool gmx_simdcall operator&&(Simd4DBool a, Simd4DBool b)
292 {
293     return { _mm512_kand(a.simdInternal_, b.simdInternal_) };
294 }
295
296 static inline Simd4DBool gmx_simdcall operator||(Simd4DBool a, Simd4DBool b)
297 {
298     return { _mm512_kor(a.simdInternal_, b.simdInternal_) };
299 }
300
301 static inline bool gmx_simdcall anyTrue(Simd4DBool a)
302 {
303     return (_mm512_mask2int(a.simdInternal_) & 0xF) != 0;
304 }
305
306 static inline Simd4Double gmx_simdcall selectByMask(Simd4Double a, Simd4DBool m)
307 {
308     return { _mm512_mask_mov_pd(_mm512_setzero_pd(), m.simdInternal_, a.simdInternal_) };
309 }
310
311 static inline Simd4Double gmx_simdcall selectByNotMask(Simd4Double a, Simd4DBool m)
312 {
313     return { _mm512_mask_mov_pd(_mm512_setzero_pd(), _mm512_knot(m.simdInternal_), a.simdInternal_) };
314 }
315
316 static inline Simd4Double gmx_simdcall blend(Simd4Double a, Simd4Double b, Simd4DBool sel)
317 {
318     return { _mm512_mask_blend_pd(sel.simdInternal_, a.simdInternal_, b.simdInternal_) };
319 }
320
321 static inline double gmx_simdcall reduce(Simd4Double a)
322 {
323     return _mm512_mask_reduce_add_pd(_mm512_int2mask(0xF), a.simdInternal_);
324 }
325
326 } // namespace gmx
327
328 #endif // GMX_SIMD_IMPL_X86_MIC_SIMD4_DOUBLE_H