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