Introduce gmxpre.h for truly global definitions
[alexxy/gromacs.git] / src / gromacs / gmxlib / nonbonded / nb_kernel_avx_256_double / nb_kernel_ElecEwSh_VdwNone_GeomW3P1_avx_256_double.c
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2012,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.
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  * Note: this file was generated by the GROMACS avx_256_double kernel generator.
37  */
38 #include "gmxpre.h"
39
40 #include "config.h"
41
42 #include <math.h>
43
44 #include "../nb_kernel.h"
45 #include "gromacs/legacyheaders/types/simple.h"
46 #include "gromacs/math/vec.h"
47 #include "gromacs/legacyheaders/nrnb.h"
48
49 #include "gromacs/simd/math_x86_avx_256_double.h"
50 #include "kernelutil_x86_avx_256_double.h"
51
52 /*
53  * Gromacs nonbonded kernel:   nb_kernel_ElecEwSh_VdwNone_GeomW3P1_VF_avx_256_double
54  * Electrostatics interaction: Ewald
55  * VdW interaction:            None
56  * Geometry:                   Water3-Particle
57  * Calculate force/pot:        PotentialAndForce
58  */
59 void
60 nb_kernel_ElecEwSh_VdwNone_GeomW3P1_VF_avx_256_double
61                     (t_nblist                    * gmx_restrict       nlist,
62                      rvec                        * gmx_restrict          xx,
63                      rvec                        * gmx_restrict          ff,
64                      t_forcerec                  * gmx_restrict          fr,
65                      t_mdatoms                   * gmx_restrict     mdatoms,
66                      nb_kernel_data_t gmx_unused * gmx_restrict kernel_data,
67                      t_nrnb                      * gmx_restrict        nrnb)
68 {
69     /* Suffixes 0,1,2,3 refer to particle indices for waters in the inner or outer loop, or 
70      * just 0 for non-waters.
71      * Suffixes A,B,C,D refer to j loop unrolling done with AVX, e.g. for the four different
72      * jnr indices corresponding to data put in the four positions in the SIMD register.
73      */
74     int              i_shift_offset,i_coord_offset,outeriter,inneriter;
75     int              j_index_start,j_index_end,jidx,nri,inr,ggid,iidx;
76     int              jnrA,jnrB,jnrC,jnrD;
77     int              jnrlistA,jnrlistB,jnrlistC,jnrlistD;
78     int              jnrlistE,jnrlistF,jnrlistG,jnrlistH;
79     int              j_coord_offsetA,j_coord_offsetB,j_coord_offsetC,j_coord_offsetD;
80     int              *iinr,*jindex,*jjnr,*shiftidx,*gid;
81     real             rcutoff_scalar;
82     real             *shiftvec,*fshift,*x,*f;
83     real             *fjptrA,*fjptrB,*fjptrC,*fjptrD;
84     real             scratch[4*DIM];
85     __m256d          tx,ty,tz,fscal,rcutoff,rcutoff2,jidxall;
86     real *           vdwioffsetptr0;
87     __m256d          ix0,iy0,iz0,fix0,fiy0,fiz0,iq0,isai0;
88     real *           vdwioffsetptr1;
89     __m256d          ix1,iy1,iz1,fix1,fiy1,fiz1,iq1,isai1;
90     real *           vdwioffsetptr2;
91     __m256d          ix2,iy2,iz2,fix2,fiy2,fiz2,iq2,isai2;
92     int              vdwjidx0A,vdwjidx0B,vdwjidx0C,vdwjidx0D;
93     __m256d          jx0,jy0,jz0,fjx0,fjy0,fjz0,jq0,isaj0;
94     __m256d          dx00,dy00,dz00,rsq00,rinv00,rinvsq00,r00,qq00,c6_00,c12_00;
95     __m256d          dx10,dy10,dz10,rsq10,rinv10,rinvsq10,r10,qq10,c6_10,c12_10;
96     __m256d          dx20,dy20,dz20,rsq20,rinv20,rinvsq20,r20,qq20,c6_20,c12_20;
97     __m256d          velec,felec,velecsum,facel,crf,krf,krf2;
98     real             *charge;
99     __m128i          ewitab;
100     __m256d          ewtabscale,eweps,sh_ewald,ewrt,ewtabhalfspace,ewtabF,ewtabFn,ewtabD,ewtabV;
101     __m256d          beta,beta2,beta3,zeta2,pmecorrF,pmecorrV,rinv3;
102     real             *ewtab;
103     __m256d          dummy_mask,cutoff_mask;
104     __m128           tmpmask0,tmpmask1;
105     __m256d          signbit = _mm256_castsi256_pd( _mm256_set1_epi32(0x80000000) );
106     __m256d          one     = _mm256_set1_pd(1.0);
107     __m256d          two     = _mm256_set1_pd(2.0);
108     x                = xx[0];
109     f                = ff[0];
110
111     nri              = nlist->nri;
112     iinr             = nlist->iinr;
113     jindex           = nlist->jindex;
114     jjnr             = nlist->jjnr;
115     shiftidx         = nlist->shift;
116     gid              = nlist->gid;
117     shiftvec         = fr->shift_vec[0];
118     fshift           = fr->fshift[0];
119     facel            = _mm256_set1_pd(fr->epsfac);
120     charge           = mdatoms->chargeA;
121
122     sh_ewald         = _mm256_set1_pd(fr->ic->sh_ewald);
123     beta             = _mm256_set1_pd(fr->ic->ewaldcoeff_q);
124     beta2            = _mm256_mul_pd(beta,beta);
125     beta3            = _mm256_mul_pd(beta,beta2);
126
127     ewtab            = fr->ic->tabq_coul_FDV0;
128     ewtabscale       = _mm256_set1_pd(fr->ic->tabq_scale);
129     ewtabhalfspace   = _mm256_set1_pd(0.5/fr->ic->tabq_scale);
130
131     /* Setup water-specific parameters */
132     inr              = nlist->iinr[0];
133     iq0              = _mm256_mul_pd(facel,_mm256_set1_pd(charge[inr+0]));
134     iq1              = _mm256_mul_pd(facel,_mm256_set1_pd(charge[inr+1]));
135     iq2              = _mm256_mul_pd(facel,_mm256_set1_pd(charge[inr+2]));
136
137     /* When we use explicit cutoffs the value must be identical for elec and VdW, so use elec as an arbitrary choice */
138     rcutoff_scalar   = fr->rcoulomb;
139     rcutoff          = _mm256_set1_pd(rcutoff_scalar);
140     rcutoff2         = _mm256_mul_pd(rcutoff,rcutoff);
141
142     /* Avoid stupid compiler warnings */
143     jnrA = jnrB = jnrC = jnrD = 0;
144     j_coord_offsetA = 0;
145     j_coord_offsetB = 0;
146     j_coord_offsetC = 0;
147     j_coord_offsetD = 0;
148
149     outeriter        = 0;
150     inneriter        = 0;
151
152     for(iidx=0;iidx<4*DIM;iidx++)
153     {
154         scratch[iidx] = 0.0;
155     }
156
157     /* Start outer loop over neighborlists */
158     for(iidx=0; iidx<nri; iidx++)
159     {
160         /* Load shift vector for this list */
161         i_shift_offset   = DIM*shiftidx[iidx];
162
163         /* Load limits for loop over neighbors */
164         j_index_start    = jindex[iidx];
165         j_index_end      = jindex[iidx+1];
166
167         /* Get outer coordinate index */
168         inr              = iinr[iidx];
169         i_coord_offset   = DIM*inr;
170
171         /* Load i particle coords and add shift vector */
172         gmx_mm256_load_shift_and_3rvec_broadcast_pd(shiftvec+i_shift_offset,x+i_coord_offset,
173                                                     &ix0,&iy0,&iz0,&ix1,&iy1,&iz1,&ix2,&iy2,&iz2);
174
175         fix0             = _mm256_setzero_pd();
176         fiy0             = _mm256_setzero_pd();
177         fiz0             = _mm256_setzero_pd();
178         fix1             = _mm256_setzero_pd();
179         fiy1             = _mm256_setzero_pd();
180         fiz1             = _mm256_setzero_pd();
181         fix2             = _mm256_setzero_pd();
182         fiy2             = _mm256_setzero_pd();
183         fiz2             = _mm256_setzero_pd();
184
185         /* Reset potential sums */
186         velecsum         = _mm256_setzero_pd();
187
188         /* Start inner kernel loop */
189         for(jidx=j_index_start; jidx<j_index_end && jjnr[jidx+3]>=0; jidx+=4)
190         {
191
192             /* Get j neighbor index, and coordinate index */
193             jnrA             = jjnr[jidx];
194             jnrB             = jjnr[jidx+1];
195             jnrC             = jjnr[jidx+2];
196             jnrD             = jjnr[jidx+3];
197             j_coord_offsetA  = DIM*jnrA;
198             j_coord_offsetB  = DIM*jnrB;
199             j_coord_offsetC  = DIM*jnrC;
200             j_coord_offsetD  = DIM*jnrD;
201
202             /* load j atom coordinates */
203             gmx_mm256_load_1rvec_4ptr_swizzle_pd(x+j_coord_offsetA,x+j_coord_offsetB,
204                                                  x+j_coord_offsetC,x+j_coord_offsetD,
205                                                  &jx0,&jy0,&jz0);
206
207             /* Calculate displacement vector */
208             dx00             = _mm256_sub_pd(ix0,jx0);
209             dy00             = _mm256_sub_pd(iy0,jy0);
210             dz00             = _mm256_sub_pd(iz0,jz0);
211             dx10             = _mm256_sub_pd(ix1,jx0);
212             dy10             = _mm256_sub_pd(iy1,jy0);
213             dz10             = _mm256_sub_pd(iz1,jz0);
214             dx20             = _mm256_sub_pd(ix2,jx0);
215             dy20             = _mm256_sub_pd(iy2,jy0);
216             dz20             = _mm256_sub_pd(iz2,jz0);
217
218             /* Calculate squared distance and things based on it */
219             rsq00            = gmx_mm256_calc_rsq_pd(dx00,dy00,dz00);
220             rsq10            = gmx_mm256_calc_rsq_pd(dx10,dy10,dz10);
221             rsq20            = gmx_mm256_calc_rsq_pd(dx20,dy20,dz20);
222
223             rinv00           = gmx_mm256_invsqrt_pd(rsq00);
224             rinv10           = gmx_mm256_invsqrt_pd(rsq10);
225             rinv20           = gmx_mm256_invsqrt_pd(rsq20);
226
227             rinvsq00         = _mm256_mul_pd(rinv00,rinv00);
228             rinvsq10         = _mm256_mul_pd(rinv10,rinv10);
229             rinvsq20         = _mm256_mul_pd(rinv20,rinv20);
230
231             /* Load parameters for j particles */
232             jq0              = gmx_mm256_load_4real_swizzle_pd(charge+jnrA+0,charge+jnrB+0,
233                                                                  charge+jnrC+0,charge+jnrD+0);
234
235             fjx0             = _mm256_setzero_pd();
236             fjy0             = _mm256_setzero_pd();
237             fjz0             = _mm256_setzero_pd();
238
239             /**************************
240              * CALCULATE INTERACTIONS *
241              **************************/
242
243             if (gmx_mm256_any_lt(rsq00,rcutoff2))
244             {
245
246             r00              = _mm256_mul_pd(rsq00,rinv00);
247
248             /* Compute parameters for interactions between i and j atoms */
249             qq00             = _mm256_mul_pd(iq0,jq0);
250
251             /* EWALD ELECTROSTATICS */
252
253             /* Calculate Ewald table index by multiplying r with scale and truncate to integer */
254             ewrt             = _mm256_mul_pd(r00,ewtabscale);
255             ewitab           = _mm256_cvttpd_epi32(ewrt);
256             eweps            = _mm256_sub_pd(ewrt,_mm256_round_pd(ewrt, _MM_FROUND_FLOOR));
257             ewitab           = _mm_slli_epi32(ewitab,2);
258             ewtabF           = _mm256_load_pd( ewtab + _mm_extract_epi32(ewitab,0) );
259             ewtabD           = _mm256_load_pd( ewtab + _mm_extract_epi32(ewitab,1) );
260             ewtabV           = _mm256_load_pd( ewtab + _mm_extract_epi32(ewitab,2) );
261             ewtabFn          = _mm256_load_pd( ewtab + _mm_extract_epi32(ewitab,3) );
262             GMX_MM256_FULLTRANSPOSE4_PD(ewtabF,ewtabD,ewtabV,ewtabFn);
263             felec            = _mm256_add_pd(ewtabF,_mm256_mul_pd(eweps,ewtabD));
264             velec            = _mm256_sub_pd(ewtabV,_mm256_mul_pd(_mm256_mul_pd(ewtabhalfspace,eweps),_mm256_add_pd(ewtabF,felec)));
265             velec            = _mm256_mul_pd(qq00,_mm256_sub_pd(_mm256_sub_pd(rinv00,sh_ewald),velec));
266             felec            = _mm256_mul_pd(_mm256_mul_pd(qq00,rinv00),_mm256_sub_pd(rinvsq00,felec));
267
268             cutoff_mask      = _mm256_cmp_pd(rsq00,rcutoff2,_CMP_LT_OQ);
269
270             /* Update potential sum for this i atom from the interaction with this j atom. */
271             velec            = _mm256_and_pd(velec,cutoff_mask);
272             velecsum         = _mm256_add_pd(velecsum,velec);
273
274             fscal            = felec;
275
276             fscal            = _mm256_and_pd(fscal,cutoff_mask);
277
278             /* Calculate temporary vectorial force */
279             tx               = _mm256_mul_pd(fscal,dx00);
280             ty               = _mm256_mul_pd(fscal,dy00);
281             tz               = _mm256_mul_pd(fscal,dz00);
282
283             /* Update vectorial force */
284             fix0             = _mm256_add_pd(fix0,tx);
285             fiy0             = _mm256_add_pd(fiy0,ty);
286             fiz0             = _mm256_add_pd(fiz0,tz);
287
288             fjx0             = _mm256_add_pd(fjx0,tx);
289             fjy0             = _mm256_add_pd(fjy0,ty);
290             fjz0             = _mm256_add_pd(fjz0,tz);
291
292             }
293
294             /**************************
295              * CALCULATE INTERACTIONS *
296              **************************/
297
298             if (gmx_mm256_any_lt(rsq10,rcutoff2))
299             {
300
301             r10              = _mm256_mul_pd(rsq10,rinv10);
302
303             /* Compute parameters for interactions between i and j atoms */
304             qq10             = _mm256_mul_pd(iq1,jq0);
305
306             /* EWALD ELECTROSTATICS */
307
308             /* Calculate Ewald table index by multiplying r with scale and truncate to integer */
309             ewrt             = _mm256_mul_pd(r10,ewtabscale);
310             ewitab           = _mm256_cvttpd_epi32(ewrt);
311             eweps            = _mm256_sub_pd(ewrt,_mm256_round_pd(ewrt, _MM_FROUND_FLOOR));
312             ewitab           = _mm_slli_epi32(ewitab,2);
313             ewtabF           = _mm256_load_pd( ewtab + _mm_extract_epi32(ewitab,0) );
314             ewtabD           = _mm256_load_pd( ewtab + _mm_extract_epi32(ewitab,1) );
315             ewtabV           = _mm256_load_pd( ewtab + _mm_extract_epi32(ewitab,2) );
316             ewtabFn          = _mm256_load_pd( ewtab + _mm_extract_epi32(ewitab,3) );
317             GMX_MM256_FULLTRANSPOSE4_PD(ewtabF,ewtabD,ewtabV,ewtabFn);
318             felec            = _mm256_add_pd(ewtabF,_mm256_mul_pd(eweps,ewtabD));
319             velec            = _mm256_sub_pd(ewtabV,_mm256_mul_pd(_mm256_mul_pd(ewtabhalfspace,eweps),_mm256_add_pd(ewtabF,felec)));
320             velec            = _mm256_mul_pd(qq10,_mm256_sub_pd(_mm256_sub_pd(rinv10,sh_ewald),velec));
321             felec            = _mm256_mul_pd(_mm256_mul_pd(qq10,rinv10),_mm256_sub_pd(rinvsq10,felec));
322
323             cutoff_mask      = _mm256_cmp_pd(rsq10,rcutoff2,_CMP_LT_OQ);
324
325             /* Update potential sum for this i atom from the interaction with this j atom. */
326             velec            = _mm256_and_pd(velec,cutoff_mask);
327             velecsum         = _mm256_add_pd(velecsum,velec);
328
329             fscal            = felec;
330
331             fscal            = _mm256_and_pd(fscal,cutoff_mask);
332
333             /* Calculate temporary vectorial force */
334             tx               = _mm256_mul_pd(fscal,dx10);
335             ty               = _mm256_mul_pd(fscal,dy10);
336             tz               = _mm256_mul_pd(fscal,dz10);
337
338             /* Update vectorial force */
339             fix1             = _mm256_add_pd(fix1,tx);
340             fiy1             = _mm256_add_pd(fiy1,ty);
341             fiz1             = _mm256_add_pd(fiz1,tz);
342
343             fjx0             = _mm256_add_pd(fjx0,tx);
344             fjy0             = _mm256_add_pd(fjy0,ty);
345             fjz0             = _mm256_add_pd(fjz0,tz);
346
347             }
348
349             /**************************
350              * CALCULATE INTERACTIONS *
351              **************************/
352
353             if (gmx_mm256_any_lt(rsq20,rcutoff2))
354             {
355
356             r20              = _mm256_mul_pd(rsq20,rinv20);
357
358             /* Compute parameters for interactions between i and j atoms */
359             qq20             = _mm256_mul_pd(iq2,jq0);
360
361             /* EWALD ELECTROSTATICS */
362
363             /* Calculate Ewald table index by multiplying r with scale and truncate to integer */
364             ewrt             = _mm256_mul_pd(r20,ewtabscale);
365             ewitab           = _mm256_cvttpd_epi32(ewrt);
366             eweps            = _mm256_sub_pd(ewrt,_mm256_round_pd(ewrt, _MM_FROUND_FLOOR));
367             ewitab           = _mm_slli_epi32(ewitab,2);
368             ewtabF           = _mm256_load_pd( ewtab + _mm_extract_epi32(ewitab,0) );
369             ewtabD           = _mm256_load_pd( ewtab + _mm_extract_epi32(ewitab,1) );
370             ewtabV           = _mm256_load_pd( ewtab + _mm_extract_epi32(ewitab,2) );
371             ewtabFn          = _mm256_load_pd( ewtab + _mm_extract_epi32(ewitab,3) );
372             GMX_MM256_FULLTRANSPOSE4_PD(ewtabF,ewtabD,ewtabV,ewtabFn);
373             felec            = _mm256_add_pd(ewtabF,_mm256_mul_pd(eweps,ewtabD));
374             velec            = _mm256_sub_pd(ewtabV,_mm256_mul_pd(_mm256_mul_pd(ewtabhalfspace,eweps),_mm256_add_pd(ewtabF,felec)));
375             velec            = _mm256_mul_pd(qq20,_mm256_sub_pd(_mm256_sub_pd(rinv20,sh_ewald),velec));
376             felec            = _mm256_mul_pd(_mm256_mul_pd(qq20,rinv20),_mm256_sub_pd(rinvsq20,felec));
377
378             cutoff_mask      = _mm256_cmp_pd(rsq20,rcutoff2,_CMP_LT_OQ);
379
380             /* Update potential sum for this i atom from the interaction with this j atom. */
381             velec            = _mm256_and_pd(velec,cutoff_mask);
382             velecsum         = _mm256_add_pd(velecsum,velec);
383
384             fscal            = felec;
385
386             fscal            = _mm256_and_pd(fscal,cutoff_mask);
387
388             /* Calculate temporary vectorial force */
389             tx               = _mm256_mul_pd(fscal,dx20);
390             ty               = _mm256_mul_pd(fscal,dy20);
391             tz               = _mm256_mul_pd(fscal,dz20);
392
393             /* Update vectorial force */
394             fix2             = _mm256_add_pd(fix2,tx);
395             fiy2             = _mm256_add_pd(fiy2,ty);
396             fiz2             = _mm256_add_pd(fiz2,tz);
397
398             fjx0             = _mm256_add_pd(fjx0,tx);
399             fjy0             = _mm256_add_pd(fjy0,ty);
400             fjz0             = _mm256_add_pd(fjz0,tz);
401
402             }
403
404             fjptrA             = f+j_coord_offsetA;
405             fjptrB             = f+j_coord_offsetB;
406             fjptrC             = f+j_coord_offsetC;
407             fjptrD             = f+j_coord_offsetD;
408
409             gmx_mm256_decrement_1rvec_4ptr_swizzle_pd(fjptrA,fjptrB,fjptrC,fjptrD,fjx0,fjy0,fjz0);
410
411             /* Inner loop uses 141 flops */
412         }
413
414         if(jidx<j_index_end)
415         {
416
417             /* Get j neighbor index, and coordinate index */
418             jnrlistA         = jjnr[jidx];
419             jnrlistB         = jjnr[jidx+1];
420             jnrlistC         = jjnr[jidx+2];
421             jnrlistD         = jjnr[jidx+3];
422             /* Sign of each element will be negative for non-real atoms.
423              * This mask will be 0xFFFFFFFF for dummy entries and 0x0 for real ones,
424              * so use it as val = _mm_andnot_pd(mask,val) to clear dummy entries.
425              */
426             tmpmask0 = gmx_mm_castsi128_ps(_mm_cmplt_epi32(_mm_loadu_si128((const __m128i *)(jjnr+jidx)),_mm_setzero_si128()));
427
428             tmpmask1 = _mm_permute_ps(tmpmask0,_GMX_MM_PERMUTE(3,3,2,2));
429             tmpmask0 = _mm_permute_ps(tmpmask0,_GMX_MM_PERMUTE(1,1,0,0));
430             dummy_mask = _mm256_castps_pd(gmx_mm256_set_m128(tmpmask1,tmpmask0));
431
432             jnrA       = (jnrlistA>=0) ? jnrlistA : 0;
433             jnrB       = (jnrlistB>=0) ? jnrlistB : 0;
434             jnrC       = (jnrlistC>=0) ? jnrlistC : 0;
435             jnrD       = (jnrlistD>=0) ? jnrlistD : 0;
436             j_coord_offsetA  = DIM*jnrA;
437             j_coord_offsetB  = DIM*jnrB;
438             j_coord_offsetC  = DIM*jnrC;
439             j_coord_offsetD  = DIM*jnrD;
440
441             /* load j atom coordinates */
442             gmx_mm256_load_1rvec_4ptr_swizzle_pd(x+j_coord_offsetA,x+j_coord_offsetB,
443                                                  x+j_coord_offsetC,x+j_coord_offsetD,
444                                                  &jx0,&jy0,&jz0);
445
446             /* Calculate displacement vector */
447             dx00             = _mm256_sub_pd(ix0,jx0);
448             dy00             = _mm256_sub_pd(iy0,jy0);
449             dz00             = _mm256_sub_pd(iz0,jz0);
450             dx10             = _mm256_sub_pd(ix1,jx0);
451             dy10             = _mm256_sub_pd(iy1,jy0);
452             dz10             = _mm256_sub_pd(iz1,jz0);
453             dx20             = _mm256_sub_pd(ix2,jx0);
454             dy20             = _mm256_sub_pd(iy2,jy0);
455             dz20             = _mm256_sub_pd(iz2,jz0);
456
457             /* Calculate squared distance and things based on it */
458             rsq00            = gmx_mm256_calc_rsq_pd(dx00,dy00,dz00);
459             rsq10            = gmx_mm256_calc_rsq_pd(dx10,dy10,dz10);
460             rsq20            = gmx_mm256_calc_rsq_pd(dx20,dy20,dz20);
461
462             rinv00           = gmx_mm256_invsqrt_pd(rsq00);
463             rinv10           = gmx_mm256_invsqrt_pd(rsq10);
464             rinv20           = gmx_mm256_invsqrt_pd(rsq20);
465
466             rinvsq00         = _mm256_mul_pd(rinv00,rinv00);
467             rinvsq10         = _mm256_mul_pd(rinv10,rinv10);
468             rinvsq20         = _mm256_mul_pd(rinv20,rinv20);
469
470             /* Load parameters for j particles */
471             jq0              = gmx_mm256_load_4real_swizzle_pd(charge+jnrA+0,charge+jnrB+0,
472                                                                  charge+jnrC+0,charge+jnrD+0);
473
474             fjx0             = _mm256_setzero_pd();
475             fjy0             = _mm256_setzero_pd();
476             fjz0             = _mm256_setzero_pd();
477
478             /**************************
479              * CALCULATE INTERACTIONS *
480              **************************/
481
482             if (gmx_mm256_any_lt(rsq00,rcutoff2))
483             {
484
485             r00              = _mm256_mul_pd(rsq00,rinv00);
486             r00              = _mm256_andnot_pd(dummy_mask,r00);
487
488             /* Compute parameters for interactions between i and j atoms */
489             qq00             = _mm256_mul_pd(iq0,jq0);
490
491             /* EWALD ELECTROSTATICS */
492
493             /* Calculate Ewald table index by multiplying r with scale and truncate to integer */
494             ewrt             = _mm256_mul_pd(r00,ewtabscale);
495             ewitab           = _mm256_cvttpd_epi32(ewrt);
496             eweps            = _mm256_sub_pd(ewrt,_mm256_round_pd(ewrt, _MM_FROUND_FLOOR));
497             ewitab           = _mm_slli_epi32(ewitab,2);
498             ewtabF           = _mm256_load_pd( ewtab + _mm_extract_epi32(ewitab,0) );
499             ewtabD           = _mm256_load_pd( ewtab + _mm_extract_epi32(ewitab,1) );
500             ewtabV           = _mm256_load_pd( ewtab + _mm_extract_epi32(ewitab,2) );
501             ewtabFn          = _mm256_load_pd( ewtab + _mm_extract_epi32(ewitab,3) );
502             GMX_MM256_FULLTRANSPOSE4_PD(ewtabF,ewtabD,ewtabV,ewtabFn);
503             felec            = _mm256_add_pd(ewtabF,_mm256_mul_pd(eweps,ewtabD));
504             velec            = _mm256_sub_pd(ewtabV,_mm256_mul_pd(_mm256_mul_pd(ewtabhalfspace,eweps),_mm256_add_pd(ewtabF,felec)));
505             velec            = _mm256_mul_pd(qq00,_mm256_sub_pd(_mm256_sub_pd(rinv00,sh_ewald),velec));
506             felec            = _mm256_mul_pd(_mm256_mul_pd(qq00,rinv00),_mm256_sub_pd(rinvsq00,felec));
507
508             cutoff_mask      = _mm256_cmp_pd(rsq00,rcutoff2,_CMP_LT_OQ);
509
510             /* Update potential sum for this i atom from the interaction with this j atom. */
511             velec            = _mm256_and_pd(velec,cutoff_mask);
512             velec            = _mm256_andnot_pd(dummy_mask,velec);
513             velecsum         = _mm256_add_pd(velecsum,velec);
514
515             fscal            = felec;
516
517             fscal            = _mm256_and_pd(fscal,cutoff_mask);
518
519             fscal            = _mm256_andnot_pd(dummy_mask,fscal);
520
521             /* Calculate temporary vectorial force */
522             tx               = _mm256_mul_pd(fscal,dx00);
523             ty               = _mm256_mul_pd(fscal,dy00);
524             tz               = _mm256_mul_pd(fscal,dz00);
525
526             /* Update vectorial force */
527             fix0             = _mm256_add_pd(fix0,tx);
528             fiy0             = _mm256_add_pd(fiy0,ty);
529             fiz0             = _mm256_add_pd(fiz0,tz);
530
531             fjx0             = _mm256_add_pd(fjx0,tx);
532             fjy0             = _mm256_add_pd(fjy0,ty);
533             fjz0             = _mm256_add_pd(fjz0,tz);
534
535             }
536
537             /**************************
538              * CALCULATE INTERACTIONS *
539              **************************/
540
541             if (gmx_mm256_any_lt(rsq10,rcutoff2))
542             {
543
544             r10              = _mm256_mul_pd(rsq10,rinv10);
545             r10              = _mm256_andnot_pd(dummy_mask,r10);
546
547             /* Compute parameters for interactions between i and j atoms */
548             qq10             = _mm256_mul_pd(iq1,jq0);
549
550             /* EWALD ELECTROSTATICS */
551
552             /* Calculate Ewald table index by multiplying r with scale and truncate to integer */
553             ewrt             = _mm256_mul_pd(r10,ewtabscale);
554             ewitab           = _mm256_cvttpd_epi32(ewrt);
555             eweps            = _mm256_sub_pd(ewrt,_mm256_round_pd(ewrt, _MM_FROUND_FLOOR));
556             ewitab           = _mm_slli_epi32(ewitab,2);
557             ewtabF           = _mm256_load_pd( ewtab + _mm_extract_epi32(ewitab,0) );
558             ewtabD           = _mm256_load_pd( ewtab + _mm_extract_epi32(ewitab,1) );
559             ewtabV           = _mm256_load_pd( ewtab + _mm_extract_epi32(ewitab,2) );
560             ewtabFn          = _mm256_load_pd( ewtab + _mm_extract_epi32(ewitab,3) );
561             GMX_MM256_FULLTRANSPOSE4_PD(ewtabF,ewtabD,ewtabV,ewtabFn);
562             felec            = _mm256_add_pd(ewtabF,_mm256_mul_pd(eweps,ewtabD));
563             velec            = _mm256_sub_pd(ewtabV,_mm256_mul_pd(_mm256_mul_pd(ewtabhalfspace,eweps),_mm256_add_pd(ewtabF,felec)));
564             velec            = _mm256_mul_pd(qq10,_mm256_sub_pd(_mm256_sub_pd(rinv10,sh_ewald),velec));
565             felec            = _mm256_mul_pd(_mm256_mul_pd(qq10,rinv10),_mm256_sub_pd(rinvsq10,felec));
566
567             cutoff_mask      = _mm256_cmp_pd(rsq10,rcutoff2,_CMP_LT_OQ);
568
569             /* Update potential sum for this i atom from the interaction with this j atom. */
570             velec            = _mm256_and_pd(velec,cutoff_mask);
571             velec            = _mm256_andnot_pd(dummy_mask,velec);
572             velecsum         = _mm256_add_pd(velecsum,velec);
573
574             fscal            = felec;
575
576             fscal            = _mm256_and_pd(fscal,cutoff_mask);
577
578             fscal            = _mm256_andnot_pd(dummy_mask,fscal);
579
580             /* Calculate temporary vectorial force */
581             tx               = _mm256_mul_pd(fscal,dx10);
582             ty               = _mm256_mul_pd(fscal,dy10);
583             tz               = _mm256_mul_pd(fscal,dz10);
584
585             /* Update vectorial force */
586             fix1             = _mm256_add_pd(fix1,tx);
587             fiy1             = _mm256_add_pd(fiy1,ty);
588             fiz1             = _mm256_add_pd(fiz1,tz);
589
590             fjx0             = _mm256_add_pd(fjx0,tx);
591             fjy0             = _mm256_add_pd(fjy0,ty);
592             fjz0             = _mm256_add_pd(fjz0,tz);
593
594             }
595
596             /**************************
597              * CALCULATE INTERACTIONS *
598              **************************/
599
600             if (gmx_mm256_any_lt(rsq20,rcutoff2))
601             {
602
603             r20              = _mm256_mul_pd(rsq20,rinv20);
604             r20              = _mm256_andnot_pd(dummy_mask,r20);
605
606             /* Compute parameters for interactions between i and j atoms */
607             qq20             = _mm256_mul_pd(iq2,jq0);
608
609             /* EWALD ELECTROSTATICS */
610
611             /* Calculate Ewald table index by multiplying r with scale and truncate to integer */
612             ewrt             = _mm256_mul_pd(r20,ewtabscale);
613             ewitab           = _mm256_cvttpd_epi32(ewrt);
614             eweps            = _mm256_sub_pd(ewrt,_mm256_round_pd(ewrt, _MM_FROUND_FLOOR));
615             ewitab           = _mm_slli_epi32(ewitab,2);
616             ewtabF           = _mm256_load_pd( ewtab + _mm_extract_epi32(ewitab,0) );
617             ewtabD           = _mm256_load_pd( ewtab + _mm_extract_epi32(ewitab,1) );
618             ewtabV           = _mm256_load_pd( ewtab + _mm_extract_epi32(ewitab,2) );
619             ewtabFn          = _mm256_load_pd( ewtab + _mm_extract_epi32(ewitab,3) );
620             GMX_MM256_FULLTRANSPOSE4_PD(ewtabF,ewtabD,ewtabV,ewtabFn);
621             felec            = _mm256_add_pd(ewtabF,_mm256_mul_pd(eweps,ewtabD));
622             velec            = _mm256_sub_pd(ewtabV,_mm256_mul_pd(_mm256_mul_pd(ewtabhalfspace,eweps),_mm256_add_pd(ewtabF,felec)));
623             velec            = _mm256_mul_pd(qq20,_mm256_sub_pd(_mm256_sub_pd(rinv20,sh_ewald),velec));
624             felec            = _mm256_mul_pd(_mm256_mul_pd(qq20,rinv20),_mm256_sub_pd(rinvsq20,felec));
625
626             cutoff_mask      = _mm256_cmp_pd(rsq20,rcutoff2,_CMP_LT_OQ);
627
628             /* Update potential sum for this i atom from the interaction with this j atom. */
629             velec            = _mm256_and_pd(velec,cutoff_mask);
630             velec            = _mm256_andnot_pd(dummy_mask,velec);
631             velecsum         = _mm256_add_pd(velecsum,velec);
632
633             fscal            = felec;
634
635             fscal            = _mm256_and_pd(fscal,cutoff_mask);
636
637             fscal            = _mm256_andnot_pd(dummy_mask,fscal);
638
639             /* Calculate temporary vectorial force */
640             tx               = _mm256_mul_pd(fscal,dx20);
641             ty               = _mm256_mul_pd(fscal,dy20);
642             tz               = _mm256_mul_pd(fscal,dz20);
643
644             /* Update vectorial force */
645             fix2             = _mm256_add_pd(fix2,tx);
646             fiy2             = _mm256_add_pd(fiy2,ty);
647             fiz2             = _mm256_add_pd(fiz2,tz);
648
649             fjx0             = _mm256_add_pd(fjx0,tx);
650             fjy0             = _mm256_add_pd(fjy0,ty);
651             fjz0             = _mm256_add_pd(fjz0,tz);
652
653             }
654
655             fjptrA             = (jnrlistA>=0) ? f+j_coord_offsetA : scratch;
656             fjptrB             = (jnrlistB>=0) ? f+j_coord_offsetB : scratch;
657             fjptrC             = (jnrlistC>=0) ? f+j_coord_offsetC : scratch;
658             fjptrD             = (jnrlistD>=0) ? f+j_coord_offsetD : scratch;
659
660             gmx_mm256_decrement_1rvec_4ptr_swizzle_pd(fjptrA,fjptrB,fjptrC,fjptrD,fjx0,fjy0,fjz0);
661
662             /* Inner loop uses 144 flops */
663         }
664
665         /* End of innermost loop */
666
667         gmx_mm256_update_iforce_3atom_swizzle_pd(fix0,fiy0,fiz0,fix1,fiy1,fiz1,fix2,fiy2,fiz2,
668                                                  f+i_coord_offset,fshift+i_shift_offset);
669
670         ggid                        = gid[iidx];
671         /* Update potential energies */
672         gmx_mm256_update_1pot_pd(velecsum,kernel_data->energygrp_elec+ggid);
673
674         /* Increment number of inner iterations */
675         inneriter                  += j_index_end - j_index_start;
676
677         /* Outer loop uses 19 flops */
678     }
679
680     /* Increment number of outer iterations */
681     outeriter        += nri;
682
683     /* Update outer/inner flops */
684
685     inc_nrnb(nrnb,eNR_NBKERNEL_ELEC_W3_VF,outeriter*19 + inneriter*144);
686 }
687 /*
688  * Gromacs nonbonded kernel:   nb_kernel_ElecEwSh_VdwNone_GeomW3P1_F_avx_256_double
689  * Electrostatics interaction: Ewald
690  * VdW interaction:            None
691  * Geometry:                   Water3-Particle
692  * Calculate force/pot:        Force
693  */
694 void
695 nb_kernel_ElecEwSh_VdwNone_GeomW3P1_F_avx_256_double
696                     (t_nblist                    * gmx_restrict       nlist,
697                      rvec                        * gmx_restrict          xx,
698                      rvec                        * gmx_restrict          ff,
699                      t_forcerec                  * gmx_restrict          fr,
700                      t_mdatoms                   * gmx_restrict     mdatoms,
701                      nb_kernel_data_t gmx_unused * gmx_restrict kernel_data,
702                      t_nrnb                      * gmx_restrict        nrnb)
703 {
704     /* Suffixes 0,1,2,3 refer to particle indices for waters in the inner or outer loop, or 
705      * just 0 for non-waters.
706      * Suffixes A,B,C,D refer to j loop unrolling done with AVX, e.g. for the four different
707      * jnr indices corresponding to data put in the four positions in the SIMD register.
708      */
709     int              i_shift_offset,i_coord_offset,outeriter,inneriter;
710     int              j_index_start,j_index_end,jidx,nri,inr,ggid,iidx;
711     int              jnrA,jnrB,jnrC,jnrD;
712     int              jnrlistA,jnrlistB,jnrlistC,jnrlistD;
713     int              jnrlistE,jnrlistF,jnrlistG,jnrlistH;
714     int              j_coord_offsetA,j_coord_offsetB,j_coord_offsetC,j_coord_offsetD;
715     int              *iinr,*jindex,*jjnr,*shiftidx,*gid;
716     real             rcutoff_scalar;
717     real             *shiftvec,*fshift,*x,*f;
718     real             *fjptrA,*fjptrB,*fjptrC,*fjptrD;
719     real             scratch[4*DIM];
720     __m256d          tx,ty,tz,fscal,rcutoff,rcutoff2,jidxall;
721     real *           vdwioffsetptr0;
722     __m256d          ix0,iy0,iz0,fix0,fiy0,fiz0,iq0,isai0;
723     real *           vdwioffsetptr1;
724     __m256d          ix1,iy1,iz1,fix1,fiy1,fiz1,iq1,isai1;
725     real *           vdwioffsetptr2;
726     __m256d          ix2,iy2,iz2,fix2,fiy2,fiz2,iq2,isai2;
727     int              vdwjidx0A,vdwjidx0B,vdwjidx0C,vdwjidx0D;
728     __m256d          jx0,jy0,jz0,fjx0,fjy0,fjz0,jq0,isaj0;
729     __m256d          dx00,dy00,dz00,rsq00,rinv00,rinvsq00,r00,qq00,c6_00,c12_00;
730     __m256d          dx10,dy10,dz10,rsq10,rinv10,rinvsq10,r10,qq10,c6_10,c12_10;
731     __m256d          dx20,dy20,dz20,rsq20,rinv20,rinvsq20,r20,qq20,c6_20,c12_20;
732     __m256d          velec,felec,velecsum,facel,crf,krf,krf2;
733     real             *charge;
734     __m128i          ewitab;
735     __m256d          ewtabscale,eweps,sh_ewald,ewrt,ewtabhalfspace,ewtabF,ewtabFn,ewtabD,ewtabV;
736     __m256d          beta,beta2,beta3,zeta2,pmecorrF,pmecorrV,rinv3;
737     real             *ewtab;
738     __m256d          dummy_mask,cutoff_mask;
739     __m128           tmpmask0,tmpmask1;
740     __m256d          signbit = _mm256_castsi256_pd( _mm256_set1_epi32(0x80000000) );
741     __m256d          one     = _mm256_set1_pd(1.0);
742     __m256d          two     = _mm256_set1_pd(2.0);
743     x                = xx[0];
744     f                = ff[0];
745
746     nri              = nlist->nri;
747     iinr             = nlist->iinr;
748     jindex           = nlist->jindex;
749     jjnr             = nlist->jjnr;
750     shiftidx         = nlist->shift;
751     gid              = nlist->gid;
752     shiftvec         = fr->shift_vec[0];
753     fshift           = fr->fshift[0];
754     facel            = _mm256_set1_pd(fr->epsfac);
755     charge           = mdatoms->chargeA;
756
757     sh_ewald         = _mm256_set1_pd(fr->ic->sh_ewald);
758     beta             = _mm256_set1_pd(fr->ic->ewaldcoeff_q);
759     beta2            = _mm256_mul_pd(beta,beta);
760     beta3            = _mm256_mul_pd(beta,beta2);
761
762     ewtab            = fr->ic->tabq_coul_F;
763     ewtabscale       = _mm256_set1_pd(fr->ic->tabq_scale);
764     ewtabhalfspace   = _mm256_set1_pd(0.5/fr->ic->tabq_scale);
765
766     /* Setup water-specific parameters */
767     inr              = nlist->iinr[0];
768     iq0              = _mm256_mul_pd(facel,_mm256_set1_pd(charge[inr+0]));
769     iq1              = _mm256_mul_pd(facel,_mm256_set1_pd(charge[inr+1]));
770     iq2              = _mm256_mul_pd(facel,_mm256_set1_pd(charge[inr+2]));
771
772     /* When we use explicit cutoffs the value must be identical for elec and VdW, so use elec as an arbitrary choice */
773     rcutoff_scalar   = fr->rcoulomb;
774     rcutoff          = _mm256_set1_pd(rcutoff_scalar);
775     rcutoff2         = _mm256_mul_pd(rcutoff,rcutoff);
776
777     /* Avoid stupid compiler warnings */
778     jnrA = jnrB = jnrC = jnrD = 0;
779     j_coord_offsetA = 0;
780     j_coord_offsetB = 0;
781     j_coord_offsetC = 0;
782     j_coord_offsetD = 0;
783
784     outeriter        = 0;
785     inneriter        = 0;
786
787     for(iidx=0;iidx<4*DIM;iidx++)
788     {
789         scratch[iidx] = 0.0;
790     }
791
792     /* Start outer loop over neighborlists */
793     for(iidx=0; iidx<nri; iidx++)
794     {
795         /* Load shift vector for this list */
796         i_shift_offset   = DIM*shiftidx[iidx];
797
798         /* Load limits for loop over neighbors */
799         j_index_start    = jindex[iidx];
800         j_index_end      = jindex[iidx+1];
801
802         /* Get outer coordinate index */
803         inr              = iinr[iidx];
804         i_coord_offset   = DIM*inr;
805
806         /* Load i particle coords and add shift vector */
807         gmx_mm256_load_shift_and_3rvec_broadcast_pd(shiftvec+i_shift_offset,x+i_coord_offset,
808                                                     &ix0,&iy0,&iz0,&ix1,&iy1,&iz1,&ix2,&iy2,&iz2);
809
810         fix0             = _mm256_setzero_pd();
811         fiy0             = _mm256_setzero_pd();
812         fiz0             = _mm256_setzero_pd();
813         fix1             = _mm256_setzero_pd();
814         fiy1             = _mm256_setzero_pd();
815         fiz1             = _mm256_setzero_pd();
816         fix2             = _mm256_setzero_pd();
817         fiy2             = _mm256_setzero_pd();
818         fiz2             = _mm256_setzero_pd();
819
820         /* Start inner kernel loop */
821         for(jidx=j_index_start; jidx<j_index_end && jjnr[jidx+3]>=0; jidx+=4)
822         {
823
824             /* Get j neighbor index, and coordinate index */
825             jnrA             = jjnr[jidx];
826             jnrB             = jjnr[jidx+1];
827             jnrC             = jjnr[jidx+2];
828             jnrD             = jjnr[jidx+3];
829             j_coord_offsetA  = DIM*jnrA;
830             j_coord_offsetB  = DIM*jnrB;
831             j_coord_offsetC  = DIM*jnrC;
832             j_coord_offsetD  = DIM*jnrD;
833
834             /* load j atom coordinates */
835             gmx_mm256_load_1rvec_4ptr_swizzle_pd(x+j_coord_offsetA,x+j_coord_offsetB,
836                                                  x+j_coord_offsetC,x+j_coord_offsetD,
837                                                  &jx0,&jy0,&jz0);
838
839             /* Calculate displacement vector */
840             dx00             = _mm256_sub_pd(ix0,jx0);
841             dy00             = _mm256_sub_pd(iy0,jy0);
842             dz00             = _mm256_sub_pd(iz0,jz0);
843             dx10             = _mm256_sub_pd(ix1,jx0);
844             dy10             = _mm256_sub_pd(iy1,jy0);
845             dz10             = _mm256_sub_pd(iz1,jz0);
846             dx20             = _mm256_sub_pd(ix2,jx0);
847             dy20             = _mm256_sub_pd(iy2,jy0);
848             dz20             = _mm256_sub_pd(iz2,jz0);
849
850             /* Calculate squared distance and things based on it */
851             rsq00            = gmx_mm256_calc_rsq_pd(dx00,dy00,dz00);
852             rsq10            = gmx_mm256_calc_rsq_pd(dx10,dy10,dz10);
853             rsq20            = gmx_mm256_calc_rsq_pd(dx20,dy20,dz20);
854
855             rinv00           = gmx_mm256_invsqrt_pd(rsq00);
856             rinv10           = gmx_mm256_invsqrt_pd(rsq10);
857             rinv20           = gmx_mm256_invsqrt_pd(rsq20);
858
859             rinvsq00         = _mm256_mul_pd(rinv00,rinv00);
860             rinvsq10         = _mm256_mul_pd(rinv10,rinv10);
861             rinvsq20         = _mm256_mul_pd(rinv20,rinv20);
862
863             /* Load parameters for j particles */
864             jq0              = gmx_mm256_load_4real_swizzle_pd(charge+jnrA+0,charge+jnrB+0,
865                                                                  charge+jnrC+0,charge+jnrD+0);
866
867             fjx0             = _mm256_setzero_pd();
868             fjy0             = _mm256_setzero_pd();
869             fjz0             = _mm256_setzero_pd();
870
871             /**************************
872              * CALCULATE INTERACTIONS *
873              **************************/
874
875             if (gmx_mm256_any_lt(rsq00,rcutoff2))
876             {
877
878             r00              = _mm256_mul_pd(rsq00,rinv00);
879
880             /* Compute parameters for interactions between i and j atoms */
881             qq00             = _mm256_mul_pd(iq0,jq0);
882
883             /* EWALD ELECTROSTATICS */
884
885             /* Calculate Ewald table index by multiplying r with scale and truncate to integer */
886             ewrt             = _mm256_mul_pd(r00,ewtabscale);
887             ewitab           = _mm256_cvttpd_epi32(ewrt);
888             eweps            = _mm256_sub_pd(ewrt,_mm256_round_pd(ewrt, _MM_FROUND_FLOOR));
889             gmx_mm256_load_4pair_swizzle_pd(ewtab + _mm_extract_epi32(ewitab,0),ewtab + _mm_extract_epi32(ewitab,1),
890                                             ewtab + _mm_extract_epi32(ewitab,2),ewtab + _mm_extract_epi32(ewitab,3),
891                                             &ewtabF,&ewtabFn);
892             felec            = _mm256_add_pd(_mm256_mul_pd( _mm256_sub_pd(one,eweps),ewtabF),_mm256_mul_pd(eweps,ewtabFn));
893             felec            = _mm256_mul_pd(_mm256_mul_pd(qq00,rinv00),_mm256_sub_pd(rinvsq00,felec));
894
895             cutoff_mask      = _mm256_cmp_pd(rsq00,rcutoff2,_CMP_LT_OQ);
896
897             fscal            = felec;
898
899             fscal            = _mm256_and_pd(fscal,cutoff_mask);
900
901             /* Calculate temporary vectorial force */
902             tx               = _mm256_mul_pd(fscal,dx00);
903             ty               = _mm256_mul_pd(fscal,dy00);
904             tz               = _mm256_mul_pd(fscal,dz00);
905
906             /* Update vectorial force */
907             fix0             = _mm256_add_pd(fix0,tx);
908             fiy0             = _mm256_add_pd(fiy0,ty);
909             fiz0             = _mm256_add_pd(fiz0,tz);
910
911             fjx0             = _mm256_add_pd(fjx0,tx);
912             fjy0             = _mm256_add_pd(fjy0,ty);
913             fjz0             = _mm256_add_pd(fjz0,tz);
914
915             }
916
917             /**************************
918              * CALCULATE INTERACTIONS *
919              **************************/
920
921             if (gmx_mm256_any_lt(rsq10,rcutoff2))
922             {
923
924             r10              = _mm256_mul_pd(rsq10,rinv10);
925
926             /* Compute parameters for interactions between i and j atoms */
927             qq10             = _mm256_mul_pd(iq1,jq0);
928
929             /* EWALD ELECTROSTATICS */
930
931             /* Calculate Ewald table index by multiplying r with scale and truncate to integer */
932             ewrt             = _mm256_mul_pd(r10,ewtabscale);
933             ewitab           = _mm256_cvttpd_epi32(ewrt);
934             eweps            = _mm256_sub_pd(ewrt,_mm256_round_pd(ewrt, _MM_FROUND_FLOOR));
935             gmx_mm256_load_4pair_swizzle_pd(ewtab + _mm_extract_epi32(ewitab,0),ewtab + _mm_extract_epi32(ewitab,1),
936                                             ewtab + _mm_extract_epi32(ewitab,2),ewtab + _mm_extract_epi32(ewitab,3),
937                                             &ewtabF,&ewtabFn);
938             felec            = _mm256_add_pd(_mm256_mul_pd( _mm256_sub_pd(one,eweps),ewtabF),_mm256_mul_pd(eweps,ewtabFn));
939             felec            = _mm256_mul_pd(_mm256_mul_pd(qq10,rinv10),_mm256_sub_pd(rinvsq10,felec));
940
941             cutoff_mask      = _mm256_cmp_pd(rsq10,rcutoff2,_CMP_LT_OQ);
942
943             fscal            = felec;
944
945             fscal            = _mm256_and_pd(fscal,cutoff_mask);
946
947             /* Calculate temporary vectorial force */
948             tx               = _mm256_mul_pd(fscal,dx10);
949             ty               = _mm256_mul_pd(fscal,dy10);
950             tz               = _mm256_mul_pd(fscal,dz10);
951
952             /* Update vectorial force */
953             fix1             = _mm256_add_pd(fix1,tx);
954             fiy1             = _mm256_add_pd(fiy1,ty);
955             fiz1             = _mm256_add_pd(fiz1,tz);
956
957             fjx0             = _mm256_add_pd(fjx0,tx);
958             fjy0             = _mm256_add_pd(fjy0,ty);
959             fjz0             = _mm256_add_pd(fjz0,tz);
960
961             }
962
963             /**************************
964              * CALCULATE INTERACTIONS *
965              **************************/
966
967             if (gmx_mm256_any_lt(rsq20,rcutoff2))
968             {
969
970             r20              = _mm256_mul_pd(rsq20,rinv20);
971
972             /* Compute parameters for interactions between i and j atoms */
973             qq20             = _mm256_mul_pd(iq2,jq0);
974
975             /* EWALD ELECTROSTATICS */
976
977             /* Calculate Ewald table index by multiplying r with scale and truncate to integer */
978             ewrt             = _mm256_mul_pd(r20,ewtabscale);
979             ewitab           = _mm256_cvttpd_epi32(ewrt);
980             eweps            = _mm256_sub_pd(ewrt,_mm256_round_pd(ewrt, _MM_FROUND_FLOOR));
981             gmx_mm256_load_4pair_swizzle_pd(ewtab + _mm_extract_epi32(ewitab,0),ewtab + _mm_extract_epi32(ewitab,1),
982                                             ewtab + _mm_extract_epi32(ewitab,2),ewtab + _mm_extract_epi32(ewitab,3),
983                                             &ewtabF,&ewtabFn);
984             felec            = _mm256_add_pd(_mm256_mul_pd( _mm256_sub_pd(one,eweps),ewtabF),_mm256_mul_pd(eweps,ewtabFn));
985             felec            = _mm256_mul_pd(_mm256_mul_pd(qq20,rinv20),_mm256_sub_pd(rinvsq20,felec));
986
987             cutoff_mask      = _mm256_cmp_pd(rsq20,rcutoff2,_CMP_LT_OQ);
988
989             fscal            = felec;
990
991             fscal            = _mm256_and_pd(fscal,cutoff_mask);
992
993             /* Calculate temporary vectorial force */
994             tx               = _mm256_mul_pd(fscal,dx20);
995             ty               = _mm256_mul_pd(fscal,dy20);
996             tz               = _mm256_mul_pd(fscal,dz20);
997
998             /* Update vectorial force */
999             fix2             = _mm256_add_pd(fix2,tx);
1000             fiy2             = _mm256_add_pd(fiy2,ty);
1001             fiz2             = _mm256_add_pd(fiz2,tz);
1002
1003             fjx0             = _mm256_add_pd(fjx0,tx);
1004             fjy0             = _mm256_add_pd(fjy0,ty);
1005             fjz0             = _mm256_add_pd(fjz0,tz);
1006
1007             }
1008
1009             fjptrA             = f+j_coord_offsetA;
1010             fjptrB             = f+j_coord_offsetB;
1011             fjptrC             = f+j_coord_offsetC;
1012             fjptrD             = f+j_coord_offsetD;
1013
1014             gmx_mm256_decrement_1rvec_4ptr_swizzle_pd(fjptrA,fjptrB,fjptrC,fjptrD,fjx0,fjy0,fjz0);
1015
1016             /* Inner loop uses 120 flops */
1017         }
1018
1019         if(jidx<j_index_end)
1020         {
1021
1022             /* Get j neighbor index, and coordinate index */
1023             jnrlistA         = jjnr[jidx];
1024             jnrlistB         = jjnr[jidx+1];
1025             jnrlistC         = jjnr[jidx+2];
1026             jnrlistD         = jjnr[jidx+3];
1027             /* Sign of each element will be negative for non-real atoms.
1028              * This mask will be 0xFFFFFFFF for dummy entries and 0x0 for real ones,
1029              * so use it as val = _mm_andnot_pd(mask,val) to clear dummy entries.
1030              */
1031             tmpmask0 = gmx_mm_castsi128_ps(_mm_cmplt_epi32(_mm_loadu_si128((const __m128i *)(jjnr+jidx)),_mm_setzero_si128()));
1032
1033             tmpmask1 = _mm_permute_ps(tmpmask0,_GMX_MM_PERMUTE(3,3,2,2));
1034             tmpmask0 = _mm_permute_ps(tmpmask0,_GMX_MM_PERMUTE(1,1,0,0));
1035             dummy_mask = _mm256_castps_pd(gmx_mm256_set_m128(tmpmask1,tmpmask0));
1036
1037             jnrA       = (jnrlistA>=0) ? jnrlistA : 0;
1038             jnrB       = (jnrlistB>=0) ? jnrlistB : 0;
1039             jnrC       = (jnrlistC>=0) ? jnrlistC : 0;
1040             jnrD       = (jnrlistD>=0) ? jnrlistD : 0;
1041             j_coord_offsetA  = DIM*jnrA;
1042             j_coord_offsetB  = DIM*jnrB;
1043             j_coord_offsetC  = DIM*jnrC;
1044             j_coord_offsetD  = DIM*jnrD;
1045
1046             /* load j atom coordinates */
1047             gmx_mm256_load_1rvec_4ptr_swizzle_pd(x+j_coord_offsetA,x+j_coord_offsetB,
1048                                                  x+j_coord_offsetC,x+j_coord_offsetD,
1049                                                  &jx0,&jy0,&jz0);
1050
1051             /* Calculate displacement vector */
1052             dx00             = _mm256_sub_pd(ix0,jx0);
1053             dy00             = _mm256_sub_pd(iy0,jy0);
1054             dz00             = _mm256_sub_pd(iz0,jz0);
1055             dx10             = _mm256_sub_pd(ix1,jx0);
1056             dy10             = _mm256_sub_pd(iy1,jy0);
1057             dz10             = _mm256_sub_pd(iz1,jz0);
1058             dx20             = _mm256_sub_pd(ix2,jx0);
1059             dy20             = _mm256_sub_pd(iy2,jy0);
1060             dz20             = _mm256_sub_pd(iz2,jz0);
1061
1062             /* Calculate squared distance and things based on it */
1063             rsq00            = gmx_mm256_calc_rsq_pd(dx00,dy00,dz00);
1064             rsq10            = gmx_mm256_calc_rsq_pd(dx10,dy10,dz10);
1065             rsq20            = gmx_mm256_calc_rsq_pd(dx20,dy20,dz20);
1066
1067             rinv00           = gmx_mm256_invsqrt_pd(rsq00);
1068             rinv10           = gmx_mm256_invsqrt_pd(rsq10);
1069             rinv20           = gmx_mm256_invsqrt_pd(rsq20);
1070
1071             rinvsq00         = _mm256_mul_pd(rinv00,rinv00);
1072             rinvsq10         = _mm256_mul_pd(rinv10,rinv10);
1073             rinvsq20         = _mm256_mul_pd(rinv20,rinv20);
1074
1075             /* Load parameters for j particles */
1076             jq0              = gmx_mm256_load_4real_swizzle_pd(charge+jnrA+0,charge+jnrB+0,
1077                                                                  charge+jnrC+0,charge+jnrD+0);
1078
1079             fjx0             = _mm256_setzero_pd();
1080             fjy0             = _mm256_setzero_pd();
1081             fjz0             = _mm256_setzero_pd();
1082
1083             /**************************
1084              * CALCULATE INTERACTIONS *
1085              **************************/
1086
1087             if (gmx_mm256_any_lt(rsq00,rcutoff2))
1088             {
1089
1090             r00              = _mm256_mul_pd(rsq00,rinv00);
1091             r00              = _mm256_andnot_pd(dummy_mask,r00);
1092
1093             /* Compute parameters for interactions between i and j atoms */
1094             qq00             = _mm256_mul_pd(iq0,jq0);
1095
1096             /* EWALD ELECTROSTATICS */
1097
1098             /* Calculate Ewald table index by multiplying r with scale and truncate to integer */
1099             ewrt             = _mm256_mul_pd(r00,ewtabscale);
1100             ewitab           = _mm256_cvttpd_epi32(ewrt);
1101             eweps            = _mm256_sub_pd(ewrt,_mm256_round_pd(ewrt, _MM_FROUND_FLOOR));
1102             gmx_mm256_load_4pair_swizzle_pd(ewtab + _mm_extract_epi32(ewitab,0),ewtab + _mm_extract_epi32(ewitab,1),
1103                                             ewtab + _mm_extract_epi32(ewitab,2),ewtab + _mm_extract_epi32(ewitab,3),
1104                                             &ewtabF,&ewtabFn);
1105             felec            = _mm256_add_pd(_mm256_mul_pd( _mm256_sub_pd(one,eweps),ewtabF),_mm256_mul_pd(eweps,ewtabFn));
1106             felec            = _mm256_mul_pd(_mm256_mul_pd(qq00,rinv00),_mm256_sub_pd(rinvsq00,felec));
1107
1108             cutoff_mask      = _mm256_cmp_pd(rsq00,rcutoff2,_CMP_LT_OQ);
1109
1110             fscal            = felec;
1111
1112             fscal            = _mm256_and_pd(fscal,cutoff_mask);
1113
1114             fscal            = _mm256_andnot_pd(dummy_mask,fscal);
1115
1116             /* Calculate temporary vectorial force */
1117             tx               = _mm256_mul_pd(fscal,dx00);
1118             ty               = _mm256_mul_pd(fscal,dy00);
1119             tz               = _mm256_mul_pd(fscal,dz00);
1120
1121             /* Update vectorial force */
1122             fix0             = _mm256_add_pd(fix0,tx);
1123             fiy0             = _mm256_add_pd(fiy0,ty);
1124             fiz0             = _mm256_add_pd(fiz0,tz);
1125
1126             fjx0             = _mm256_add_pd(fjx0,tx);
1127             fjy0             = _mm256_add_pd(fjy0,ty);
1128             fjz0             = _mm256_add_pd(fjz0,tz);
1129
1130             }
1131
1132             /**************************
1133              * CALCULATE INTERACTIONS *
1134              **************************/
1135
1136             if (gmx_mm256_any_lt(rsq10,rcutoff2))
1137             {
1138
1139             r10              = _mm256_mul_pd(rsq10,rinv10);
1140             r10              = _mm256_andnot_pd(dummy_mask,r10);
1141
1142             /* Compute parameters for interactions between i and j atoms */
1143             qq10             = _mm256_mul_pd(iq1,jq0);
1144
1145             /* EWALD ELECTROSTATICS */
1146
1147             /* Calculate Ewald table index by multiplying r with scale and truncate to integer */
1148             ewrt             = _mm256_mul_pd(r10,ewtabscale);
1149             ewitab           = _mm256_cvttpd_epi32(ewrt);
1150             eweps            = _mm256_sub_pd(ewrt,_mm256_round_pd(ewrt, _MM_FROUND_FLOOR));
1151             gmx_mm256_load_4pair_swizzle_pd(ewtab + _mm_extract_epi32(ewitab,0),ewtab + _mm_extract_epi32(ewitab,1),
1152                                             ewtab + _mm_extract_epi32(ewitab,2),ewtab + _mm_extract_epi32(ewitab,3),
1153                                             &ewtabF,&ewtabFn);
1154             felec            = _mm256_add_pd(_mm256_mul_pd( _mm256_sub_pd(one,eweps),ewtabF),_mm256_mul_pd(eweps,ewtabFn));
1155             felec            = _mm256_mul_pd(_mm256_mul_pd(qq10,rinv10),_mm256_sub_pd(rinvsq10,felec));
1156
1157             cutoff_mask      = _mm256_cmp_pd(rsq10,rcutoff2,_CMP_LT_OQ);
1158
1159             fscal            = felec;
1160
1161             fscal            = _mm256_and_pd(fscal,cutoff_mask);
1162
1163             fscal            = _mm256_andnot_pd(dummy_mask,fscal);
1164
1165             /* Calculate temporary vectorial force */
1166             tx               = _mm256_mul_pd(fscal,dx10);
1167             ty               = _mm256_mul_pd(fscal,dy10);
1168             tz               = _mm256_mul_pd(fscal,dz10);
1169
1170             /* Update vectorial force */
1171             fix1             = _mm256_add_pd(fix1,tx);
1172             fiy1             = _mm256_add_pd(fiy1,ty);
1173             fiz1             = _mm256_add_pd(fiz1,tz);
1174
1175             fjx0             = _mm256_add_pd(fjx0,tx);
1176             fjy0             = _mm256_add_pd(fjy0,ty);
1177             fjz0             = _mm256_add_pd(fjz0,tz);
1178
1179             }
1180
1181             /**************************
1182              * CALCULATE INTERACTIONS *
1183              **************************/
1184
1185             if (gmx_mm256_any_lt(rsq20,rcutoff2))
1186             {
1187
1188             r20              = _mm256_mul_pd(rsq20,rinv20);
1189             r20              = _mm256_andnot_pd(dummy_mask,r20);
1190
1191             /* Compute parameters for interactions between i and j atoms */
1192             qq20             = _mm256_mul_pd(iq2,jq0);
1193
1194             /* EWALD ELECTROSTATICS */
1195
1196             /* Calculate Ewald table index by multiplying r with scale and truncate to integer */
1197             ewrt             = _mm256_mul_pd(r20,ewtabscale);
1198             ewitab           = _mm256_cvttpd_epi32(ewrt);
1199             eweps            = _mm256_sub_pd(ewrt,_mm256_round_pd(ewrt, _MM_FROUND_FLOOR));
1200             gmx_mm256_load_4pair_swizzle_pd(ewtab + _mm_extract_epi32(ewitab,0),ewtab + _mm_extract_epi32(ewitab,1),
1201                                             ewtab + _mm_extract_epi32(ewitab,2),ewtab + _mm_extract_epi32(ewitab,3),
1202                                             &ewtabF,&ewtabFn);
1203             felec            = _mm256_add_pd(_mm256_mul_pd( _mm256_sub_pd(one,eweps),ewtabF),_mm256_mul_pd(eweps,ewtabFn));
1204             felec            = _mm256_mul_pd(_mm256_mul_pd(qq20,rinv20),_mm256_sub_pd(rinvsq20,felec));
1205
1206             cutoff_mask      = _mm256_cmp_pd(rsq20,rcutoff2,_CMP_LT_OQ);
1207
1208             fscal            = felec;
1209
1210             fscal            = _mm256_and_pd(fscal,cutoff_mask);
1211
1212             fscal            = _mm256_andnot_pd(dummy_mask,fscal);
1213
1214             /* Calculate temporary vectorial force */
1215             tx               = _mm256_mul_pd(fscal,dx20);
1216             ty               = _mm256_mul_pd(fscal,dy20);
1217             tz               = _mm256_mul_pd(fscal,dz20);
1218
1219             /* Update vectorial force */
1220             fix2             = _mm256_add_pd(fix2,tx);
1221             fiy2             = _mm256_add_pd(fiy2,ty);
1222             fiz2             = _mm256_add_pd(fiz2,tz);
1223
1224             fjx0             = _mm256_add_pd(fjx0,tx);
1225             fjy0             = _mm256_add_pd(fjy0,ty);
1226             fjz0             = _mm256_add_pd(fjz0,tz);
1227
1228             }
1229
1230             fjptrA             = (jnrlistA>=0) ? f+j_coord_offsetA : scratch;
1231             fjptrB             = (jnrlistB>=0) ? f+j_coord_offsetB : scratch;
1232             fjptrC             = (jnrlistC>=0) ? f+j_coord_offsetC : scratch;
1233             fjptrD             = (jnrlistD>=0) ? f+j_coord_offsetD : scratch;
1234
1235             gmx_mm256_decrement_1rvec_4ptr_swizzle_pd(fjptrA,fjptrB,fjptrC,fjptrD,fjx0,fjy0,fjz0);
1236
1237             /* Inner loop uses 123 flops */
1238         }
1239
1240         /* End of innermost loop */
1241
1242         gmx_mm256_update_iforce_3atom_swizzle_pd(fix0,fiy0,fiz0,fix1,fiy1,fiz1,fix2,fiy2,fiz2,
1243                                                  f+i_coord_offset,fshift+i_shift_offset);
1244
1245         /* Increment number of inner iterations */
1246         inneriter                  += j_index_end - j_index_start;
1247
1248         /* Outer loop uses 18 flops */
1249     }
1250
1251     /* Increment number of outer iterations */
1252     outeriter        += nri;
1253
1254     /* Update outer/inner flops */
1255
1256     inc_nrnb(nrnb,eNR_NBKERNEL_ELEC_W3_F,outeriter*18 + inneriter*123);
1257 }