48debf7f2fb4a18040635262caa1959dcc118076
[alexxy/gromacs.git] / src / gromacs / simd / impl_x86_avx_512 / impl_x86_avx_512_simd4_float.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2014,2015,2016,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_512_SIMD4_FLOAT_H
37 #define GMX_SIMD_IMPL_X86_AVX_512_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_avx_512_general.h"
48
49 namespace gmx
50 {
51
52 class Simd4Float
53 {
54 public:
55     Simd4Float() {}
56
57     Simd4Float(float f) : simdInternal_(_mm_set1_ps(f)) {}
58
59     // Internal utility constructor to simplify return statements
60     Simd4Float(__m128 simd) : simdInternal_(simd) {}
61
62     __m128 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 { _mm_load_ps(m) };
80 }
81
82 static inline void gmx_simdcall store4(float* m, Simd4Float a)
83 {
84     assert(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 static inline Simd4Float gmx_simdcall fma(Simd4Float a, Simd4Float b, Simd4Float c)
144 {
145     return { _mm_fmadd_ps(a.simdInternal_, b.simdInternal_, c.simdInternal_) };
146 }
147
148 static inline Simd4Float gmx_simdcall fms(Simd4Float a, Simd4Float b, Simd4Float c)
149 {
150     return { _mm_fmsub_ps(a.simdInternal_, b.simdInternal_, c.simdInternal_) };
151 }
152
153 static inline Simd4Float gmx_simdcall fnma(Simd4Float a, Simd4Float b, Simd4Float c)
154 {
155     return { _mm_fnmadd_ps(a.simdInternal_, b.simdInternal_, c.simdInternal_) };
156 }
157
158 static inline Simd4Float gmx_simdcall fnms(Simd4Float a, Simd4Float b, Simd4Float c)
159 {
160     return { _mm_fnmsub_ps(a.simdInternal_, b.simdInternal_, c.simdInternal_) };
161 }
162
163 // Override for AVX-512-KNL
164 #if GMX_SIMD_X86_AVX_512
165 static inline Simd4Float gmx_simdcall rsqrt(Simd4Float x)
166 {
167     return { _mm512_castps512_ps128(_mm512_rsqrt14_ps(_mm512_castps128_ps512(x.simdInternal_))) };
168 }
169 #endif
170
171 static inline Simd4Float gmx_simdcall abs(Simd4Float x)
172 {
173     return { _mm_andnot_ps(_mm_set1_ps(GMX_FLOAT_NEGZERO), x.simdInternal_) };
174 }
175
176 static inline Simd4Float gmx_simdcall max(Simd4Float a, Simd4Float b)
177 {
178     return { _mm_max_ps(a.simdInternal_, b.simdInternal_) };
179 }
180
181 static inline Simd4Float gmx_simdcall min(Simd4Float a, Simd4Float b)
182 {
183     return { _mm_min_ps(a.simdInternal_, b.simdInternal_) };
184 }
185
186 static inline Simd4Float gmx_simdcall round(Simd4Float x)
187 {
188     return { _mm_round_ps(x.simdInternal_, _MM_FROUND_NINT) };
189 }
190
191 static inline Simd4Float gmx_simdcall trunc(Simd4Float x)
192 {
193     return { _mm_round_ps(x.simdInternal_, _MM_FROUND_TRUNC) };
194 }
195
196 static inline float gmx_simdcall dotProduct(Simd4Float a, Simd4Float b)
197 {
198     __m128 c, d;
199     c = _mm_mul_ps(a.simdInternal_, b.simdInternal_);
200     d = _mm_add_ps(c, _mm_permute_ps(c, _MM_SHUFFLE(0, 3, 2, 1)));
201     d = _mm_add_ps(d, _mm_permute_ps(c, _MM_SHUFFLE(1, 0, 3, 2)));
202     return *reinterpret_cast<float*>(&d);
203 }
204
205 static inline void gmx_simdcall transpose(Simd4Float* v0, Simd4Float* v1, Simd4Float* v2, Simd4Float* v3)
206 {
207     __m128 t0, t1, t2, t3;
208
209     t0                = _mm_unpacklo_ps(v0->simdInternal_, v2->simdInternal_);
210     t1                = _mm_unpackhi_ps(v0->simdInternal_, v2->simdInternal_);
211     t2                = _mm_unpacklo_ps(v1->simdInternal_, v3->simdInternal_);
212     t3                = _mm_unpackhi_ps(v1->simdInternal_, v3->simdInternal_);
213     v0->simdInternal_ = _mm_unpacklo_ps(t0, t2);
214     v1->simdInternal_ = _mm_unpackhi_ps(t0, t2);
215     v2->simdInternal_ = _mm_unpacklo_ps(t1, t3);
216     v3->simdInternal_ = _mm_unpackhi_ps(t1, t3);
217 }
218
219 static inline Simd4FBool gmx_simdcall operator==(Simd4Float a, Simd4Float b)
220 {
221     return { _mm512_mask_cmp_ps_mask(avx512Int2Mask(0xF), _mm512_castps128_ps512(a.simdInternal_),
222                                      _mm512_castps128_ps512(b.simdInternal_), _CMP_EQ_OQ) };
223 }
224
225 static inline Simd4FBool gmx_simdcall operator!=(Simd4Float a, Simd4Float b)
226 {
227     return { _mm512_mask_cmp_ps_mask(avx512Int2Mask(0xF), _mm512_castps128_ps512(a.simdInternal_),
228                                      _mm512_castps128_ps512(b.simdInternal_), _CMP_NEQ_OQ) };
229 }
230
231 static inline Simd4FBool gmx_simdcall operator<(Simd4Float a, Simd4Float b)
232 {
233     return { _mm512_mask_cmp_ps_mask(avx512Int2Mask(0xF), _mm512_castps128_ps512(a.simdInternal_),
234                                      _mm512_castps128_ps512(b.simdInternal_), _CMP_LT_OQ) };
235 }
236
237 static inline Simd4FBool gmx_simdcall operator<=(Simd4Float a, Simd4Float b)
238 {
239     return { _mm512_mask_cmp_ps_mask(avx512Int2Mask(0xF), _mm512_castps128_ps512(a.simdInternal_),
240                                      _mm512_castps128_ps512(b.simdInternal_), _CMP_LE_OQ) };
241 }
242
243 static inline Simd4FBool gmx_simdcall operator&&(Simd4FBool a, Simd4FBool b)
244 {
245     return { _mm512_kand(a.simdInternal_, b.simdInternal_) };
246 }
247
248 static inline Simd4FBool gmx_simdcall operator||(Simd4FBool a, Simd4FBool b)
249 {
250     return { _mm512_kor(a.simdInternal_, b.simdInternal_) };
251 }
252
253 static inline bool gmx_simdcall anyTrue(Simd4FBool a)
254 {
255     return (avx512Mask2Int(a.simdInternal_) & 0xF) != 0;
256 }
257
258 static inline Simd4Float gmx_simdcall selectByMask(Simd4Float a, Simd4FBool m)
259 {
260     return { _mm512_castps512_ps128(_mm512_mask_mov_ps(_mm512_setzero_ps(), m.simdInternal_,
261                                                        _mm512_castps128_ps512(a.simdInternal_))) };
262 }
263
264 static inline Simd4Float gmx_simdcall selectByNotMask(Simd4Float a, Simd4FBool m)
265 {
266     return { _mm512_castps512_ps128(_mm512_mask_mov_ps(_mm512_castps128_ps512(a.simdInternal_),
267                                                        m.simdInternal_, _mm512_setzero_ps())) };
268 }
269
270 static inline Simd4Float gmx_simdcall blend(Simd4Float a, Simd4Float b, Simd4FBool sel)
271 {
272     return { _mm512_castps512_ps128(_mm512_mask_blend_ps(sel.simdInternal_,
273                                                          _mm512_castps128_ps512(a.simdInternal_),
274                                                          _mm512_castps128_ps512(b.simdInternal_))) };
275 }
276
277 static inline float gmx_simdcall reduce(Simd4Float a)
278 {
279     __m128 b;
280     b = _mm_add_ps(a.simdInternal_, _mm_permute_ps(a.simdInternal_, _MM_SHUFFLE(1, 0, 3, 2)));
281     b = _mm_add_ss(b, _mm_permute_ps(b, _MM_SHUFFLE(0, 3, 2, 1)));
282     return *reinterpret_cast<float*>(&b);
283 }
284
285 } // namespace gmx
286
287 #endif // GMX_SIMD_IMPL_X86_AVX_512_SIMD4_FLOAT_H