// File: /workspace/github_c_makefile_star2_O1/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
movsd %xmm7, 0x38(%rsp)
movsd %xmm4, 0x10(%rsp)
movsd %xmm2, 0x20(%rsp)
movq %rdi, 0x70(%rsp)
movapd %xmm1, %xmm3
movapd %xmm1, %xmm2
movsd %xmm1, 0x18(%rsp)
subsd %xmm0, %xmm3
movapd %xmm0, %xmm1
movsd %xmm0, 0x8(%rsp)
movq %rsi, 0x68(%rsp)
leaq -0x1(%rsi), %rax
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
movsd %xmm0, 0x48(%rsp)
divsd %xmm0, %xmm3
movsd %xmm3, 0x30(%rsp)
movapd %xmm3, %xmm0
mulsd %xmm3, %xmm0
movsd %xmm0, 0x60(%rsp)
movsd 0x1d21(%rip), %xmm0     # 0xb428
mulsd %xmm3, %xmm0
movsd %xmm0, 0x40(%rsp)
movapd %xmm0, %xmm3
movsd %xmm6, 0x58(%rsp)
addsd %xmm6, %xmm3
movsd %xmm3, 0x28(%rsp)
movapd %xmm7, %xmm0
subsd %xmm3, %xmm0
callq 0x81d8
subsd 0x8(%rsp), %xmm0
divsd 0x30(%rsp), %xmm0
callq 0x2370
xorpd %xmm1, %xmm1
callq 0x81b9
cvttsd2si %xmm0, %r14
movsd 0x28(%rsp), %xmm0
addsd 0x38(%rsp), %xmm0
movsd 0x8(%rsp), %xmm1
movsd 0x18(%rsp), %xmm2
callq 0x81d8
subsd 0x8(%rsp), %xmm0
divsd 0x30(%rsp), %xmm0
callq 0x2050
movsd 0x48(%rsp), %xmm1
callq 0x81b0
xorpd %xmm3, %xmm3
cvttsd2si %xmm0, %rax
movq %rax, 0x88(%rsp)
cmpq %rax, %r14
jg 0x9b36
movsd 0xe8(%rsp), %xmm3
movsd 0xe0(%rsp), %xmm4
movsd 0x28(%rsp), %xmm1
mulsd %xmm1, %xmm1
movsd 0x60(%rsp), %xmm0
divsd %xmm0, %xmm1
movsd %xmm1, 0x28(%rsp)
movsd 0x58(%rsp), %xmm1
movapd %xmm1, %xmm2
subsd 0x40(%rsp), %xmm2
mulsd %xmm2, %xmm2
divsd %xmm0, %xmm2
movsd %xmm2, 0x40(%rsp)
movsd 0x38(%rsp), %xmm0
subsd 0x8(%rsp), %xmm0
movsd 0x30(%rsp), %xmm2
divsd %xmm2, %xmm0
movsd %xmm0, 0x38(%rsp)
subsd 0x20(%rsp), %xmm4
divsd %xmm2, %xmm4
movsd %xmm4, 0x50(%rsp)
subsd 0x10(%rsp), %xmm3
divsd %xmm2, %xmm3
movsd %xmm3, 0x10(%rsp)
divsd %xmm2, %xmm1
movsd %xmm1, 0x58(%rsp)
movq 0x68(%rsp), %rax
movq %rax, %rcx
imulq %rax, %rcx
shlq $0x3, %rcx
movq %rcx, 0x80(%rsp)
movq %r14, %rcx
imulq %rax, %rcx
shlq $0x3, %rcx
movq %rcx, 0x78(%rsp)
leaq (,%rax,8), %rbx
xorpd %xmm3, %xmm3
xorps %xmm1, %xmm1
cvtsi2sd %r14, %xmm1
subsd 0x38(%rsp), %xmm1
mulsd %xmm1, %xmm1
movsd 0x28(%rsp), %xmm0
movsd %xmm1, 0x98(%rsp)
subsd %xmm1, %xmm0
ucomisd 0x1beb(%rip), %xmm0     # 0xb480
movq %r14, 0x90(%rsp)
movsd %xmm3, 0x8(%rsp)
movsd %xmm0, 0xa0(%rsp)
jb 0x98b7
xorps %xmm1, %xmm1
sqrtsd %xmm0, %xmm1
jmp 0x98c6
callq 0x2160
movsd 0x8(%rsp), %xmm3
movapd %xmm0, %xmm1
ucomisd 0x1bb2(%rip), %xmm1     # 0xb480
jbe 0x9b0f
movsd 0x50(%rsp), %xmm0
subsd %xmm1, %xmm0
movsd %xmm1, 0x18(%rsp)
callq 0x2370
xorpd %xmm1, %xmm1
callq 0x81b9
cvttsd2si %xmm0, %rbp
movsd 0x18(%rsp), %xmm0
addsd 0x50(%rsp), %xmm0
callq 0x2050
movsd 0x48(%rsp), %xmm1
callq 0x81b0
cvttsd2si %xmm0, %r12
cmpq %r12, %rbp
jle 0x9928
movsd 0x8(%rsp), %xmm3
jmp 0x9b0f
movq 0x78(%rsp), %rax
leaq (%rax,%rbp,8), %r13
imulq 0x68(%rsp), %r13
addq 0x70(%rsp), %r13
movsd 0x10(%rsp), %xmm2
movsd 0x8(%rsp), %xmm3
xorps %xmm1, %xmm1
cvtsi2sd %rbp, %xmm1
subsd 0x50(%rsp), %xmm1
mulsd %xmm1, %xmm1
movsd 0xa0(%rsp), %xmm0
subsd %xmm1, %xmm0
ucomisd 0x1b11(%rip), %xmm0     # 0xb480
movsd %xmm3, 0x8(%rsp)
movsd %xmm1, 0x20(%rsp)
jb 0x9986
xorps %xmm1, %xmm1
sqrtsd %xmm0, %xmm1
jmp 0x9995
callq 0x2160
movsd 0x10(%rsp), %xmm2
movapd %xmm0, %xmm1
ucomisd 0x1ae3(%rip), %xmm1     # 0xb480
jbe 0x99ec
movapd %xmm2, %xmm0
subsd %xmm1, %xmm0
movsd %xmm1, 0x18(%rsp)
callq 0x2370
xorpd %xmm1, %xmm1
callq 0x81b9
cvttsd2si %xmm0, %r14
movsd 0x18(%rsp), %xmm0
addsd 0x10(%rsp), %xmm0
callq 0x2050
movsd 0x48(%rsp), %xmm1
callq 0x81b0
cvttsd2si %xmm0, %r15
cmpq %r15, %r14
jle 0x99f7
movsd 0x10(%rsp), %xmm2
movsd 0x8(%rsp), %xmm3
jmp 0x9afc
movsd 0x20(%rsp), %xmm5
addsd 0x98(%rsp), %xmm5
incq %r15
movsd 0x10(%rsp), %xmm2
movsd 0x8(%rsp), %xmm3
movsd 0x40(%rsp), %xmm4
movsd %xmm5, 0x20(%rsp)
xorps %xmm0, %xmm0
cvtsi2sd %r14, %xmm0
subsd %xmm2, %xmm0
mulsd %xmm0, %xmm0
addsd %xmm5, %xmm0
ucomisd %xmm0, %xmm4
jbe 0x9a47
addsd (%r13,%r14,8), %xmm3
jmp 0x9af0
movsd 0x28(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x9af0
movsd %xmm3, 0x8(%rsp)
ucomisd 0x1a1b(%rip), %xmm0     # 0xb480
jb 0x9a6d
sqrtsd %xmm0, %xmm0
jmp 0x9a72
callq 0x2160
movsd 0x58(%rsp), %xmm1
subsd %xmm0, %xmm1
addsd 0x19a4(%rip), %xmm1     # 0xb428
movsd %xmm1, 0x18(%rsp)
movapd %xmm1, %xmm0
movsd 0x15e2(%rip), %xmm1     # 0xb078
callq 0x2390
movsd 0x18(%rsp), %xmm3
movapd %xmm3, %xmm1
mulsd 0x1e23(%rip), %xmm1     # 0xb8d0
addsd 0x15d3(%rip), %xmm1     # 0xb088
movapd %xmm3, %xmm2
mulsd 0x1e07(%rip), %xmm2     # 0xb8c8
mulsd %xmm3, %xmm2
addsd %xmm1, %xmm2
mulsd %xmm0, %xmm2
mulsd (%r13,%r14,8), %xmm2
movsd 0x8(%rsp), %xmm3
addsd %xmm2, %xmm3
movsd 0x10(%rsp), %xmm2
movsd 0x40(%rsp), %xmm4
movsd 0x20(%rsp), %xmm5
incq %r14
cmpq %r14, %r15
jne 0x9a21
leaq 0x1(%rbp), %rax
addq %rbx, %r13
cmpq %r12, %rbp
movq %rax, %rbp
jne 0x9948
movq 0x90(%rsp), %rcx
leaq 0x1(%rcx), %r14
movq 0x80(%rsp), %rdx
addq %rdx, 0x70(%rsp)
cmpq 0x88(%rsp), %rcx
jne 0x9868
movsd 0x30(%rsp), %xmm0
mulsd 0x60(%rsp), %xmm0
mulsd %xmm3, %xmm0
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq