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