Introduce gmxpre.h for truly global definitions
[alexxy/gromacs.git] / src / gromacs / gmxlib / nonbonded / nb_kernel_avx_128_fma_single / nb_kernel_ElecCoul_VdwNone_GeomW3P1_avx_128_fma_single.c
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2012,2013,2014, by the GROMACS development team, led by
5  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6  * and including many others, as listed in the AUTHORS file in the
7  * top-level source directory and at http://www.gromacs.org.
8  *
9  * GROMACS is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public License
11  * as published by the Free Software Foundation; either version 2.1
12  * of the License, or (at your option) any later version.
13  *
14  * GROMACS is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with GROMACS; if not, see
21  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
23  *
24  * If you want to redistribute modifications to GROMACS, please
25  * consider that scientific software is very special. Version
26  * control is crucial - bugs must be traceable. We will be happy to
27  * consider code for inclusion in the official distribution, but
28  * derived work must not be called official GROMACS. Details are found
29  * in the README & COPYING files - if they are missing, get the
30  * official version at http://www.gromacs.org.
31  *
32  * To help us fund GROMACS development, we humbly ask that you cite
33  * the research papers on the package. Check out http://www.gromacs.org.
34  */
35 /*
36  * Note: this file was generated by the GROMACS avx_128_fma_single kernel generator.
37  */
38 #include "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_avx_128_fma_single.h"
50 #include "kernelutil_x86_avx_128_fma_single.h"
51
52 /*
53  * Gromacs nonbonded kernel:   nb_kernel_ElecCoul_VdwNone_GeomW3P1_VF_avx_128_fma_single
54  * Electrostatics interaction: Coulomb
55  * VdW interaction:            None
56  * Geometry:                   Water3-Particle
57  * Calculate force/pot:        PotentialAndForce
58  */
59 void
60 nb_kernel_ElecCoul_VdwNone_GeomW3P1_VF_avx_128_fma_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 AVX_128, 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           fscal,rcutoff,rcutoff2,jidxall;
85     int              vdwioffset0;
86     __m128           ix0,iy0,iz0,fix0,fiy0,fiz0,iq0,isai0;
87     int              vdwioffset1;
88     __m128           ix1,iy1,iz1,fix1,fiy1,fiz1,iq1,isai1;
89     int              vdwioffset2;
90     __m128           ix2,iy2,iz2,fix2,fiy2,fiz2,iq2,isai2;
91     int              vdwjidx0A,vdwjidx0B,vdwjidx0C,vdwjidx0D;
92     __m128           jx0,jy0,jz0,fjx0,fjy0,fjz0,jq0,isaj0;
93     __m128           dx00,dy00,dz00,rsq00,rinv00,rinvsq00,r00,qq00,c6_00,c12_00;
94     __m128           dx10,dy10,dz10,rsq10,rinv10,rinvsq10,r10,qq10,c6_10,c12_10;
95     __m128           dx20,dy20,dz20,rsq20,rinv20,rinvsq20,r20,qq20,c6_20,c12_20;
96     __m128           velec,felec,velecsum,facel,crf,krf,krf2;
97     real             *charge;
98     __m128           dummy_mask,cutoff_mask;
99     __m128           signbit = _mm_castsi128_ps( _mm_set1_epi32(0x80000000) );
100     __m128           one     = _mm_set1_ps(1.0);
101     __m128           two     = _mm_set1_ps(2.0);
102     x                = xx[0];
103     f                = ff[0];
104
105     nri              = nlist->nri;
106     iinr             = nlist->iinr;
107     jindex           = nlist->jindex;
108     jjnr             = nlist->jjnr;
109     shiftidx         = nlist->shift;
110     gid              = nlist->gid;
111     shiftvec         = fr->shift_vec[0];
112     fshift           = fr->fshift[0];
113     facel            = _mm_set1_ps(fr->epsfac);
114     charge           = mdatoms->chargeA;
115
116     /* Setup water-specific parameters */
117     inr              = nlist->iinr[0];
118     iq0              = _mm_mul_ps(facel,_mm_set1_ps(charge[inr+0]));
119     iq1              = _mm_mul_ps(facel,_mm_set1_ps(charge[inr+1]));
120     iq2              = _mm_mul_ps(facel,_mm_set1_ps(charge[inr+2]));
121
122     /* Avoid stupid compiler warnings */
123     jnrA = jnrB = jnrC = jnrD = 0;
124     j_coord_offsetA = 0;
125     j_coord_offsetB = 0;
126     j_coord_offsetC = 0;
127     j_coord_offsetD = 0;
128
129     outeriter        = 0;
130     inneriter        = 0;
131
132     for(iidx=0;iidx<4*DIM;iidx++)
133     {
134         scratch[iidx] = 0.0;
135     }
136
137     /* Start outer loop over neighborlists */
138     for(iidx=0; iidx<nri; iidx++)
139     {
140         /* Load shift vector for this list */
141         i_shift_offset   = DIM*shiftidx[iidx];
142
143         /* Load limits for loop over neighbors */
144         j_index_start    = jindex[iidx];
145         j_index_end      = jindex[iidx+1];
146
147         /* Get outer coordinate index */
148         inr              = iinr[iidx];
149         i_coord_offset   = DIM*inr;
150
151         /* Load i particle coords and add shift vector */
152         gmx_mm_load_shift_and_3rvec_broadcast_ps(shiftvec+i_shift_offset,x+i_coord_offset,
153                                                  &ix0,&iy0,&iz0,&ix1,&iy1,&iz1,&ix2,&iy2,&iz2);
154
155         fix0             = _mm_setzero_ps();
156         fiy0             = _mm_setzero_ps();
157         fiz0             = _mm_setzero_ps();
158         fix1             = _mm_setzero_ps();
159         fiy1             = _mm_setzero_ps();
160         fiz1             = _mm_setzero_ps();
161         fix2             = _mm_setzero_ps();
162         fiy2             = _mm_setzero_ps();
163         fiz2             = _mm_setzero_ps();
164
165         /* Reset potential sums */
166         velecsum         = _mm_setzero_ps();
167
168         /* Start inner kernel loop */
169         for(jidx=j_index_start; jidx<j_index_end && jjnr[jidx+3]>=0; jidx+=4)
170         {
171
172             /* Get j neighbor index, and coordinate index */
173             jnrA             = jjnr[jidx];
174             jnrB             = jjnr[jidx+1];
175             jnrC             = jjnr[jidx+2];
176             jnrD             = jjnr[jidx+3];
177             j_coord_offsetA  = DIM*jnrA;
178             j_coord_offsetB  = DIM*jnrB;
179             j_coord_offsetC  = DIM*jnrC;
180             j_coord_offsetD  = DIM*jnrD;
181
182             /* load j atom coordinates */
183             gmx_mm_load_1rvec_4ptr_swizzle_ps(x+j_coord_offsetA,x+j_coord_offsetB,
184                                               x+j_coord_offsetC,x+j_coord_offsetD,
185                                               &jx0,&jy0,&jz0);
186
187             /* Calculate displacement vector */
188             dx00             = _mm_sub_ps(ix0,jx0);
189             dy00             = _mm_sub_ps(iy0,jy0);
190             dz00             = _mm_sub_ps(iz0,jz0);
191             dx10             = _mm_sub_ps(ix1,jx0);
192             dy10             = _mm_sub_ps(iy1,jy0);
193             dz10             = _mm_sub_ps(iz1,jz0);
194             dx20             = _mm_sub_ps(ix2,jx0);
195             dy20             = _mm_sub_ps(iy2,jy0);
196             dz20             = _mm_sub_ps(iz2,jz0);
197
198             /* Calculate squared distance and things based on it */
199             rsq00            = gmx_mm_calc_rsq_ps(dx00,dy00,dz00);
200             rsq10            = gmx_mm_calc_rsq_ps(dx10,dy10,dz10);
201             rsq20            = gmx_mm_calc_rsq_ps(dx20,dy20,dz20);
202
203             rinv00           = gmx_mm_invsqrt_ps(rsq00);
204             rinv10           = gmx_mm_invsqrt_ps(rsq10);
205             rinv20           = gmx_mm_invsqrt_ps(rsq20);
206
207             rinvsq00         = _mm_mul_ps(rinv00,rinv00);
208             rinvsq10         = _mm_mul_ps(rinv10,rinv10);
209             rinvsq20         = _mm_mul_ps(rinv20,rinv20);
210
211             /* Load parameters for j particles */
212             jq0              = gmx_mm_load_4real_swizzle_ps(charge+jnrA+0,charge+jnrB+0,
213                                                               charge+jnrC+0,charge+jnrD+0);
214
215             fjx0             = _mm_setzero_ps();
216             fjy0             = _mm_setzero_ps();
217             fjz0             = _mm_setzero_ps();
218
219             /**************************
220              * CALCULATE INTERACTIONS *
221              **************************/
222
223             /* Compute parameters for interactions between i and j atoms */
224             qq00             = _mm_mul_ps(iq0,jq0);
225
226             /* COULOMB ELECTROSTATICS */
227             velec            = _mm_mul_ps(qq00,rinv00);
228             felec            = _mm_mul_ps(velec,rinvsq00);
229
230             /* Update potential sum for this i atom from the interaction with this j atom. */
231             velecsum         = _mm_add_ps(velecsum,velec);
232
233             fscal            = felec;
234
235              /* Update vectorial force */
236             fix0             = _mm_macc_ps(dx00,fscal,fix0);
237             fiy0             = _mm_macc_ps(dy00,fscal,fiy0);
238             fiz0             = _mm_macc_ps(dz00,fscal,fiz0);
239
240             fjx0             = _mm_macc_ps(dx00,fscal,fjx0);
241             fjy0             = _mm_macc_ps(dy00,fscal,fjy0);
242             fjz0             = _mm_macc_ps(dz00,fscal,fjz0);
243
244             /**************************
245              * CALCULATE INTERACTIONS *
246              **************************/
247
248             /* Compute parameters for interactions between i and j atoms */
249             qq10             = _mm_mul_ps(iq1,jq0);
250
251             /* COULOMB ELECTROSTATICS */
252             velec            = _mm_mul_ps(qq10,rinv10);
253             felec            = _mm_mul_ps(velec,rinvsq10);
254
255             /* Update potential sum for this i atom from the interaction with this j atom. */
256             velecsum         = _mm_add_ps(velecsum,velec);
257
258             fscal            = felec;
259
260              /* Update vectorial force */
261             fix1             = _mm_macc_ps(dx10,fscal,fix1);
262             fiy1             = _mm_macc_ps(dy10,fscal,fiy1);
263             fiz1             = _mm_macc_ps(dz10,fscal,fiz1);
264
265             fjx0             = _mm_macc_ps(dx10,fscal,fjx0);
266             fjy0             = _mm_macc_ps(dy10,fscal,fjy0);
267             fjz0             = _mm_macc_ps(dz10,fscal,fjz0);
268
269             /**************************
270              * CALCULATE INTERACTIONS *
271              **************************/
272
273             /* Compute parameters for interactions between i and j atoms */
274             qq20             = _mm_mul_ps(iq2,jq0);
275
276             /* COULOMB ELECTROSTATICS */
277             velec            = _mm_mul_ps(qq20,rinv20);
278             felec            = _mm_mul_ps(velec,rinvsq20);
279
280             /* Update potential sum for this i atom from the interaction with this j atom. */
281             velecsum         = _mm_add_ps(velecsum,velec);
282
283             fscal            = felec;
284
285              /* Update vectorial force */
286             fix2             = _mm_macc_ps(dx20,fscal,fix2);
287             fiy2             = _mm_macc_ps(dy20,fscal,fiy2);
288             fiz2             = _mm_macc_ps(dz20,fscal,fiz2);
289
290             fjx0             = _mm_macc_ps(dx20,fscal,fjx0);
291             fjy0             = _mm_macc_ps(dy20,fscal,fjy0);
292             fjz0             = _mm_macc_ps(dz20,fscal,fjz0);
293
294             fjptrA             = f+j_coord_offsetA;
295             fjptrB             = f+j_coord_offsetB;
296             fjptrC             = f+j_coord_offsetC;
297             fjptrD             = f+j_coord_offsetD;
298
299             gmx_mm_decrement_1rvec_4ptr_swizzle_ps(fjptrA,fjptrB,fjptrC,fjptrD,fjx0,fjy0,fjz0);
300
301             /* Inner loop uses 93 flops */
302         }
303
304         if(jidx<j_index_end)
305         {
306
307             /* Get j neighbor index, and coordinate index */
308             jnrlistA         = jjnr[jidx];
309             jnrlistB         = jjnr[jidx+1];
310             jnrlistC         = jjnr[jidx+2];
311             jnrlistD         = jjnr[jidx+3];
312             /* Sign of each element will be negative for non-real atoms.
313              * This mask will be 0xFFFFFFFF for dummy entries and 0x0 for real ones,
314              * so use it as val = _mm_andnot_ps(mask,val) to clear dummy entries.
315              */
316             dummy_mask = gmx_mm_castsi128_ps(_mm_cmplt_epi32(_mm_loadu_si128((const __m128i *)(jjnr+jidx)),_mm_setzero_si128()));
317             jnrA       = (jnrlistA>=0) ? jnrlistA : 0;
318             jnrB       = (jnrlistB>=0) ? jnrlistB : 0;
319             jnrC       = (jnrlistC>=0) ? jnrlistC : 0;
320             jnrD       = (jnrlistD>=0) ? jnrlistD : 0;
321             j_coord_offsetA  = DIM*jnrA;
322             j_coord_offsetB  = DIM*jnrB;
323             j_coord_offsetC  = DIM*jnrC;
324             j_coord_offsetD  = DIM*jnrD;
325
326             /* load j atom coordinates */
327             gmx_mm_load_1rvec_4ptr_swizzle_ps(x+j_coord_offsetA,x+j_coord_offsetB,
328                                               x+j_coord_offsetC,x+j_coord_offsetD,
329                                               &jx0,&jy0,&jz0);
330
331             /* Calculate displacement vector */
332             dx00             = _mm_sub_ps(ix0,jx0);
333             dy00             = _mm_sub_ps(iy0,jy0);
334             dz00             = _mm_sub_ps(iz0,jz0);
335             dx10             = _mm_sub_ps(ix1,jx0);
336             dy10             = _mm_sub_ps(iy1,jy0);
337             dz10             = _mm_sub_ps(iz1,jz0);
338             dx20             = _mm_sub_ps(ix2,jx0);
339             dy20             = _mm_sub_ps(iy2,jy0);
340             dz20             = _mm_sub_ps(iz2,jz0);
341
342             /* Calculate squared distance and things based on it */
343             rsq00            = gmx_mm_calc_rsq_ps(dx00,dy00,dz00);
344             rsq10            = gmx_mm_calc_rsq_ps(dx10,dy10,dz10);
345             rsq20            = gmx_mm_calc_rsq_ps(dx20,dy20,dz20);
346
347             rinv00           = gmx_mm_invsqrt_ps(rsq00);
348             rinv10           = gmx_mm_invsqrt_ps(rsq10);
349             rinv20           = gmx_mm_invsqrt_ps(rsq20);
350
351             rinvsq00         = _mm_mul_ps(rinv00,rinv00);
352             rinvsq10         = _mm_mul_ps(rinv10,rinv10);
353             rinvsq20         = _mm_mul_ps(rinv20,rinv20);
354
355             /* Load parameters for j particles */
356             jq0              = gmx_mm_load_4real_swizzle_ps(charge+jnrA+0,charge+jnrB+0,
357                                                               charge+jnrC+0,charge+jnrD+0);
358
359             fjx0             = _mm_setzero_ps();
360             fjy0             = _mm_setzero_ps();
361             fjz0             = _mm_setzero_ps();
362
363             /**************************
364              * CALCULATE INTERACTIONS *
365              **************************/
366
367             /* Compute parameters for interactions between i and j atoms */
368             qq00             = _mm_mul_ps(iq0,jq0);
369
370             /* COULOMB ELECTROSTATICS */
371             velec            = _mm_mul_ps(qq00,rinv00);
372             felec            = _mm_mul_ps(velec,rinvsq00);
373
374             /* Update potential sum for this i atom from the interaction with this j atom. */
375             velec            = _mm_andnot_ps(dummy_mask,velec);
376             velecsum         = _mm_add_ps(velecsum,velec);
377
378             fscal            = felec;
379
380             fscal            = _mm_andnot_ps(dummy_mask,fscal);
381
382              /* Update vectorial force */
383             fix0             = _mm_macc_ps(dx00,fscal,fix0);
384             fiy0             = _mm_macc_ps(dy00,fscal,fiy0);
385             fiz0             = _mm_macc_ps(dz00,fscal,fiz0);
386
387             fjx0             = _mm_macc_ps(dx00,fscal,fjx0);
388             fjy0             = _mm_macc_ps(dy00,fscal,fjy0);
389             fjz0             = _mm_macc_ps(dz00,fscal,fjz0);
390
391             /**************************
392              * CALCULATE INTERACTIONS *
393              **************************/
394
395             /* Compute parameters for interactions between i and j atoms */
396             qq10             = _mm_mul_ps(iq1,jq0);
397
398             /* COULOMB ELECTROSTATICS */
399             velec            = _mm_mul_ps(qq10,rinv10);
400             felec            = _mm_mul_ps(velec,rinvsq10);
401
402             /* Update potential sum for this i atom from the interaction with this j atom. */
403             velec            = _mm_andnot_ps(dummy_mask,velec);
404             velecsum         = _mm_add_ps(velecsum,velec);
405
406             fscal            = felec;
407
408             fscal            = _mm_andnot_ps(dummy_mask,fscal);
409
410              /* Update vectorial force */
411             fix1             = _mm_macc_ps(dx10,fscal,fix1);
412             fiy1             = _mm_macc_ps(dy10,fscal,fiy1);
413             fiz1             = _mm_macc_ps(dz10,fscal,fiz1);
414
415             fjx0             = _mm_macc_ps(dx10,fscal,fjx0);
416             fjy0             = _mm_macc_ps(dy10,fscal,fjy0);
417             fjz0             = _mm_macc_ps(dz10,fscal,fjz0);
418
419             /**************************
420              * CALCULATE INTERACTIONS *
421              **************************/
422
423             /* Compute parameters for interactions between i and j atoms */
424             qq20             = _mm_mul_ps(iq2,jq0);
425
426             /* COULOMB ELECTROSTATICS */
427             velec            = _mm_mul_ps(qq20,rinv20);
428             felec            = _mm_mul_ps(velec,rinvsq20);
429
430             /* Update potential sum for this i atom from the interaction with this j atom. */
431             velec            = _mm_andnot_ps(dummy_mask,velec);
432             velecsum         = _mm_add_ps(velecsum,velec);
433
434             fscal            = felec;
435
436             fscal            = _mm_andnot_ps(dummy_mask,fscal);
437
438              /* Update vectorial force */
439             fix2             = _mm_macc_ps(dx20,fscal,fix2);
440             fiy2             = _mm_macc_ps(dy20,fscal,fiy2);
441             fiz2             = _mm_macc_ps(dz20,fscal,fiz2);
442
443             fjx0             = _mm_macc_ps(dx20,fscal,fjx0);
444             fjy0             = _mm_macc_ps(dy20,fscal,fjy0);
445             fjz0             = _mm_macc_ps(dz20,fscal,fjz0);
446
447             fjptrA             = (jnrlistA>=0) ? f+j_coord_offsetA : scratch;
448             fjptrB             = (jnrlistB>=0) ? f+j_coord_offsetB : scratch;
449             fjptrC             = (jnrlistC>=0) ? f+j_coord_offsetC : scratch;
450             fjptrD             = (jnrlistD>=0) ? f+j_coord_offsetD : scratch;
451
452             gmx_mm_decrement_1rvec_4ptr_swizzle_ps(fjptrA,fjptrB,fjptrC,fjptrD,fjx0,fjy0,fjz0);
453
454             /* Inner loop uses 93 flops */
455         }
456
457         /* End of innermost loop */
458
459         gmx_mm_update_iforce_3atom_swizzle_ps(fix0,fiy0,fiz0,fix1,fiy1,fiz1,fix2,fiy2,fiz2,
460                                               f+i_coord_offset,fshift+i_shift_offset);
461
462         ggid                        = gid[iidx];
463         /* Update potential energies */
464         gmx_mm_update_1pot_ps(velecsum,kernel_data->energygrp_elec+ggid);
465
466         /* Increment number of inner iterations */
467         inneriter                  += j_index_end - j_index_start;
468
469         /* Outer loop uses 19 flops */
470     }
471
472     /* Increment number of outer iterations */
473     outeriter        += nri;
474
475     /* Update outer/inner flops */
476
477     inc_nrnb(nrnb,eNR_NBKERNEL_ELEC_W3_VF,outeriter*19 + inneriter*93);
478 }
479 /*
480  * Gromacs nonbonded kernel:   nb_kernel_ElecCoul_VdwNone_GeomW3P1_F_avx_128_fma_single
481  * Electrostatics interaction: Coulomb
482  * VdW interaction:            None
483  * Geometry:                   Water3-Particle
484  * Calculate force/pot:        Force
485  */
486 void
487 nb_kernel_ElecCoul_VdwNone_GeomW3P1_F_avx_128_fma_single
488                     (t_nblist                    * gmx_restrict       nlist,
489                      rvec                        * gmx_restrict          xx,
490                      rvec                        * gmx_restrict          ff,
491                      t_forcerec                  * gmx_restrict          fr,
492                      t_mdatoms                   * gmx_restrict     mdatoms,
493                      nb_kernel_data_t gmx_unused * gmx_restrict kernel_data,
494                      t_nrnb                      * gmx_restrict        nrnb)
495 {
496     /* Suffixes 0,1,2,3 refer to particle indices for waters in the inner or outer loop, or
497      * just 0 for non-waters.
498      * Suffixes A,B,C,D refer to j loop unrolling done with AVX_128, e.g. for the four different
499      * jnr indices corresponding to data put in the four positions in the SIMD register.
500      */
501     int              i_shift_offset,i_coord_offset,outeriter,inneriter;
502     int              j_index_start,j_index_end,jidx,nri,inr,ggid,iidx;
503     int              jnrA,jnrB,jnrC,jnrD;
504     int              jnrlistA,jnrlistB,jnrlistC,jnrlistD;
505     int              j_coord_offsetA,j_coord_offsetB,j_coord_offsetC,j_coord_offsetD;
506     int              *iinr,*jindex,*jjnr,*shiftidx,*gid;
507     real             rcutoff_scalar;
508     real             *shiftvec,*fshift,*x,*f;
509     real             *fjptrA,*fjptrB,*fjptrC,*fjptrD;
510     real             scratch[4*DIM];
511     __m128           fscal,rcutoff,rcutoff2,jidxall;
512     int              vdwioffset0;
513     __m128           ix0,iy0,iz0,fix0,fiy0,fiz0,iq0,isai0;
514     int              vdwioffset1;
515     __m128           ix1,iy1,iz1,fix1,fiy1,fiz1,iq1,isai1;
516     int              vdwioffset2;
517     __m128           ix2,iy2,iz2,fix2,fiy2,fiz2,iq2,isai2;
518     int              vdwjidx0A,vdwjidx0B,vdwjidx0C,vdwjidx0D;
519     __m128           jx0,jy0,jz0,fjx0,fjy0,fjz0,jq0,isaj0;
520     __m128           dx00,dy00,dz00,rsq00,rinv00,rinvsq00,r00,qq00,c6_00,c12_00;
521     __m128           dx10,dy10,dz10,rsq10,rinv10,rinvsq10,r10,qq10,c6_10,c12_10;
522     __m128           dx20,dy20,dz20,rsq20,rinv20,rinvsq20,r20,qq20,c6_20,c12_20;
523     __m128           velec,felec,velecsum,facel,crf,krf,krf2;
524     real             *charge;
525     __m128           dummy_mask,cutoff_mask;
526     __m128           signbit = _mm_castsi128_ps( _mm_set1_epi32(0x80000000) );
527     __m128           one     = _mm_set1_ps(1.0);
528     __m128           two     = _mm_set1_ps(2.0);
529     x                = xx[0];
530     f                = ff[0];
531
532     nri              = nlist->nri;
533     iinr             = nlist->iinr;
534     jindex           = nlist->jindex;
535     jjnr             = nlist->jjnr;
536     shiftidx         = nlist->shift;
537     gid              = nlist->gid;
538     shiftvec         = fr->shift_vec[0];
539     fshift           = fr->fshift[0];
540     facel            = _mm_set1_ps(fr->epsfac);
541     charge           = mdatoms->chargeA;
542
543     /* Setup water-specific parameters */
544     inr              = nlist->iinr[0];
545     iq0              = _mm_mul_ps(facel,_mm_set1_ps(charge[inr+0]));
546     iq1              = _mm_mul_ps(facel,_mm_set1_ps(charge[inr+1]));
547     iq2              = _mm_mul_ps(facel,_mm_set1_ps(charge[inr+2]));
548
549     /* Avoid stupid compiler warnings */
550     jnrA = jnrB = jnrC = jnrD = 0;
551     j_coord_offsetA = 0;
552     j_coord_offsetB = 0;
553     j_coord_offsetC = 0;
554     j_coord_offsetD = 0;
555
556     outeriter        = 0;
557     inneriter        = 0;
558
559     for(iidx=0;iidx<4*DIM;iidx++)
560     {
561         scratch[iidx] = 0.0;
562     }
563
564     /* Start outer loop over neighborlists */
565     for(iidx=0; iidx<nri; iidx++)
566     {
567         /* Load shift vector for this list */
568         i_shift_offset   = DIM*shiftidx[iidx];
569
570         /* Load limits for loop over neighbors */
571         j_index_start    = jindex[iidx];
572         j_index_end      = jindex[iidx+1];
573
574         /* Get outer coordinate index */
575         inr              = iinr[iidx];
576         i_coord_offset   = DIM*inr;
577
578         /* Load i particle coords and add shift vector */
579         gmx_mm_load_shift_and_3rvec_broadcast_ps(shiftvec+i_shift_offset,x+i_coord_offset,
580                                                  &ix0,&iy0,&iz0,&ix1,&iy1,&iz1,&ix2,&iy2,&iz2);
581
582         fix0             = _mm_setzero_ps();
583         fiy0             = _mm_setzero_ps();
584         fiz0             = _mm_setzero_ps();
585         fix1             = _mm_setzero_ps();
586         fiy1             = _mm_setzero_ps();
587         fiz1             = _mm_setzero_ps();
588         fix2             = _mm_setzero_ps();
589         fiy2             = _mm_setzero_ps();
590         fiz2             = _mm_setzero_ps();
591
592         /* Start inner kernel loop */
593         for(jidx=j_index_start; jidx<j_index_end && jjnr[jidx+3]>=0; jidx+=4)
594         {
595
596             /* Get j neighbor index, and coordinate index */
597             jnrA             = jjnr[jidx];
598             jnrB             = jjnr[jidx+1];
599             jnrC             = jjnr[jidx+2];
600             jnrD             = jjnr[jidx+3];
601             j_coord_offsetA  = DIM*jnrA;
602             j_coord_offsetB  = DIM*jnrB;
603             j_coord_offsetC  = DIM*jnrC;
604             j_coord_offsetD  = DIM*jnrD;
605
606             /* load j atom coordinates */
607             gmx_mm_load_1rvec_4ptr_swizzle_ps(x+j_coord_offsetA,x+j_coord_offsetB,
608                                               x+j_coord_offsetC,x+j_coord_offsetD,
609                                               &jx0,&jy0,&jz0);
610
611             /* Calculate displacement vector */
612             dx00             = _mm_sub_ps(ix0,jx0);
613             dy00             = _mm_sub_ps(iy0,jy0);
614             dz00             = _mm_sub_ps(iz0,jz0);
615             dx10             = _mm_sub_ps(ix1,jx0);
616             dy10             = _mm_sub_ps(iy1,jy0);
617             dz10             = _mm_sub_ps(iz1,jz0);
618             dx20             = _mm_sub_ps(ix2,jx0);
619             dy20             = _mm_sub_ps(iy2,jy0);
620             dz20             = _mm_sub_ps(iz2,jz0);
621
622             /* Calculate squared distance and things based on it */
623             rsq00            = gmx_mm_calc_rsq_ps(dx00,dy00,dz00);
624             rsq10            = gmx_mm_calc_rsq_ps(dx10,dy10,dz10);
625             rsq20            = gmx_mm_calc_rsq_ps(dx20,dy20,dz20);
626
627             rinv00           = gmx_mm_invsqrt_ps(rsq00);
628             rinv10           = gmx_mm_invsqrt_ps(rsq10);
629             rinv20           = gmx_mm_invsqrt_ps(rsq20);
630
631             rinvsq00         = _mm_mul_ps(rinv00,rinv00);
632             rinvsq10         = _mm_mul_ps(rinv10,rinv10);
633             rinvsq20         = _mm_mul_ps(rinv20,rinv20);
634
635             /* Load parameters for j particles */
636             jq0              = gmx_mm_load_4real_swizzle_ps(charge+jnrA+0,charge+jnrB+0,
637                                                               charge+jnrC+0,charge+jnrD+0);
638
639             fjx0             = _mm_setzero_ps();
640             fjy0             = _mm_setzero_ps();
641             fjz0             = _mm_setzero_ps();
642
643             /**************************
644              * CALCULATE INTERACTIONS *
645              **************************/
646
647             /* Compute parameters for interactions between i and j atoms */
648             qq00             = _mm_mul_ps(iq0,jq0);
649
650             /* COULOMB ELECTROSTATICS */
651             velec            = _mm_mul_ps(qq00,rinv00);
652             felec            = _mm_mul_ps(velec,rinvsq00);
653
654             fscal            = felec;
655
656              /* Update vectorial force */
657             fix0             = _mm_macc_ps(dx00,fscal,fix0);
658             fiy0             = _mm_macc_ps(dy00,fscal,fiy0);
659             fiz0             = _mm_macc_ps(dz00,fscal,fiz0);
660
661             fjx0             = _mm_macc_ps(dx00,fscal,fjx0);
662             fjy0             = _mm_macc_ps(dy00,fscal,fjy0);
663             fjz0             = _mm_macc_ps(dz00,fscal,fjz0);
664
665             /**************************
666              * CALCULATE INTERACTIONS *
667              **************************/
668
669             /* Compute parameters for interactions between i and j atoms */
670             qq10             = _mm_mul_ps(iq1,jq0);
671
672             /* COULOMB ELECTROSTATICS */
673             velec            = _mm_mul_ps(qq10,rinv10);
674             felec            = _mm_mul_ps(velec,rinvsq10);
675
676             fscal            = felec;
677
678              /* Update vectorial force */
679             fix1             = _mm_macc_ps(dx10,fscal,fix1);
680             fiy1             = _mm_macc_ps(dy10,fscal,fiy1);
681             fiz1             = _mm_macc_ps(dz10,fscal,fiz1);
682
683             fjx0             = _mm_macc_ps(dx10,fscal,fjx0);
684             fjy0             = _mm_macc_ps(dy10,fscal,fjy0);
685             fjz0             = _mm_macc_ps(dz10,fscal,fjz0);
686
687             /**************************
688              * CALCULATE INTERACTIONS *
689              **************************/
690
691             /* Compute parameters for interactions between i and j atoms */
692             qq20             = _mm_mul_ps(iq2,jq0);
693
694             /* COULOMB ELECTROSTATICS */
695             velec            = _mm_mul_ps(qq20,rinv20);
696             felec            = _mm_mul_ps(velec,rinvsq20);
697
698             fscal            = felec;
699
700              /* Update vectorial force */
701             fix2             = _mm_macc_ps(dx20,fscal,fix2);
702             fiy2             = _mm_macc_ps(dy20,fscal,fiy2);
703             fiz2             = _mm_macc_ps(dz20,fscal,fiz2);
704
705             fjx0             = _mm_macc_ps(dx20,fscal,fjx0);
706             fjy0             = _mm_macc_ps(dy20,fscal,fjy0);
707             fjz0             = _mm_macc_ps(dz20,fscal,fjz0);
708
709             fjptrA             = f+j_coord_offsetA;
710             fjptrB             = f+j_coord_offsetB;
711             fjptrC             = f+j_coord_offsetC;
712             fjptrD             = f+j_coord_offsetD;
713
714             gmx_mm_decrement_1rvec_4ptr_swizzle_ps(fjptrA,fjptrB,fjptrC,fjptrD,fjx0,fjy0,fjz0);
715
716             /* Inner loop uses 90 flops */
717         }
718
719         if(jidx<j_index_end)
720         {
721
722             /* Get j neighbor index, and coordinate index */
723             jnrlistA         = jjnr[jidx];
724             jnrlistB         = jjnr[jidx+1];
725             jnrlistC         = jjnr[jidx+2];
726             jnrlistD         = jjnr[jidx+3];
727             /* Sign of each element will be negative for non-real atoms.
728              * This mask will be 0xFFFFFFFF for dummy entries and 0x0 for real ones,
729              * so use it as val = _mm_andnot_ps(mask,val) to clear dummy entries.
730              */
731             dummy_mask = gmx_mm_castsi128_ps(_mm_cmplt_epi32(_mm_loadu_si128((const __m128i *)(jjnr+jidx)),_mm_setzero_si128()));
732             jnrA       = (jnrlistA>=0) ? jnrlistA : 0;
733             jnrB       = (jnrlistB>=0) ? jnrlistB : 0;
734             jnrC       = (jnrlistC>=0) ? jnrlistC : 0;
735             jnrD       = (jnrlistD>=0) ? jnrlistD : 0;
736             j_coord_offsetA  = DIM*jnrA;
737             j_coord_offsetB  = DIM*jnrB;
738             j_coord_offsetC  = DIM*jnrC;
739             j_coord_offsetD  = DIM*jnrD;
740
741             /* load j atom coordinates */
742             gmx_mm_load_1rvec_4ptr_swizzle_ps(x+j_coord_offsetA,x+j_coord_offsetB,
743                                               x+j_coord_offsetC,x+j_coord_offsetD,
744                                               &jx0,&jy0,&jz0);
745
746             /* Calculate displacement vector */
747             dx00             = _mm_sub_ps(ix0,jx0);
748             dy00             = _mm_sub_ps(iy0,jy0);
749             dz00             = _mm_sub_ps(iz0,jz0);
750             dx10             = _mm_sub_ps(ix1,jx0);
751             dy10             = _mm_sub_ps(iy1,jy0);
752             dz10             = _mm_sub_ps(iz1,jz0);
753             dx20             = _mm_sub_ps(ix2,jx0);
754             dy20             = _mm_sub_ps(iy2,jy0);
755             dz20             = _mm_sub_ps(iz2,jz0);
756
757             /* Calculate squared distance and things based on it */
758             rsq00            = gmx_mm_calc_rsq_ps(dx00,dy00,dz00);
759             rsq10            = gmx_mm_calc_rsq_ps(dx10,dy10,dz10);
760             rsq20            = gmx_mm_calc_rsq_ps(dx20,dy20,dz20);
761
762             rinv00           = gmx_mm_invsqrt_ps(rsq00);
763             rinv10           = gmx_mm_invsqrt_ps(rsq10);
764             rinv20           = gmx_mm_invsqrt_ps(rsq20);
765
766             rinvsq00         = _mm_mul_ps(rinv00,rinv00);
767             rinvsq10         = _mm_mul_ps(rinv10,rinv10);
768             rinvsq20         = _mm_mul_ps(rinv20,rinv20);
769
770             /* Load parameters for j particles */
771             jq0              = gmx_mm_load_4real_swizzle_ps(charge+jnrA+0,charge+jnrB+0,
772                                                               charge+jnrC+0,charge+jnrD+0);
773
774             fjx0             = _mm_setzero_ps();
775             fjy0             = _mm_setzero_ps();
776             fjz0             = _mm_setzero_ps();
777
778             /**************************
779              * CALCULATE INTERACTIONS *
780              **************************/
781
782             /* Compute parameters for interactions between i and j atoms */
783             qq00             = _mm_mul_ps(iq0,jq0);
784
785             /* COULOMB ELECTROSTATICS */
786             velec            = _mm_mul_ps(qq00,rinv00);
787             felec            = _mm_mul_ps(velec,rinvsq00);
788
789             fscal            = felec;
790
791             fscal            = _mm_andnot_ps(dummy_mask,fscal);
792
793              /* Update vectorial force */
794             fix0             = _mm_macc_ps(dx00,fscal,fix0);
795             fiy0             = _mm_macc_ps(dy00,fscal,fiy0);
796             fiz0             = _mm_macc_ps(dz00,fscal,fiz0);
797
798             fjx0             = _mm_macc_ps(dx00,fscal,fjx0);
799             fjy0             = _mm_macc_ps(dy00,fscal,fjy0);
800             fjz0             = _mm_macc_ps(dz00,fscal,fjz0);
801
802             /**************************
803              * CALCULATE INTERACTIONS *
804              **************************/
805
806             /* Compute parameters for interactions between i and j atoms */
807             qq10             = _mm_mul_ps(iq1,jq0);
808
809             /* COULOMB ELECTROSTATICS */
810             velec            = _mm_mul_ps(qq10,rinv10);
811             felec            = _mm_mul_ps(velec,rinvsq10);
812
813             fscal            = felec;
814
815             fscal            = _mm_andnot_ps(dummy_mask,fscal);
816
817              /* Update vectorial force */
818             fix1             = _mm_macc_ps(dx10,fscal,fix1);
819             fiy1             = _mm_macc_ps(dy10,fscal,fiy1);
820             fiz1             = _mm_macc_ps(dz10,fscal,fiz1);
821
822             fjx0             = _mm_macc_ps(dx10,fscal,fjx0);
823             fjy0             = _mm_macc_ps(dy10,fscal,fjy0);
824             fjz0             = _mm_macc_ps(dz10,fscal,fjz0);
825
826             /**************************
827              * CALCULATE INTERACTIONS *
828              **************************/
829
830             /* Compute parameters for interactions between i and j atoms */
831             qq20             = _mm_mul_ps(iq2,jq0);
832
833             /* COULOMB ELECTROSTATICS */
834             velec            = _mm_mul_ps(qq20,rinv20);
835             felec            = _mm_mul_ps(velec,rinvsq20);
836
837             fscal            = felec;
838
839             fscal            = _mm_andnot_ps(dummy_mask,fscal);
840
841              /* Update vectorial force */
842             fix2             = _mm_macc_ps(dx20,fscal,fix2);
843             fiy2             = _mm_macc_ps(dy20,fscal,fiy2);
844             fiz2             = _mm_macc_ps(dz20,fscal,fiz2);
845
846             fjx0             = _mm_macc_ps(dx20,fscal,fjx0);
847             fjy0             = _mm_macc_ps(dy20,fscal,fjy0);
848             fjz0             = _mm_macc_ps(dz20,fscal,fjz0);
849
850             fjptrA             = (jnrlistA>=0) ? f+j_coord_offsetA : scratch;
851             fjptrB             = (jnrlistB>=0) ? f+j_coord_offsetB : scratch;
852             fjptrC             = (jnrlistC>=0) ? f+j_coord_offsetC : scratch;
853             fjptrD             = (jnrlistD>=0) ? f+j_coord_offsetD : scratch;
854
855             gmx_mm_decrement_1rvec_4ptr_swizzle_ps(fjptrA,fjptrB,fjptrC,fjptrD,fjx0,fjy0,fjz0);
856
857             /* Inner loop uses 90 flops */
858         }
859
860         /* End of innermost loop */
861
862         gmx_mm_update_iforce_3atom_swizzle_ps(fix0,fiy0,fiz0,fix1,fiy1,fiz1,fix2,fiy2,fiz2,
863                                               f+i_coord_offset,fshift+i_shift_offset);
864
865         /* Increment number of inner iterations */
866         inneriter                  += j_index_end - j_index_start;
867
868         /* Outer loop uses 18 flops */
869     }
870
871     /* Increment number of outer iterations */
872     outeriter        += nri;
873
874     /* Update outer/inner flops */
875
876     inc_nrnb(nrnb,eNR_NBKERNEL_ELEC_W3_F,outeriter*18 + inneriter*90);
877 }