// File: /workspace/github_cpp_makefile_star2_O2/hdlguy[P]fft_demo/fft.cpp
// Function: Fft::transformRadix2(std::vector<double, std::allocator<double>>&, std::vector<double, std::allocator<double>>&)
void Fft::transformRadix2(vector<double> &real, vector<double> &imag) {
	// Compute levels = floor(log2(n))
	if (real.size() != imag.size())
		throw "Mismatched lengths";
	size_t n = real.size();
	unsigned int levels;
	{
		size_t temp = n;
		levels = 0;
		while (temp > 1) {
			levels++;
			temp >>= 1;
		}
		if (1u << levels != n)
			throw "Length is not a power of 2";
	}
	
	// Trignometric tables
	vector<double> cosTable(n / 2);
	vector<double> sinTable(n / 2);
	for (size_t i = 0; i < n / 2; i++) {
		cosTable[i] = cos(2 * M_PI * i / n);
		sinTable[i] = sin(2 * M_PI * i / n);
	}
	
	// Bit-reversed addressing permutation
	for (size_t i = 0; i < n; i++) {
		size_t j = reverseBits(i, levels);
		if (j > i) {
			double temp = real[i];
			real[i] = real[j];
			real[j] = temp;
			temp = imag[i];
			imag[i] = imag[j];
			imag[j] = temp;
		}
	}
	
	// Cooley-Tukey decimation-in-time radix-2 FFT
	for (size_t size = 2; size <= n; size *= 2) {
		size_t halfsize = size / 2;
		size_t tablestep = n / size;
		for (size_t i = 0; i < n; i += size) {
			for (size_t j = i, k = 0; j < i + halfsize; j++, k += tablestep) {
				double tpre =  real[j+halfsize] * cosTable[k] + imag[j+halfsize] * sinTable[k];
				double tpim = -real[j+halfsize] * sinTable[k] + imag[j+halfsize] * cosTable[k];
				real[j + halfsize] = real[j] - tpre;
				imag[j + halfsize] = imag[j] - tpim;
				real[j] += tpre;
				imag[j] += tpim;
			}
		}
		if (size == n)  // Prevent overflow in 'size *= 2'
			break;
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq 0x8(%rdi), %r15
movq %rdi, -0x40(%rbp)
subq (%rdi), %r15
movq 0x8(%rsi), %rax
movq %rsi, -0x48(%rbp)
subq (%rsi), %rax
cmpq %rax, %r15
jne 0x156b
sarq $0x3, %r15
xorl %r12d, %r12d
movq %r15, %rax
cmpq $0x2, %rax
jb 0x1360
incl %r12d
shrq %rax
jmp 0x1352
pushq $0x1
popq %rax
movl %r12d, %ecx
shll %cl, %eax
cmpq %rax, %r15
jne 0x157c
movq %r15, %r13
shrq %r13
leaq -0x80(%rbp), %rdi
leaq -0x68(%rbp), %rdx
movq %r13, %rsi
callq 0x1cde
leaq -0x68(%rbp), %rdi
leaq -0x29(%rbp), %rdx
movq %r13, %rsi
callq 0x1cde
cvtsi2sd %r15, %xmm0
movsd %xmm0, -0x50(%rbp)
xorl %ebx, %ebx
cmpq %rbx, %r13
je 0x13e5
cvtsi2sd %rbx, %xmm0
mulsd 0x1c53(%rip), %xmm0     # 0x3008
divsd -0x50(%rbp), %xmm0
movsd %xmm0, -0x38(%rbp)
callq 0x1160
movq -0x80(%rbp), %rax
movsd %xmm0, (%rax,%rbx,8)
movsd -0x38(%rbp), %xmm0
callq 0x1030
movq -0x68(%rbp), %rax
movsd %xmm0, (%rax,%rbx,8)
incq %rbx
jmp 0x13a3
movq -0x40(%rbp), %rax
movq (%rax), %rax
movq -0x48(%rbp), %rcx
movq (%rcx), %rcx
xorl %edx, %edx
cmpq %r15, %rdx
je 0x1449
movl %r12d, %edi
movq %rdx, %r8
xorl %esi, %esi
subl $0x1, %edi
jb 0x1417
movl %r8d, %r9d
andl $0x1, %r9d
leaq (%r9,%rsi,2), %rsi
shrq %r8
jmp 0x1402
cmpq %rdx, %rsi
jbe 0x1444
movsd (%rax,%rdx,8), %xmm0
movsd (%rax,%rsi,8), %xmm1
movsd %xmm1, (%rax,%rdx,8)
movsd %xmm0, (%rax,%rsi,8)
movsd (%rcx,%rdx,8), %xmm0
movsd (%rcx,%rsi,8), %xmm1
movsd %xmm1, (%rcx,%rdx,8)
movsd %xmm0, (%rcx,%rsi,8)
incq %rdx
jmp 0x13f5
pushq $0x2
popq %r10
cmpq %r15, %r10
ja 0x154a
movq %r10, %rsi
shrq %rsi
movq %r15, %rax
xorl %edx, %edx
divq %r10
movq -0x40(%rbp), %rdx
movq (%rdx), %rdx
movq -0x80(%rbp), %rdi
movq -0x48(%rbp), %r8
movq (%r8), %rbx
movq -0x68(%rbp), %r9
leaq (,%r10,8), %rcx
movq %rcx, -0x38(%rbp)
xorl %r14d, %r14d
cmpq %r15, %r14
jae 0x153a
movq %r15, %rcx
leaq (%r14,%rsi), %r12
movq %rdx, %r13
movq %rbx, %r8
movq %r14, %r11
xorl %r15d, %r15d
cmpq %r12, %r11
jae 0x1525
movsd (%r13,%rsi,8), %xmm0
movsd (%rdi,%r15,8), %xmm1
movsd (%r8,%rsi,8), %xmm2
movsd (%r9,%r15,8), %xmm3
movapd %xmm0, %xmm4
mulsd %xmm1, %xmm4
mulsd %xmm2, %xmm1
mulsd %xmm3, %xmm2
addsd %xmm2, %xmm4
mulsd %xmm3, %xmm0
subsd %xmm0, %xmm1
movsd (%r13), %xmm0
subsd %xmm4, %xmm0
movsd %xmm0, (%r13,%rsi,8)
movsd (%r8), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, (%r8,%rsi,8)
addsd (%r13), %xmm4
movsd %xmm4, (%r13)
addsd (%r8), %xmm1
movsd %xmm1, (%r8)
incq %r11
addq %rax, %r15
addq $0x8, %r8
addq $0x8, %r13
jmp 0x14a5
addq %r10, %r14
movq -0x38(%rbp), %r8
addq %r8, %rbx
addq %r8, %rdx
movq %rcx, %r15
jmp 0x1489
leaq (%r10,%r10), %rax
cmpq %r15, %r10
movq %rax, %r10
jne 0x144d
leaq -0x68(%rbp), %rdi
callq 0x1dc2
leaq -0x80(%rbp), %rdi
callq 0x1dc2
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x8
popq %rdi
callq 0x1070
leaq 0x1ac6(%rip), %rcx      # 0x3040
jmp 0x158b
pushq $0x8
popq %rdi
callq 0x1070
leaq 0x1ac8(%rip), %rcx      # 0x3053
movq %rcx, (%rax)
movq 0x3a33(%rip), %rsi      # 0x4fc8
movq %rax, %rdi
xorl %edx, %edx
callq 0x1130
movq %rax, %rbx
leaq -0x80(%rbp), %rdi
callq 0x1dc2
movq %rbx, %rdi
callq 0x1150