Update copyright statements and change license to LGPL
[alexxy/gromacs.git] / include / gmx_x86_avx_256.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2012, by the GROMACS development team, led by
5  * David van der Spoel, Berk Hess, Erik Lindahl, and including many
6  * others, as listed in the AUTHORS file in the top-level source
7  * 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 #ifndef _gmx_x86_avx_256_h_
36 #define _gmx_x86_avx_256_h_
37
38
39 #include <immintrin.h>
40 #ifdef HAVE_X86INTRIN_H
41 #include <x86intrin.h> /* FMA */
42 #endif
43
44
45 #include <stdio.h>
46
47 #include "types/simple.h"
48
49
50 #define gmx_mm_extract_epi32(x, imm) _mm_cvtsi128_si32(_mm_srli_si128((x), 4 * (imm)))
51
52 #define _GMX_MM_BLEND256D(b3,b2,b1,b0) (((b3) << 3) | ((b2) << 2) | ((b1) << 1) | ((b0)))
53 #define _GMX_MM_PERMUTE(fp3,fp2,fp1,fp0) (((fp3) << 6) | ((fp2) << 4) | ((fp1) << 2) | ((fp0)))
54 #define _GMX_MM_PERMUTE256D(fp3,fp2,fp1,fp0) (((fp3) << 3) | ((fp2) << 2) | ((fp1) << 1) | ((fp0)))
55 #define _GMX_MM_PERMUTE128D(fp1,fp0)         (((fp1) << 1) | ((fp0)))
56
57
58 #define GMX_MM_TRANSPOSE2_PD(row0, row1) {           \
59     __m128d __gmx_t1 = row0;                         \
60     row0           = _mm_unpacklo_pd(row0,row1);     \
61     row1           = _mm_unpackhi_pd(__gmx_t1,row1); \
62 }
63
64 #define GMX_MM256_FULLTRANSPOSE4_PD(row0,row1,row2,row3) \
65 {                                                        \
66     __m256d _t0, _t1, _t2, _t3;                          \
67     _t0  = _mm256_unpacklo_pd((row0), (row1));           \
68     _t1  = _mm256_unpackhi_pd((row0), (row1));           \
69     _t2  = _mm256_unpacklo_pd((row2), (row3));           \
70     _t3  = _mm256_unpackhi_pd((row2), (row3));           \
71     row0 = _mm256_permute2f128_pd(_t0, _t2, 0x20);       \
72     row1 = _mm256_permute2f128_pd(_t1, _t3, 0x20);       \
73     row2 = _mm256_permute2f128_pd(_t0, _t2, 0x31);       \
74     row3 = _mm256_permute2f128_pd(_t1, _t3, 0x31);       \
75 }
76
77 #if (defined (_MSC_VER) || defined(__INTEL_COMPILER))
78 #  define gmx_mm_castsi128_ps(a) _mm_castsi128_ps(a)
79 #  define gmx_mm_castps_si128(a) _mm_castps_si128(a)
80 #  define gmx_mm_castps_ps128(a) (a)
81 #  define gmx_mm_castsi128_pd(a) _mm_castsi128_pd(a)
82 #  define gmx_mm_castpd_si128(a) _mm_castpd_si128(a)
83 #elif defined(__GNUC__)
84 #  define gmx_mm_castsi128_ps(a) ((__m128)(a))
85 #  define gmx_mm_castps_si128(a) ((__m128i)(a))
86 #  define gmx_mm_castps_ps128(a) ((__m128)(a))
87 #  define gmx_mm_castsi128_pd(a) ((__m128d)(a))
88 #  define gmx_mm_castpd_si128(a) ((__m128i)(a))
89 #else
90 static __m128  gmx_mm_castsi128_ps(__m128i a)
91 {
92     return *(__m128 *) &a;
93 }
94 static __m128i gmx_mm_castps_si128(__m128 a)
95 {
96     return *(__m128i *) &a;
97 }
98 static __m128  gmx_mm_castps_ps128(__m128 a)
99 {
100     return *(__m128 *) &a;
101 }
102 static __m128d gmx_mm_castsi128_pd(__m128i a)
103 {
104     return *(__m128d *) &a;
105 }
106 static __m128i gmx_mm_castpd_si128(__m128d a)
107 {
108     return *(__m128i *) &a;
109 }
110 #endif
111
112 static gmx_inline __m256
113 gmx_mm256_unpack128lo_ps(__m256 xmm1, __m256 xmm2)
114 {
115     return _mm256_permute2f128_ps(xmm1,xmm2,0x20);
116 }
117
118 static gmx_inline __m256
119 gmx_mm256_unpack128hi_ps(__m256 xmm1, __m256 xmm2)
120 {
121     return _mm256_permute2f128_ps(xmm1,xmm2,0x31);
122 }
123
124 static gmx_inline __m256
125 gmx_mm256_set_m128(__m128 hi, __m128 lo)
126 {
127     return _mm256_insertf128_ps(_mm256_castps128_ps256(lo), hi, 0x1);
128 }
129
130
131 static __m256d
132 gmx_mm256_unpack128lo_pd(__m256d xmm1, __m256d xmm2)
133 {
134     return _mm256_permute2f128_pd(xmm1,xmm2,0x20);
135 }
136
137 static __m256d
138 gmx_mm256_unpack128hi_pd(__m256d xmm1, __m256d xmm2)
139 {
140     return _mm256_permute2f128_pd(xmm1,xmm2,0x31);
141 }
142
143 static __m256d
144 gmx_mm256_set_m128d(__m128d hi, __m128d lo)
145 {
146     return _mm256_insertf128_pd(_mm256_castpd128_pd256(lo), hi, 0x1);
147 }
148
149
150
151
152 static void
153 gmx_mm_printxmm_ps(const char *s,__m128 xmm)
154 {
155     float f[4];
156
157     _mm_storeu_ps(f,xmm);
158     printf("%s: %15.10e %15.10e %15.10e %15.10e\n",s,f[0],f[1],f[2],f[3]);
159 }
160
161
162 static void
163 gmx_mm_printxmmsum_ps(const char *s,__m128 xmm)
164 {
165     float f[4];
166
167     _mm_storeu_ps(f,xmm);
168     printf("%s (sum): %15.10g\n",s,f[0]+f[1]+f[2]+f[3]);
169 }
170
171
172 static void
173 gmx_mm_printxmm_pd(const char *s,__m128d xmm)
174 {
175     double f[2];
176
177     _mm_storeu_pd(f,xmm);
178     printf("%s: %30.20e %30.20e\n",s,f[0],f[1]);
179 }
180
181 static void
182 gmx_mm_printxmmsum_pd(const char *s,__m128d xmm)
183 {
184     double f[2];
185
186     _mm_storeu_pd(f,xmm);
187     printf("%s (sum): %15.10g\n",s,f[0]+f[1]);
188 }
189
190
191 static void
192 gmx_mm_printxmm_epi32(const char *s,__m128i xmmi)
193 {
194     int i[4];
195
196     _mm_storeu_si128((__m128i *)i,xmmi);
197     printf("%10s: %2d %2d %2d %2d\n",s,i[0],i[1],i[2],i[3]);
198 }
199
200 static void
201 gmx_mm256_printymm_ps(const char *s,__m256 ymm)
202 {
203     float f[8];
204
205     _mm256_storeu_ps(f,ymm);
206     printf("%s: %12.7f %12.7f %12.7f %12.7f %12.7f %12.7f %12.7f %12.7f\n",s,f[0],f[1],f[2],f[3],f[4],f[5],f[6],f[7]);
207 }
208
209 static void
210 gmx_mm256_printymmsum_ps(const char *s,__m256 ymm)
211 {
212     float f[8];
213
214     _mm256_storeu_ps(f,ymm);
215     printf("%s (sum): %15.10g\n",s,f[0]+f[1]+f[2]+f[3]+f[4]+f[5]+f[6]+f[7]);
216 }
217
218
219 static void
220 gmx_mm256_printymm_pd(const char *s,__m256d ymm)
221 {
222     double f[4];
223
224     _mm256_storeu_pd(f,ymm);
225     printf("%s: %16.12f %16.12f %16.12f %16.12f\n",s,f[0],f[1],f[2],f[3]);
226 }
227
228 static void
229 gmx_mm256_printymmsum_pd(const char *s,__m256d ymm)
230 {
231     double f[4];
232
233     _mm256_storeu_pd(f,ymm);
234     printf("%s (sum): %15.10g\n",s,f[0]+f[1]+f[2]+f[3]);
235 }
236
237
238
239 static void
240 gmx_mm256_printymm_epi32(const char *s,__m256i ymmi)
241 {
242     int i[8];
243
244     _mm256_storeu_si256((__m256i *)i,ymmi);
245     printf("%10s: %2d %2d %2d %2d %2d %2d %2d %2d\n",s,i[0],i[1],i[2],i[3],i[4],i[5],i[6],i[7]);
246 }
247
248
249
250 static int gmx_mm_check_and_reset_overflow(void)
251 {
252     int MXCSR;
253     int sse_overflow;
254
255     MXCSR = _mm_getcsr();
256     /* The overflow flag is bit 3 in the register */
257     if (MXCSR & 0x0008)
258     {
259         sse_overflow = 1;
260         /* Set the overflow flag to zero */
261         MXCSR = MXCSR & 0xFFF7;
262         _mm_setcsr(MXCSR);
263     }
264     else
265     {
266         sse_overflow = 0;
267     }
268
269     return sse_overflow;
270 }
271
272
273
274 #endif /* _gmx_x86_avx_256_h_ */