// File: /workspace/github_cpp_makefile_star2_O3/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 $0x68, %rsp
movq (%rdi), %rdx
movq 0x8(%rdi), %r8
movq %r8, %rbx
subq %rdx, %rbx
movq 0x8(%rsi), %rax
subq (%rsi), %rax
cmpq %rax, %rbx
jne 0x15bb
sarq $0x3, %rbx
xorl %r12d, %r12d
cmpq $0x2, %rbx
jb 0x139d
xorl %r12d, %r12d
movq %rbx, %rcx
movq %rbx, %rax
incl %r12d
shrq %rax
cmpq $0x3, %rcx
movq %rax, %rcx
ja 0x138e
movq %r8, -0x40(%rbp)
movq %rdx, -0x48(%rbp)
movl $0x1, %eax
movl %r12d, %ecx
shll %cl, %eax
cmpq %rax, %rbx
jne 0x15ce
movq %rdi, -0x38(%rbp)
movq %rsi, %r15
movq %rbx, %r13
shrq %r13
leaq -0x70(%rbp), %rdi
leaq -0x88(%rbp), %rdx
movq %r13, %rsi
callq 0x1e84
leaq -0x88(%rbp), %rdi
leaq -0x29(%rbp), %rdx
movq %r13, %rsi
callq 0x1e84
cmpq $0x2, %rbx
jb 0x1446
cvtsi2sd %rbx, %xmm0
movsd %xmm0, -0x50(%rbp)
xorl %r14d, %r14d
xorps %xmm0, %xmm0
cvtsi2sd %r14, %xmm0
mulsd 0x1bfa(%rip), %xmm0     # 0x3008
divsd -0x50(%rbp), %xmm0
movsd %xmm0, -0x58(%rbp)
callq 0x1170
movq -0x70(%rbp), %rax
movsd %xmm0, (%rax,%r14,8)
movsd -0x58(%rbp), %xmm0
callq 0x1030
movq -0x88(%rbp), %rax
movsd %xmm0, (%rax,%r14,8)
incq %r14
cmpq %r14, %r13
jne 0x13fe
movq -0x40(%rbp), %rax
cmpq -0x48(%rbp), %rax
movq -0x38(%rbp), %r14
je 0x14bc
movq (%r14), %rax
movq (%r15), %rcx
cmpq $0x1, %rbx
movq %rbx, %rdx
adcq $0x0, %rdx
xorl %esi, %esi
testl %r12d, %r12d
je 0x14b4
movl %r12d, %r8d
xorl %edi, %edi
movq %rsi, %r9
movl %r9d, %r10d
andl $0x1, %r10d
leaq (%r10,%rdi,2), %rdi
shrq %r9
decl %r8d
jne 0x1474
cmpq %rsi, %rdi
jbe 0x14b4
movsd (%rax,%rsi,8), %xmm0
movsd (%rax,%rdi,8), %xmm1
movsd %xmm1, (%rax,%rsi,8)
movsd %xmm0, (%rax,%rdi,8)
movsd (%rcx,%rsi,8), %xmm0
movsd (%rcx,%rdi,8), %xmm1
movsd %xmm1, (%rcx,%rsi,8)
movsd %xmm0, (%rcx,%rdi,8)
incq %rsi
cmpq %rdx, %rsi
jne 0x1467
movq -0x88(%rbp), %rdi
cmpq $0x2, %rbx
jb 0x1594
movq (%r14), %rcx
movq -0x70(%rbp), %rsi
movq (%r15), %r8
movl $0x2, %r9d
movq %r9, %r10
shrq %r10
movq %rbx, %rax
xorl %edx, %edx
divq %r9
leaq (%r8,%r9,4), %rdx
leaq (%rcx,%r9,4), %r11
xorl %r14d, %r14d
leaq (%r14,%r10), %r15
xorl %r12d, %r12d
movq %r14, %r13
movsd (%r11,%r13,8), %xmm0
movsd (%rsi,%r12,8), %xmm1
movsd (%rdx,%r13,8), %xmm2
movsd (%rdi,%r12,8), %xmm3
movapd %xmm0, %xmm4
mulsd %xmm1, %xmm4
mulsd %xmm2, %xmm1
mulsd %xmm3, %xmm2
addsd %xmm2, %xmm4
mulsd %xmm3, %xmm0
subsd %xmm0, %xmm1
movsd (%rcx,%r13,8), %xmm0
subsd %xmm4, %xmm0
movsd %xmm0, (%r11,%r13,8)
movsd (%r8,%r13,8), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, (%rdx,%r13,8)
addsd (%rcx,%r13,8), %xmm4
movsd %xmm4, (%rcx,%r13,8)
addsd (%r8,%r13,8), %xmm1
movsd %xmm1, (%r8,%r13,8)
incq %r13
addq %rax, %r12
cmpq %r15, %r13
jb 0x1500
addq %r9, %r14
cmpq %rbx, %r14
jb 0x14f6
cmpq %rbx, %r9
je 0x1599
addq %r9, %r9
cmpq %rbx, %r9
jbe 0x14dd
testq %rdi, %rdi
je 0x159e
callq 0x10c0
movq -0x70(%rbp), %rdi
testq %rdi, %rdi
je 0x15ac
callq 0x10c0
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x8, %edi
callq 0x1070
leaq 0x1a74(%rip), %rcx      # 0x3040
jmp 0x15df
movl $0x8, %edi
callq 0x1070
leaq 0x1a74(%rip), %rcx      # 0x3053
movq %rcx, (%rax)
movq 0x39df(%rip), %rsi      # 0x4fc8
movq %rax, %rdi
xorl %edx, %edx
callq 0x1140
movq %rax, %rbx
movq -0x70(%rbp), %rdi
testq %rdi, %rdi
je 0x1604
callq 0x10c0
movq %rbx, %rdi
callq 0x1160