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