// File: /workspace/github_cpp_makefile_star2_O1/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 $0x38, %rsp
movl 0x30(%rdi), %eax
cmpl 0x30(%rsi), %eax
jne 0x1f1d5
movq %rsi, %rcx
movq %rdi, %r14
movl 0x34(%rdi), %eax
cmpl 0x34(%rsi), %eax
jne 0x1f1d5
movslq 0x38(%r14), %rsi
cmpl 0x38(%rcx), %esi
jne 0x1f1d5
movq %rdx, %rbx
movq %rcx, 0x28(%rsp)
movq %rdx, %rdi
callq 0xa084
cmpl $0x0, 0x38(%r14)
jle 0x1f12b
xorl %r13d, %r13d
leaq 0xf3d0(%rip), %r12      # 0x2e198
movq %rbx, 0x30(%rsp)
movq %r14, 0x20(%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 %xmm8, %xmm8
xorpd %xmm9, %xmm9
xorpd %xmm0, %xmm0
cmpl $0x2, %edi
jl 0x1ef79
movq 0x20(%rsp), %rsi
movq 0x8(%rsi), %rax
movq (%rax,%r13,8), %r8
movq 0x28(%rsp), %rdx
movq 0x8(%rdx), %rax
movq (%rax,%r13,8), %r9
movsd (%r9), %xmm1
movsd (%r8), %xmm2
movq (%rsi), %rax
movq %rax, 0x10(%rsp)
movq 0x10(%rsi), %rax
movq %rax, 0x8(%rsp)
movq (%rdx), %rax
movq %rax, (%rsp)
movq 0x10(%rdx), %rbx
movq 0x20(%rsi), %r14
movq 0x20(%rdx), %r15
movq 0x18(%rsi), %r10
movq 0x18(%rdx), %r11
movl $0x1, %esi
movl %esi, %eax
cltd
idivl %ecx
cmpl $0x4, %eax
ja 0x1f13a
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 0x1ef08
movl %edx, %edx
movq 0x10(%rsp), %rax
leaq (%rax,%rdx,8), %rax
movq (%rsp), %rbp
leaq (,%rdx,8), %rdx
addq %rbp, %rdx
jmp 0x1ef08
movl %esi, %eax
cltd
idivl %ecx
shlq $0x3, %rdx
movq (%r10,%r13,8), %rax
addq %rdx, %rax
addq (%r11,%r13,8), %rdx
jmp 0x1ef08
movl %esi, %eax
cltd
idivl %ecx
shlq $0x3, %rdx
movq (%r14,%r13,8), %rax
addq %rdx, %rax
addq (%r15,%r13,8), %rdx
jmp 0x1ef08
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 %xmm3, %xmm3
cvtsi2sd %esi, %xmm3
leal -0x1(%rsi), %ebp
xorps %xmm4, %xmm4
cvtsi2sd %ebp, %xmm4
divsd %xmm3, %xmm4
movsd (%rdx), %xmm5
subsd %xmm1, %xmm5
movsd (%rax), %xmm6
subsd %xmm2, %xmm6
movapd %xmm6, %xmm7
mulsd %xmm6, %xmm7
mulsd %xmm4, %xmm7
addsd %xmm7, %xmm0
movapd %xmm5, %xmm7
mulsd %xmm5, %xmm7
mulsd %xmm4, %xmm7
addsd %xmm7, %xmm9
movapd %xmm5, %xmm7
mulsd %xmm6, %xmm7
mulsd %xmm4, %xmm7
addsd %xmm7, %xmm8
divsd %xmm3, %xmm6
addsd %xmm6, %xmm2
divsd %xmm3, %xmm5
addsd %xmm5, %xmm1
incl %esi
cmpl %esi, %edi
jne 0x1ee81
xorps %xmm3, %xmm3
cvtsi2sd %ecx, %xmm3
divsd %xmm3, %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
movapd %xmm8, 0x10(%rsp)
movsd %xmm3, 0x8(%rsp)
jb 0x1efa4
xorps %xmm4, %xmm4
sqrtsd %xmm0, %xmm4
jmp 0x1efc3
movsd %xmm9, (%rsp)
callq 0x3240
movsd 0x8(%rsp), %xmm3
movsd (%rsp), %xmm9
xorpd %xmm1, %xmm1
movapd %xmm0, %xmm4
movq 0x20(%rsp), %r14
divsd %xmm3, %xmm9
ucomisd %xmm1, %xmm9
jb 0x1efde
xorps %xmm0, %xmm0
sqrtsd %xmm9, %xmm0
jmp 0x1effc
movapd %xmm9, %xmm0
movsd %xmm4, (%rsp)
callq 0x3240
movsd (%rsp), %xmm4
movsd 0x8(%rsp), %xmm3
xorpd %xmm1, %xmm1
movapd 0x10(%rsp), %xmm2
mulsd %xmm0, %xmm4
divsd %xmm3, %xmm2
ucomisd %xmm1, %xmm4
jne 0x1f012
jnp 0x1f018
divsd %xmm4, %xmm2
jmp 0x1f030
ucomisd %xmm1, %xmm2
movsd 0xc034(%rip), %xmm2     # 0x2b058
jne 0x1f1b6
jp 0x1f1b6
ucomisd %xmm2, %xmm2
movq %xmm2, %rbx
jp 0x1f06f
movq %rbx, %rax
btrq $0x3f, %rax
movabsq $0x7ff0000000000000, %rcx # imm = 0x7FF0000000000000
cmpq %rcx, %rax
sete %al
ucomisd 0xbffd(%rip), %xmm2     # 0x2b058
ja 0x1f06f
movsd 0xc00b(%rip), %xmm0     # 0x2b070
ucomisd %xmm2, %xmm0
ja 0x1f06f
testb %al, %al
je 0x1f0cd
movl $0xd, %edx
movq 0x17f55(%rip), %r15     # 0x36fd0
movq %r15, %rdi
leaq 0x101a3(%rip), %rsi     # 0x2f228
movaps %xmm2, 0x10(%rsp)
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), %xmm2
ucomisd %xmm2, %xmm2
jp 0x1f159
btrq $0x3f, %rbx
movabsq $0x7ff0000000000000, %rax # imm = 0x7FF0000000000000
cmpq %rax, %rbx
je 0x1f178
movapd %xmm2, %xmm0
andpd 0xbf15(%rip), %xmm0     # 0x2b010
movsd 0xbf55(%rip), %xmm1     # 0x2b058
ucomisd %xmm0, %xmm1
movq 0x30(%rsp), %rbx
jb 0x1f197
movq (%rbx), %rax
movsd %xmm2, (%rax,%r13,8)
incq %r13
movslq 0x38(%r14), %rax
cmpq %rax, %r13
jl 0x1edd2
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xf3f5(%rip), %rdi      # 0x2e536
leaq 0xf172(%rip), %rsi      # 0x2e2ba
leaq 0x10071(%rip), %rcx     # 0x2f1c0
movl $0x6a0, %edx            # imm = 0x6A0
callq 0x31c0
leaq 0x100d6(%rip), %rdi     # 0x2f236
leaq 0xf153(%rip), %rsi      # 0x2e2ba
leaq 0x10052(%rip), %rcx     # 0x2f1c0
movl $0x6b3, %edx            # imm = 0x6B3
callq 0x31c0
leaq 0x100cb(%rip), %rdi     # 0x2f24a
leaq 0xf134(%rip), %rsi      # 0x2e2ba
leaq 0x10033(%rip), %rcx     # 0x2f1c0
movl $0x6b4, %edx            # imm = 0x6B4
callq 0x31c0
leaq 0x100c0(%rip), %rdi     # 0x2f25e
leaq 0xf115(%rip), %rsi      # 0x2e2ba
leaq 0x10014(%rip), %rcx     # 0x2f1c0
movl $0x6b5, %edx            # imm = 0x6B5
callq 0x31c0
leaq 0x1005e(%rip), %rdi     # 0x2f21b
leaq 0xf0f6(%rip), %rsi      # 0x2e2ba
leaq 0xfff5(%rip), %rcx      # 0x2f1c0
movl $0x6af, %edx            # imm = 0x6AF
callq 0x31c0
leaq 0xff98(%rip), %rdi      # 0x2f174
leaq 0xf0d7(%rip), %rsi      # 0x2e2ba
leaq 0xffd6(%rip), %rcx      # 0x2f1c0
movl $0x672, %edx            # imm = 0x672
callq 0x31c0