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