// File: /workspace/github_cpp_makefile_star2_O3/kebwi[P]Trifid/src/FourierTransform.cpp
// Function: generatePowerSpectrum(ImageReal const&, ImageReal&, GainType, bool)
void generatePowerSpectrum(const ImageReal &ft, ImageReal &ps, GainType gainType, bool shiftCenter)
{
	long dim = ft.width();
	assert(ft.height() == dim);
	long halfDim = dim / 2;
	int totalPixels = ft.totalPixels();
	
	ps.resize(ft.width(), ft.height());
	
	ImageReal psDCinCorner(ft.width(), ft.height());
	
	//Main body
	for (int y = 0; y < dim; y++)
		for (int x = 2; x < dim; x += 2)
			psDCinCorner(x / 2, y) = pow(ft(x, y), 2) + pow(ft(x + 1, y), 2);
	
	//Upper left corner (DC term)
	psDCinCorner(0, 0) = pow(ft(0, 0), 2) + pow(ft(1, 0), 2);
	
	//Left edge (DC stuff)
	for (int y = 1; y < halfDim; y++)
		psDCinCorner(0, y) = psDCinCorner(0, dim - y) = pow(ft(0, y), 2) + pow(ft(1, y), 2);
	psDCinCorner(0, halfDim) = pow(ft(1, 0), 2);
	
	//Center column (Nyquist)
	for (int y = 1; y < halfDim; y++)
		psDCinCorner(halfDim, y) = psDCinCorner(halfDim, dim - y) =
			pow(ft(0, halfDim + y), 2) + pow(ft(1, halfDim + y), 2);
	psDCinCorner(halfDim, 0) = pow(ft(0, halfDim), 2);
	psDCinCorner(halfDim, halfDim) = pow(ft(1, halfDim), 2);
	
	//Reflection of main body into right half of image
	for (int y = 1; y < dim; y++)
		for (int x = 1; x < halfDim; x++)
			psDCinCorner(halfDim + x, y) = psDCinCorner(halfDim - x, dim - y);
	
	//Reflection along top edge (DC stuff)
	for (int x = 1; x <= halfDim; x++)
		psDCinCorner(dim - x, 0) = psDCinCorner(x, 0);
	
	for (int i = 0; i < totalPixels; i++)
		assert(psDCinCorner[i] >= 0);
	
	if (gainType != GT_NONE)
	{
		//Gain
		for (int i = 0; i < totalPixels; i++)
			switch (gainType)
			{
				case GT_LINEAR:
					//psArr[y * dim + x] /= sInvertedGain;
					psDCinCorner[i] /= sInvertedGain;
					break;
				case GT_SQRT3:
					//psArr[y * dim + x] = sqrt(sqrt(sqrt(psArr[y * dim + x])));
					psDCinCorner[i] = sqrt(sqrt(sqrt(psDCinCorner[i])));
					break;
				case GT_LOG10:
					//psArr[y * dim + x] = log10(psArr[y * dim + x]);
					if (psDCinCorner[i] > 0)
						psDCinCorner[i] = log10(psDCinCorner[i]);
					else psDCinCorner[i] = 0;
					break;
				case GT_LOGLOG10:
					//psArr[y * dim + x] = log10(log10(psArr[y * dim + x]));
					if (psDCinCorner[i] > 0)
						psDCinCorner[i] = log10(log10(psDCinCorner[i]));
					else psDCinCorner[i] = 0;
					break;
				case GT_NONE:
					//I just added this case statement to make a warning go away
					assert(false);
			}
		
		for (int i = 0; i < totalPixels; i++)
			assert(!isnan(psDCinCorner[i]));
	}
	
	//Floor nonnegative
	for (int i = 0; i < totalPixels; i++)
		if (psDCinCorner[i] < 0)
			psDCinCorner[i] = 0;
	
	//Normalize
	if (gainType != GT_LINEAR)
	{
		Float_t maxVal = 0;
		for (int i = 0; i < totalPixels; i++)
			if (psDCinCorner[i] > maxVal)
				maxVal = psDCinCorner[i];
		assert(maxVal > 0);
		for (int i = 0; i < totalPixels; i++)
			psDCinCorner[i] /= maxVal;
	}
	
	//Floor/Ceiling to [0-1]
	for (int i = 0; i < totalPixels; i++)
		if (psDCinCorner[i] < 0)
			psDCinCorner[i] = 0;
		else if (psDCinCorner[i] > 1.0)
			psDCinCorner[i] = 1.0;
	
	for (int i = 0; i < totalPixels; i++)
		assert(!isnan(psDCinCorner[i]));
	
	//180 degree phase shift (put DC term in center instead of upper left corner)
	//This is wasteful, the power spectrum should be produced into this arrangement in the first place
	if (shiftCenter)
		for (int y = 0; y < dim; y++)
			for (int x = 0; x < dim; x++)
				ps(x, y) = psDCinCorner((x + halfDim) % dim, (y + halfDim) % dim);
	else
		for (int y = 0; y < dim; y++)
			for (int x = 0; x < dim; x++)
				ps(x, y) = psDCinCorner(x, y);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movl %ecx, 0x4(%rsp)
movl 0x28(%rdi), %ebx
cmpl %ebx, 0x2c(%rdi)
jne 0x19489
movl %edx, %r15d
movq %rdi, %r12
movl %ebx, %r13d
shrl $0x1f, %r13d
addl %ebx, %r13d
movl 0x30(%rdi), %ebp
movq (%rsi), %rax
movq %rsi, 0x8(%rsp)
movq %rsi, %rdi
movl %ebx, %esi
movl %ebx, %edx
callq *0x28(%rax)
movl 0x28(%r12), %esi
movl 0x2c(%r12), %edx
leaq 0x18(%rsp), %rdi
callq 0xcfba
testl %ebx, %ebx
jle 0x18fae
movq 0x38(%r12), %rcx
movq %r12, 0x10(%rsp)
movl 0x28(%r12), %edx
movq 0x50(%rsp), %rax
xorl %esi, %esi
movl $0x3, %edi
movl $0x1, %r8d
movl 0x40(%rsp), %r9d
cmpl $0x3, %ebx
jb 0x18f9c
movl $0x2, %r10d
movl %r8d, %r11d
movl %edi, %r14d
leal -0x1(%r14), %r12d
movsd (%rcx,%r12,8), %xmm0
mulsd %xmm0, %xmm0
movl %r14d, %r12d
movsd (%rcx,%r12,8), %xmm1
mulsd %xmm1, %xmm1
addsd %xmm0, %xmm1
movl %r11d, %r12d
movsd %xmm1, (%rax,%r12,8)
addq $0x2, %r10
addl $0x2, %r14d
incl %r11d
cmpq %rbx, %r10
jb 0x18f64
incl %esi
addl %edx, %edi
addl %r9d, %r8d
cmpl %ebx, %esi
jne 0x18f53
movq 0x10(%rsp), %r12
jmp 0x18fb8
movq 0x38(%r12), %rcx
movq 0x50(%rsp), %rax
sarl %r13d
movupd (%rcx), %xmm0
mulpd %xmm0, %xmm0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movsd %xmm1, (%rax)
movl 0x40(%rsp), %edx
cmpl $0x3, %ebx
jle 0x190cc
movl 0x28(%r12), %esi
cmpl $0x3, %r13d
movl $0x2, %edi
cmovgel %r13d, %edi
leal -0x1(%rbx), %r8d
imull %edx, %r8d
decq %rdi
movl %esi, %r9d
movl %edx, %r10d
movl %r9d, %r11d
movsd (%rcx,%r11,8), %xmm0
mulsd %xmm0, %xmm0
leal 0x1(%r9), %r11d
movsd (%rcx,%r11,8), %xmm1
mulsd %xmm1, %xmm1
addsd %xmm0, %xmm1
movl %r8d, %r11d
movsd %xmm1, (%rax,%r11,8)
movl %r10d, %r11d
movsd %xmm1, (%rax,%r11,8)
subl %edx, %r8d
addl %edx, %r10d
addl %esi, %r9d
decq %rdi
jne 0x19003
movl %edx, %esi
imull %r13d, %esi
movsd 0x8(%rcx), %xmm0
mulsd %xmm0, %xmm0
movsd %xmm0, (%rax,%rsi,8)
cmpl $0x4, %ebx
jl 0x190e0
cmpl $0x3, %r13d
movl $0x2, %esi
cmovgel %r13d, %esi
movl 0x28(%r12), %edi
leal -0x1(%rbx), %r8d
imull %edx, %r8d
addl %r13d, %r8d
leal 0x1(%r13), %r9d
imull %edi, %r9d
leal (%rdx,%r13), %r10d
decq %rsi
movl %r9d, %r11d
movsd (%rcx,%r11,8), %xmm0
mulsd %xmm0, %xmm0
leal 0x1(%r9), %r11d
movsd (%rcx,%r11,8), %xmm1
mulsd %xmm1, %xmm1
addsd %xmm0, %xmm1
movl %r8d, %r11d
movsd %xmm1, (%rax,%r11,8)
movl %r10d, %r11d
movsd %xmm1, (%rax,%r11,8)
subl %edx, %r8d
addl %edi, %r9d
addl %edx, %r10d
decq %rsi
jne 0x1908b
jmp 0x190e0
movsd 0x8(%rcx), %xmm0
mulsd %xmm0, %xmm0
movl %edx, %esi
imull %r13d, %esi
movsd %xmm0, (%rax,%rsi,8)
movl %r13d, %r14d
movl 0x28(%r12), %esi
imull %r13d, %esi
movsd (%rcx,%rsi,8), %xmm0
mulsd %xmm0, %xmm0
movsd %xmm0, (%rax,%r14,8)
incl %esi
movsd (%rcx,%rsi,8), %xmm0
mulsd %xmm0, %xmm0
leal 0x1(%rdx), %ecx
imull %r13d, %ecx
movsd %xmm0, (%rax,%rcx,8)
cmpl $0x2, %ebx
jl 0x1919b
cmpl $0x3, %r13d
movl $0x2, %ecx
cmovgel %r13d, %ecx
leal (%rdx,%r13), %esi
incl %esi
leal -0x1(%rbx), %edi
imull %edx, %edi
addl %r13d, %edi
decl %edi
decq %rcx
movl $0x1, %r8d
movq %rcx, %r9
movl %edi, %r10d
movl %esi, %r11d
cmpl $0x4, %ebx
jl 0x1916d
movl %r10d, %r12d
movsd (%rax,%r12,8), %xmm0
movl %r11d, %r12d
movsd %xmm0, (%rax,%r12,8)
incl %r11d
decl %r10d
decq %r9
jne 0x19150
incl %r8d
addl %edx, %esi
subl %edx, %edi
cmpl %ebx, %r8d
jne 0x19142
leal 0x1(%r13), %ecx
leal -0x1(%rbx), %edx
movl $0x1, %esi
movsd (%rax,%rsi,8), %xmm0
movl %edx, %edi
movsd %xmm0, (%rax,%rdi,8)
incq %rsi
decl %edx
cmpq %rsi, %rcx
jne 0x19185
testl %ebp, %ebp
jle 0x192b8
xorl %ecx, %ecx
xorpd %xmm0, %xmm0
movsd (%rax,%rcx,8), %xmm1
ucomisd %xmm0, %xmm1
jb 0x1942c
incq %rcx
cmpq %rcx, %rbp
jne 0x191a9
testl %r15d, %r15d
je 0x19297
testl %ebp, %ebp
jle 0x192b8
xorl %r12d, %r12d
xorpd %xmm1, %xmm1
cmpl $0x4, %r15d
je 0x1920e
cmpl $0x3, %r15d
je 0x19200
cmpl $0x2, %r15d
jne 0x1926e
movsd (%rax,%r12,8), %xmm0
ucomisd %xmm1, %xmm0
jb 0x19230
sqrtsd %xmm0, %xmm0
jmp 0x19239
movsd (%rax,%r12,8), %xmm0
ucomisd %xmm1, %xmm0
ja 0x1921f
jmp 0x19226
movsd (%rax,%r12,8), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x19226
callq 0x3150
callq 0x3150
jmp 0x1925f
movq $0x0, (%rax,%r12,8)
jmp 0x1926e
callq 0x3240
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x19245
sqrtsd %xmm0, %xmm0
jmp 0x1924e
callq 0x3240
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x1925a
sqrtsd %xmm0, %xmm0
jmp 0x19263
callq 0x3240
xorpd %xmm1, %xmm1
movq 0x50(%rsp), %rax
movsd %xmm0, (%rax,%r12,8)
incq %r12
cmpq %r12, %rbp
jne 0x191d8
testl %ebp, %ebp
jle 0x192b8
xorl %ecx, %ecx
movsd (%rax,%rcx,8), %xmm0
ucomisd %xmm0, %xmm0
jp 0x1946a
incq %rcx
cmpq %rcx, %rbp
jne 0x19280
testl %ebp, %ebp
jle 0x192b8
xorl %ecx, %ecx
xorpd %xmm0, %xmm0
ucomisd (%rax,%rcx,8), %xmm0
jbe 0x192b0
movq $0x0, (%rax,%rcx,8)
incq %rcx
cmpq %rcx, %rbp
jne 0x192a1
cmpl $0x1, %r15d
je 0x1930b
testl %ebp, %ebp
jle 0x194a8
xorpd %xmm0, %xmm0
xorl %ecx, %ecx
movapd %xmm0, %xmm1
movsd (%rax,%rcx,8), %xmm0
maxsd %xmm1, %xmm0
incq %rcx
cmpq %rcx, %rbp
jne 0x192cc
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x194a8
testl %ebp, %ebp
jle 0x1935c
xorl %ecx, %ecx
movsd (%rax,%rcx,8), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, (%rax,%rcx,8)
incq %rcx
cmpq %rcx, %rbp
jne 0x192f5
testl %ebp, %ebp
jle 0x1935c
xorl %ecx, %ecx
movsd 0x10d3f(%rip), %xmm0    # 0x2a058
movsd (%rax,%rcx,8), %xmm2
xorpd %xmm1, %xmm1
ucomisd %xmm2, %xmm1
ja 0x19332
ucomisd %xmm0, %xmm2
movapd %xmm0, %xmm1
jbe 0x19337
movsd %xmm1, (%rax,%rcx,8)
incq %rcx
cmpq %rcx, %rbp
jne 0x19319
testl %ebp, %ebp
jle 0x1935c
xorl %ecx, %ecx
movsd (%rax,%rcx,8), %xmm0
ucomisd %xmm0, %xmm0
jp 0x1944b
incq %rcx
cmpq %rcx, %rbp
jne 0x19345
cmpb $0x0, 0x4(%rsp)
je 0x193c5
testl %ebx, %ebx
jle 0x19413
movq 0x50(%rsp), %rsi
movl 0x40(%rsp), %edi
movq 0x8(%rsp), %rax
movq 0x38(%rax), %r8
movl 0x28(%rax), %r9d
xorl %r10d, %r10d
xorl %r11d, %r11d
leal (%r11,%r13), %eax
xorl %edx, %edx
divl %ebx
movl %edx, %ecx
imull %edi, %ecx
xorl %r15d, %r15d
leal (%r14,%r15), %eax
xorl %edx, %edx
divl %ebx
addl %ecx, %edx
movsd (%rsi,%rdx,8), %xmm0
leal (%r10,%r15), %eax
movsd %xmm0, (%r8,%rax,8)
incq %r15
cmpq %r15, %rbx
jne 0x19397
incl %r11d
addq %r9, %r10
cmpl %ebx, %r11d
jne 0x19387
jmp 0x19413
testl %ebx, %ebx
jle 0x19413
movq 0x50(%rsp), %rax
movl 0x40(%rsp), %ecx
movq 0x8(%rsp), %rsi
movq 0x38(%rsi), %rdx
movl 0x28(%rsi), %esi
xorl %edi, %edi
xorl %r8d, %r8d
xorl %r9d, %r9d
xorl %r10d, %r10d
leal (%rdi,%r10), %r11d
movsd (%rax,%r11,8), %xmm0
leal (%r8,%r10), %r11d
movsd %xmm0, (%rdx,%r11,8)
incq %r10
cmpq %r10, %rbx
jne 0x193e9
incl %r9d
addq %rsi, %r8
addq %rcx, %rdi
cmpl %ebx, %r9d
jne 0x193e6
leaq 0x18(%rsp), %rdi
callq 0xd038
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x13818(%rip), %rdi     # 0x2cc4b
leaq 0x137b1(%rip), %rsi     # 0x2cbeb
leaq 0x137bf(%rip), %rcx     # 0x2cc00
movl $0x62, %edx
callq 0x31c0
leaq 0x1380e(%rip), %rdi     # 0x2cc60
leaq 0x13792(%rip), %rsi     # 0x2cbeb
leaq 0x137a0(%rip), %rcx     # 0x2cc00
movl $0xa0, %edx
callq 0x31c0
leaq 0x137ef(%rip), %rdi     # 0x2cc60
leaq 0x13773(%rip), %rsi     # 0x2cbeb
leaq 0x13781(%rip), %rcx     # 0x2cc00
movl $0x84, %edx
callq 0x31c0
leaq 0x13748(%rip), %rdi     # 0x2cbd8
leaq 0x13754(%rip), %rsi     # 0x2cbeb
leaq 0x13762(%rip), %rcx     # 0x2cc00
movl $0x3c, %edx
callq 0x31c0
leaq 0x137c9(%rip), %rdi     # 0x2cc78
leaq 0x13735(%rip), %rsi     # 0x2cbeb
leaq 0x13743(%rip), %rcx     # 0x2cc00
movl $0x93, %edx
callq 0x31c0