2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2013,2014, 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_MACROS_REF_H
37 #define GMX_SIMD_MACROS_REF_H
39 /* This file contains a reference plain-C implementation of arbitrary width.
40 * This code is only useful for testing and documentation.
41 * The SIMD width is set by defining GMX_SIMD_REF_WIDTH before including.
45 #ifndef GMX_SIMD_REF_WIDTH
46 #error "GMX_SIMD_REF_WIDTH should be defined before including gromacs/simd/macros_ref.h"
51 /* float/double SIMD register type */
53 real r[GMX_SIMD_REF_WIDTH];
56 /* boolean SIMD register type */
58 char r[GMX_SIMD_REF_WIDTH];
61 /* integer SIMD register type, only for table indexing and exclusion masks */
63 int r[GMX_SIMD_REF_WIDTH];
65 #define GMX_SIMD_REF_EPI32_WIDTH GMX_SIMD_REF_WIDTH
67 /* Load GMX_SIMD_REF_WIDTH reals for memory starting at r */
68 static gmx_inline gmx_simd_ref_pr
69 gmx_simd_ref_load_pr(const real *r)
74 for (i = 0; i < GMX_SIMD_REF_WIDTH; i++)
82 /* Set all SIMD register elements to *r */
83 static gmx_inline gmx_simd_ref_pr
84 gmx_simd_ref_load1_pr(const real *r)
89 for (i = 0; i < GMX_SIMD_REF_WIDTH; i++)
97 /* Set all SIMD register elements to r */
98 static gmx_inline gmx_simd_ref_pr
99 gmx_simd_ref_set1_pr(real r)
104 for (i = 0; i < GMX_SIMD_REF_WIDTH; i++)
112 /* Set all SIMD register elements to 0 */
113 static gmx_inline gmx_simd_ref_pr
114 gmx_simd_ref_setzero_pr()
119 for (i = 0; i < GMX_SIMD_REF_WIDTH; i++)
127 static gmx_inline void
128 gmx_simd_ref_store_pr(real *dest, gmx_simd_ref_pr src)
132 for (i = 0; i < GMX_SIMD_REF_WIDTH; i++)
138 static gmx_inline gmx_simd_ref_pr
139 gmx_simd_ref_add_pr(gmx_simd_ref_pr a, gmx_simd_ref_pr b)
144 for (i = 0; i < GMX_SIMD_REF_WIDTH; i++)
146 c.r[i] = a.r[i] + b.r[i];
152 static gmx_inline gmx_simd_ref_pr
153 gmx_simd_ref_sub_pr(gmx_simd_ref_pr a, gmx_simd_ref_pr b)
158 for (i = 0; i < GMX_SIMD_REF_WIDTH; i++)
160 c.r[i] = a.r[i] - b.r[i];
166 static gmx_inline gmx_simd_ref_pr
167 gmx_simd_ref_mul_pr(gmx_simd_ref_pr a, gmx_simd_ref_pr b)
172 for (i = 0; i < GMX_SIMD_REF_WIDTH; i++)
174 c.r[i] = a.r[i]*b.r[i];
180 static gmx_inline gmx_simd_ref_pr
181 gmx_simd_ref_madd_pr(gmx_simd_ref_pr a, gmx_simd_ref_pr b, gmx_simd_ref_pr c)
186 for (i = 0; i < GMX_SIMD_REF_WIDTH; i++)
188 d.r[i] = a.r[i]*b.r[i] + c.r[i];
194 static gmx_inline gmx_simd_ref_pr
195 gmx_simd_ref_nmsub_pr(gmx_simd_ref_pr a, gmx_simd_ref_pr b, gmx_simd_ref_pr c)
200 for (i = 0; i < GMX_SIMD_REF_WIDTH; i++)
202 d.r[i] = -a.r[i]*b.r[i] + c.r[i];
208 static gmx_inline gmx_simd_ref_pr
209 gmx_simd_ref_max_pr(gmx_simd_ref_pr a, gmx_simd_ref_pr b)
214 for (i = 0; i < GMX_SIMD_REF_WIDTH; i++)
216 c.r[i] = (a.r[i] >= b.r[i] ? a.r[i] : b.r[i]);
222 static gmx_inline gmx_simd_ref_pr
223 gmx_simd_ref_blendzero_pr(gmx_simd_ref_pr a, gmx_simd_ref_pb b)
228 for (i = 0; i < GMX_SIMD_REF_WIDTH; i++)
230 c.r[i] = (b.r[i] ? a.r[i] : 0.0);
236 /* Note that this reference implementation rounds away from zero,
237 * whereas most SIMD intrinsics will round to nearest even. Since this
238 * function is only used for periodic image calculations, the rounding
239 * of mantissas close to 0.5 is irrelevant, except in testing. This
240 * could be fixed by using rint/rintf, but the bigger problem is that
241 * MSVC does not support full C99, and none of the round or rint
242 * functions are defined. It's much easier to approximately implement
243 * round() than rint(), so we do that and hope we never get bitten in
244 * testing. (Thanks, Microsoft.)
246 static gmx_inline gmx_simd_ref_pr
247 gmx_simd_ref_round_pr(gmx_simd_ref_pr a)
252 for (i = 0; i < GMX_SIMD_REF_WIDTH; i++)
255 int temp = (a.r[i] >= 0.)
258 b.r[i] = (real) temp;
259 #elif defined GMX_DOUBLE
260 b.r[i] = round(a.r[i]);
262 b.r[i] = roundf(a.r[i]);
269 /* Not required, only used to speed up the nbnxn tabulated PME kernels */
270 static gmx_inline gmx_simd_ref_pr
271 gmx_simd_ref_floor_pr(gmx_simd_ref_pr a)
276 for (i = 0; i < GMX_SIMD_REF_WIDTH; i++)
279 b.r[i] = floor(a.r[i]);
281 b.r[i] = floorf(a.r[i]);
288 /* Not required, only used when blendv is faster than comparison */
289 static gmx_inline gmx_simd_ref_pr
290 gmx_simd_ref_blendv_pr(gmx_simd_ref_pr a, gmx_simd_ref_pr b, gmx_simd_ref_pr c)
295 for (i = 0; i < GMX_SIMD_REF_WIDTH; i++)
297 d.r[i] = (c.r[i] >= 0) ? a.r[i] : b.r[i];
303 /* Copy the sign of a to b, assumes b >= 0 for efficiency */
304 static gmx_inline gmx_simd_ref_pr
305 gmx_simd_ref_cpsgn_nonneg_pr(gmx_simd_ref_pr a, gmx_simd_ref_pr b)
310 for (i = 0; i < GMX_SIMD_REF_WIDTH; i++)
312 c.r[i] = (a.r[i] >= 0) ? b.r[i] : -b.r[i];
318 /* Very specific operation required in the non-bonded kernels */
319 static gmx_inline gmx_simd_ref_pr
320 gmx_simd_ref_masknot_add_pr(gmx_simd_ref_pb a, gmx_simd_ref_pr b, gmx_simd_ref_pr c)
325 for (i = 0; i < GMX_SIMD_REF_WIDTH; i++)
327 d.r[i] = a.r[i] ? b.r[i] : b.r[i] + c.r[i];
334 static gmx_inline gmx_simd_ref_pb
335 gmx_simd_ref_cmplt_pr(gmx_simd_ref_pr a, gmx_simd_ref_pr b)
340 for (i = 0; i < GMX_SIMD_REF_WIDTH; i++)
342 c.r[i] = (a.r[i] < b.r[i]);
348 /* Logical AND on SIMD booleans. */
349 static gmx_inline gmx_simd_ref_pb
350 gmx_simd_ref_and_pb(gmx_simd_ref_pb a, gmx_simd_ref_pb b)
355 for (i = 0; i < GMX_SIMD_REF_WIDTH; i++)
357 c.r[i] = (a.r[i] && b.r[i]);
363 /* Logical OR on SIMD booleans. */
364 static gmx_inline gmx_simd_ref_pb
365 gmx_simd_ref_or_pb(gmx_simd_ref_pb a, gmx_simd_ref_pb b)
370 for (i = 0; i < GMX_SIMD_REF_WIDTH; i++)
372 c.r[i] = (a.r[i] || b.r[i]);
378 /* Returns a single int (0/1) which tells if any of the booleans is True */
379 static gmx_inline int
380 gmx_simd_ref_anytrue_pb(gmx_simd_ref_pb a)
386 for (i = 0; i < GMX_SIMD_REF_WIDTH; i++)
397 /* Conversions only used for PME table lookup */
398 static gmx_inline gmx_simd_ref_epi32
399 gmx_simd_ref_cvttpr_epi32(gmx_simd_ref_pr a)
401 gmx_simd_ref_epi32 b;
404 for (i = 0; i < GMX_SIMD_REF_WIDTH; i++)
406 b.r[i] = (int)a.r[i];
412 /* These two function only need to be approximate, Newton-Raphson iteration
413 * is used for full accuracy in gmx_simd_invsqrt_r and gmx_simd_inv_r.
415 static gmx_inline gmx_simd_ref_pr
416 gmx_simd_ref_rsqrt_pr(gmx_simd_ref_pr a)
421 for (i = 0; i < GMX_SIMD_REF_WIDTH; i++)
424 b.r[i] = 1.0/sqrt(a.r[i]);
426 b.r[i] = 1.0/sqrtf(a.r[i]);
433 static gmx_inline gmx_simd_ref_pr
434 gmx_simd_ref_rcp_pr(gmx_simd_ref_pr a)
439 for (i = 0; i < GMX_SIMD_REF_WIDTH; i++)
447 static gmx_inline gmx_simd_ref_pr
448 gmx_simd_ref_exp_pr(gmx_simd_ref_pr a)
453 for (i = 0; i < GMX_SIMD_REF_WIDTH; i++)
456 b.r[i] = exp(a.r[i]);
458 b.r[i] = expf(a.r[i]);
465 static gmx_inline gmx_simd_ref_pr
466 gmx_simd_ref_sqrt_pr(gmx_simd_ref_pr a)
471 for (i = 0; i < GMX_SIMD_REF_WIDTH; i++)
474 b.r[i] = sqrt(a.r[i]);
476 b.r[i] = sqrtf(a.r[i]);
483 static gmx_inline int
484 gmx_simd_ref_sincos_pr(gmx_simd_ref_pr a,
485 gmx_simd_ref_pr *s, gmx_simd_ref_pr *c)
489 for (i = 0; i < GMX_SIMD_REF_WIDTH; i++)
491 s->r[i] = sin(a.r[i]);
492 c->r[i] = cos(a.r[i]);
498 static gmx_inline gmx_simd_ref_pr
499 gmx_simd_ref_acos_pr(gmx_simd_ref_pr a)
504 for (i = 0; i < GMX_SIMD_REF_WIDTH; i++)
506 b.r[i] = acos(a.r[i]);
512 static gmx_inline gmx_simd_ref_pr
513 gmx_simd_ref_atan2_pr(gmx_simd_ref_pr a, gmx_simd_ref_pr b)
518 for (i = 0; i < GMX_SIMD_REF_WIDTH; i++)
520 c.r[i] = atan2(a.r[i], b.r[i]);