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