Merge release-5-0 into master
[alexxy/gromacs.git] / src / gromacs / gmxlib / nonbonded / nb_kernel_avx_128_fma_single / nb_kernel_ElecRF_VdwNone_GeomW4P1_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_ElecRF_VdwNone_GeomW4P1_VF_avx_128_fma_single
54  * Electrostatics interaction: ReactionField
55  * VdW interaction:            None
56  * Geometry:                   Water4-Particle
57  * Calculate force/pot:        PotentialAndForce
58  */
59 void
60 nb_kernel_ElecRF_VdwNone_GeomW4P1_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              vdwioffset1;
86     __m128           ix1,iy1,iz1,fix1,fiy1,fiz1,iq1,isai1;
87     int              vdwioffset2;
88     __m128           ix2,iy2,iz2,fix2,fiy2,fiz2,iq2,isai2;
89     int              vdwioffset3;
90     __m128           ix3,iy3,iz3,fix3,fiy3,fiz3,iq3,isai3;
91     int              vdwjidx0A,vdwjidx0B,vdwjidx0C,vdwjidx0D;
92     __m128           jx0,jy0,jz0,fjx0,fjy0,fjz0,jq0,isaj0;
93     __m128           dx10,dy10,dz10,rsq10,rinv10,rinvsq10,r10,qq10,c6_10,c12_10;
94     __m128           dx20,dy20,dz20,rsq20,rinv20,rinvsq20,r20,qq20,c6_20,c12_20;
95     __m128           dx30,dy30,dz30,rsq30,rinv30,rinvsq30,r30,qq30,c6_30,c12_30;
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     krf              = _mm_set1_ps(fr->ic->k_rf);
116     krf2             = _mm_set1_ps(fr->ic->k_rf*2.0);
117     crf              = _mm_set1_ps(fr->ic->c_rf);
118
119     /* Setup water-specific parameters */
120     inr              = nlist->iinr[0];
121     iq1              = _mm_mul_ps(facel,_mm_set1_ps(charge[inr+1]));
122     iq2              = _mm_mul_ps(facel,_mm_set1_ps(charge[inr+2]));
123     iq3              = _mm_mul_ps(facel,_mm_set1_ps(charge[inr+3]));
124
125     /* Avoid stupid compiler warnings */
126     jnrA = jnrB = jnrC = jnrD = 0;
127     j_coord_offsetA = 0;
128     j_coord_offsetB = 0;
129     j_coord_offsetC = 0;
130     j_coord_offsetD = 0;
131
132     outeriter        = 0;
133     inneriter        = 0;
134
135     for(iidx=0;iidx<4*DIM;iidx++)
136     {
137         scratch[iidx] = 0.0;
138     }
139
140     /* Start outer loop over neighborlists */
141     for(iidx=0; iidx<nri; iidx++)
142     {
143         /* Load shift vector for this list */
144         i_shift_offset   = DIM*shiftidx[iidx];
145
146         /* Load limits for loop over neighbors */
147         j_index_start    = jindex[iidx];
148         j_index_end      = jindex[iidx+1];
149
150         /* Get outer coordinate index */
151         inr              = iinr[iidx];
152         i_coord_offset   = DIM*inr;
153
154         /* Load i particle coords and add shift vector */
155         gmx_mm_load_shift_and_3rvec_broadcast_ps(shiftvec+i_shift_offset,x+i_coord_offset+DIM,
156                                                  &ix1,&iy1,&iz1,&ix2,&iy2,&iz2,&ix3,&iy3,&iz3);
157
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         fix3             = _mm_setzero_ps();
165         fiy3             = _mm_setzero_ps();
166         fiz3             = _mm_setzero_ps();
167
168         /* Reset potential sums */
169         velecsum         = _mm_setzero_ps();
170
171         /* Start inner kernel loop */
172         for(jidx=j_index_start; jidx<j_index_end && jjnr[jidx+3]>=0; jidx+=4)
173         {
174
175             /* Get j neighbor index, and coordinate index */
176             jnrA             = jjnr[jidx];
177             jnrB             = jjnr[jidx+1];
178             jnrC             = jjnr[jidx+2];
179             jnrD             = jjnr[jidx+3];
180             j_coord_offsetA  = DIM*jnrA;
181             j_coord_offsetB  = DIM*jnrB;
182             j_coord_offsetC  = DIM*jnrC;
183             j_coord_offsetD  = DIM*jnrD;
184
185             /* load j atom coordinates */
186             gmx_mm_load_1rvec_4ptr_swizzle_ps(x+j_coord_offsetA,x+j_coord_offsetB,
187                                               x+j_coord_offsetC,x+j_coord_offsetD,
188                                               &jx0,&jy0,&jz0);
189
190             /* Calculate displacement vector */
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             dx30             = _mm_sub_ps(ix3,jx0);
198             dy30             = _mm_sub_ps(iy3,jy0);
199             dz30             = _mm_sub_ps(iz3,jz0);
200
201             /* Calculate squared distance and things based on it */
202             rsq10            = gmx_mm_calc_rsq_ps(dx10,dy10,dz10);
203             rsq20            = gmx_mm_calc_rsq_ps(dx20,dy20,dz20);
204             rsq30            = gmx_mm_calc_rsq_ps(dx30,dy30,dz30);
205
206             rinv10           = gmx_mm_invsqrt_ps(rsq10);
207             rinv20           = gmx_mm_invsqrt_ps(rsq20);
208             rinv30           = gmx_mm_invsqrt_ps(rsq30);
209
210             rinvsq10         = _mm_mul_ps(rinv10,rinv10);
211             rinvsq20         = _mm_mul_ps(rinv20,rinv20);
212             rinvsq30         = _mm_mul_ps(rinv30,rinv30);
213
214             /* Load parameters for j particles */
215             jq0              = gmx_mm_load_4real_swizzle_ps(charge+jnrA+0,charge+jnrB+0,
216                                                               charge+jnrC+0,charge+jnrD+0);
217
218             fjx0             = _mm_setzero_ps();
219             fjy0             = _mm_setzero_ps();
220             fjz0             = _mm_setzero_ps();
221
222             /**************************
223              * CALCULATE INTERACTIONS *
224              **************************/
225
226             /* Compute parameters for interactions between i and j atoms */
227             qq10             = _mm_mul_ps(iq1,jq0);
228
229             /* REACTION-FIELD ELECTROSTATICS */
230             velec            = _mm_mul_ps(qq10,_mm_sub_ps(_mm_macc_ps(krf,rsq10,rinv10),crf));
231             felec            = _mm_mul_ps(qq10,_mm_msub_ps(rinv10,rinvsq10,krf2));
232
233             /* Update potential sum for this i atom from the interaction with this j atom. */
234             velecsum         = _mm_add_ps(velecsum,velec);
235
236             fscal            = felec;
237
238              /* Update vectorial force */
239             fix1             = _mm_macc_ps(dx10,fscal,fix1);
240             fiy1             = _mm_macc_ps(dy10,fscal,fiy1);
241             fiz1             = _mm_macc_ps(dz10,fscal,fiz1);
242
243             fjx0             = _mm_macc_ps(dx10,fscal,fjx0);
244             fjy0             = _mm_macc_ps(dy10,fscal,fjy0);
245             fjz0             = _mm_macc_ps(dz10,fscal,fjz0);
246
247             /**************************
248              * CALCULATE INTERACTIONS *
249              **************************/
250
251             /* Compute parameters for interactions between i and j atoms */
252             qq20             = _mm_mul_ps(iq2,jq0);
253
254             /* REACTION-FIELD ELECTROSTATICS */
255             velec            = _mm_mul_ps(qq20,_mm_sub_ps(_mm_macc_ps(krf,rsq20,rinv20),crf));
256             felec            = _mm_mul_ps(qq20,_mm_msub_ps(rinv20,rinvsq20,krf2));
257
258             /* Update potential sum for this i atom from the interaction with this j atom. */
259             velecsum         = _mm_add_ps(velecsum,velec);
260
261             fscal            = felec;
262
263              /* Update vectorial force */
264             fix2             = _mm_macc_ps(dx20,fscal,fix2);
265             fiy2             = _mm_macc_ps(dy20,fscal,fiy2);
266             fiz2             = _mm_macc_ps(dz20,fscal,fiz2);
267
268             fjx0             = _mm_macc_ps(dx20,fscal,fjx0);
269             fjy0             = _mm_macc_ps(dy20,fscal,fjy0);
270             fjz0             = _mm_macc_ps(dz20,fscal,fjz0);
271
272             /**************************
273              * CALCULATE INTERACTIONS *
274              **************************/
275
276             /* Compute parameters for interactions between i and j atoms */
277             qq30             = _mm_mul_ps(iq3,jq0);
278
279             /* REACTION-FIELD ELECTROSTATICS */
280             velec            = _mm_mul_ps(qq30,_mm_sub_ps(_mm_macc_ps(krf,rsq30,rinv30),crf));
281             felec            = _mm_mul_ps(qq30,_mm_msub_ps(rinv30,rinvsq30,krf2));
282
283             /* Update potential sum for this i atom from the interaction with this j atom. */
284             velecsum         = _mm_add_ps(velecsum,velec);
285
286             fscal            = felec;
287
288              /* Update vectorial force */
289             fix3             = _mm_macc_ps(dx30,fscal,fix3);
290             fiy3             = _mm_macc_ps(dy30,fscal,fiy3);
291             fiz3             = _mm_macc_ps(dz30,fscal,fiz3);
292
293             fjx0             = _mm_macc_ps(dx30,fscal,fjx0);
294             fjy0             = _mm_macc_ps(dy30,fscal,fjy0);
295             fjz0             = _mm_macc_ps(dz30,fscal,fjz0);
296
297             fjptrA             = f+j_coord_offsetA;
298             fjptrB             = f+j_coord_offsetB;
299             fjptrC             = f+j_coord_offsetC;
300             fjptrD             = f+j_coord_offsetD;
301
302             gmx_mm_decrement_1rvec_4ptr_swizzle_ps(fjptrA,fjptrB,fjptrC,fjptrD,fjx0,fjy0,fjz0);
303
304             /* Inner loop uses 105 flops */
305         }
306
307         if(jidx<j_index_end)
308         {
309
310             /* Get j neighbor index, and coordinate index */
311             jnrlistA         = jjnr[jidx];
312             jnrlistB         = jjnr[jidx+1];
313             jnrlistC         = jjnr[jidx+2];
314             jnrlistD         = jjnr[jidx+3];
315             /* Sign of each element will be negative for non-real atoms.
316              * This mask will be 0xFFFFFFFF for dummy entries and 0x0 for real ones,
317              * so use it as val = _mm_andnot_ps(mask,val) to clear dummy entries.
318              */
319             dummy_mask = gmx_mm_castsi128_ps(_mm_cmplt_epi32(_mm_loadu_si128((const __m128i *)(jjnr+jidx)),_mm_setzero_si128()));
320             jnrA       = (jnrlistA>=0) ? jnrlistA : 0;
321             jnrB       = (jnrlistB>=0) ? jnrlistB : 0;
322             jnrC       = (jnrlistC>=0) ? jnrlistC : 0;
323             jnrD       = (jnrlistD>=0) ? jnrlistD : 0;
324             j_coord_offsetA  = DIM*jnrA;
325             j_coord_offsetB  = DIM*jnrB;
326             j_coord_offsetC  = DIM*jnrC;
327             j_coord_offsetD  = DIM*jnrD;
328
329             /* load j atom coordinates */
330             gmx_mm_load_1rvec_4ptr_swizzle_ps(x+j_coord_offsetA,x+j_coord_offsetB,
331                                               x+j_coord_offsetC,x+j_coord_offsetD,
332                                               &jx0,&jy0,&jz0);
333
334             /* Calculate displacement vector */
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             dx30             = _mm_sub_ps(ix3,jx0);
342             dy30             = _mm_sub_ps(iy3,jy0);
343             dz30             = _mm_sub_ps(iz3,jz0);
344
345             /* Calculate squared distance and things based on it */
346             rsq10            = gmx_mm_calc_rsq_ps(dx10,dy10,dz10);
347             rsq20            = gmx_mm_calc_rsq_ps(dx20,dy20,dz20);
348             rsq30            = gmx_mm_calc_rsq_ps(dx30,dy30,dz30);
349
350             rinv10           = gmx_mm_invsqrt_ps(rsq10);
351             rinv20           = gmx_mm_invsqrt_ps(rsq20);
352             rinv30           = gmx_mm_invsqrt_ps(rsq30);
353
354             rinvsq10         = _mm_mul_ps(rinv10,rinv10);
355             rinvsq20         = _mm_mul_ps(rinv20,rinv20);
356             rinvsq30         = _mm_mul_ps(rinv30,rinv30);
357
358             /* Load parameters for j particles */
359             jq0              = gmx_mm_load_4real_swizzle_ps(charge+jnrA+0,charge+jnrB+0,
360                                                               charge+jnrC+0,charge+jnrD+0);
361
362             fjx0             = _mm_setzero_ps();
363             fjy0             = _mm_setzero_ps();
364             fjz0             = _mm_setzero_ps();
365
366             /**************************
367              * CALCULATE INTERACTIONS *
368              **************************/
369
370             /* Compute parameters for interactions between i and j atoms */
371             qq10             = _mm_mul_ps(iq1,jq0);
372
373             /* REACTION-FIELD ELECTROSTATICS */
374             velec            = _mm_mul_ps(qq10,_mm_sub_ps(_mm_macc_ps(krf,rsq10,rinv10),crf));
375             felec            = _mm_mul_ps(qq10,_mm_msub_ps(rinv10,rinvsq10,krf2));
376
377             /* Update potential sum for this i atom from the interaction with this j atom. */
378             velec            = _mm_andnot_ps(dummy_mask,velec);
379             velecsum         = _mm_add_ps(velecsum,velec);
380
381             fscal            = felec;
382
383             fscal            = _mm_andnot_ps(dummy_mask,fscal);
384
385              /* Update vectorial force */
386             fix1             = _mm_macc_ps(dx10,fscal,fix1);
387             fiy1             = _mm_macc_ps(dy10,fscal,fiy1);
388             fiz1             = _mm_macc_ps(dz10,fscal,fiz1);
389
390             fjx0             = _mm_macc_ps(dx10,fscal,fjx0);
391             fjy0             = _mm_macc_ps(dy10,fscal,fjy0);
392             fjz0             = _mm_macc_ps(dz10,fscal,fjz0);
393
394             /**************************
395              * CALCULATE INTERACTIONS *
396              **************************/
397
398             /* Compute parameters for interactions between i and j atoms */
399             qq20             = _mm_mul_ps(iq2,jq0);
400
401             /* REACTION-FIELD ELECTROSTATICS */
402             velec            = _mm_mul_ps(qq20,_mm_sub_ps(_mm_macc_ps(krf,rsq20,rinv20),crf));
403             felec            = _mm_mul_ps(qq20,_mm_msub_ps(rinv20,rinvsq20,krf2));
404
405             /* Update potential sum for this i atom from the interaction with this j atom. */
406             velec            = _mm_andnot_ps(dummy_mask,velec);
407             velecsum         = _mm_add_ps(velecsum,velec);
408
409             fscal            = felec;
410
411             fscal            = _mm_andnot_ps(dummy_mask,fscal);
412
413              /* Update vectorial force */
414             fix2             = _mm_macc_ps(dx20,fscal,fix2);
415             fiy2             = _mm_macc_ps(dy20,fscal,fiy2);
416             fiz2             = _mm_macc_ps(dz20,fscal,fiz2);
417
418             fjx0             = _mm_macc_ps(dx20,fscal,fjx0);
419             fjy0             = _mm_macc_ps(dy20,fscal,fjy0);
420             fjz0             = _mm_macc_ps(dz20,fscal,fjz0);
421
422             /**************************
423              * CALCULATE INTERACTIONS *
424              **************************/
425
426             /* Compute parameters for interactions between i and j atoms */
427             qq30             = _mm_mul_ps(iq3,jq0);
428
429             /* REACTION-FIELD ELECTROSTATICS */
430             velec            = _mm_mul_ps(qq30,_mm_sub_ps(_mm_macc_ps(krf,rsq30,rinv30),crf));
431             felec            = _mm_mul_ps(qq30,_mm_msub_ps(rinv30,rinvsq30,krf2));
432
433             /* Update potential sum for this i atom from the interaction with this j atom. */
434             velec            = _mm_andnot_ps(dummy_mask,velec);
435             velecsum         = _mm_add_ps(velecsum,velec);
436
437             fscal            = felec;
438
439             fscal            = _mm_andnot_ps(dummy_mask,fscal);
440
441              /* Update vectorial force */
442             fix3             = _mm_macc_ps(dx30,fscal,fix3);
443             fiy3             = _mm_macc_ps(dy30,fscal,fiy3);
444             fiz3             = _mm_macc_ps(dz30,fscal,fiz3);
445
446             fjx0             = _mm_macc_ps(dx30,fscal,fjx0);
447             fjy0             = _mm_macc_ps(dy30,fscal,fjy0);
448             fjz0             = _mm_macc_ps(dz30,fscal,fjz0);
449
450             fjptrA             = (jnrlistA>=0) ? f+j_coord_offsetA : scratch;
451             fjptrB             = (jnrlistB>=0) ? f+j_coord_offsetB : scratch;
452             fjptrC             = (jnrlistC>=0) ? f+j_coord_offsetC : scratch;
453             fjptrD             = (jnrlistD>=0) ? f+j_coord_offsetD : scratch;
454
455             gmx_mm_decrement_1rvec_4ptr_swizzle_ps(fjptrA,fjptrB,fjptrC,fjptrD,fjx0,fjy0,fjz0);
456
457             /* Inner loop uses 105 flops */
458         }
459
460         /* End of innermost loop */
461
462         gmx_mm_update_iforce_3atom_swizzle_ps(fix1,fiy1,fiz1,fix2,fiy2,fiz2,fix3,fiy3,fiz3,
463                                               f+i_coord_offset+DIM,fshift+i_shift_offset);
464
465         ggid                        = gid[iidx];
466         /* Update potential energies */
467         gmx_mm_update_1pot_ps(velecsum,kernel_data->energygrp_elec+ggid);
468
469         /* Increment number of inner iterations */
470         inneriter                  += j_index_end - j_index_start;
471
472         /* Outer loop uses 19 flops */
473     }
474
475     /* Increment number of outer iterations */
476     outeriter        += nri;
477
478     /* Update outer/inner flops */
479
480     inc_nrnb(nrnb,eNR_NBKERNEL_ELEC_W4_VF,outeriter*19 + inneriter*105);
481 }
482 /*
483  * Gromacs nonbonded kernel:   nb_kernel_ElecRF_VdwNone_GeomW4P1_F_avx_128_fma_single
484  * Electrostatics interaction: ReactionField
485  * VdW interaction:            None
486  * Geometry:                   Water4-Particle
487  * Calculate force/pot:        Force
488  */
489 void
490 nb_kernel_ElecRF_VdwNone_GeomW4P1_F_avx_128_fma_single
491                     (t_nblist                    * gmx_restrict       nlist,
492                      rvec                        * gmx_restrict          xx,
493                      rvec                        * gmx_restrict          ff,
494                      t_forcerec                  * gmx_restrict          fr,
495                      t_mdatoms                   * gmx_restrict     mdatoms,
496                      nb_kernel_data_t gmx_unused * gmx_restrict kernel_data,
497                      t_nrnb                      * gmx_restrict        nrnb)
498 {
499     /* Suffixes 0,1,2,3 refer to particle indices for waters in the inner or outer loop, or
500      * just 0 for non-waters.
501      * Suffixes A,B,C,D refer to j loop unrolling done with AVX_128, e.g. for the four different
502      * jnr indices corresponding to data put in the four positions in the SIMD register.
503      */
504     int              i_shift_offset,i_coord_offset,outeriter,inneriter;
505     int              j_index_start,j_index_end,jidx,nri,inr,ggid,iidx;
506     int              jnrA,jnrB,jnrC,jnrD;
507     int              jnrlistA,jnrlistB,jnrlistC,jnrlistD;
508     int              j_coord_offsetA,j_coord_offsetB,j_coord_offsetC,j_coord_offsetD;
509     int              *iinr,*jindex,*jjnr,*shiftidx,*gid;
510     real             rcutoff_scalar;
511     real             *shiftvec,*fshift,*x,*f;
512     real             *fjptrA,*fjptrB,*fjptrC,*fjptrD;
513     real             scratch[4*DIM];
514     __m128           fscal,rcutoff,rcutoff2,jidxall;
515     int              vdwioffset1;
516     __m128           ix1,iy1,iz1,fix1,fiy1,fiz1,iq1,isai1;
517     int              vdwioffset2;
518     __m128           ix2,iy2,iz2,fix2,fiy2,fiz2,iq2,isai2;
519     int              vdwioffset3;
520     __m128           ix3,iy3,iz3,fix3,fiy3,fiz3,iq3,isai3;
521     int              vdwjidx0A,vdwjidx0B,vdwjidx0C,vdwjidx0D;
522     __m128           jx0,jy0,jz0,fjx0,fjy0,fjz0,jq0,isaj0;
523     __m128           dx10,dy10,dz10,rsq10,rinv10,rinvsq10,r10,qq10,c6_10,c12_10;
524     __m128           dx20,dy20,dz20,rsq20,rinv20,rinvsq20,r20,qq20,c6_20,c12_20;
525     __m128           dx30,dy30,dz30,rsq30,rinv30,rinvsq30,r30,qq30,c6_30,c12_30;
526     __m128           velec,felec,velecsum,facel,crf,krf,krf2;
527     real             *charge;
528     __m128           dummy_mask,cutoff_mask;
529     __m128           signbit = _mm_castsi128_ps( _mm_set1_epi32(0x80000000) );
530     __m128           one     = _mm_set1_ps(1.0);
531     __m128           two     = _mm_set1_ps(2.0);
532     x                = xx[0];
533     f                = ff[0];
534
535     nri              = nlist->nri;
536     iinr             = nlist->iinr;
537     jindex           = nlist->jindex;
538     jjnr             = nlist->jjnr;
539     shiftidx         = nlist->shift;
540     gid              = nlist->gid;
541     shiftvec         = fr->shift_vec[0];
542     fshift           = fr->fshift[0];
543     facel            = _mm_set1_ps(fr->epsfac);
544     charge           = mdatoms->chargeA;
545     krf              = _mm_set1_ps(fr->ic->k_rf);
546     krf2             = _mm_set1_ps(fr->ic->k_rf*2.0);
547     crf              = _mm_set1_ps(fr->ic->c_rf);
548
549     /* Setup water-specific parameters */
550     inr              = nlist->iinr[0];
551     iq1              = _mm_mul_ps(facel,_mm_set1_ps(charge[inr+1]));
552     iq2              = _mm_mul_ps(facel,_mm_set1_ps(charge[inr+2]));
553     iq3              = _mm_mul_ps(facel,_mm_set1_ps(charge[inr+3]));
554
555     /* Avoid stupid compiler warnings */
556     jnrA = jnrB = jnrC = jnrD = 0;
557     j_coord_offsetA = 0;
558     j_coord_offsetB = 0;
559     j_coord_offsetC = 0;
560     j_coord_offsetD = 0;
561
562     outeriter        = 0;
563     inneriter        = 0;
564
565     for(iidx=0;iidx<4*DIM;iidx++)
566     {
567         scratch[iidx] = 0.0;
568     }
569
570     /* Start outer loop over neighborlists */
571     for(iidx=0; iidx<nri; iidx++)
572     {
573         /* Load shift vector for this list */
574         i_shift_offset   = DIM*shiftidx[iidx];
575
576         /* Load limits for loop over neighbors */
577         j_index_start    = jindex[iidx];
578         j_index_end      = jindex[iidx+1];
579
580         /* Get outer coordinate index */
581         inr              = iinr[iidx];
582         i_coord_offset   = DIM*inr;
583
584         /* Load i particle coords and add shift vector */
585         gmx_mm_load_shift_and_3rvec_broadcast_ps(shiftvec+i_shift_offset,x+i_coord_offset+DIM,
586                                                  &ix1,&iy1,&iz1,&ix2,&iy2,&iz2,&ix3,&iy3,&iz3);
587
588         fix1             = _mm_setzero_ps();
589         fiy1             = _mm_setzero_ps();
590         fiz1             = _mm_setzero_ps();
591         fix2             = _mm_setzero_ps();
592         fiy2             = _mm_setzero_ps();
593         fiz2             = _mm_setzero_ps();
594         fix3             = _mm_setzero_ps();
595         fiy3             = _mm_setzero_ps();
596         fiz3             = _mm_setzero_ps();
597
598         /* Start inner kernel loop */
599         for(jidx=j_index_start; jidx<j_index_end && jjnr[jidx+3]>=0; jidx+=4)
600         {
601
602             /* Get j neighbor index, and coordinate index */
603             jnrA             = jjnr[jidx];
604             jnrB             = jjnr[jidx+1];
605             jnrC             = jjnr[jidx+2];
606             jnrD             = jjnr[jidx+3];
607             j_coord_offsetA  = DIM*jnrA;
608             j_coord_offsetB  = DIM*jnrB;
609             j_coord_offsetC  = DIM*jnrC;
610             j_coord_offsetD  = DIM*jnrD;
611
612             /* load j atom coordinates */
613             gmx_mm_load_1rvec_4ptr_swizzle_ps(x+j_coord_offsetA,x+j_coord_offsetB,
614                                               x+j_coord_offsetC,x+j_coord_offsetD,
615                                               &jx0,&jy0,&jz0);
616
617             /* Calculate displacement vector */
618             dx10             = _mm_sub_ps(ix1,jx0);
619             dy10             = _mm_sub_ps(iy1,jy0);
620             dz10             = _mm_sub_ps(iz1,jz0);
621             dx20             = _mm_sub_ps(ix2,jx0);
622             dy20             = _mm_sub_ps(iy2,jy0);
623             dz20             = _mm_sub_ps(iz2,jz0);
624             dx30             = _mm_sub_ps(ix3,jx0);
625             dy30             = _mm_sub_ps(iy3,jy0);
626             dz30             = _mm_sub_ps(iz3,jz0);
627
628             /* Calculate squared distance and things based on it */
629             rsq10            = gmx_mm_calc_rsq_ps(dx10,dy10,dz10);
630             rsq20            = gmx_mm_calc_rsq_ps(dx20,dy20,dz20);
631             rsq30            = gmx_mm_calc_rsq_ps(dx30,dy30,dz30);
632
633             rinv10           = gmx_mm_invsqrt_ps(rsq10);
634             rinv20           = gmx_mm_invsqrt_ps(rsq20);
635             rinv30           = gmx_mm_invsqrt_ps(rsq30);
636
637             rinvsq10         = _mm_mul_ps(rinv10,rinv10);
638             rinvsq20         = _mm_mul_ps(rinv20,rinv20);
639             rinvsq30         = _mm_mul_ps(rinv30,rinv30);
640
641             /* Load parameters for j particles */
642             jq0              = gmx_mm_load_4real_swizzle_ps(charge+jnrA+0,charge+jnrB+0,
643                                                               charge+jnrC+0,charge+jnrD+0);
644
645             fjx0             = _mm_setzero_ps();
646             fjy0             = _mm_setzero_ps();
647             fjz0             = _mm_setzero_ps();
648
649             /**************************
650              * CALCULATE INTERACTIONS *
651              **************************/
652
653             /* Compute parameters for interactions between i and j atoms */
654             qq10             = _mm_mul_ps(iq1,jq0);
655
656             /* REACTION-FIELD ELECTROSTATICS */
657             felec            = _mm_mul_ps(qq10,_mm_msub_ps(rinv10,rinvsq10,krf2));
658
659             fscal            = felec;
660
661              /* Update vectorial force */
662             fix1             = _mm_macc_ps(dx10,fscal,fix1);
663             fiy1             = _mm_macc_ps(dy10,fscal,fiy1);
664             fiz1             = _mm_macc_ps(dz10,fscal,fiz1);
665
666             fjx0             = _mm_macc_ps(dx10,fscal,fjx0);
667             fjy0             = _mm_macc_ps(dy10,fscal,fjy0);
668             fjz0             = _mm_macc_ps(dz10,fscal,fjz0);
669
670             /**************************
671              * CALCULATE INTERACTIONS *
672              **************************/
673
674             /* Compute parameters for interactions between i and j atoms */
675             qq20             = _mm_mul_ps(iq2,jq0);
676
677             /* REACTION-FIELD ELECTROSTATICS */
678             felec            = _mm_mul_ps(qq20,_mm_msub_ps(rinv20,rinvsq20,krf2));
679
680             fscal            = felec;
681
682              /* Update vectorial force */
683             fix2             = _mm_macc_ps(dx20,fscal,fix2);
684             fiy2             = _mm_macc_ps(dy20,fscal,fiy2);
685             fiz2             = _mm_macc_ps(dz20,fscal,fiz2);
686
687             fjx0             = _mm_macc_ps(dx20,fscal,fjx0);
688             fjy0             = _mm_macc_ps(dy20,fscal,fjy0);
689             fjz0             = _mm_macc_ps(dz20,fscal,fjz0);
690
691             /**************************
692              * CALCULATE INTERACTIONS *
693              **************************/
694
695             /* Compute parameters for interactions between i and j atoms */
696             qq30             = _mm_mul_ps(iq3,jq0);
697
698             /* REACTION-FIELD ELECTROSTATICS */
699             felec            = _mm_mul_ps(qq30,_mm_msub_ps(rinv30,rinvsq30,krf2));
700
701             fscal            = felec;
702
703              /* Update vectorial force */
704             fix3             = _mm_macc_ps(dx30,fscal,fix3);
705             fiy3             = _mm_macc_ps(dy30,fscal,fiy3);
706             fiz3             = _mm_macc_ps(dz30,fscal,fiz3);
707
708             fjx0             = _mm_macc_ps(dx30,fscal,fjx0);
709             fjy0             = _mm_macc_ps(dy30,fscal,fjy0);
710             fjz0             = _mm_macc_ps(dz30,fscal,fjz0);
711
712             fjptrA             = f+j_coord_offsetA;
713             fjptrB             = f+j_coord_offsetB;
714             fjptrC             = f+j_coord_offsetC;
715             fjptrD             = f+j_coord_offsetD;
716
717             gmx_mm_decrement_1rvec_4ptr_swizzle_ps(fjptrA,fjptrB,fjptrC,fjptrD,fjx0,fjy0,fjz0);
718
719             /* Inner loop uses 90 flops */
720         }
721
722         if(jidx<j_index_end)
723         {
724
725             /* Get j neighbor index, and coordinate index */
726             jnrlistA         = jjnr[jidx];
727             jnrlistB         = jjnr[jidx+1];
728             jnrlistC         = jjnr[jidx+2];
729             jnrlistD         = jjnr[jidx+3];
730             /* Sign of each element will be negative for non-real atoms.
731              * This mask will be 0xFFFFFFFF for dummy entries and 0x0 for real ones,
732              * so use it as val = _mm_andnot_ps(mask,val) to clear dummy entries.
733              */
734             dummy_mask = gmx_mm_castsi128_ps(_mm_cmplt_epi32(_mm_loadu_si128((const __m128i *)(jjnr+jidx)),_mm_setzero_si128()));
735             jnrA       = (jnrlistA>=0) ? jnrlistA : 0;
736             jnrB       = (jnrlistB>=0) ? jnrlistB : 0;
737             jnrC       = (jnrlistC>=0) ? jnrlistC : 0;
738             jnrD       = (jnrlistD>=0) ? jnrlistD : 0;
739             j_coord_offsetA  = DIM*jnrA;
740             j_coord_offsetB  = DIM*jnrB;
741             j_coord_offsetC  = DIM*jnrC;
742             j_coord_offsetD  = DIM*jnrD;
743
744             /* load j atom coordinates */
745             gmx_mm_load_1rvec_4ptr_swizzle_ps(x+j_coord_offsetA,x+j_coord_offsetB,
746                                               x+j_coord_offsetC,x+j_coord_offsetD,
747                                               &jx0,&jy0,&jz0);
748
749             /* Calculate displacement vector */
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             dx30             = _mm_sub_ps(ix3,jx0);
757             dy30             = _mm_sub_ps(iy3,jy0);
758             dz30             = _mm_sub_ps(iz3,jz0);
759
760             /* Calculate squared distance and things based on it */
761             rsq10            = gmx_mm_calc_rsq_ps(dx10,dy10,dz10);
762             rsq20            = gmx_mm_calc_rsq_ps(dx20,dy20,dz20);
763             rsq30            = gmx_mm_calc_rsq_ps(dx30,dy30,dz30);
764
765             rinv10           = gmx_mm_invsqrt_ps(rsq10);
766             rinv20           = gmx_mm_invsqrt_ps(rsq20);
767             rinv30           = gmx_mm_invsqrt_ps(rsq30);
768
769             rinvsq10         = _mm_mul_ps(rinv10,rinv10);
770             rinvsq20         = _mm_mul_ps(rinv20,rinv20);
771             rinvsq30         = _mm_mul_ps(rinv30,rinv30);
772
773             /* Load parameters for j particles */
774             jq0              = gmx_mm_load_4real_swizzle_ps(charge+jnrA+0,charge+jnrB+0,
775                                                               charge+jnrC+0,charge+jnrD+0);
776
777             fjx0             = _mm_setzero_ps();
778             fjy0             = _mm_setzero_ps();
779             fjz0             = _mm_setzero_ps();
780
781             /**************************
782              * CALCULATE INTERACTIONS *
783              **************************/
784
785             /* Compute parameters for interactions between i and j atoms */
786             qq10             = _mm_mul_ps(iq1,jq0);
787
788             /* REACTION-FIELD ELECTROSTATICS */
789             felec            = _mm_mul_ps(qq10,_mm_msub_ps(rinv10,rinvsq10,krf2));
790
791             fscal            = felec;
792
793             fscal            = _mm_andnot_ps(dummy_mask,fscal);
794
795              /* Update vectorial force */
796             fix1             = _mm_macc_ps(dx10,fscal,fix1);
797             fiy1             = _mm_macc_ps(dy10,fscal,fiy1);
798             fiz1             = _mm_macc_ps(dz10,fscal,fiz1);
799
800             fjx0             = _mm_macc_ps(dx10,fscal,fjx0);
801             fjy0             = _mm_macc_ps(dy10,fscal,fjy0);
802             fjz0             = _mm_macc_ps(dz10,fscal,fjz0);
803
804             /**************************
805              * CALCULATE INTERACTIONS *
806              **************************/
807
808             /* Compute parameters for interactions between i and j atoms */
809             qq20             = _mm_mul_ps(iq2,jq0);
810
811             /* REACTION-FIELD ELECTROSTATICS */
812             felec            = _mm_mul_ps(qq20,_mm_msub_ps(rinv20,rinvsq20,krf2));
813
814             fscal            = felec;
815
816             fscal            = _mm_andnot_ps(dummy_mask,fscal);
817
818              /* Update vectorial force */
819             fix2             = _mm_macc_ps(dx20,fscal,fix2);
820             fiy2             = _mm_macc_ps(dy20,fscal,fiy2);
821             fiz2             = _mm_macc_ps(dz20,fscal,fiz2);
822
823             fjx0             = _mm_macc_ps(dx20,fscal,fjx0);
824             fjy0             = _mm_macc_ps(dy20,fscal,fjy0);
825             fjz0             = _mm_macc_ps(dz20,fscal,fjz0);
826
827             /**************************
828              * CALCULATE INTERACTIONS *
829              **************************/
830
831             /* Compute parameters for interactions between i and j atoms */
832             qq30             = _mm_mul_ps(iq3,jq0);
833
834             /* REACTION-FIELD ELECTROSTATICS */
835             felec            = _mm_mul_ps(qq30,_mm_msub_ps(rinv30,rinvsq30,krf2));
836
837             fscal            = felec;
838
839             fscal            = _mm_andnot_ps(dummy_mask,fscal);
840
841              /* Update vectorial force */
842             fix3             = _mm_macc_ps(dx30,fscal,fix3);
843             fiy3             = _mm_macc_ps(dy30,fscal,fiy3);
844             fiz3             = _mm_macc_ps(dz30,fscal,fiz3);
845
846             fjx0             = _mm_macc_ps(dx30,fscal,fjx0);
847             fjy0             = _mm_macc_ps(dy30,fscal,fjy0);
848             fjz0             = _mm_macc_ps(dz30,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(fix1,fiy1,fiz1,fix2,fiy2,fiz2,fix3,fiy3,fiz3,
863                                               f+i_coord_offset+DIM,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_W4_F,outeriter*18 + inneriter*90);
877 }