// File: /workspace/github_cpp_makefile_star2_O2/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 $0x68, %rsp
movl %edx, 0x8(%rsp)
movl 0x28(%rdi), %ebx
cmpl %ebx, 0x2c(%rdi)
jne 0x140b3
movl %ecx, %r15d
movq %rsi, %r14
movq %rdi, %r13
movslq %ebx, %rbp
pushq $0x2
popq %rcx
movl %ebx, %eax
cltd
idivl %ecx
movq %rax, 0x18(%rsp)
movl 0x30(%rdi), %r12d
movq (%rsi), %rax
movq %rsi, %rdi
movl %ebx, %esi
movl %ebx, %edx
callq *0x28(%rax)
movl 0x28(%r13), %esi
movl 0x2c(%r13), %edx
leaq 0x28(%rsp), %rdi
callq 0xa62e
xorl %eax, %eax
testl %ebp, %ebp
movl $0x0, %r11d
cmovgl %ebp, %r11d
pushq $0x2
popq %rcx
movq 0x38(%r13), %rdx
cmpl %r11d, %eax
je 0x13c09
movl 0x28(%r13), %esi
imull %eax, %esi
movq 0x60(%rsp), %rdi
movl 0x50(%rsp), %r8d
imull %eax, %r8d
addl $0x3, %esi
incl %r8d
movq %rcx, %r9
cmpq %rbp, %r9
jge 0x13c05
leal -0x1(%rsi), %r10d
movsd (%rdx,%r10,8), %xmm0
mulsd %xmm0, %xmm0
movl %esi, %r10d
movsd (%rdx,%r10,8), %xmm1
mulsd %xmm1, %xmm1
addsd %xmm0, %xmm1
movl %r8d, %r10d
movsd %xmm1, (%rdi,%r10,8)
addq $0x2, %r9
addl $0x2, %esi
incl %r8d
jmp 0x13bcc
incl %eax
jmp 0x13ba5
movl %r15d, 0x24(%rsp)
movl %r11d, 0x14(%rsp)
movupd (%rdx), %xmm0
mulpd %xmm0, %xmm0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movq 0x60(%rsp), %rax
movsd %xmm1, (%rax)
movq 0x38(%r13), %rdx
movl 0x28(%r13), %esi
movq 0x60(%rsp), %rax
movl 0x50(%rsp), %ecx
movslq 0x18(%rsp), %r15
leal -0x1(%rbx), %edi
imull %ecx, %edi
pushq $0x1
popq %r8
movl %esi, %r9d
movl %ecx, %r10d
cmpq %r15, %r8
jge 0x13c99
movl %r9d, %r11d
movsd (%rdx,%r11,8), %xmm0
mulsd %xmm0, %xmm0
leal 0x1(%r9), %r11d
movsd (%rdx,%r11,8), %xmm1
mulsd %xmm1, %xmm1
addsd %xmm0, %xmm1
movl %edi, %r11d
movsd %xmm1, (%rax,%r11,8)
movl %r10d, %r11d
movsd %xmm1, (%rax,%r11,8)
incq %r8
addl %ecx, %r10d
subl %ecx, %edi
addl %esi, %r9d
jmp 0x13c56
movsd 0x8(%rdx), %xmm0
mulsd %xmm0, %xmm0
movq 0x18(%rsp), %r10
imull %r10d, %ecx
movsd %xmm0, (%rax,%rcx,8)
movq 0x38(%r13), %rdx
movl 0x28(%r13), %esi
movq 0x60(%rsp), %rax
movl 0x50(%rsp), %ecx
leal 0x1(%r10), %edi
imull %esi, %edi
leal (%rcx,%r10), %r8d
leal -0x1(%rbx), %r9d
imull %ecx, %r9d
addl %r10d, %r9d
pushq $0x1
popq %r10
cmpq %r15, %r10
jge 0x13d1e
movl %edi, %r11d
movsd (%rdx,%r11,8), %xmm0
mulsd %xmm0, %xmm0
leal 0x1(%rdi), %r11d
movsd (%rdx,%r11,8), %xmm1
mulsd %xmm1, %xmm1
addsd %xmm0, %xmm1
movl %r9d, %r11d
movsd %xmm1, (%rax,%r11,8)
movl %r8d, %r11d
movsd %xmm1, (%rax,%r11,8)
incq %r10
addl %esi, %edi
addl %ecx, %r8d
subl %ecx, %r9d
jmp 0x13cdb
movq 0x18(%rsp), %r8
imull %r8d, %esi
movsd (%rdx,%rsi,8), %xmm0
mulsd %xmm0, %xmm0
movl %r8d, %edi
movsd %xmm0, (%rax,%rdi,8)
incl %esi
movsd (%rdx,%rsi,8), %xmm0
mulsd %xmm0, %xmm0
leal 0x1(%rcx), %edx
imull %r8d, %edx
movsd %xmm0, (%rax,%rdx,8)
leal (%rcx,%r8), %edx
leal -0x1(%rbx), %esi
imull %ecx, %esi
addl %r8d, %esi
decl %esi
pushq $0x1
popq %rdi
pushq $0x1
popq %r8
cmpl %ebx, %edi
jge 0x13d98
movl %esi, %r9d
movq %r8, %r10
cmpq %r15, %r10
jge 0x13d8f
movl %r9d, %r11d
movsd (%rax,%r11,8), %xmm0
leal (%rdx,%r10), %r11d
movsd %xmm0, (%rax,%r11,8)
incq %r10
decl %r9d
jmp 0x13d6f
incl %edi
addq %rcx, %rdx
subl %ecx, %esi
jmp 0x13d65
leal -0x1(%rbx), %ecx
pushq $0x1
popq %rdx
cmpq %r15, %rdx
jg 0x13db6
movsd (%rax,%rdx,8), %xmm0
movl %ecx, %esi
movsd %xmm0, (%rax,%rsi,8)
incq %rdx
decl %ecx
jmp 0x13d9e
xorl %ecx, %ecx
testl %r12d, %r12d
cmovlel %ecx, %r12d
xorpd %xmm0, %xmm0
movl 0x8(%rsp), %edx
cmpq %rcx, %r12
je 0x13df7
movsd (%rax,%rcx,8), %xmm1
incq %rcx
ucomisd %xmm0, %xmm1
jae 0x13dc7
leaq 0x10e3a(%rip), %rdi     # 0x24c1b
leaq 0x10dd3(%rip), %rsi     # 0x24bbb
leaq 0x10de1(%rip), %rcx     # 0x24bd0
pushq $0x62
popq %rdx
callq 0x31a0
testl %edx, %edx
je 0x13ef3
xorl %r13d, %r13d
xorpd %xmm1, %xmm1
cmpq %r13, %r12
je 0x13eba
cmpl $0x4, %edx
je 0x13e4c
cmpl $0x3, %edx
je 0x13e39
cmpl $0x2, %edx
jne 0x13eb2
movq 0x60(%rsp), %rax
movsd (%rax,%r13,8), %xmm0
ucomisd %xmm1, %xmm0
jb 0x13e70
sqrtsd %xmm0, %xmm0
jmp 0x13e79
movq 0x60(%rsp), %rax
movsd (%rax,%r13,8), %xmm0
ucomisd %xmm1, %xmm0
ja 0x13e62
jmp 0x13e69
movq 0x60(%rsp), %rax
movsd (%rax,%r13,8), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x13e69
callq 0x3130
callq 0x3130
jmp 0x13e9f
andq $0x0, (%rax,%r13,8)
jmp 0x13eb2
callq 0x3220
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x13e85
sqrtsd %xmm0, %xmm0
jmp 0x13e8e
callq 0x3220
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x13e9a
sqrtsd %xmm0, %xmm0
jmp 0x13ea3
callq 0x3220
xorpd %xmm1, %xmm1
movl 0x8(%rsp), %edx
movq 0x60(%rsp), %rax
movsd %xmm0, (%rax,%r13,8)
incq %r13
jmp 0x13e06
movq 0x60(%rsp), %rax
xorl %ecx, %ecx
cmpq %rcx, %r12
je 0x13ef8
movsd (%rax,%rcx,8), %xmm0
incq %rcx
ucomisd %xmm0, %xmm0
jnp 0x13ec1
leaq 0x10d55(%rip), %rdi     # 0x24c30
leaq 0x10cd9(%rip), %rsi     # 0x24bbb
leaq 0x10ce7(%rip), %rcx     # 0x24bd0
movl $0x84, %edx
callq 0x31a0
movq 0x60(%rsp), %rax
xorl %ecx, %ecx
xorpd %xmm0, %xmm0
cmpq %rcx, %r12
je 0x13f14
ucomisd (%rax,%rcx,8), %xmm0
jbe 0x13f0f
andq $0x0, (%rax,%rcx,8)
incq %rcx
jmp 0x13efe
movq 0x60(%rsp), %rax
cmpl $0x1, %edx
je 0x13f68
xorpd %xmm0, %xmm0
xorl %ecx, %ecx
cmpq %rcx, %r12
je 0x13f3b
movsd (%rax,%rcx,8), %xmm1
maxsd %xmm0, %xmm1
incq %rcx
movapd %xmm1, %xmm0
jmp 0x13f24
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x140cf
xorl %ecx, %ecx
cmpq %rcx, %r12
je 0x13f63
movsd (%rax,%rcx,8), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, (%rax,%rcx,8)
incq %rcx
jmp 0x13f4b
movq 0x60(%rsp), %rax
xorl %ecx, %ecx
movsd 0xe0e6(%rip), %xmm0     # 0x22058
cmpq %rcx, %r12
je 0x13f9a
movsd (%rax,%rcx,8), %xmm2
xorpd %xmm1, %xmm1
ucomisd %xmm2, %xmm1
ja 0x13f90
ucomisd %xmm0, %xmm2
movapd %xmm0, %xmm1
jbe 0x13f95
movsd %xmm1, (%rax,%rcx,8)
incq %rcx
jmp 0x13f72
movq 0x60(%rsp), %rax
xorl %ecx, %ecx
cmpq %rcx, %r12
je 0x13fd3
movsd (%rax,%rcx,8), %xmm0
incq %rcx
ucomisd %xmm0, %xmm0
jnp 0x13fa1
leaq 0x10c75(%rip), %rdi     # 0x24c30
leaq 0x10bf9(%rip), %rsi     # 0x24bbb
leaq 0x10c07(%rip), %rcx     # 0x24bd0
movl $0xa0, %edx
callq 0x31a0
cmpb $0x0, 0x24(%rsp)
je 0x14052
movl 0x14(%rsp), %eax
movq %rax, 0x8(%rsp)
xorl %r9d, %r9d
cmpq 0x8(%rsp), %r9
je 0x1409a
leaq (%r9,%r15), %rcx
movq 0x60(%rsp), %r10
movl 0x50(%rsp), %r11d
movq 0x38(%r14), %r12
movq %r14, %r8
movl 0x28(%r14), %r13d
imull %r9d, %r13d
movq %r15, %rsi
movq %rbx, %r14
subq $0x1, %r14
jb 0x1404a
movq %rsi, %rax
cqto
idivq %rbp
movq %rdx, %rdi
movq %rcx, %rax
cqto
idivq %rbp
imull %r11d, %edx
addl %edi, %edx
movsd (%r10,%rdx,8), %xmm0
movl %r13d, %eax
movsd %xmm0, (%r12,%rax,8)
incl %r13d
incq %rsi
jmp 0x14014
incq %r9
movq %r8, %r14
jmp 0x13fe6
xorl %eax, %eax
movl 0x14(%rsp), %r10d
cmpl %r10d, %eax
je 0x1409a
movq 0x60(%rsp), %rcx
movl 0x50(%rsp), %edx
imull %eax, %edx
movq 0x38(%r14), %rsi
movl 0x28(%r14), %edi
imull %eax, %edi
movq %rbx, %r8
subq $0x1, %r8
jb 0x14096
movl %edx, %r9d
movsd (%rcx,%r9,8), %xmm0
movl %edi, %r9d
movsd %xmm0, (%rsi,%r9,8)
incl %edi
incl %edx
jmp 0x14078
incl %eax
jmp 0x14059
leaq 0x28(%rsp), %rdi
callq 0xa6a6
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x10aee(%rip), %rdi     # 0x24ba8
leaq 0x10afa(%rip), %rsi     # 0x24bbb
leaq 0x10b08(%rip), %rcx     # 0x24bd0
pushq $0x3c
jmp 0x13df1
leaq 0x10b72(%rip), %rdi     # 0x24c48
leaq 0x10ade(%rip), %rsi     # 0x24bbb
leaq 0x10aec(%rip), %rcx     # 0x24bd0
movl $0x93, %edx
callq 0x31a0