// File: /workspace/github_c_makefile_star2_O2/guszejnovdavid[P]MISFIT/SumScalarInRegion.c
// Function: SumScalar_Sphere(double*, long, double, double, double, double, double, double, double, double, double, double)
double SumScalar_Sphere(
       double* Field, long N, //field array and length of one dimension
       double Xmin, double Xmax, double Ymin, double Ymax, double Zmin, double Zmax, // limits of grid
       double R, double x0, double y0, double z0) //parameters of sphere
{
double res,temp;
long i,j,k;
long Imin,Imax,Jmin,Jmax,Kmin,Kmax; //indices
double xsq_norm, ysq_norm/*, zsq_norm*/ ; //used in distance calculations
double rsq; //radial distance square of point to origin normalized to d
double contribfactor; //fraction of volume element within the sphere, <1.0 for boundary points
double reducedr; // (R-(r-d/2))/d, used for getting contribfactor

const long Nsq=N*N; //N^2 used for indexing
const double d=(Xmax-Xmin)/(double)(N-1); //spacing
//const double Rsq=R*R/(d*d); //radius squared renormalized to grid
const double dV=d*d*d; //volume element, assuming cubic lattice
const double Rprime=R+d/2.0; //actual radius within which points contribute
const double Rprimesq=Rprime*Rprime/(d*d); //modified radius squared renormalized to grid
const double Rfull=R-d/2.0; //radius within which points contribute at 100%
const double Rfullsq=Rfull*Rfull/(d*d); //modified radius squared renormalized to grid
// Origin coordinates
const double x0_norm=(x0-Xmin)/d;
const double y0_norm=(y0-Ymin)/d;
const double z0_norm=(z0-Zmin)/d;

res=0; //final result

//Get smaller cube where the points can be
  //X index
Imin=(long)Max_2_double(floor((InLimits_double(x0-Rprime, Xmin, Xmax)-Xmin)/d), 0.0);
Imax=(long)Min_2_double(ceil((InLimits_double(x0+Rprime, Xmin, Xmax)-Xmin)/d),(double)(N-1));

 //Go through all points in subcube
for (i = Imin; i <=Imax; i++){
    xsq_norm=(i-x0_norm)*(i-x0_norm);
    temp=sqrt(Rprimesq-xsq_norm);
	if(temp>0){ //check to make sure we don't get negative values
	//Y index
	Jmin=(long)Max_2_double(floor(y0_norm-temp), 0.0);
	Jmax=(long)Min_2_double(ceil(y0_norm+temp), (double)(N-1));
    for (j = Jmin; j <=Jmax; j++){
        ysq_norm=(j-y0_norm)*(j-y0_norm);
        temp=sqrt(Rprimesq-xsq_norm-ysq_norm);
		if(temp>0){ //check to make sure we don't get negative values
		//Z index
		Kmin=(long)Max_2_double(floor(z0_norm-temp), 0.0);
		Kmax=(long)Min_2_double(ceil(z0_norm+temp), (double)(N-1));
        for (k = Kmin; k <=Kmax; k++){
        	//zsq_norm=(k-z0_norm)*(k-z0_norm);
        	rsq=xsq_norm+ysq_norm+(k-z0_norm)*(k-z0_norm);
        	if(Rfullsq>rsq){ //close enough to contribute 100%
        		res+=Field[k+j*N+i*Nsq];
			}
			else{
				if(Rprimesq>rsq){ //close enough to contribute but not 100%
					reducedr=R/d-sqrt(rsq)+0.5; // (R-(r-d/2))/d used for getting contribfactor, between 0 and 1
					contribfactor=pow(reducedr,3.0)*(10.0-15.0*reducedr+6.0*reducedr*reducedr); //10x^3+6x^5-15x^4
        			res+=contribfactor*Field[k+j*N+i*Nsq];
				}	
			}
         }
     }//end if
     }
 }//end if
}

              
 return (res*dV); //return sum of scalar field             
              
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movapd %xmm1, %xmm11
movq %rsi, %rbx
movq %rdi, %r14
movsd 0xe8(%rsp), %xmm8
movsd 0xe0(%rsp), %xmm9
movapd %xmm1, %xmm5
movsd %xmm1, 0x20(%rsp)
subsd %xmm0, %xmm5
movapd %xmm0, %xmm10
leaq -0x1(%rsi), %rax
cvtsi2sd %rax, %xmm0
movsd %xmm0, 0x30(%rsp)
divsd %xmm0, %xmm5
movapd %xmm5, %xmm3
mulsd %xmm5, %xmm3
movsd 0x254e(%rip), %xmm1     # 0xb418
mulsd %xmm5, %xmm1
movapd %xmm5, %xmm0
movsd %xmm6, 0x48(%rsp)
movapd %xmm6, %xmm5
subsd %xmm1, %xmm5
addsd %xmm6, %xmm1
movapd %xmm1, %xmm6
movsd %xmm1, 0x38(%rsp)
mulsd %xmm1, %xmm6
divsd %xmm3, %xmm6
movsd %xmm6, 0x58(%rsp)
mulsd %xmm5, %xmm5
movsd %xmm3, 0x60(%rsp)
divsd %xmm3, %xmm5
movsd %xmm5, 0x50(%rsp)
movsd %xmm7, 0x10(%rsp)
movapd %xmm7, %xmm3
movsd %xmm10, 0x8(%rsp)
subsd %xmm10, %xmm3
movsd %xmm0, 0x28(%rsp)
divsd %xmm0, %xmm3
movsd %xmm3, 0x98(%rsp)
subsd %xmm2, %xmm9
divsd %xmm0, %xmm9
movsd %xmm9, 0x40(%rsp)
subsd %xmm4, %xmm8
divsd %xmm0, %xmm8
movsd %xmm8, 0x18(%rsp)
movapd %xmm7, %xmm0
subsd %xmm1, %xmm0
movapd %xmm10, %xmm1
movapd %xmm11, %xmm2
callq 0x7b08
subsd 0x8(%rsp), %xmm0
divsd 0x28(%rsp), %xmm0
callq 0x2330
xorpd %xmm1, %xmm1
callq 0x7ae9
cvttsd2si %xmm0, %r15
movsd 0x38(%rsp), %xmm0
addsd 0x10(%rsp), %xmm0
movsd 0x8(%rsp), %xmm1
movsd 0x20(%rsp), %xmm2
callq 0x7b08
subsd 0x8(%rsp), %xmm0
divsd 0x28(%rsp), %xmm0
callq 0x2050
movsd 0x30(%rsp), %xmm1
callq 0x7ae0
cvttsd2si %xmm0, %r12
movsd 0x48(%rsp), %xmm0
divsd 0x28(%rsp), %xmm0
movsd %xmm0, 0x48(%rsp)
movq %rbx, %r13
imulq %rbx, %r13
shlq $0x3, %r13
movq %r15, %rax
imulq %rbx, %rax
shlq $0x3, %rax
movq %rax, 0x68(%rsp)
movq %rbx, 0x80(%rsp)
leaq (,%rbx,8), %rbx
xorpd %xmm4, %xmm4
movq %r12, 0x78(%rsp)
movq %r13, 0x70(%rsp)
cmpq %r12, %r15
jg 0x92b8
cvtsi2sd %r15, %xmm1
subsd 0x98(%rsp), %xmm1
mulsd %xmm1, %xmm1
movsd 0x58(%rsp), %xmm0
movsd %xmm1, 0xa0(%rsp)
subsd %xmm1, %xmm0
ucomisd 0x2418(%rip), %xmm0     # 0xb468
movsd %xmm4, 0x8(%rsp)
movsd %xmm0, 0x38(%rsp)
jb 0x9064
sqrtsd %xmm0, %xmm1
jmp 0x9073
callq 0x2140
movsd 0x8(%rsp), %xmm4
movapd %xmm0, %xmm1
ucomisd 0x23ed(%rip), %xmm1     # 0xb468
jbe 0x92ad
movq %r15, 0x88(%rsp)
movsd 0x40(%rsp), %xmm0
subsd %xmm1, %xmm0
movsd %xmm1, 0x10(%rsp)
callq 0x2330
xorpd %xmm1, %xmm1
callq 0x7ae9
cvttsd2si %xmm0, %rbp
movsd 0x10(%rsp), %xmm0
addsd 0x40(%rsp), %xmm0
callq 0x2050
movsd 0x30(%rsp), %xmm1
callq 0x7ae0
movsd 0x8(%rsp), %xmm4
cvttsd2si %xmm0, %r12
movq 0x68(%rsp), %rax
leaq (%rax,%rbp,8), %r13
imulq 0x80(%rsp), %r13
movq %r14, 0x90(%rsp)
addq %r14, %r13
movsd 0x18(%rsp), %xmm1
cmpq %r12, %rbp
jg 0x9293
cvtsi2sd %rbp, %xmm2
subsd 0x40(%rsp), %xmm2
mulsd %xmm2, %xmm2
movsd 0x38(%rsp), %xmm0
subsd %xmm2, %xmm0
ucomisd 0x2348(%rip), %xmm0     # 0xb468
movsd %xmm4, 0x8(%rsp)
movsd %xmm2, 0x20(%rsp)
jb 0x9134
sqrtsd %xmm0, %xmm2
jmp 0x9143
callq 0x2140
movsd 0x18(%rsp), %xmm1
movapd %xmm0, %xmm2
ucomisd 0x231d(%rip), %xmm2     # 0xb468
jbe 0x9282
movapd %xmm1, %xmm0
subsd %xmm2, %xmm0
movsd %xmm2, 0x10(%rsp)
callq 0x2330
xorpd %xmm1, %xmm1
callq 0x7ae9
cvttsd2si %xmm0, %r14
movsd 0x10(%rsp), %xmm0
addsd 0x18(%rsp), %xmm0
callq 0x2050
movsd 0x30(%rsp), %xmm1
callq 0x7ae0
movsd 0x18(%rsp), %xmm1
cvttsd2si %xmm0, %r15
movsd 0x20(%rsp), %xmm5
addsd 0xa0(%rsp), %xmm5
movsd 0x50(%rsp), %xmm2
movsd 0x8(%rsp), %xmm4
movsd %xmm5, 0x20(%rsp)
cmpq %r15, %r14
jg 0x9288
cvtsi2sd %r14, %xmm0
subsd %xmm1, %xmm0
mulsd %xmm0, %xmm0
addsd %xmm5, %xmm0
ucomisd %xmm0, %xmm2
jbe 0x91e6
addsd (%r13,%r14,8), %xmm4
jmp 0x927a
movsd 0x58(%rsp), %xmm3
ucomisd %xmm0, %xmm3
jbe 0x927a
sqrtsd %xmm0, %xmm1
movsd 0x48(%rsp), %xmm0
subsd %xmm1, %xmm0
addsd 0x220c(%rip), %xmm0     # 0xb418
movsd %xmm0, 0x10(%rsp)
movsd 0x1e5e(%rip), %xmm1     # 0xb078
movsd %xmm4, 0x8(%rsp)
callq 0x2350
movsd 0x20(%rsp), %xmm5
movsd 0x8(%rsp), %xmm4
movsd 0x10(%rsp), %xmm3
movapd %xmm3, %xmm1
mulsd 0x262d(%rip), %xmm1     # 0xb870
addsd 0x1e3d(%rip), %xmm1     # 0xb088
movapd %xmm3, %xmm2
mulsd 0x2611(%rip), %xmm2     # 0xb868
mulsd %xmm3, %xmm2
addsd %xmm1, %xmm2
movsd 0x18(%rsp), %xmm1
mulsd %xmm0, %xmm2
mulsd (%r13,%r14,8), %xmm2
addsd %xmm2, %xmm4
movsd 0x50(%rsp), %xmm2
incq %r14
jmp 0x91ba
movsd 0x8(%rsp), %xmm4
incq %rbp
addq %rbx, %r13
jmp 0x90f6
movq 0x90(%rsp), %r14
movq 0x88(%rsp), %r15
movq 0x78(%rsp), %r12
movq 0x70(%rsp), %r13
incq %r15
addq %r13, %r14
jmp 0x901a
movsd 0x28(%rsp), %xmm0
mulsd 0x60(%rsp), %xmm0
mulsd %xmm4, %xmm0
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq