2 * This file is part of the GROMACS molecular simulation package.
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.
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.
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.
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.
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.
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.
36 #ifndef GMX_SIMD_IMPL_X86_AVX_256_SIMD4_FLOAT_H
37 #define GMX_SIMD_IMPL_X86_AVX_256_SIMD4_FLOAT_H
44 #include <immintrin.h>
54 Simd4Float(float f) : simdInternal_(_mm_set1_ps(f)) {}
56 // Internal utility constructor to simplify return statements
57 Simd4Float(__m128 simd) : simdInternal_(simd) {}
67 //! \brief Construct from scalar bool
68 Simd4FBool(bool b) : simdInternal_(_mm_castsi128_ps(_mm_set1_epi32(b ? 0xFFFFFFFF : 0))) {}
70 // Internal utility constructor to simplify return statements
71 Simd4FBool(__m128 simd) : simdInternal_(simd) {}
76 static inline Simd4Float gmx_simdcall load4(const float* m)
78 assert(std::size_t(m) % 16 == 0);
79 return { _mm_load_ps(m) };
82 static inline void gmx_simdcall store4(float* m, Simd4Float a)
84 assert(std::size_t(m) % 16 == 0);
85 _mm_store_ps(m, a.simdInternal_);
88 static inline Simd4Float gmx_simdcall load4U(const float* m)
90 return { _mm_loadu_ps(m) };
93 static inline void gmx_simdcall store4U(float* m, Simd4Float a)
95 _mm_storeu_ps(m, a.simdInternal_);
98 static inline Simd4Float gmx_simdcall simd4SetZeroF()
100 return { _mm_setzero_ps() };
103 static inline Simd4Float gmx_simdcall operator&(Simd4Float a, Simd4Float b)
105 return { _mm_and_ps(a.simdInternal_, b.simdInternal_) };
108 static inline Simd4Float gmx_simdcall andNot(Simd4Float a, Simd4Float b)
110 return { _mm_andnot_ps(a.simdInternal_, b.simdInternal_) };
113 static inline Simd4Float gmx_simdcall operator|(Simd4Float a, Simd4Float b)
115 return { _mm_or_ps(a.simdInternal_, b.simdInternal_) };
118 static inline Simd4Float gmx_simdcall operator^(Simd4Float a, Simd4Float b)
120 return { _mm_xor_ps(a.simdInternal_, b.simdInternal_) };
123 static inline Simd4Float gmx_simdcall operator+(Simd4Float a, Simd4Float b)
125 return { _mm_add_ps(a.simdInternal_, b.simdInternal_) };
128 static inline Simd4Float gmx_simdcall operator-(Simd4Float a, Simd4Float b)
130 return { _mm_sub_ps(a.simdInternal_, b.simdInternal_) };
133 static inline Simd4Float gmx_simdcall operator-(Simd4Float x)
135 return { _mm_xor_ps(x.simdInternal_, _mm_set1_ps(GMX_FLOAT_NEGZERO)) };
138 static inline Simd4Float gmx_simdcall operator*(Simd4Float a, Simd4Float b)
140 return { _mm_mul_ps(a.simdInternal_, b.simdInternal_) };
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)
147 return { _mm_add_ps(_mm_mul_ps(a.simdInternal_, b.simdInternal_), c.simdInternal_) };
150 static inline Simd4Float gmx_simdcall fms(Simd4Float a, Simd4Float b, Simd4Float c)
152 return { _mm_sub_ps(_mm_mul_ps(a.simdInternal_, b.simdInternal_), c.simdInternal_) };
155 static inline Simd4Float gmx_simdcall fnma(Simd4Float a, Simd4Float b, Simd4Float c)
157 return { _mm_sub_ps(c.simdInternal_, _mm_mul_ps(a.simdInternal_, b.simdInternal_)) };
160 static inline Simd4Float gmx_simdcall fnms(Simd4Float a, Simd4Float b, Simd4Float c)
162 return { _mm_sub_ps(_mm_setzero_ps(),
163 _mm_add_ps(_mm_mul_ps(a.simdInternal_, b.simdInternal_), c.simdInternal_)) };
167 static inline Simd4Float gmx_simdcall rsqrt(Simd4Float x)
169 return { _mm_rsqrt_ps(x.simdInternal_) };
172 static inline Simd4Float gmx_simdcall abs(Simd4Float x)
174 return { _mm_andnot_ps(_mm_set1_ps(GMX_FLOAT_NEGZERO), x.simdInternal_) };
177 static inline Simd4Float gmx_simdcall max(Simd4Float a, Simd4Float b)
179 return { _mm_max_ps(a.simdInternal_, b.simdInternal_) };
182 static inline Simd4Float gmx_simdcall min(Simd4Float a, Simd4Float b)
184 return { _mm_min_ps(a.simdInternal_, b.simdInternal_) };
187 static inline Simd4Float gmx_simdcall round(Simd4Float x)
189 return { _mm_round_ps(x.simdInternal_, _MM_FROUND_NINT) };
192 static inline Simd4Float gmx_simdcall trunc(Simd4Float x)
194 return { _mm_round_ps(x.simdInternal_, _MM_FROUND_TRUNC) };
197 static inline float gmx_simdcall dotProduct(Simd4Float a, Simd4Float b)
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);
206 static inline void gmx_simdcall transpose(Simd4Float* v0, Simd4Float* v1, Simd4Float* v2, Simd4Float* v3)
208 _MM_TRANSPOSE4_PS(v0->simdInternal_, v1->simdInternal_, v2->simdInternal_, v3->simdInternal_);
211 static inline Simd4FBool gmx_simdcall operator==(Simd4Float a, Simd4Float b)
213 return { _mm_cmp_ps(a.simdInternal_, b.simdInternal_, _CMP_EQ_OQ) };
216 static inline Simd4FBool gmx_simdcall operator!=(Simd4Float a, Simd4Float b)
218 return { _mm_cmp_ps(a.simdInternal_, b.simdInternal_, _CMP_NEQ_OQ) };
221 static inline Simd4FBool gmx_simdcall operator<(Simd4Float a, Simd4Float b)
223 return { _mm_cmp_ps(a.simdInternal_, b.simdInternal_, _CMP_LT_OQ) };
226 static inline Simd4FBool gmx_simdcall operator<=(Simd4Float a, Simd4Float b)
228 return { _mm_cmp_ps(a.simdInternal_, b.simdInternal_, _CMP_LE_OQ) };
231 static inline Simd4FBool gmx_simdcall operator&&(Simd4FBool a, Simd4FBool b)
233 return { _mm_and_ps(a.simdInternal_, b.simdInternal_) };
236 static inline Simd4FBool gmx_simdcall operator||(Simd4FBool a, Simd4FBool b)
238 return { _mm_or_ps(a.simdInternal_, b.simdInternal_) };
241 static inline bool gmx_simdcall anyTrue(Simd4FBool a)
243 return _mm_movemask_ps(a.simdInternal_) != 0;
246 static inline Simd4Float gmx_simdcall selectByMask(Simd4Float a, Simd4FBool mask)
248 return { _mm_and_ps(a.simdInternal_, mask.simdInternal_) };
251 static inline Simd4Float gmx_simdcall selectByNotMask(Simd4Float a, Simd4FBool mask)
253 return { _mm_andnot_ps(mask.simdInternal_, a.simdInternal_) };
256 static inline Simd4Float gmx_simdcall blend(Simd4Float a, Simd4Float b, Simd4FBool sel)
258 return { _mm_blendv_ps(a.simdInternal_, b.simdInternal_, sel.simdInternal_) };
261 static inline float gmx_simdcall reduce(Simd4Float a)
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);
271 #endif // GMX_SIMD_IMPL_X86_AVX_256_SIMD4_FLOAT_H