// File: /workspace/github_cpp_makefile_star2_O1/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, 0x8(%rsp)
movl %edx, 0xc(%rsp)
movl 0x28(%rdi), %ebx
movl 0x2c(%rdi), %edx
cmpl %ebx, %edx
jne 0x1991d
movq %rdi, %r12
movslq %ebx, %r13
movq %r13, %rax
shrq $0x3f, %rax
addq %r13, %rax
movq %rax, (%rsp)
movl 0x30(%rdi), %r14d
movq (%rsi), %rax
movq %rsi, 0x50(%rsp)
movq %rsi, %rdi
movl %ebx, %esi
callq *0x28(%rax)
movl 0x28(%r12), %esi
movl 0x2c(%r12), %edx
leaq 0x10(%rsp), %rdi
callq 0xd07e
testl %ebx, %ebx
jle 0x193c8
movq 0x38(%r12), %rax
movl 0x28(%r12), %ecx
movq 0x48(%rsp), %rdx
xorl %esi, %esi
movl $0x3, %edi
movl $0x1, %r8d
movl 0x38(%rsp), %r9d
cmpl $0x3, %ebx
jl 0x193bd
movl $0x2, %r10d
movl %r8d, %r11d
movl %edi, %ebp
leal -0x1(%rbp), %r15d
movsd (%rax,%r15,8), %xmm0
mulsd %xmm0, %xmm0
movl %ebp, %r15d
movsd (%rax,%r15,8), %xmm1
mulsd %xmm1, %xmm1
addsd %xmm0, %xmm1
movl %r11d, %r15d
movsd %xmm1, (%rdx,%r15,8)
addq $0x2, %r10
addl $0x2, %ebp
incl %r11d
cmpq %rbx, %r10
jb 0x19386
incl %esi
addl %ecx, %edi
addl %r9d, %r8d
cmpl %ebx, %esi
jne 0x19376
sarq (%rsp)
movq 0x38(%r12), %rax
movsd (%rax), %xmm0
movsd 0x8(%rax), %xmm1
mulsd %xmm0, %xmm0
mulsd %xmm1, %xmm1
addsd %xmm0, %xmm1
movq 0x48(%rsp), %rax
movsd %xmm1, (%rax)
cmpl $0x4, %ebx
jl 0x19468
movq 0x38(%r12), %rax
movl 0x28(%r12), %ecx
movq 0x48(%rsp), %rdx
movl 0x38(%rsp), %esi
movq (%rsp), %r8
cmpq $0x3, %r8
movl $0x2, %edi
cmovgeq %r8, %rdi
leal -0x1(%rbx), %r8d
imull %esi, %r8d
decq %rdi
movl %esi, %r9d
movl %ecx, %r10d
movl %r10d, %r11d
movsd (%rax,%r11,8), %xmm0
mulsd %xmm0, %xmm0
leal 0x1(%r10), %r11d
movsd (%rax,%r11,8), %xmm1
mulsd %xmm1, %xmm1
addsd %xmm0, %xmm1
movl %r8d, %r11d
movsd %xmm1, (%rdx,%r11,8)
movl %r9d, %r11d
movsd %xmm1, (%rdx,%r11,8)
subl %esi, %r8d
addl %ecx, %r10d
addl %esi, %r9d
decq %rdi
jne 0x19429
movq 0x38(%r12), %rax
movsd 0x8(%rax), %xmm0
mulsd %xmm0, %xmm0
movq 0x48(%rsp), %rax
movl 0x38(%rsp), %ecx
imull (%rsp), %ecx
movsd %xmm0, (%rax,%rcx,8)
cmpl $0x4, %ebx
movl 0xc(%rsp), %ebp
jl 0x1950d
movq 0x38(%r12), %rax
movl 0x28(%r12), %ecx
movq 0x48(%rsp), %rdx
movq (%rsp), %r10
cmpq $0x3, %r10
movl $0x2, %esi
cmovgeq %r10, %rsi
movl 0x38(%rsp), %edi
leal -0x1(%rbx), %r8d
imull %edi, %r8d
addl %r10d, %r8d
leal 0x1(%r10), %r9d
imull %ecx, %r9d
addl %edi, %r10d
decq %rsi
movl %r9d, %r11d
movsd (%rax,%r11,8), %xmm0
mulsd %xmm0, %xmm0
leal 0x1(%r9), %r11d
movsd (%rax,%r11,8), %xmm1
mulsd %xmm1, %xmm1
addsd %xmm0, %xmm1
movl %r8d, %r11d
movsd %xmm1, (%rdx,%r11,8)
movl %r10d, %r11d
movsd %xmm1, (%rdx,%r11,8)
subl %edi, %r8d
addl %ecx, %r9d
addl %edi, %r10d
decq %rsi
jne 0x194ce
movq 0x38(%r12), %rax
movl 0x28(%r12), %ecx
movq (%rsp), %rdx
imull %edx, %ecx
movsd (%rax,%rcx,8), %xmm0
mulsd %xmm0, %xmm0
movq 0x48(%rsp), %rax
movl %edx, %ecx
movsd %xmm0, (%rax,%rcx,8)
movq 0x38(%r12), %rax
movl 0x28(%r12), %ecx
movq %rdx, %r12
imull %r12d, %ecx
incl %ecx
movsd (%rax,%rcx,8), %xmm0
mulsd %xmm0, %xmm0
movq 0x48(%rsp), %rax
movl 0x38(%rsp), %ecx
leal 0x1(%rcx), %edx
imull %r12d, %edx
movsd %xmm0, (%rax,%rdx,8)
cmpl $0x2, %ebx
jl 0x195c7
cmpq $0x3, %r12
movl $0x2, %edx
cmovgeq %r12, %rdx
leal -0x1(%rbx), %esi
imull %ecx, %esi
addl %r12d, %esi
decl %esi
leal (%rcx,%r12), %edi
incl %edi
decq %rdx
movl $0x1, %r8d
movq %rdx, %r9
movl %edi, %r10d
movl %esi, %r11d
cmpl $0x4, %ebx
jl 0x195bb
movl %r11d, %r15d
movsd (%rax,%r15,8), %xmm0
movl %r10d, %r15d
movsd %xmm0, (%rax,%r15,8)
decl %r11d
incl %r10d
decq %r9
jne 0x1959e
incl %r8d
subl %ecx, %esi
addl %ecx, %edi
cmpl %ebx, %r8d
jne 0x19590
cmpl $0x2, %ebx
jl 0x195f5
cmpq $0x2, %r12
movl $0x1, %ecx
cmovgeq %r12, %rcx
leal -0x1(%rbx), %edx
xorl %esi, %esi
movsd 0x8(%rax,%rsi,8), %xmm0
movl %edx, %edi
movsd %xmm0, (%rax,%rdi,8)
incq %rsi
decl %edx
cmpq %rsi, %rcx
jne 0x195de
testl %r14d, %r14d
jle 0x19617
xorl %ecx, %ecx
xorpd %xmm0, %xmm0
movsd (%rax,%rcx,8), %xmm1
ucomisd %xmm0, %xmm1
jb 0x198c0
incq %rcx
cmpq %rcx, %r14
jne 0x19600
testl %ebp, %ebp
je 0x19704
testl %r14d, %r14d
jle 0x196dd
xorl %r12d, %r12d
xorpd %xmm1, %xmm1
cmpl $0x4, %ebp
je 0x1966c
cmpl $0x3, %ebp
je 0x19659
cmpl $0x2, %ebp
jne 0x196d1
movq 0x48(%rsp), %rax
movsd (%rax,%r12,8), %xmm0
ucomisd %xmm1, %xmm0
jb 0x19693
sqrtsd %xmm0, %xmm0
jmp 0x1969c
movq 0x48(%rsp), %rax
movsd (%rax,%r12,8), %xmm0
ucomisd %xmm1, %xmm0
ja 0x19682
jmp 0x19689
movq 0x48(%rsp), %rax
movsd (%rax,%r12,8), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x19689
callq 0x3150
callq 0x3150
jmp 0x196c2
movq $0x0, (%rax,%r12,8)
jmp 0x196d1
callq 0x3240
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x196a8
sqrtsd %xmm0, %xmm0
jmp 0x196b1
callq 0x3240
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x196bd
sqrtsd %xmm0, %xmm0
jmp 0x196c6
callq 0x3240
xorpd %xmm1, %xmm1
movq 0x48(%rsp), %rax
movsd %xmm0, (%rax,%r12,8)
incq %r12
cmpq %r12, %r14
jne 0x1962f
testl %r14d, %r14d
movq (%rsp), %r12
jle 0x19704
movq 0x48(%rsp), %rax
xorl %ecx, %ecx
movsd (%rax,%rcx,8), %xmm0
ucomisd %xmm0, %xmm0
jp 0x198fe
incq %rcx
cmpq %rcx, %r14
jne 0x196ed
testl %r14d, %r14d
jle 0x1972b
movq 0x48(%rsp), %rax
xorl %ecx, %ecx
xorpd %xmm0, %xmm0
ucomisd (%rax,%rcx,8), %xmm0
jbe 0x19723
movq $0x0, (%rax,%rcx,8)
incq %rcx
cmpq %rcx, %r14
jne 0x19714
cmpl $0x1, %ebp
je 0x19789
xorpd %xmm1, %xmm1
xorpd %xmm0, %xmm0
testl %r14d, %r14d
jle 0x1975d
movq 0x48(%rsp), %rax
xorpd %xmm2, %xmm2
xorl %ecx, %ecx
movsd (%rax,%rcx,8), %xmm0
maxsd %xmm2, %xmm0
incq %rcx
movapd %xmm0, %xmm2
cmpq %rcx, %r14
jne 0x19748
ucomisd %xmm1, %xmm0
jbe 0x1993c
testl %r14d, %r14d
jle 0x19789
movq 0x48(%rsp), %rax
xorl %ecx, %ecx
movsd (%rax,%rcx,8), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, (%rax,%rcx,8)
incq %rcx
cmpq %rcx, %r14
jne 0x19773
testl %r14d, %r14d
jle 0x197c3
movq 0x48(%rsp), %rax
xorl %ecx, %ecx
movsd 0x118bb(%rip), %xmm0    # 0x2b058
movsd (%rax,%rcx,8), %xmm2
xorpd %xmm1, %xmm1
ucomisd %xmm2, %xmm1
ja 0x197b6
ucomisd %xmm0, %xmm2
movapd %xmm0, %xmm1
jbe 0x197bb
movsd %xmm1, (%rax,%rcx,8)
incq %rcx
cmpq %rcx, %r14
jne 0x1979d
testl %r14d, %r14d
jle 0x197e6
movq 0x48(%rsp), %rax
xorl %ecx, %ecx
movsd (%rax,%rcx,8), %xmm0
ucomisd %xmm0, %xmm0
jp 0x198df
incq %rcx
cmpq %rcx, %r14
jne 0x197cf
cmpb $0x0, 0x8(%rsp)
je 0x19859
testl %ebx, %ebx
jle 0x198a7
movq 0x48(%rsp), %rdi
movl 0x38(%rsp), %r8d
movq 0x50(%rsp), %rax
movq 0x38(%rax), %r9
movl 0x28(%rax), %r10d
xorl %r11d, %r11d
xorl %r14d, %r14d
leaq (%r14,%r12), %rax
xorl %edx, %edx
divq %r13
movq %rdx, %rcx
imull %r8d, %ecx
movq %rbx, %r15
movl %r11d, %ebp
movq %r12, %rsi
movq %rsi, %rax
xorl %edx, %edx
divq %r13
addl %ecx, %edx
movsd (%rdi,%rdx,8), %xmm0
movl %ebp, %eax
movsd %xmm0, (%r9,%rax,8)
incq %rsi
incl %ebp
decq %r15
jne 0x1982b
incq %r14
addl %r10d, %r11d
cmpq %rbx, %r14
jne 0x19812
jmp 0x198a7
testl %ebx, %ebx
jle 0x198a7
movq 0x48(%rsp), %rax
movl 0x38(%rsp), %ecx
movq 0x50(%rsp), %rsi
movq 0x38(%rsi), %rdx
movl 0x28(%rsi), %esi
xorl %edi, %edi
xorl %r8d, %r8d
xorl %r9d, %r9d
xorl %r10d, %r10d
leal (%r8,%r10), %r11d
movsd (%rax,%r11,8), %xmm0
leal (%rdi,%r10), %r11d
movsd %xmm0, (%rdx,%r11,8)
incq %r10
cmpq %r10, %rbx
jne 0x1987d
incl %r9d
addq %rcx, %r8
addq %rsi, %rdi
cmpl %ebx, %r9d
jne 0x1987a
leaq 0x10(%rsp), %rdi
callq 0xd0fc
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1434c(%rip), %rdi     # 0x2dc13
leaq 0x142e5(%rip), %rsi     # 0x2dbb3
leaq 0x142f3(%rip), %rcx     # 0x2dbc8
movl $0x62, %edx
callq 0x31c0
leaq 0x14342(%rip), %rdi     # 0x2dc28
leaq 0x142c6(%rip), %rsi     # 0x2dbb3
leaq 0x142d4(%rip), %rcx     # 0x2dbc8
movl $0xa0, %edx
callq 0x31c0
leaq 0x14323(%rip), %rdi     # 0x2dc28
leaq 0x142a7(%rip), %rsi     # 0x2dbb3
leaq 0x142b5(%rip), %rcx     # 0x2dbc8
movl $0x84, %edx
callq 0x31c0
leaq 0x1427c(%rip), %rdi     # 0x2dba0
leaq 0x14288(%rip), %rsi     # 0x2dbb3
leaq 0x14296(%rip), %rcx     # 0x2dbc8
movl $0x3c, %edx
callq 0x31c0
leaq 0x142fd(%rip), %rdi     # 0x2dc40
leaq 0x14269(%rip), %rsi     # 0x2dbb3
leaq 0x14277(%rip), %rcx     # 0x2dbc8
movl $0x93, %edx
callq 0x31c0