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