Introduce gmxpre.h for truly global definitions
[alexxy/gromacs.git] / src / gromacs / gmxlib / nonbonded / nb_kernel_avx_256_single / nb_kernel_ElecCoul_VdwLJ_GeomP1P1_avx_256_single.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_single 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_single.h"
50 #include "kernelutil_x86_avx_256_single.h"
51
52 /*
53  * Gromacs nonbonded kernel:   nb_kernel_ElecCoul_VdwLJ_GeomP1P1_VF_avx_256_single
54  * Electrostatics interaction: Coulomb
55  * VdW interaction:            LennardJones
56  * Geometry:                   Particle-Particle
57  * Calculate force/pot:        PotentialAndForce
58  */
59 void
60 nb_kernel_ElecCoul_VdwLJ_GeomP1P1_VF_avx_256_single
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,E,F,G,H refer to j loop unrolling done with AVX, e.g. for the eight 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              jnrE,jnrF,jnrG,jnrH;
78     int              jnrlistA,jnrlistB,jnrlistC,jnrlistD;
79     int              jnrlistE,jnrlistF,jnrlistG,jnrlistH;
80     int              j_coord_offsetA,j_coord_offsetB,j_coord_offsetC,j_coord_offsetD;
81     int              j_coord_offsetE,j_coord_offsetF,j_coord_offsetG,j_coord_offsetH;
82     int              *iinr,*jindex,*jjnr,*shiftidx,*gid;
83     real             rcutoff_scalar;
84     real             *shiftvec,*fshift,*x,*f;
85     real             *fjptrA,*fjptrB,*fjptrC,*fjptrD,*fjptrE,*fjptrF,*fjptrG,*fjptrH;
86     real             scratch[4*DIM];
87     __m256           tx,ty,tz,fscal,rcutoff,rcutoff2,jidxall;
88     real *           vdwioffsetptr0;
89     __m256           ix0,iy0,iz0,fix0,fiy0,fiz0,iq0,isai0;
90     int              vdwjidx0A,vdwjidx0B,vdwjidx0C,vdwjidx0D,vdwjidx0E,vdwjidx0F,vdwjidx0G,vdwjidx0H;
91     __m256           jx0,jy0,jz0,fjx0,fjy0,fjz0,jq0,isaj0;
92     __m256           dx00,dy00,dz00,rsq00,rinv00,rinvsq00,r00,qq00,c6_00,c12_00;
93     __m256           velec,felec,velecsum,facel,crf,krf,krf2;
94     real             *charge;
95     int              nvdwtype;
96     __m256           rinvsix,rvdw,vvdw,vvdw6,vvdw12,fvdw,fvdw6,fvdw12,vvdwsum,sh_vdw_invrcut6;
97     int              *vdwtype;
98     real             *vdwparam;
99     __m256           one_sixth   = _mm256_set1_ps(1.0/6.0);
100     __m256           one_twelfth = _mm256_set1_ps(1.0/12.0);
101     __m256           dummy_mask,cutoff_mask;
102     __m256           signbit = _mm256_castsi256_ps( _mm256_set1_epi32(0x80000000) );
103     __m256           one     = _mm256_set1_ps(1.0);
104     __m256           two     = _mm256_set1_ps(2.0);
105     x                = xx[0];
106     f                = ff[0];
107
108     nri              = nlist->nri;
109     iinr             = nlist->iinr;
110     jindex           = nlist->jindex;
111     jjnr             = nlist->jjnr;
112     shiftidx         = nlist->shift;
113     gid              = nlist->gid;
114     shiftvec         = fr->shift_vec[0];
115     fshift           = fr->fshift[0];
116     facel            = _mm256_set1_ps(fr->epsfac);
117     charge           = mdatoms->chargeA;
118     nvdwtype         = fr->ntype;
119     vdwparam         = fr->nbfp;
120     vdwtype          = mdatoms->typeA;
121
122     /* Avoid stupid compiler warnings */
123     jnrA = jnrB = jnrC = jnrD = jnrE = jnrF = jnrG = jnrH = 0;
124     j_coord_offsetA = 0;
125     j_coord_offsetB = 0;
126     j_coord_offsetC = 0;
127     j_coord_offsetD = 0;
128     j_coord_offsetE = 0;
129     j_coord_offsetF = 0;
130     j_coord_offsetG = 0;
131     j_coord_offsetH = 0;
132
133     outeriter        = 0;
134     inneriter        = 0;
135
136     for(iidx=0;iidx<4*DIM;iidx++)
137     {
138         scratch[iidx] = 0.0;
139     }
140
141     /* Start outer loop over neighborlists */
142     for(iidx=0; iidx<nri; iidx++)
143     {
144         /* Load shift vector for this list */
145         i_shift_offset   = DIM*shiftidx[iidx];
146
147         /* Load limits for loop over neighbors */
148         j_index_start    = jindex[iidx];
149         j_index_end      = jindex[iidx+1];
150
151         /* Get outer coordinate index */
152         inr              = iinr[iidx];
153         i_coord_offset   = DIM*inr;
154
155         /* Load i particle coords and add shift vector */
156         gmx_mm256_load_shift_and_1rvec_broadcast_ps(shiftvec+i_shift_offset,x+i_coord_offset,&ix0,&iy0,&iz0);
157
158         fix0             = _mm256_setzero_ps();
159         fiy0             = _mm256_setzero_ps();
160         fiz0             = _mm256_setzero_ps();
161
162         /* Load parameters for i particles */
163         iq0              = _mm256_mul_ps(facel,_mm256_set1_ps(charge[inr+0]));
164         vdwioffsetptr0   = vdwparam+2*nvdwtype*vdwtype[inr+0];
165
166         /* Reset potential sums */
167         velecsum         = _mm256_setzero_ps();
168         vvdwsum          = _mm256_setzero_ps();
169
170         /* Start inner kernel loop */
171         for(jidx=j_index_start; jidx<j_index_end && jjnr[jidx+7]>=0; jidx+=8)
172         {
173
174             /* Get j neighbor index, and coordinate index */
175             jnrA             = jjnr[jidx];
176             jnrB             = jjnr[jidx+1];
177             jnrC             = jjnr[jidx+2];
178             jnrD             = jjnr[jidx+3];
179             jnrE             = jjnr[jidx+4];
180             jnrF             = jjnr[jidx+5];
181             jnrG             = jjnr[jidx+6];
182             jnrH             = jjnr[jidx+7];
183             j_coord_offsetA  = DIM*jnrA;
184             j_coord_offsetB  = DIM*jnrB;
185             j_coord_offsetC  = DIM*jnrC;
186             j_coord_offsetD  = DIM*jnrD;
187             j_coord_offsetE  = DIM*jnrE;
188             j_coord_offsetF  = DIM*jnrF;
189             j_coord_offsetG  = DIM*jnrG;
190             j_coord_offsetH  = DIM*jnrH;
191
192             /* load j atom coordinates */
193             gmx_mm256_load_1rvec_8ptr_swizzle_ps(x+j_coord_offsetA,x+j_coord_offsetB,
194                                                  x+j_coord_offsetC,x+j_coord_offsetD,
195                                                  x+j_coord_offsetE,x+j_coord_offsetF,
196                                                  x+j_coord_offsetG,x+j_coord_offsetH,
197                                                  &jx0,&jy0,&jz0);
198
199             /* Calculate displacement vector */
200             dx00             = _mm256_sub_ps(ix0,jx0);
201             dy00             = _mm256_sub_ps(iy0,jy0);
202             dz00             = _mm256_sub_ps(iz0,jz0);
203
204             /* Calculate squared distance and things based on it */
205             rsq00            = gmx_mm256_calc_rsq_ps(dx00,dy00,dz00);
206
207             rinv00           = gmx_mm256_invsqrt_ps(rsq00);
208
209             rinvsq00         = _mm256_mul_ps(rinv00,rinv00);
210
211             /* Load parameters for j particles */
212             jq0              = gmx_mm256_load_8real_swizzle_ps(charge+jnrA+0,charge+jnrB+0,
213                                                                  charge+jnrC+0,charge+jnrD+0,
214                                                                  charge+jnrE+0,charge+jnrF+0,
215                                                                  charge+jnrG+0,charge+jnrH+0);
216             vdwjidx0A        = 2*vdwtype[jnrA+0];
217             vdwjidx0B        = 2*vdwtype[jnrB+0];
218             vdwjidx0C        = 2*vdwtype[jnrC+0];
219             vdwjidx0D        = 2*vdwtype[jnrD+0];
220             vdwjidx0E        = 2*vdwtype[jnrE+0];
221             vdwjidx0F        = 2*vdwtype[jnrF+0];
222             vdwjidx0G        = 2*vdwtype[jnrG+0];
223             vdwjidx0H        = 2*vdwtype[jnrH+0];
224
225             /**************************
226              * CALCULATE INTERACTIONS *
227              **************************/
228
229             /* Compute parameters for interactions between i and j atoms */
230             qq00             = _mm256_mul_ps(iq0,jq0);
231             gmx_mm256_load_8pair_swizzle_ps(vdwioffsetptr0+vdwjidx0A,
232                                             vdwioffsetptr0+vdwjidx0B,
233                                             vdwioffsetptr0+vdwjidx0C,
234                                             vdwioffsetptr0+vdwjidx0D,
235                                             vdwioffsetptr0+vdwjidx0E,
236                                             vdwioffsetptr0+vdwjidx0F,
237                                             vdwioffsetptr0+vdwjidx0G,
238                                             vdwioffsetptr0+vdwjidx0H,
239                                             &c6_00,&c12_00);
240
241             /* COULOMB ELECTROSTATICS */
242             velec            = _mm256_mul_ps(qq00,rinv00);
243             felec            = _mm256_mul_ps(velec,rinvsq00);
244
245             /* LENNARD-JONES DISPERSION/REPULSION */
246
247             rinvsix          = _mm256_mul_ps(_mm256_mul_ps(rinvsq00,rinvsq00),rinvsq00);
248             vvdw6            = _mm256_mul_ps(c6_00,rinvsix);
249             vvdw12           = _mm256_mul_ps(c12_00,_mm256_mul_ps(rinvsix,rinvsix));
250             vvdw             = _mm256_sub_ps( _mm256_mul_ps(vvdw12,one_twelfth) , _mm256_mul_ps(vvdw6,one_sixth) );
251             fvdw             = _mm256_mul_ps(_mm256_sub_ps(vvdw12,vvdw6),rinvsq00);
252
253             /* Update potential sum for this i atom from the interaction with this j atom. */
254             velecsum         = _mm256_add_ps(velecsum,velec);
255             vvdwsum          = _mm256_add_ps(vvdwsum,vvdw);
256
257             fscal            = _mm256_add_ps(felec,fvdw);
258
259             /* Calculate temporary vectorial force */
260             tx               = _mm256_mul_ps(fscal,dx00);
261             ty               = _mm256_mul_ps(fscal,dy00);
262             tz               = _mm256_mul_ps(fscal,dz00);
263
264             /* Update vectorial force */
265             fix0             = _mm256_add_ps(fix0,tx);
266             fiy0             = _mm256_add_ps(fiy0,ty);
267             fiz0             = _mm256_add_ps(fiz0,tz);
268
269             fjptrA             = f+j_coord_offsetA;
270             fjptrB             = f+j_coord_offsetB;
271             fjptrC             = f+j_coord_offsetC;
272             fjptrD             = f+j_coord_offsetD;
273             fjptrE             = f+j_coord_offsetE;
274             fjptrF             = f+j_coord_offsetF;
275             fjptrG             = f+j_coord_offsetG;
276             fjptrH             = f+j_coord_offsetH;
277             gmx_mm256_decrement_1rvec_8ptr_swizzle_ps(fjptrA,fjptrB,fjptrC,fjptrD,fjptrE,fjptrF,fjptrG,fjptrH,tx,ty,tz);
278
279             /* Inner loop uses 39 flops */
280         }
281
282         if(jidx<j_index_end)
283         {
284
285             /* Get j neighbor index, and coordinate index */
286             jnrlistA         = jjnr[jidx];
287             jnrlistB         = jjnr[jidx+1];
288             jnrlistC         = jjnr[jidx+2];
289             jnrlistD         = jjnr[jidx+3];
290             jnrlistE         = jjnr[jidx+4];
291             jnrlistF         = jjnr[jidx+5];
292             jnrlistG         = jjnr[jidx+6];
293             jnrlistH         = jjnr[jidx+7];
294             /* Sign of each element will be negative for non-real atoms.
295              * This mask will be 0xFFFFFFFF for dummy entries and 0x0 for real ones,
296              * so use it as val = _mm_andnot_ps(mask,val) to clear dummy entries.
297              */
298             dummy_mask = gmx_mm256_set_m128(gmx_mm_castsi128_ps(_mm_cmplt_epi32(_mm_loadu_si128((const __m128i *)(jjnr+jidx+4)),_mm_setzero_si128())),
299                                             gmx_mm_castsi128_ps(_mm_cmplt_epi32(_mm_loadu_si128((const __m128i *)(jjnr+jidx)),_mm_setzero_si128())));
300                                             
301             jnrA       = (jnrlistA>=0) ? jnrlistA : 0;
302             jnrB       = (jnrlistB>=0) ? jnrlistB : 0;
303             jnrC       = (jnrlistC>=0) ? jnrlistC : 0;
304             jnrD       = (jnrlistD>=0) ? jnrlistD : 0;
305             jnrE       = (jnrlistE>=0) ? jnrlistE : 0;
306             jnrF       = (jnrlistF>=0) ? jnrlistF : 0;
307             jnrG       = (jnrlistG>=0) ? jnrlistG : 0;
308             jnrH       = (jnrlistH>=0) ? jnrlistH : 0;
309             j_coord_offsetA  = DIM*jnrA;
310             j_coord_offsetB  = DIM*jnrB;
311             j_coord_offsetC  = DIM*jnrC;
312             j_coord_offsetD  = DIM*jnrD;
313             j_coord_offsetE  = DIM*jnrE;
314             j_coord_offsetF  = DIM*jnrF;
315             j_coord_offsetG  = DIM*jnrG;
316             j_coord_offsetH  = DIM*jnrH;
317
318             /* load j atom coordinates */
319             gmx_mm256_load_1rvec_8ptr_swizzle_ps(x+j_coord_offsetA,x+j_coord_offsetB,
320                                                  x+j_coord_offsetC,x+j_coord_offsetD,
321                                                  x+j_coord_offsetE,x+j_coord_offsetF,
322                                                  x+j_coord_offsetG,x+j_coord_offsetH,
323                                                  &jx0,&jy0,&jz0);
324
325             /* Calculate displacement vector */
326             dx00             = _mm256_sub_ps(ix0,jx0);
327             dy00             = _mm256_sub_ps(iy0,jy0);
328             dz00             = _mm256_sub_ps(iz0,jz0);
329
330             /* Calculate squared distance and things based on it */
331             rsq00            = gmx_mm256_calc_rsq_ps(dx00,dy00,dz00);
332
333             rinv00           = gmx_mm256_invsqrt_ps(rsq00);
334
335             rinvsq00         = _mm256_mul_ps(rinv00,rinv00);
336
337             /* Load parameters for j particles */
338             jq0              = gmx_mm256_load_8real_swizzle_ps(charge+jnrA+0,charge+jnrB+0,
339                                                                  charge+jnrC+0,charge+jnrD+0,
340                                                                  charge+jnrE+0,charge+jnrF+0,
341                                                                  charge+jnrG+0,charge+jnrH+0);
342             vdwjidx0A        = 2*vdwtype[jnrA+0];
343             vdwjidx0B        = 2*vdwtype[jnrB+0];
344             vdwjidx0C        = 2*vdwtype[jnrC+0];
345             vdwjidx0D        = 2*vdwtype[jnrD+0];
346             vdwjidx0E        = 2*vdwtype[jnrE+0];
347             vdwjidx0F        = 2*vdwtype[jnrF+0];
348             vdwjidx0G        = 2*vdwtype[jnrG+0];
349             vdwjidx0H        = 2*vdwtype[jnrH+0];
350
351             /**************************
352              * CALCULATE INTERACTIONS *
353              **************************/
354
355             /* Compute parameters for interactions between i and j atoms */
356             qq00             = _mm256_mul_ps(iq0,jq0);
357             gmx_mm256_load_8pair_swizzle_ps(vdwioffsetptr0+vdwjidx0A,
358                                             vdwioffsetptr0+vdwjidx0B,
359                                             vdwioffsetptr0+vdwjidx0C,
360                                             vdwioffsetptr0+vdwjidx0D,
361                                             vdwioffsetptr0+vdwjidx0E,
362                                             vdwioffsetptr0+vdwjidx0F,
363                                             vdwioffsetptr0+vdwjidx0G,
364                                             vdwioffsetptr0+vdwjidx0H,
365                                             &c6_00,&c12_00);
366
367             /* COULOMB ELECTROSTATICS */
368             velec            = _mm256_mul_ps(qq00,rinv00);
369             felec            = _mm256_mul_ps(velec,rinvsq00);
370
371             /* LENNARD-JONES DISPERSION/REPULSION */
372
373             rinvsix          = _mm256_mul_ps(_mm256_mul_ps(rinvsq00,rinvsq00),rinvsq00);
374             vvdw6            = _mm256_mul_ps(c6_00,rinvsix);
375             vvdw12           = _mm256_mul_ps(c12_00,_mm256_mul_ps(rinvsix,rinvsix));
376             vvdw             = _mm256_sub_ps( _mm256_mul_ps(vvdw12,one_twelfth) , _mm256_mul_ps(vvdw6,one_sixth) );
377             fvdw             = _mm256_mul_ps(_mm256_sub_ps(vvdw12,vvdw6),rinvsq00);
378
379             /* Update potential sum for this i atom from the interaction with this j atom. */
380             velec            = _mm256_andnot_ps(dummy_mask,velec);
381             velecsum         = _mm256_add_ps(velecsum,velec);
382             vvdw             = _mm256_andnot_ps(dummy_mask,vvdw);
383             vvdwsum          = _mm256_add_ps(vvdwsum,vvdw);
384
385             fscal            = _mm256_add_ps(felec,fvdw);
386
387             fscal            = _mm256_andnot_ps(dummy_mask,fscal);
388
389             /* Calculate temporary vectorial force */
390             tx               = _mm256_mul_ps(fscal,dx00);
391             ty               = _mm256_mul_ps(fscal,dy00);
392             tz               = _mm256_mul_ps(fscal,dz00);
393
394             /* Update vectorial force */
395             fix0             = _mm256_add_ps(fix0,tx);
396             fiy0             = _mm256_add_ps(fiy0,ty);
397             fiz0             = _mm256_add_ps(fiz0,tz);
398
399             fjptrA             = (jnrlistA>=0) ? f+j_coord_offsetA : scratch;
400             fjptrB             = (jnrlistB>=0) ? f+j_coord_offsetB : scratch;
401             fjptrC             = (jnrlistC>=0) ? f+j_coord_offsetC : scratch;
402             fjptrD             = (jnrlistD>=0) ? f+j_coord_offsetD : scratch;
403             fjptrE             = (jnrlistE>=0) ? f+j_coord_offsetE : scratch;
404             fjptrF             = (jnrlistF>=0) ? f+j_coord_offsetF : scratch;
405             fjptrG             = (jnrlistG>=0) ? f+j_coord_offsetG : scratch;
406             fjptrH             = (jnrlistH>=0) ? f+j_coord_offsetH : scratch;
407             gmx_mm256_decrement_1rvec_8ptr_swizzle_ps(fjptrA,fjptrB,fjptrC,fjptrD,fjptrE,fjptrF,fjptrG,fjptrH,tx,ty,tz);
408
409             /* Inner loop uses 39 flops */
410         }
411
412         /* End of innermost loop */
413
414         gmx_mm256_update_iforce_1atom_swizzle_ps(fix0,fiy0,fiz0,
415                                                  f+i_coord_offset,fshift+i_shift_offset);
416
417         ggid                        = gid[iidx];
418         /* Update potential energies */
419         gmx_mm256_update_1pot_ps(velecsum,kernel_data->energygrp_elec+ggid);
420         gmx_mm256_update_1pot_ps(vvdwsum,kernel_data->energygrp_vdw+ggid);
421
422         /* Increment number of inner iterations */
423         inneriter                  += j_index_end - j_index_start;
424
425         /* Outer loop uses 9 flops */
426     }
427
428     /* Increment number of outer iterations */
429     outeriter        += nri;
430
431     /* Update outer/inner flops */
432
433     inc_nrnb(nrnb,eNR_NBKERNEL_ELEC_VDW_VF,outeriter*9 + inneriter*39);
434 }
435 /*
436  * Gromacs nonbonded kernel:   nb_kernel_ElecCoul_VdwLJ_GeomP1P1_F_avx_256_single
437  * Electrostatics interaction: Coulomb
438  * VdW interaction:            LennardJones
439  * Geometry:                   Particle-Particle
440  * Calculate force/pot:        Force
441  */
442 void
443 nb_kernel_ElecCoul_VdwLJ_GeomP1P1_F_avx_256_single
444                     (t_nblist                    * gmx_restrict       nlist,
445                      rvec                        * gmx_restrict          xx,
446                      rvec                        * gmx_restrict          ff,
447                      t_forcerec                  * gmx_restrict          fr,
448                      t_mdatoms                   * gmx_restrict     mdatoms,
449                      nb_kernel_data_t gmx_unused * gmx_restrict kernel_data,
450                      t_nrnb                      * gmx_restrict        nrnb)
451 {
452     /* Suffixes 0,1,2,3 refer to particle indices for waters in the inner or outer loop, or 
453      * just 0 for non-waters.
454      * Suffixes A,B,C,D,E,F,G,H refer to j loop unrolling done with AVX, e.g. for the eight different
455      * jnr indices corresponding to data put in the four positions in the SIMD register.
456      */
457     int              i_shift_offset,i_coord_offset,outeriter,inneriter;
458     int              j_index_start,j_index_end,jidx,nri,inr,ggid,iidx;
459     int              jnrA,jnrB,jnrC,jnrD;
460     int              jnrE,jnrF,jnrG,jnrH;
461     int              jnrlistA,jnrlistB,jnrlistC,jnrlistD;
462     int              jnrlistE,jnrlistF,jnrlistG,jnrlistH;
463     int              j_coord_offsetA,j_coord_offsetB,j_coord_offsetC,j_coord_offsetD;
464     int              j_coord_offsetE,j_coord_offsetF,j_coord_offsetG,j_coord_offsetH;
465     int              *iinr,*jindex,*jjnr,*shiftidx,*gid;
466     real             rcutoff_scalar;
467     real             *shiftvec,*fshift,*x,*f;
468     real             *fjptrA,*fjptrB,*fjptrC,*fjptrD,*fjptrE,*fjptrF,*fjptrG,*fjptrH;
469     real             scratch[4*DIM];
470     __m256           tx,ty,tz,fscal,rcutoff,rcutoff2,jidxall;
471     real *           vdwioffsetptr0;
472     __m256           ix0,iy0,iz0,fix0,fiy0,fiz0,iq0,isai0;
473     int              vdwjidx0A,vdwjidx0B,vdwjidx0C,vdwjidx0D,vdwjidx0E,vdwjidx0F,vdwjidx0G,vdwjidx0H;
474     __m256           jx0,jy0,jz0,fjx0,fjy0,fjz0,jq0,isaj0;
475     __m256           dx00,dy00,dz00,rsq00,rinv00,rinvsq00,r00,qq00,c6_00,c12_00;
476     __m256           velec,felec,velecsum,facel,crf,krf,krf2;
477     real             *charge;
478     int              nvdwtype;
479     __m256           rinvsix,rvdw,vvdw,vvdw6,vvdw12,fvdw,fvdw6,fvdw12,vvdwsum,sh_vdw_invrcut6;
480     int              *vdwtype;
481     real             *vdwparam;
482     __m256           one_sixth   = _mm256_set1_ps(1.0/6.0);
483     __m256           one_twelfth = _mm256_set1_ps(1.0/12.0);
484     __m256           dummy_mask,cutoff_mask;
485     __m256           signbit = _mm256_castsi256_ps( _mm256_set1_epi32(0x80000000) );
486     __m256           one     = _mm256_set1_ps(1.0);
487     __m256           two     = _mm256_set1_ps(2.0);
488     x                = xx[0];
489     f                = ff[0];
490
491     nri              = nlist->nri;
492     iinr             = nlist->iinr;
493     jindex           = nlist->jindex;
494     jjnr             = nlist->jjnr;
495     shiftidx         = nlist->shift;
496     gid              = nlist->gid;
497     shiftvec         = fr->shift_vec[0];
498     fshift           = fr->fshift[0];
499     facel            = _mm256_set1_ps(fr->epsfac);
500     charge           = mdatoms->chargeA;
501     nvdwtype         = fr->ntype;
502     vdwparam         = fr->nbfp;
503     vdwtype          = mdatoms->typeA;
504
505     /* Avoid stupid compiler warnings */
506     jnrA = jnrB = jnrC = jnrD = jnrE = jnrF = jnrG = jnrH = 0;
507     j_coord_offsetA = 0;
508     j_coord_offsetB = 0;
509     j_coord_offsetC = 0;
510     j_coord_offsetD = 0;
511     j_coord_offsetE = 0;
512     j_coord_offsetF = 0;
513     j_coord_offsetG = 0;
514     j_coord_offsetH = 0;
515
516     outeriter        = 0;
517     inneriter        = 0;
518
519     for(iidx=0;iidx<4*DIM;iidx++)
520     {
521         scratch[iidx] = 0.0;
522     }
523
524     /* Start outer loop over neighborlists */
525     for(iidx=0; iidx<nri; iidx++)
526     {
527         /* Load shift vector for this list */
528         i_shift_offset   = DIM*shiftidx[iidx];
529
530         /* Load limits for loop over neighbors */
531         j_index_start    = jindex[iidx];
532         j_index_end      = jindex[iidx+1];
533
534         /* Get outer coordinate index */
535         inr              = iinr[iidx];
536         i_coord_offset   = DIM*inr;
537
538         /* Load i particle coords and add shift vector */
539         gmx_mm256_load_shift_and_1rvec_broadcast_ps(shiftvec+i_shift_offset,x+i_coord_offset,&ix0,&iy0,&iz0);
540
541         fix0             = _mm256_setzero_ps();
542         fiy0             = _mm256_setzero_ps();
543         fiz0             = _mm256_setzero_ps();
544
545         /* Load parameters for i particles */
546         iq0              = _mm256_mul_ps(facel,_mm256_set1_ps(charge[inr+0]));
547         vdwioffsetptr0   = vdwparam+2*nvdwtype*vdwtype[inr+0];
548
549         /* Start inner kernel loop */
550         for(jidx=j_index_start; jidx<j_index_end && jjnr[jidx+7]>=0; jidx+=8)
551         {
552
553             /* Get j neighbor index, and coordinate index */
554             jnrA             = jjnr[jidx];
555             jnrB             = jjnr[jidx+1];
556             jnrC             = jjnr[jidx+2];
557             jnrD             = jjnr[jidx+3];
558             jnrE             = jjnr[jidx+4];
559             jnrF             = jjnr[jidx+5];
560             jnrG             = jjnr[jidx+6];
561             jnrH             = jjnr[jidx+7];
562             j_coord_offsetA  = DIM*jnrA;
563             j_coord_offsetB  = DIM*jnrB;
564             j_coord_offsetC  = DIM*jnrC;
565             j_coord_offsetD  = DIM*jnrD;
566             j_coord_offsetE  = DIM*jnrE;
567             j_coord_offsetF  = DIM*jnrF;
568             j_coord_offsetG  = DIM*jnrG;
569             j_coord_offsetH  = DIM*jnrH;
570
571             /* load j atom coordinates */
572             gmx_mm256_load_1rvec_8ptr_swizzle_ps(x+j_coord_offsetA,x+j_coord_offsetB,
573                                                  x+j_coord_offsetC,x+j_coord_offsetD,
574                                                  x+j_coord_offsetE,x+j_coord_offsetF,
575                                                  x+j_coord_offsetG,x+j_coord_offsetH,
576                                                  &jx0,&jy0,&jz0);
577
578             /* Calculate displacement vector */
579             dx00             = _mm256_sub_ps(ix0,jx0);
580             dy00             = _mm256_sub_ps(iy0,jy0);
581             dz00             = _mm256_sub_ps(iz0,jz0);
582
583             /* Calculate squared distance and things based on it */
584             rsq00            = gmx_mm256_calc_rsq_ps(dx00,dy00,dz00);
585
586             rinv00           = gmx_mm256_invsqrt_ps(rsq00);
587
588             rinvsq00         = _mm256_mul_ps(rinv00,rinv00);
589
590             /* Load parameters for j particles */
591             jq0              = gmx_mm256_load_8real_swizzle_ps(charge+jnrA+0,charge+jnrB+0,
592                                                                  charge+jnrC+0,charge+jnrD+0,
593                                                                  charge+jnrE+0,charge+jnrF+0,
594                                                                  charge+jnrG+0,charge+jnrH+0);
595             vdwjidx0A        = 2*vdwtype[jnrA+0];
596             vdwjidx0B        = 2*vdwtype[jnrB+0];
597             vdwjidx0C        = 2*vdwtype[jnrC+0];
598             vdwjidx0D        = 2*vdwtype[jnrD+0];
599             vdwjidx0E        = 2*vdwtype[jnrE+0];
600             vdwjidx0F        = 2*vdwtype[jnrF+0];
601             vdwjidx0G        = 2*vdwtype[jnrG+0];
602             vdwjidx0H        = 2*vdwtype[jnrH+0];
603
604             /**************************
605              * CALCULATE INTERACTIONS *
606              **************************/
607
608             /* Compute parameters for interactions between i and j atoms */
609             qq00             = _mm256_mul_ps(iq0,jq0);
610             gmx_mm256_load_8pair_swizzle_ps(vdwioffsetptr0+vdwjidx0A,
611                                             vdwioffsetptr0+vdwjidx0B,
612                                             vdwioffsetptr0+vdwjidx0C,
613                                             vdwioffsetptr0+vdwjidx0D,
614                                             vdwioffsetptr0+vdwjidx0E,
615                                             vdwioffsetptr0+vdwjidx0F,
616                                             vdwioffsetptr0+vdwjidx0G,
617                                             vdwioffsetptr0+vdwjidx0H,
618                                             &c6_00,&c12_00);
619
620             /* COULOMB ELECTROSTATICS */
621             velec            = _mm256_mul_ps(qq00,rinv00);
622             felec            = _mm256_mul_ps(velec,rinvsq00);
623
624             /* LENNARD-JONES DISPERSION/REPULSION */
625
626             rinvsix          = _mm256_mul_ps(_mm256_mul_ps(rinvsq00,rinvsq00),rinvsq00);
627             fvdw             = _mm256_mul_ps(_mm256_sub_ps(_mm256_mul_ps(c12_00,rinvsix),c6_00),_mm256_mul_ps(rinvsix,rinvsq00));
628
629             fscal            = _mm256_add_ps(felec,fvdw);
630
631             /* Calculate temporary vectorial force */
632             tx               = _mm256_mul_ps(fscal,dx00);
633             ty               = _mm256_mul_ps(fscal,dy00);
634             tz               = _mm256_mul_ps(fscal,dz00);
635
636             /* Update vectorial force */
637             fix0             = _mm256_add_ps(fix0,tx);
638             fiy0             = _mm256_add_ps(fiy0,ty);
639             fiz0             = _mm256_add_ps(fiz0,tz);
640
641             fjptrA             = f+j_coord_offsetA;
642             fjptrB             = f+j_coord_offsetB;
643             fjptrC             = f+j_coord_offsetC;
644             fjptrD             = f+j_coord_offsetD;
645             fjptrE             = f+j_coord_offsetE;
646             fjptrF             = f+j_coord_offsetF;
647             fjptrG             = f+j_coord_offsetG;
648             fjptrH             = f+j_coord_offsetH;
649             gmx_mm256_decrement_1rvec_8ptr_swizzle_ps(fjptrA,fjptrB,fjptrC,fjptrD,fjptrE,fjptrF,fjptrG,fjptrH,tx,ty,tz);
650
651             /* Inner loop uses 33 flops */
652         }
653
654         if(jidx<j_index_end)
655         {
656
657             /* Get j neighbor index, and coordinate index */
658             jnrlistA         = jjnr[jidx];
659             jnrlistB         = jjnr[jidx+1];
660             jnrlistC         = jjnr[jidx+2];
661             jnrlistD         = jjnr[jidx+3];
662             jnrlistE         = jjnr[jidx+4];
663             jnrlistF         = jjnr[jidx+5];
664             jnrlistG         = jjnr[jidx+6];
665             jnrlistH         = jjnr[jidx+7];
666             /* Sign of each element will be negative for non-real atoms.
667              * This mask will be 0xFFFFFFFF for dummy entries and 0x0 for real ones,
668              * so use it as val = _mm_andnot_ps(mask,val) to clear dummy entries.
669              */
670             dummy_mask = gmx_mm256_set_m128(gmx_mm_castsi128_ps(_mm_cmplt_epi32(_mm_loadu_si128((const __m128i *)(jjnr+jidx+4)),_mm_setzero_si128())),
671                                             gmx_mm_castsi128_ps(_mm_cmplt_epi32(_mm_loadu_si128((const __m128i *)(jjnr+jidx)),_mm_setzero_si128())));
672                                             
673             jnrA       = (jnrlistA>=0) ? jnrlistA : 0;
674             jnrB       = (jnrlistB>=0) ? jnrlistB : 0;
675             jnrC       = (jnrlistC>=0) ? jnrlistC : 0;
676             jnrD       = (jnrlistD>=0) ? jnrlistD : 0;
677             jnrE       = (jnrlistE>=0) ? jnrlistE : 0;
678             jnrF       = (jnrlistF>=0) ? jnrlistF : 0;
679             jnrG       = (jnrlistG>=0) ? jnrlistG : 0;
680             jnrH       = (jnrlistH>=0) ? jnrlistH : 0;
681             j_coord_offsetA  = DIM*jnrA;
682             j_coord_offsetB  = DIM*jnrB;
683             j_coord_offsetC  = DIM*jnrC;
684             j_coord_offsetD  = DIM*jnrD;
685             j_coord_offsetE  = DIM*jnrE;
686             j_coord_offsetF  = DIM*jnrF;
687             j_coord_offsetG  = DIM*jnrG;
688             j_coord_offsetH  = DIM*jnrH;
689
690             /* load j atom coordinates */
691             gmx_mm256_load_1rvec_8ptr_swizzle_ps(x+j_coord_offsetA,x+j_coord_offsetB,
692                                                  x+j_coord_offsetC,x+j_coord_offsetD,
693                                                  x+j_coord_offsetE,x+j_coord_offsetF,
694                                                  x+j_coord_offsetG,x+j_coord_offsetH,
695                                                  &jx0,&jy0,&jz0);
696
697             /* Calculate displacement vector */
698             dx00             = _mm256_sub_ps(ix0,jx0);
699             dy00             = _mm256_sub_ps(iy0,jy0);
700             dz00             = _mm256_sub_ps(iz0,jz0);
701
702             /* Calculate squared distance and things based on it */
703             rsq00            = gmx_mm256_calc_rsq_ps(dx00,dy00,dz00);
704
705             rinv00           = gmx_mm256_invsqrt_ps(rsq00);
706
707             rinvsq00         = _mm256_mul_ps(rinv00,rinv00);
708
709             /* Load parameters for j particles */
710             jq0              = gmx_mm256_load_8real_swizzle_ps(charge+jnrA+0,charge+jnrB+0,
711                                                                  charge+jnrC+0,charge+jnrD+0,
712                                                                  charge+jnrE+0,charge+jnrF+0,
713                                                                  charge+jnrG+0,charge+jnrH+0);
714             vdwjidx0A        = 2*vdwtype[jnrA+0];
715             vdwjidx0B        = 2*vdwtype[jnrB+0];
716             vdwjidx0C        = 2*vdwtype[jnrC+0];
717             vdwjidx0D        = 2*vdwtype[jnrD+0];
718             vdwjidx0E        = 2*vdwtype[jnrE+0];
719             vdwjidx0F        = 2*vdwtype[jnrF+0];
720             vdwjidx0G        = 2*vdwtype[jnrG+0];
721             vdwjidx0H        = 2*vdwtype[jnrH+0];
722
723             /**************************
724              * CALCULATE INTERACTIONS *
725              **************************/
726
727             /* Compute parameters for interactions between i and j atoms */
728             qq00             = _mm256_mul_ps(iq0,jq0);
729             gmx_mm256_load_8pair_swizzle_ps(vdwioffsetptr0+vdwjidx0A,
730                                             vdwioffsetptr0+vdwjidx0B,
731                                             vdwioffsetptr0+vdwjidx0C,
732                                             vdwioffsetptr0+vdwjidx0D,
733                                             vdwioffsetptr0+vdwjidx0E,
734                                             vdwioffsetptr0+vdwjidx0F,
735                                             vdwioffsetptr0+vdwjidx0G,
736                                             vdwioffsetptr0+vdwjidx0H,
737                                             &c6_00,&c12_00);
738
739             /* COULOMB ELECTROSTATICS */
740             velec            = _mm256_mul_ps(qq00,rinv00);
741             felec            = _mm256_mul_ps(velec,rinvsq00);
742
743             /* LENNARD-JONES DISPERSION/REPULSION */
744
745             rinvsix          = _mm256_mul_ps(_mm256_mul_ps(rinvsq00,rinvsq00),rinvsq00);
746             fvdw             = _mm256_mul_ps(_mm256_sub_ps(_mm256_mul_ps(c12_00,rinvsix),c6_00),_mm256_mul_ps(rinvsix,rinvsq00));
747
748             fscal            = _mm256_add_ps(felec,fvdw);
749
750             fscal            = _mm256_andnot_ps(dummy_mask,fscal);
751
752             /* Calculate temporary vectorial force */
753             tx               = _mm256_mul_ps(fscal,dx00);
754             ty               = _mm256_mul_ps(fscal,dy00);
755             tz               = _mm256_mul_ps(fscal,dz00);
756
757             /* Update vectorial force */
758             fix0             = _mm256_add_ps(fix0,tx);
759             fiy0             = _mm256_add_ps(fiy0,ty);
760             fiz0             = _mm256_add_ps(fiz0,tz);
761
762             fjptrA             = (jnrlistA>=0) ? f+j_coord_offsetA : scratch;
763             fjptrB             = (jnrlistB>=0) ? f+j_coord_offsetB : scratch;
764             fjptrC             = (jnrlistC>=0) ? f+j_coord_offsetC : scratch;
765             fjptrD             = (jnrlistD>=0) ? f+j_coord_offsetD : scratch;
766             fjptrE             = (jnrlistE>=0) ? f+j_coord_offsetE : scratch;
767             fjptrF             = (jnrlistF>=0) ? f+j_coord_offsetF : scratch;
768             fjptrG             = (jnrlistG>=0) ? f+j_coord_offsetG : scratch;
769             fjptrH             = (jnrlistH>=0) ? f+j_coord_offsetH : scratch;
770             gmx_mm256_decrement_1rvec_8ptr_swizzle_ps(fjptrA,fjptrB,fjptrC,fjptrD,fjptrE,fjptrF,fjptrG,fjptrH,tx,ty,tz);
771
772             /* Inner loop uses 33 flops */
773         }
774
775         /* End of innermost loop */
776
777         gmx_mm256_update_iforce_1atom_swizzle_ps(fix0,fiy0,fiz0,
778                                                  f+i_coord_offset,fshift+i_shift_offset);
779
780         /* Increment number of inner iterations */
781         inneriter                  += j_index_end - j_index_start;
782
783         /* Outer loop uses 7 flops */
784     }
785
786     /* Increment number of outer iterations */
787     outeriter        += nri;
788
789     /* Update outer/inner flops */
790
791     inc_nrnb(nrnb,eNR_NBKERNEL_ELEC_VDW_F,outeriter*7 + inneriter*33);
792 }