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