// File: /workspace/github_cpp_makefile_star2_O3/kebwi[P]Trifid/src/SteerablePyramid.cpp
// Function: SteerablePyramid::findCorrelations(SteerablePyramid const&, std::vector<double, std::allocator<double>>&) const
void SteerablePyramid::findCorrelations(const SteerablePyramid &sp, vector<Float_t> &lvlCorrs) const
{
	assert(width_ == sp.width_ && height_ == sp.height_ && numLevels_ == sp.numLevels_);
	
	lvlCorrs.resize(numLevels_);
	for (int level = 0; level < numLevels_; level++)
	{
		int totalPixels = (width_ * height_) / (int)(pow(4, level));
		int totalCoeffs = totalPixels * ((level == 0) ? 5 : 4);
		
		lvlCorrs[level] = 0;
		
		//This algorithm is from http://en.wikipedia.org/wiki/Correlation
		Float_t sum_sq_x = 0;
		Float_t sum_sq_y = 0;
		Float_t sum_coproduct = 0;
		Float_t mean_x = b1Coeffs_[level][0];
		Float_t mean_y = sp.b1Coeffs_[level][0];
		Float_t sweep, delta_x, delta_y;
		for (int i = 1; i < totalCoeffs; i++)
		{
			sweep = (i - 1.0) / i;
			
			if (i / totalPixels == 0)
			{
				delta_x = b1Coeffs_[level][i % totalPixels] - mean_x;
				delta_y = sp.b1Coeffs_[level][i % totalPixels] - mean_y;
			}
			else if (i / totalPixels == 1)
			{
				delta_x = b2Coeffs_[level][i % totalPixels] - mean_x;
				delta_y = sp.b2Coeffs_[level][i % totalPixels] - mean_y;
			}
			else if (i / totalPixels == 2)
			{
				delta_x = b3Coeffs_[level][i % totalPixels] - mean_x;
				delta_y = sp.b3Coeffs_[level][i % totalPixels] - mean_y;
			}
			else if (i / totalPixels == 3)
			{
				delta_x = b4Coeffs_[level][i % totalPixels] - mean_x;
				delta_y = sp.b4Coeffs_[level][i % totalPixels] - mean_y;
			}
			else if (i / totalPixels == 4)
			{
				delta_x = h0Coeffs_[i % totalPixels] - mean_x;
				delta_y = sp.h0Coeffs_[i % totalPixels] - mean_y;
			}
			else assert(false);
			
			sum_sq_x += delta_x * delta_x * sweep;
			sum_sq_y += delta_y * delta_y * sweep;
			sum_coproduct += delta_x * delta_y * sweep;
			mean_x += delta_x / i;
			mean_y += delta_y / i;
		}
		Float_t pop_sd_x = sqrt(sum_sq_x / totalPixels);
		Float_t pop_sd_y = sqrt(sum_sq_y / totalPixels);
		Float_t cov_x_y = sum_coproduct / totalPixels;
		Float_t denom = pop_sd_x * pop_sd_y;
		Float_t correlation = 1;
		if (denom != 0)
			correlation = cov_x_y / denom;
		else assert(cov_x_y == 0);	//So long as both numerator and denominator are 0, call the correlation 0/0, or 1...sorta kinda.
		
		if (isnan(correlation) || isinf(correlation) || correlation < -1 || correlation > 1)
			cout << "Correlation: " << correlation << endl;
		assert(!isnan(correlation));
		assert(!isinf(correlation));
		assert(correlation >= -1 && correlation <= 1);
		
		lvlCorrs[level] = correlation;
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movl 0x30(%rdi), %eax
cmpl 0x30(%rsi), %eax
jne 0x1ef34
movq %rsi, %rcx
movq %rdi, %r14
movl 0x34(%rdi), %eax
cmpl 0x34(%rsi), %eax
jne 0x1ef34
movslq 0x38(%r14), %rsi
cmpl 0x38(%rcx), %esi
jne 0x1ef34
movq %rdx, %rbx
movq %rcx, 0x38(%rsp)
movq %rdx, %rdi
callq 0xa036
cmpl $0x0, 0x38(%r14)
jle 0x1ee8a
xorl %r13d, %r13d
leaq 0xe6ce(%rip), %r12      # 0x2d200
movq %rbx, 0x40(%rsp)
movq %r14, 0x28(%rsp)
movl 0x34(%r14), %ebp
imull 0x30(%r14), %ebp
leal (,%r13,2), %eax
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
callq 0x32a0
cvttsd2si %xmm0, %ecx
movl %ebp, %eax
cltd
idivl %ecx
movl %eax, %ecx
cmpq $0x1, %r13
movl $0x0, %edi
adcl $0x4, %edi
imull %eax, %edi
movq (%rbx), %rax
movq $0x0, (%rax,%r13,8)
xorpd %xmm6, %xmm6
xorpd %xmm0, %xmm0
cmpl $0x2, %edi
jl 0x1eccd
movq 0x28(%rsp), %rsi
movq 0x8(%rsi), %rax
movq (%rax,%r13,8), %r8
movq 0x38(%rsp), %rdx
movq 0x8(%rdx), %rax
movq (%rax,%r13,8), %r9
movq (%rsi), %rax
movq %rax, 0x10(%rsp)
movq 0x10(%rsi), %rax
movq %rax, 0x8(%rsp)
movq (%rdx), %rax
movq %rax, 0x30(%rsp)
movq 0x10(%rdx), %rbx
movq 0x20(%rsi), %r14
movq 0x20(%rdx), %r15
movq 0x18(%rsi), %r10
movsd (%r8), %xmm1
movhpd (%r9), %xmm1            # xmm1 = xmm1[0],mem[0]
movq 0x18(%rdx), %r11
movl $0x1, %esi
movl %esi, %eax
cltd
idivl %ecx
cmpl $0x4, %eax
ja 0x1ee99
movslq (%r12,%rax,4), %rax
addq %r12, %rax
jmpq *%rax
movl %esi, %eax
cltd
idivl %ecx
leaq (%r8,%rdx,8), %rax
leaq (%r9,%rdx,8), %rdx
jmp 0x1ec6e
movl %edx, %edx
movq 0x10(%rsp), %rax
leaq (%rax,%rdx,8), %rax
movq 0x30(%rsp), %rbp
leaq (,%rdx,8), %rdx
addq %rbp, %rdx
jmp 0x1ec6e
movl %esi, %eax
cltd
idivl %ecx
shlq $0x3, %rdx
movq (%r10,%r13,8), %rax
addq %rdx, %rax
addq (%r11,%r13,8), %rdx
jmp 0x1ec6e
movl %esi, %eax
cltd
idivl %ecx
shlq $0x3, %rdx
movq (%r14,%r13,8), %rax
addq %rdx, %rax
addq (%r15,%r13,8), %rdx
jmp 0x1ec6e
movl %esi, %eax
cltd
idivl %ecx
shlq $0x3, %rdx
movq 0x8(%rsp), %rax
movq (%rax,%r13,8), %rax
addq %rdx, %rax
addq (%rbx,%r13,8), %rdx
xorps %xmm2, %xmm2
cvtsi2sd %esi, %xmm2
leal -0x1(%rsi), %ebp
xorps %xmm3, %xmm3
cvtsi2sd %ebp, %xmm3
divsd %xmm2, %xmm3
movsd (%rax), %xmm4
movhpd (%rdx), %xmm4           # xmm4 = xmm4[0],mem[0]
subpd %xmm1, %xmm4
movapd %xmm4, %xmm5
mulsd %xmm4, %xmm5
mulsd %xmm3, %xmm5
addsd %xmm5, %xmm0
movapd %xmm4, %xmm5
unpckhpd %xmm4, %xmm5            # xmm5 = xmm5[1],xmm4[1]
mulpd %xmm4, %xmm5
unpcklpd %xmm3, %xmm3            # xmm3 = xmm3[0,0]
mulpd %xmm5, %xmm3
addpd %xmm3, %xmm6
unpcklpd %xmm2, %xmm2            # xmm2 = xmm2[0,0]
divpd %xmm2, %xmm4
addpd %xmm4, %xmm1
incl %esi
cmpl %esi, %edi
jne 0x1ebe6
xorps %xmm2, %xmm2
cvtsi2sd %ecx, %xmm2
divsd %xmm2, %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
movapd %xmm6, 0x10(%rsp)
movsd %xmm2, 0x8(%rsp)
jb 0x1ecf7
xorps %xmm3, %xmm3
sqrtsd %xmm0, %xmm3
jmp 0x1ed10
callq 0x3240
movsd 0x8(%rsp), %xmm2
movapd 0x10(%rsp), %xmm6
xorpd %xmm1, %xmm1
movapd %xmm0, %xmm3
movq 0x28(%rsp), %r14
movapd %xmm6, %xmm0
unpckhpd %xmm6, %xmm0            # xmm0 = xmm0[1],xmm6[1]
divsd %xmm2, %xmm0
ucomisd %xmm1, %xmm0
jb 0x1ed2d
sqrtsd %xmm0, %xmm0
jmp 0x1ed4e
movsd %xmm3, 0x30(%rsp)
callq 0x3240
movsd 0x30(%rsp), %xmm3
movsd 0x8(%rsp), %xmm2
movapd 0x10(%rsp), %xmm6
xorpd %xmm1, %xmm1
mulsd %xmm0, %xmm3
divsd %xmm2, %xmm6
ucomisd %xmm1, %xmm3
jne 0x1ed5e
jnp 0x1ed76
divsd %xmm3, %xmm6
ucomisd %xmm6, %xmm6
jp 0x1ee83
movsd 0xb2e4(%rip), %xmm1     # 0x2a058
jmp 0x1ed92
ucomisd %xmm1, %xmm6
movsd 0xb2d6(%rip), %xmm1     # 0x2a058
movapd %xmm1, %xmm6
jne 0x1ef15
jp 0x1ef15
movq %xmm6, %rax
btrq $0x3f, %rax
movabsq $0x7ff0000000000000, %rcx # imm = 0x7FF0000000000000
cmpq %rcx, %rax
sete %al
xorl %ebx, %ebx
ucomisd %xmm1, %xmm6
ja 0x1edc6
movsd 0xb2b4(%rip), %xmm0     # 0x2a070
ucomisd %xmm6, %xmm0
ja 0x1edc6
testb %al, %al
je 0x1ee34
movaps %xmm6, 0x10(%rsp)
movl $0xd, %edx
movq 0x181f9(%rip), %r15     # 0x36fd0
movq %r15, %rdi
leaq 0xf4af(%rip), %rsi      # 0x2e290
callq 0x3360
movq %r15, %rdi
movapd 0x10(%rsp), %xmm0
callq 0x3480
movq %rax, %rbp
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbp, %rdi
movl $0xa, %esi
callq 0x32e0
movsbl %al, %esi
movq %rbp, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x31f0
movapd 0x10(%rsp), %xmm6
movsd 0xb22c(%rip), %xmm1     # 0x2a058
testb %bl, %bl
jne 0x1eef6
movq %xmm6, %rax
btrq $0x3f, %rax
movabsq $0x7ff0000000000000, %rcx # imm = 0x7FF0000000000000
cmpq %rcx, %rax
je 0x1eed7
movapd %xmm6, %xmm0
andpd 0xb1b3(%rip), %xmm0     # 0x2a010
ucomisd %xmm0, %xmm1
movq 0x40(%rsp), %rbx
jb 0x1eeb8
movq (%rbx), %rax
movsd %xmm6, (%rax,%r13,8)
incq %r13
movslq 0x38(%r14), %rax
cmpq %rax, %r13
jl 0x1eb3c
jmp 0x1ee8a
movb $0x1, %bl
jmp 0x1edc6
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xe6fe(%rip), %rdi      # 0x2d59e
leaq 0xe47b(%rip), %rsi      # 0x2d322
leaq 0xf37a(%rip), %rcx      # 0x2e228
movl $0x6a0, %edx            # imm = 0x6A0
callq 0x31c0
leaq 0xf407(%rip), %rdi      # 0x2e2c6
leaq 0xe45c(%rip), %rsi      # 0x2d322
leaq 0xf35b(%rip), %rcx      # 0x2e228
movl $0x6b5, %edx            # imm = 0x6B5
callq 0x31c0
leaq 0xf3d4(%rip), %rdi      # 0x2e2b2
leaq 0xe43d(%rip), %rsi      # 0x2d322
leaq 0xf33c(%rip), %rcx      # 0x2e228
movl $0x6b4, %edx            # imm = 0x6B4
callq 0x31c0
leaq 0xf3a1(%rip), %rdi      # 0x2e29e
leaq 0xe41e(%rip), %rsi      # 0x2d322
leaq 0xf31d(%rip), %rcx      # 0x2e228
movl $0x6b3, %edx            # imm = 0x6B3
callq 0x31c0
leaq 0xf367(%rip), %rdi      # 0x2e283
leaq 0xe3ff(%rip), %rsi      # 0x2d322
leaq 0xf2fe(%rip), %rcx      # 0x2e228
movl $0x6af, %edx            # imm = 0x6AF
callq 0x31c0
leaq 0xf2a1(%rip), %rdi      # 0x2e1dc
leaq 0xe3e0(%rip), %rsi      # 0x2d322
leaq 0xf2df(%rip), %rcx      # 0x2e228
movl $0x672, %edx            # imm = 0x672
callq 0x31c0
nop