Fix some #include problems.
[alexxy/gromacs.git] / src / gromacs / legacyheaders / gmx_simd_macros.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2012,2013, 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 /* The macros in this file are intended to be used for writing
37  * architecture-independent SIMD intrinsics code.
38  * To support a new architecture, adding macros here should be (nearly)
39  * all that is needed.
40  */
41
42 #ifdef _gmx_simd_macros_h_
43 #error "gmx_simd_macros.h included twice"
44 #else
45 #define _gmx_simd_macros_h_
46
47 /* NOTE: SSE2 acceleration does not include floor or blendv */
48
49
50 /* Uncomment the next line, without other SIMD active, for testing plain-C */
51 /* #define GMX_SIMD_REFERENCE_PLAIN_C */
52 #ifdef GMX_SIMD_REFERENCE_PLAIN_C
53 /* Plain C SIMD reference implementation, also serves as documentation */
54 #define GMX_HAVE_SIMD_MACROS
55
56 /* In general the reference SIMD supports any SIMD width, including 1.
57  * See types/nb_verlet.h for details
58  */
59 #define GMX_SIMD_REF_WIDTH  4
60
61 /* Include plain-C reference implementation, also serves as documentation */
62 #include "gmx_simd_ref.h"
63
64 #define GMX_SIMD_WIDTH_HERE  GMX_SIMD_REF_WIDTH
65
66 /* float/double SIMD register type */
67 #define gmx_mm_pr  gmx_simd_ref_pr
68
69 /* boolean SIMD register type */
70 #define gmx_mm_pb  gmx_simd_ref_pb
71
72 /* integer SIMD register type, only for table indexing and exclusion masks */
73 #define gmx_epi32  gmx_simd_ref_epi32
74 #define GMX_SIMD_EPI32_WIDTH  GMX_SIMD_REF_EPI32_WIDTH
75
76 /* Load GMX_SIMD_WIDTH_HERE reals for memory starting at r */
77 #define gmx_load_pr       gmx_simd_ref_load_pr
78 /* Set all SIMD register elements to *r */
79 #define gmx_load1_pr      gmx_simd_ref_load1_pr
80 #define gmx_set1_pr       gmx_simd_ref_set1_pr
81 #define gmx_setzero_pr    gmx_simd_ref_setzero_pr
82 #define gmx_store_pr      gmx_simd_ref_store_pr
83
84 #define gmx_add_pr        gmx_simd_ref_add_pr
85 #define gmx_sub_pr        gmx_simd_ref_sub_pr
86 #define gmx_mul_pr        gmx_simd_ref_mul_pr
87 /* For the FMA macros below, aim for c=d in code, so FMA3 uses 1 instruction */
88 #define gmx_madd_pr       gmx_simd_ref_madd_pr
89 #define gmx_nmsub_pr      gmx_simd_ref_nmsub_pr
90
91 #define gmx_max_pr        gmx_simd_ref_max_pr
92 #define gmx_blendzero_pr  gmx_simd_ref_blendzero_pr
93
94 #define gmx_round_pr      gmx_simd_ref_round_pr
95
96 /* Not required, only used to speed up the nbnxn tabulated PME kernels */
97 #define GMX_SIMD_HAVE_FLOOR
98 #ifdef GMX_SIMD_HAVE_FLOOR
99 #define gmx_floor_pr      gmx_simd_ref_floor_pr
100 #endif
101
102 /* Not required, only used when blendv is faster than comparison */
103 #define GMX_SIMD_HAVE_BLENDV
104 #ifdef GMX_SIMD_HAVE_BLENDV
105 #define gmx_blendv_pr     gmx_simd_ref_blendv_pr
106 #endif
107
108 /* Copy the sign of a to b, assumes b >= 0 for efficiency */
109 #define gmx_cpsgn_nonneg_pr  gmx_simd_ref_cpsgn_nonneg_pr
110
111 /* Very specific operation required in the non-bonded kernels */
112 #define gmx_masknot_add_pr   gmx_simd_ref_masknot_add_pr
113
114 /* Comparison */
115 #define gmx_cmplt_pr      gmx_simd_ref_cmplt_pr
116
117 /* Logical operations on SIMD booleans */
118 #define gmx_and_pb        gmx_simd_ref_and_pb
119 #define gmx_or_pb         gmx_simd_ref_or_pb
120
121 /* Returns a single int (0/1) which tells if any of the 4 booleans is True */
122 #define gmx_anytrue_pb    gmx_simd_ref_anytrue_pb
123
124 /* Conversions only used for PME table lookup */
125 #define gmx_cvttpr_epi32  gmx_simd_ref_cvttpr_epi32
126 #define gmx_cvtepi32_pr   gmx_simd_ref_cvtepi32_pr
127
128 /* These two function only need to be approximate, Newton-Raphson iteration
129  * is used for full accuracy in gmx_invsqrt_pr and gmx_inv_pr.
130  */
131 #define gmx_rsqrt_pr      gmx_simd_ref_rsqrt_pr
132 #define gmx_rcp_pr        gmx_simd_ref_rcp_pr
133
134 /* sqrt+inv+sin+cos+acos+atan2 are used for bonded potentials, exp for PME */
135 #define GMX_SIMD_HAVE_EXP
136 #ifdef GMX_SIMD_HAVE_EXP
137 #define gmx_exp_pr        gmx_simd_ref_exp_pr
138 #endif
139 #define GMX_SIMD_HAVE_TRIGONOMETRIC
140 #ifdef GMX_SIMD_HAVE_TRIGONOMETRIC
141 #define gmx_sqrt_pr       gmx_simd_ref_sqrt_pr
142 #define gmx_sincos_pr     gmx_simd_ref_sincos_pr
143 #define gmx_acos_pr       gmx_simd_ref_acos_pr
144 #define gmx_atan2_pr      gmx_simd_ref_atan2_pr
145 #endif
146
147 #endif /* GMX_SIMD_REFERENCE_PLAIN_C */
148
149
150 /* The same SIMD macros can be translated to SIMD intrinsics (and compiled
151  * to instructions for) different SIMD width and float precision.
152  *
153  * On x86: The gmx_ prefix is replaced by _mm_ or _mm256_ (SSE or AVX).
154  * The _pr suffix is replaced by _ps or _pd (for single or double precision).
155  * Compiler settings will decide if 128-bit intrinsics will
156  * be translated into SSE or AVX instructions.
157  */
158
159
160 #ifdef GMX_USE_HALF_WIDTH_SIMD_HERE
161 #if defined GMX_X86_AVX_256
162 /* We have half SIMD width support, continue */
163 #else
164 #error "half SIMD width intrinsics are not supported"
165 #endif
166 #endif
167
168 #ifdef GMX_TARGET_X86
169
170 #ifdef GMX_X86_SSE2
171 /* This is for general x86 SIMD instruction sets that also support SSE2 */
172 #define GMX_HAVE_SIMD_MACROS
173
174 /* Include the highest supported x86 SIMD intrisics + math functions */
175 #ifdef GMX_X86_AVX_256
176 #include "gmx_x86_avx_256.h"
177 #ifdef GMX_DOUBLE
178 #include "gmx_math_x86_avx_256_double.h"
179 #else  /* GMX_DOUBLE */
180 #include "gmx_math_x86_avx_256_single.h"
181 #endif /* GMX_DOUBLE */
182 #else  /* GMX_X86_AVX_256 */
183 #ifdef GMX_X86_AVX_128_FMA
184 #include "gmx_x86_avx_128_fma.h"
185 #ifdef GMX_DOUBLE
186 #include "gmx_math_x86_avx_128_fma_double.h"
187 #else  /* GMX_DOUBLE */
188 #include "gmx_math_x86_avx_128_fma_single.h"
189 #endif /* GMX_DOUBLE */
190 #else  /* GMX_X86_AVX_128_FMA */
191 #ifdef GMX_X86_SSE4_1
192 #include "gmx_x86_sse4_1.h"
193 #ifdef GMX_DOUBLE
194 #include "gmx_math_x86_sse4_1_double.h"
195 #else  /* GMX_DOUBLE */
196 #include "gmx_math_x86_sse4_1_single.h"
197 #endif /* GMX_DOUBLE */
198 #else  /* GMX_X86_SSE4_1 */
199 #ifdef GMX_X86_SSE2
200 #include "gmx_x86_sse2.h"
201 #ifdef GMX_DOUBLE
202 #include "gmx_math_x86_sse2_double.h"
203 #else  /* GMX_DOUBLE */
204 #include "gmx_math_x86_sse2_single.h"
205 #endif /* GMX_DOUBLE */
206 #else  /* GMX_X86_SSE2 */
207 #error No x86 acceleration defined
208 #endif /* GMX_X86_SSE2 */
209 #endif /* GMX_X86_SSE4_1 */
210 #endif /* GMX_X86_AVX_128_FMA */
211 #endif /* GMX_X86_AVX_256 */
212
213 /* exp and trigonometric functions are included above */
214 #define GMX_SIMD_HAVE_EXP
215 #define GMX_SIMD_HAVE_TRIGONOMETRIC
216
217 #if !defined GMX_X86_AVX_256 || defined GMX_USE_HALF_WIDTH_SIMD_HERE
218
219 #ifndef GMX_DOUBLE
220
221 #define GMX_SIMD_WIDTH_HERE  4
222
223 #define gmx_mm_pr  __m128
224
225 #define gmx_mm_pb  __m128
226
227 #define gmx_epi32  __m128i
228 #define GMX_SIMD_EPI32_WIDTH  4
229
230 #define gmx_load_pr       _mm_load_ps
231 #define gmx_load1_pr      _mm_load1_ps
232 #define gmx_set1_pr       _mm_set1_ps
233 #define gmx_setzero_pr    _mm_setzero_ps
234 #define gmx_store_pr      _mm_store_ps
235
236 #define gmx_add_pr        _mm_add_ps
237 #define gmx_sub_pr        _mm_sub_ps
238 #define gmx_mul_pr        _mm_mul_ps
239 #ifdef GMX_X86_AVX_128_FMA
240 #define GMX_SIMD_HAVE_FMA
241 #define gmx_madd_pr(a, b, c)   _mm_macc_ps(a, b, c)
242 #define gmx_nmsub_pr(a, b, c)  _mm_nmacc_ps(a, b, c)
243 #else
244 #define gmx_madd_pr(a, b, c)   _mm_add_ps(c, _mm_mul_ps(a, b))
245 #define gmx_nmsub_pr(a, b, c)  _mm_sub_ps(c, _mm_mul_ps(a, b))
246 #endif
247 #define gmx_max_pr        _mm_max_ps
248 #define gmx_blendzero_pr  _mm_and_ps
249
250 #define gmx_cmplt_pr      _mm_cmplt_ps
251 #define gmx_and_pb        _mm_and_ps
252 #define gmx_or_pb         _mm_or_ps
253
254 #ifdef GMX_X86_SSE4_1
255 #define gmx_round_pr(x)   _mm_round_ps(x, 0x0)
256 #define GMX_SIMD_HAVE_FLOOR
257 #define gmx_floor_pr      _mm_floor_ps
258 #else
259 #define gmx_round_pr(x)   _mm_cvtepi32_ps(_mm_cvtps_epi32(x))
260 #endif
261
262 #ifdef GMX_X86_SSE4_1
263 #define GMX_SIMD_HAVE_BLENDV
264 #define gmx_blendv_pr     _mm_blendv_ps
265 #endif
266
267 static gmx_inline gmx_mm_pr gmx_cpsgn_nonneg_pr(gmx_mm_pr a, gmx_mm_pr b)
268 {
269     /* The value -0.0 has only the sign-bit set */
270     gmx_mm_pr sign_mask = _mm_set1_ps(-0.0);
271     return _mm_or_ps(_mm_and_ps(a, sign_mask), b);
272 };
273
274 static gmx_inline gmx_mm_pr gmx_masknot_add_pr(gmx_mm_pb a, gmx_mm_pr b, gmx_mm_pr c)
275 {
276     return _mm_add_ps(b, _mm_andnot_ps(a, c));
277 };
278
279 #define gmx_anytrue_pb    _mm_movemask_ps
280
281 #define gmx_cvttpr_epi32  _mm_cvttps_epi32
282 #define gmx_cvtepi32_pr   _mm_cvtepi32_ps
283
284 #define gmx_rsqrt_pr      _mm_rsqrt_ps
285 #define gmx_rcp_pr        _mm_rcp_ps
286
287 #define gmx_exp_pr        gmx_mm_exp_ps
288 #define gmx_sqrt_pr       gmx_mm_sqrt_ps
289 #define gmx_sincos_pr     gmx_mm_sincos_ps
290 #define gmx_acos_pr       gmx_mm_acos_ps
291 #define gmx_atan2_pr      gmx_mm_atan2_ps
292
293 #else /* ifndef GMX_DOUBLE */
294
295 #define GMX_SIMD_WIDTH_HERE  2
296
297 #define gmx_mm_pr  __m128d
298
299 #define gmx_mm_pb  __m128d
300
301 #define gmx_epi32  __m128i
302 #define GMX_SIMD_EPI32_WIDTH  4
303
304 #define gmx_load_pr       _mm_load_pd
305 #define gmx_load1_pr      _mm_load1_pd
306 #define gmx_set1_pr       _mm_set1_pd
307 #define gmx_setzero_pr    _mm_setzero_pd
308 #define gmx_store_pr      _mm_store_pd
309
310 #define gmx_add_pr        _mm_add_pd
311 #define gmx_sub_pr        _mm_sub_pd
312 #define gmx_mul_pr        _mm_mul_pd
313 #ifdef GMX_X86_AVX_128_FMA
314 #define GMX_SIMD_HAVE_FMA
315 #define gmx_madd_pr(a, b, c)   _mm_macc_pd(a, b, c)
316 #define gmx_nmsub_pr(a, b, c)  _mm_nmacc_pd(a, b, c)
317 #else
318 #define gmx_madd_pr(a, b, c)   _mm_add_pd(c, _mm_mul_pd(a, b))
319 #define gmx_nmsub_pr(a, b, c)  _mm_sub_pd(c, _mm_mul_pd(a, b))
320 #endif
321 #define gmx_max_pr        _mm_max_pd
322 #define gmx_blendzero_pr  _mm_and_pd
323
324 #ifdef GMX_X86_SSE4_1
325 #define gmx_round_pr(x)   _mm_round_pd(x, 0x0)
326 #define GMX_SIMD_HAVE_FLOOR
327 #define gmx_floor_pr      _mm_floor_pd
328 #else
329 #define gmx_round_pr(x)   _mm_cvtepi32_pd(_mm_cvtpd_epi32(x))
330 /* gmx_floor_pr is not used in code for pre-SSE4_1 hardware */
331 #endif
332
333 #ifdef GMX_X86_SSE4_1
334 #define GMX_SIMD_HAVE_BLENDV
335 #define gmx_blendv_pr     _mm_blendv_pd
336 #endif
337
338 static gmx_inline gmx_mm_pr gmx_cpsgn_nonneg_pr(gmx_mm_pr a, gmx_mm_pr b)
339 {
340     gmx_mm_pr sign_mask = _mm_set1_pd(-0.0);
341     return _mm_or_pd(_mm_and_pd(a, sign_mask), b);
342 };
343
344 static gmx_inline gmx_mm_pr gmx_masknot_add_pr(gmx_mm_pb a, gmx_mm_pr b, gmx_mm_pr c)
345 {
346     return _mm_add_pd(b, _mm_andnot_pd(a, c));
347 };
348
349 #define gmx_cmplt_pr      _mm_cmplt_pd
350
351 #define gmx_and_pb        _mm_and_pd
352 #define gmx_or_pb         _mm_or_pd
353
354 #define gmx_anytrue_pb    _mm_movemask_pd
355
356 #define gmx_cvttpr_epi32  _mm_cvttpd_epi32
357 #define gmx_cvtepi32_pr   _mm_cvtepi32_pd
358
359 #define gmx_rsqrt_pr(r)   _mm_cvtps_pd(_mm_rsqrt_ps(_mm_cvtpd_ps(r)))
360 #define gmx_rcp_pr(r)     _mm_cvtps_pd(_mm_rcp_ps(_mm_cvtpd_ps(r)))
361
362 #define gmx_exp_pr        gmx_mm_exp_pd
363 #define gmx_sqrt_pr       gmx_mm_sqrt_pd
364 #define gmx_sincos_pr     gmx_mm_sincos_pd
365 #define gmx_acos_pr       gmx_mm_acos_pd
366 #define gmx_atan2_pr      gmx_mm_atan2_pd
367
368 #endif /* ifndef GMX_DOUBLE */
369
370 #else
371 /* We have GMX_X86_AVX_256 and not GMX_USE_HALF_WIDTH_SIMD_HERE,
372  * so we use 256-bit SIMD.
373  */
374
375 #ifndef GMX_DOUBLE
376
377 #define GMX_SIMD_WIDTH_HERE  8
378
379 #define gmx_mm_pr  __m256
380
381 #define gmx_mm_pb  __m256
382
383 #define gmx_epi32  __m256i
384 #define GMX_SIMD_EPI32_WIDTH  8
385
386 #define gmx_load_pr       _mm256_load_ps
387 #define gmx_load1_pr(x)   _mm256_set1_ps((x)[0])
388 #define gmx_set1_pr       _mm256_set1_ps
389 #define gmx_setzero_pr    _mm256_setzero_ps
390 #define gmx_store_pr      _mm256_store_ps
391
392 #define gmx_add_pr        _mm256_add_ps
393 #define gmx_sub_pr        _mm256_sub_ps
394 #define gmx_mul_pr        _mm256_mul_ps
395 #define gmx_madd_pr(a, b, c)   _mm256_add_ps(c, _mm256_mul_ps(a, b))
396 #define gmx_nmsub_pr(a, b, c)  _mm256_sub_ps(c, _mm256_mul_ps(a, b))
397 #define gmx_max_pr        _mm256_max_ps
398 #define gmx_blendzero_pr  _mm256_and_ps
399
400 #define gmx_round_pr(x)   _mm256_round_ps(x, 0x0)
401 #define GMX_SIMD_HAVE_FLOOR
402 #define gmx_floor_pr      _mm256_floor_ps
403
404 #define GMX_SIMD_HAVE_BLENDV
405 #define gmx_blendv_pr     _mm256_blendv_ps
406
407 static gmx_inline gmx_mm_pr gmx_cpsgn_nonneg_pr(gmx_mm_pr a, gmx_mm_pr b)
408 {
409     gmx_mm_pr sign_mask = _mm256_set1_ps(-0.0);
410     return _mm256_or_ps(_mm256_and_ps(a, sign_mask), b);
411 };
412
413 static gmx_inline gmx_mm_pr gmx_masknot_add_pr(gmx_mm_pb a, gmx_mm_pr b, gmx_mm_pr c)
414 {
415     return _mm256_add_ps(b, _mm256_andnot_ps(a, c));
416 };
417
418 /* Less-than (we use ordered, non-signaling, but that's not required) */
419 #define gmx_cmplt_pr(x, y) _mm256_cmp_ps(x, y, 0x11)
420 #define gmx_and_pb        _mm256_and_ps
421 #define gmx_or_pb         _mm256_or_ps
422
423 #define gmx_anytrue_pb    _mm256_movemask_ps
424
425 #define gmx_cvttpr_epi32  _mm256_cvttps_epi32
426
427 #define gmx_rsqrt_pr      _mm256_rsqrt_ps
428 #define gmx_rcp_pr        _mm256_rcp_ps
429
430 #define gmx_exp_pr        gmx_mm256_exp_ps
431 #define gmx_sqrt_pr       gmx_mm256_sqrt_ps
432 #define gmx_sincos_pr     gmx_mm256_sincos_ps
433 #define gmx_acos_pr       gmx_mm256_acos_ps
434 #define gmx_atan2_pr      gmx_mm256_atan2_ps
435
436 #else /* ifndef GMX_DOUBLE */
437
438 #define GMX_SIMD_WIDTH_HERE  4
439
440 #define gmx_mm_pr  __m256d
441
442 #define gmx_mm_pb  __m256d
443
444 /* We use 128-bit integer registers because of missing 256-bit operations */
445 #define gmx_epi32  __m128i
446 #define GMX_SIMD_EPI32_WIDTH  4
447
448 #define gmx_load_pr       _mm256_load_pd
449 #define gmx_load1_pr(x)   _mm256_set1_pd((x)[0])
450 #define gmx_set1_pr       _mm256_set1_pd
451 #define gmx_setzero_pr    _mm256_setzero_pd
452 #define gmx_store_pr      _mm256_store_pd
453
454 #define gmx_add_pr        _mm256_add_pd
455 #define gmx_sub_pr        _mm256_sub_pd
456 #define gmx_mul_pr        _mm256_mul_pd
457 #define gmx_madd_pr(a, b, c)   _mm256_add_pd(c, _mm256_mul_pd(a, b))
458 #define gmx_nmsub_pr(a, b, c)  _mm256_sub_pd(c, _mm256_mul_pd(a, b))
459 #define gmx_max_pr        _mm256_max_pd
460 #define gmx_blendzero_pr  _mm256_and_pd
461
462 #define gmx_round_pr(x)   _mm256_round_pd(x, 0x0)
463 #define GMX_SIMD_HAVE_FLOOR
464 #define gmx_floor_pr      _mm256_floor_pd
465
466 #define GMX_SIMD_HAVE_BLENDV
467 #define gmx_blendv_pr     _mm256_blendv_pd
468
469 static gmx_inline gmx_mm_pr gmx_cpsgn_nonneg_pr(gmx_mm_pr a, gmx_mm_pr b)
470 {
471     gmx_mm_pr sign_mask = _mm256_set1_pd(-0.0);
472     return _mm256_or_pd(_mm256_and_pd(a, sign_mask), b);
473 };
474
475 static gmx_inline gmx_mm_pr gmx_masknot_add_pr(gmx_mm_pb a, gmx_mm_pr b, gmx_mm_pr c)
476 {
477     return _mm256_add_pd(b, _mm256_andnot_pd(a, c));
478 };
479
480 /* Less-than (we use ordered, non-signaling, but that's not required) */
481 #define gmx_cmplt_pr(x, y) _mm256_cmp_pd(x, y, 0x11)
482
483 #define gmx_and_pb        _mm256_and_pd
484 #define gmx_or_pb         _mm256_or_pd
485
486 #define gmx_anytrue_pb    _mm256_movemask_pd
487
488 #define gmx_cvttpr_epi32  _mm256_cvttpd_epi32
489
490 #define gmx_rsqrt_pr(r)   _mm256_cvtps_pd(_mm_rsqrt_ps(_mm256_cvtpd_ps(r)))
491 #define gmx_rcp_pr(r)     _mm256_cvtps_pd(_mm_rcp_ps(_mm256_cvtpd_ps(r)))
492
493 #define gmx_exp_pr        gmx_mm256_exp_pd
494 #define gmx_sqrt_pr       gmx_mm256_sqrt_pd
495 #define gmx_sincos_pr     gmx_mm256_sincos_pd
496 #define gmx_acos_pr       gmx_mm256_acos_pd
497 #define gmx_atan2_pr      gmx_mm256_atan2_pd
498
499 #endif /* ifndef GMX_DOUBLE */
500
501 #endif /* 128- or 256-bit x86 SIMD */
502
503 #endif /* GMX_X86_SSE2 */
504
505 #endif /* GMX_TARGET_X86 */
506
507 #ifdef GMX_CPU_ACCELERATION_IBM_QPX
508
509 /* This hack works on the compilers that can reach this code. A real
510    solution with broader scope will be proposed in master branch. */
511 #define gmx_always_inline __attribute__((always_inline))
512
513 /* This is for the A2 core on BlueGene/Q that supports IBM's QPX
514    vector built-in functions */
515 #define GMX_HAVE_SIMD_MACROS
516 #ifdef __clang__
517 #include <qpxmath.h>
518 #else
519 #include <mass_simd.h>
520 #endif
521
522 /* No need to version the code by the precision, because the QPX AXU
523    extends to and truncates from double precision for free. */
524
525 #define GMX_SIMD_WIDTH_HERE  4
526 typedef vector4double gmx_mm_pr;
527 typedef vector4double gmx_mm_pb;
528 typedef vector4double gmx_epi32;
529 #define GMX_SIMD_EPI32_WIDTH  4
530
531 static gmx_inline gmx_mm_pr gmx_always_inline gmx_load_pr(const real *a)
532 {
533 #ifdef NDEBUG
534     return vec_ld(0, (real *) a);
535 #else
536     return vec_lda(0, (real *) a);
537 #endif
538 }
539
540 static gmx_inline gmx_mm_pr gmx_always_inline gmx_load1_pr(const real *a)
541 {
542     return vec_splats(*a);
543 }
544
545 static gmx_inline gmx_mm_pr gmx_always_inline gmx_set1_pr(real a)
546 {
547     return vec_splats(a);
548 }
549
550 static gmx_inline gmx_mm_pr gmx_always_inline gmx_setzero_pr()
551 {
552     return vec_splats(0.0);
553 }
554
555 static gmx_inline void gmx_always_inline gmx_store_pr(real *a, gmx_mm_pr b)
556 {
557 #ifdef NDEBUG
558     vec_st(b, 0, a);
559 #else
560     vec_sta(b, 0, a);
561 #endif
562 }
563
564 static gmx_inline gmx_mm_pr gmx_always_inline gmx_add_pr(gmx_mm_pr a, gmx_mm_pr b)
565 {
566     return vec_add(a, b);
567 }
568
569 static gmx_inline gmx_mm_pr gmx_always_inline gmx_sub_pr(gmx_mm_pr a, gmx_mm_pr b)
570 {
571     return vec_sub(a, b);
572 }
573
574 static gmx_inline gmx_mm_pr gmx_always_inline gmx_mul_pr(gmx_mm_pr a, gmx_mm_pr b)
575 {
576     return vec_mul(a, b);
577 }
578
579 static gmx_inline gmx_mm_pr gmx_always_inline gmx_madd_pr(gmx_mm_pr a, gmx_mm_pr b, gmx_mm_pr c)
580 {
581     return vec_madd(a, b, c);
582 }
583
584 static gmx_inline gmx_mm_pr gmx_always_inline gmx_nmsub_pr(gmx_mm_pr a, gmx_mm_pr b, gmx_mm_pr c)
585 {
586     return vec_nmsub(a, b, c);
587 }
588
589 static gmx_inline gmx_mm_pr gmx_always_inline gmx_max_pr(gmx_mm_pr a, gmx_mm_pr b)
590 {
591     return vec_sel(b, a, vec_sub(a, b));
592 }
593
594 static gmx_inline gmx_mm_pr gmx_always_inline gmx_blendzero_pr(gmx_mm_pr a, gmx_mm_pr b)
595 {
596     return vec_sel(gmx_setzero_pr(), a, b);
597 }
598
599 static gmx_inline gmx_mm_pb gmx_always_inline gmx_cmplt_pr(gmx_mm_pr a, gmx_mm_pr b)
600 {
601     return vec_cmplt(a, b);
602 }
603
604 static gmx_inline gmx_mm_pb gmx_always_inline gmx_and_pb(gmx_mm_pb a, gmx_mm_pb b)
605 {
606     return vec_and(a, b);
607 }
608
609 static gmx_inline gmx_mm_pb gmx_always_inline gmx_or_pb(gmx_mm_pb a, gmx_mm_pb b)
610 {
611     return vec_or(a, b);
612 }
613
614 static gmx_inline gmx_mm_pr gmx_always_inline gmx_round_pr(gmx_mm_pr a)
615 {
616     return vec_round(a);
617 }
618
619 #define GMX_SIMD_HAVE_FLOOR
620 static gmx_inline gmx_mm_pr gmx_always_inline gmx_floor_pr(gmx_mm_pr a)
621 {
622     return vec_floor(a);
623 }
624
625 #define GMX_SIMD_HAVE_BLENDV
626 static gmx_inline gmx_mm_pr gmx_always_inline gmx_blendv_pr(gmx_mm_pr a, gmx_mm_pr b, gmx_mm_pr c)
627 {
628     return vec_sel(b, a, gmx_cmplt_pr(gmx_setzero_pr(), c));
629 }
630
631 static gmx_inline gmx_mm_pr gmx_always_inline gmx_cpsgn_nonneg_pr(gmx_mm_pr a, gmx_mm_pr b)
632 {
633     return vec_cpsgn(a, b);
634 };
635
636 static gmx_inline gmx_mm_pr gmx_always_inline gmx_masknot_add_pr(gmx_mm_pb a, gmx_mm_pr b, gmx_mm_pr c)
637 {
638     return vec_add(b, vec_sel(c, gmx_setzero_pr(), a));
639 };
640
641 static gmx_inline gmx_bool gmx_always_inline
642 GMX_SIMD_IS_TRUE(real x)
643 {
644     return x >= 0.0;
645 }
646
647 static gmx_inline gmx_epi32 gmx_always_inline gmx_cvttpr_epi32(gmx_mm_pr a)
648 {
649     return vec_ctiwuz(a);
650 }
651 /* Don't want this, we have floor */
652 /* #define gmx_cvtepi32_pr   vec_cvtepi32 */
653
654 /* A2 core on BG/Q delivers relative error of 2^-14, whereas Power ISA
655    Architecture only promises 2^-8. So probably no need for
656    Newton-Raphson iterates at single or double. */
657 static gmx_inline gmx_mm_pr gmx_always_inline gmx_rsqrt_pr(gmx_mm_pr a)
658 {
659     return vec_rsqrte(a);
660 }
661
662 /* A2 core on BG/Q delivers relative error of 2^-14, whereas Power ISA
663    Architecture only promises 2^-5. So probably no need for
664    Newton-Raphson iterates at single or double. */
665 static gmx_inline gmx_mm_pr gmx_always_inline gmx_rcp_pr(gmx_mm_pr a)
666 {
667     return vec_re(a);
668 }
669
670 /* Note that here, and below, we use the built-in SLEEF port when
671    compiling on BlueGene/Q with clang */
672
673 #define GMX_SIMD_HAVE_EXP
674 static gmx_inline gmx_mm_pr gmx_always_inline gmx_exp_pr(gmx_mm_pr a)
675 {
676 #ifdef __clang__
677 #ifndef GMX_DOUBLE
678     return xexpf(a);
679 #else
680     return xexp(a);
681 #endif
682 #else
683 #ifndef GMX_DOUBLE
684     return expf4(a);
685 #else
686     return expd4(a);
687 #endif
688 #endif
689 }
690
691 static gmx_inline gmx_mm_pr gmx_always_inline gmx_sqrt_pr(gmx_mm_pr a)
692 {
693 #ifdef NDEBUG
694     return vec_swsqrt_nochk(a);
695 #else
696     return vec_swsqrt(a);
697 #endif
698 }
699
700 #define GMX_SIMD_HAVE_TRIGONOMETRIC
701 static gmx_inline int gmx_always_inline gmx_sincos_pr(gmx_mm_pr a, gmx_mm_pr *b, gmx_mm_pr *c)
702 {
703 #ifdef __clang__
704 #ifndef GMX_DOUBLE
705     xsincosf(a, b, c);
706 #else
707     xsincos(a, b, c);
708 #endif
709 #else
710 #ifndef GMX_DOUBLE
711     sincosf4(a, b, c);
712 #else
713     sincosd4(a, b, c);
714 #endif
715 #endif
716     return 1;
717 }
718
719 static gmx_inline gmx_mm_pr gmx_always_inline gmx_acos_pr(gmx_mm_pr a)
720 {
721 #ifdef __clang__
722 #ifndef GMX_DOUBLE
723     return xacosf(a);
724 #else
725     return xacos(a);
726 #endif
727 #else
728 #ifndef GMX_DOUBLE
729     return acosf4(a);
730 #else
731     return acosd4(a);
732 #endif
733 #endif
734 }
735
736 /* NB The order of parameters here is correct; the
737    documentation of atan2[df]4 in SIMD MASS is wrong. */
738 static gmx_inline gmx_mm_pr gmx_always_inline gmx_atan2_pr(gmx_mm_pr a, gmx_mm_pr b)
739 {
740 #ifdef __clang__
741 #ifndef GMX_DOUBLE
742     return xatan2f(a, b);
743 #else
744     return xatan2(a, b);
745 #endif
746 #else
747 #ifndef GMX_DOUBLE
748     return atan2f4(a, b);
749 #else
750     return atan2d4(a, b);
751 #endif
752 #endif
753 }
754
755 static gmx_inline int gmx_always_inline
756 gmx_anytrue_pb(gmx_mm_pb a)
757 {
758     /* The "anytrue" is done solely on the QPX AXU (which is the only
759        available FPU). This is awkward, because pretty much no
760        "horizontal" SIMD-vector operations exist, unlike x86 where
761        SSE4.1 added various kinds of horizontal operations. So we have
762        to make do with shifting vector elements and operating on the
763        results. This makes for lots of data dependency, but the main
764        alternative of storing to memory and reloading is not going to
765        help, either. OpenMP over 2 or 4 hardware threads per core will
766        hide much of the latency from the data dependency. The
767        vec_extract() lets the compiler correctly use a floating-point
768        comparison on the zeroth vector element, which avoids needing
769        memory at all.
770      */
771     gmx_mm_pb vec_shifted_left_0 = a;
772     gmx_mm_pb vec_shifted_left_1 = vec_sldw(a, a, 1);
773     gmx_mm_pb vec_shifted_left_2 = vec_sldw(a, a, 2);
774     gmx_mm_pb vec_shifted_left_3 = vec_sldw(a, a, 3);
775
776     gmx_mm_pb vec_return = vec_or(vec_or(vec_shifted_left_2, vec_shifted_left_3),
777                                   vec_or(vec_shifted_left_0, vec_shifted_left_1));
778     return (0.0 < vec_extract(vec_return, 0));
779 };
780
781 #undef gmx_always_inline
782
783 #endif /* GMX_CPU_ACCELERATION_IBM_QPX */
784
785 #ifdef GMX_HAVE_SIMD_MACROS
786 /* Generic functions to extract a SIMD aligned pointer from a pointer x.
787  * x should have at least GMX_SIMD_WIDTH_HERE elements extra compared
788  * to how many you want to use, to avoid indexing outside the aligned region.
789  */
790
791 static gmx_inline real *
792 gmx_simd_align_real(const real *x)
793 {
794     return (real *)(((size_t)((x)+GMX_SIMD_WIDTH_HERE)) & (~((size_t)(GMX_SIMD_WIDTH_HERE*sizeof(real)-1))));
795 }
796
797 static gmx_inline int *
798 gmx_simd_align_int(const int *x)
799 {
800     return (int  *)(((size_t)((x)+GMX_SIMD_WIDTH_HERE)) & (~((size_t)(GMX_SIMD_WIDTH_HERE*sizeof(int )-1))));
801 }
802
803
804 /* Include the math functions which only need the above macros,
805  * generally these are the ones that don't need masking operations.
806  */
807 #ifdef GMX_DOUBLE
808 #include "gmx_simd_math_double.h"
809 #else
810 #include "gmx_simd_math_single.h"
811 #endif
812
813
814 #endif /* GMX_HAVE_SIMD_MACROS */
815
816 #endif /* _gmx_simd_macros_h_ */