// File: /workspace/github_cpp_makefile_star2_O2/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 0x18f37
movq %rsi, %rcx
movq %rdi, %r14
movl 0x34(%rdi), %eax
cmpl 0x34(%rsi), %eax
jne 0x18f37
movslq 0x38(%r14), %rsi
cmpl 0x38(%rcx), %esi
jne 0x18f37
movq %rdx, %rbx
movq %rcx, 0x30(%rsp)
movq %rdx, %rdi
callq 0x80fc
xorl %r12d, %r12d
leaq 0xc613(%rip), %r13      # 0x251a0
movq %rbx, 0x28(%rsp)
movq %r14, 0x20(%rsp)
movslq 0x38(%r14), %rax
cmpq %rax, %r12
jge 0x18e8d
movl 0x34(%r14), %ebp
imull 0x30(%r14), %ebp
leal (%r12,%r12), %eax
cvtsi2sd %rax, %xmm0
callq 0x3280
cvttsd2si %xmm0, %ecx
movl %ebp, %eax
cltd
idivl %ecx
movl %eax, %ecx
xorl %esi, %esi
cmpq $0x1, %r12
movl $0x0, %edi
adcl $0x4, %edi
imull %eax, %edi
movq (%rbx), %rax
andq $0x0, (%rax,%r12,8)
movq (%r14), %rax
movq %rax, (%rsp)
movq 0x8(%r14), %rax
movq (%rax,%r12,8), %r9
movq 0x30(%rsp), %rdx
movq (%rdx), %rax
movq %rax, 0x8(%rsp)
movq 0x8(%rdx), %rax
movq (%rax,%r12,8), %r11
movq 0x20(%r14), %rax
movq %rax, 0x40(%rsp)
movq 0x20(%rdx), %rax
movq %rax, 0x38(%rsp)
movq %r14, %rax
movq 0x18(%r14), %r14
movq 0x18(%rdx), %r15
movq 0x10(%rax), %r8
movq 0x10(%rdx), %r10
movsd (%r9), %xmm1
movhpd (%r11), %xmm1           # xmm1 = xmm1[0],mem[0]
xorpd %xmm6, %xmm6
xorpd %xmm0, %xmm0
movl %esi, %ebp
leal 0x1(%rbp), %esi
cmpl %edi, %esi
jge 0x18d1e
movl %esi, %eax
cltd
idivl %ecx
cmpl $0x4, %eax
ja 0x18e9c
movslq (%r13,%rax,4), %rax
addq %r13, %rax
jmpq *%rax
movl %esi, %eax
cltd
idivl %ecx
leaq (%r9,%rdx,8), %rax
leaq (%r11,%rdx,8), %rdx
jmp 0x18ccd
movl %edx, %edx
movq (%rsp), %rax
leaq (%rax,%rdx,8), %rax
movq 0x8(%rsp), %rbx
leaq (%rbx,%rdx,8), %rdx
jmp 0x18ccd
movl %esi, %eax
cltd
idivl %ecx
shlq $0x3, %rdx
movq (%r14,%r12,8), %rax
addq %rdx, %rax
addq (%r15,%r12,8), %rdx
jmp 0x18ccd
movl %esi, %eax
cltd
idivl %ecx
shlq $0x3, %rdx
movq 0x40(%rsp), %rax
movq (%rax,%r12,8), %rax
addq %rdx, %rax
movq 0x38(%rsp), %rbx
addq (%rbx,%r12,8), %rdx
jmp 0x18ccd
movl %esi, %eax
cltd
idivl %ecx
shlq $0x3, %rdx
movq (%r8,%r12,8), %rax
addq %rdx, %rax
addq (%r10,%r12,8), %rdx
cvtsi2sd %esi, %xmm2
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
jmp 0x18c3a
cvtsi2sd %ecx, %xmm2
divsd %xmm2, %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
movapd %xmm6, 0x10(%rsp)
movsd %xmm2, (%rsp)
jb 0x18d41
sqrtsd %xmm0, %xmm3
jmp 0x18d59
callq 0x3220
movsd (%rsp), %xmm2
movapd 0x10(%rsp), %xmm6
xorpd %xmm1, %xmm1
movapd %xmm0, %xmm3
movq 0x20(%rsp), %r14
movabsq $0x7ff0000000000000, %r15 # imm = 0x7FF0000000000000
movapd %xmm6, %xmm0
unpckhpd %xmm6, %xmm0            # xmm0 = xmm0[1],xmm6[1]
divsd %xmm2, %xmm0
ucomisd %xmm1, %xmm0
jb 0x18d80
sqrtsd %xmm0, %xmm0
jmp 0x18da0
movsd %xmm3, 0x8(%rsp)
callq 0x3220
movsd 0x8(%rsp), %xmm3
movsd (%rsp), %xmm2
movapd 0x10(%rsp), %xmm6
xorpd %xmm1, %xmm1
mulsd %xmm0, %xmm3
divsd %xmm2, %xmm6
ucomisd %xmm1, %xmm3
jne 0x18db0
jnp 0x18dc8
divsd %xmm3, %xmm6
ucomisd %xmm6, %xmm6
jp 0x18e89
movsd 0x9292(%rip), %xmm1     # 0x22058
jmp 0x18de4
ucomisd %xmm1, %xmm6
movsd 0x9284(%rip), %xmm1     # 0x22058
movapd %xmm1, %xmm6
jne 0x18f18
jp 0x18f18
movq %xmm6, %rax
btrq $0x3f, %rax
cmpq %r15, %rax
sete %al
xorl %ebx, %ebx
ucomisd %xmm1, %xmm6
ja 0x18e0e
movsd 0x926c(%rip), %xmm0     # 0x22070
ucomisd %xmm6, %xmm0
ja 0x18e0e
testb %al, %al
je 0x18e52
movaps %xmm6, 0x10(%rsp)
movq 0x171b6(%rip), %rdi     # 0x2ffd0
leaq 0xd40f(%rip), %rsi      # 0x26230
callq 0x32c0
movq %rax, %rdi
movapd 0x10(%rsp), %xmm0
callq 0x3460
movq %rax, %rdi
callq 0x3120
movapd 0x10(%rsp), %xmm6
movsd 0x920e(%rip), %xmm1     # 0x22058
testb %bl, %bl
jne 0x18ef9
movq %xmm6, %rax
btrq $0x3f, %rax
cmpq %r15, %rax
je 0x18eda
movapd %xmm6, %xmm0
andpd 0x91a3(%rip), %xmm0     # 0x22010
ucomisd %xmm0, %xmm1
movq 0x28(%rsp), %rbx
jb 0x18ebb
movq (%rbx), %rax
movsd %xmm6, (%rax,%r12,8)
incq %r12
jmp 0x18b97
movb $0x1, %bl
jmp 0x18e0e
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xc69b(%rip), %rdi      # 0x2553e
leaq 0xc418(%rip), %rsi      # 0x252c2
leaq 0xd317(%rip), %rcx      # 0x261c8
movl $0x6a0, %edx            # imm = 0x6A0
callq 0x31a0
leaq 0xd3a4(%rip), %rdi      # 0x26266
leaq 0xc3f9(%rip), %rsi      # 0x252c2
leaq 0xd2f8(%rip), %rcx      # 0x261c8
movl $0x6b5, %edx            # imm = 0x6B5
callq 0x31a0
leaq 0xd371(%rip), %rdi      # 0x26252
leaq 0xc3da(%rip), %rsi      # 0x252c2
leaq 0xd2d9(%rip), %rcx      # 0x261c8
movl $0x6b4, %edx            # imm = 0x6B4
callq 0x31a0
leaq 0xd33e(%rip), %rdi      # 0x2623e
leaq 0xc3bb(%rip), %rsi      # 0x252c2
leaq 0xd2ba(%rip), %rcx      # 0x261c8
movl $0x6b3, %edx            # imm = 0x6B3
callq 0x31a0
leaq 0xd304(%rip), %rdi      # 0x26223
leaq 0xc39c(%rip), %rsi      # 0x252c2
leaq 0xd29b(%rip), %rcx      # 0x261c8
movl $0x6af, %edx            # imm = 0x6AF
callq 0x31a0
leaq 0xd23e(%rip), %rdi      # 0x2617c
leaq 0xc37d(%rip), %rsi      # 0x252c2
leaq 0xd27c(%rip), %rcx      # 0x261c8
movl $0x672, %edx            # imm = 0x672
callq 0x31a0