// File: 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
subq $0x1c0, %rsp            # imm = 0x1C0
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rdi
callq 0x2e00
movq %rax, -0xe0(%rbp)
movq -0x10(%rbp), %rdi
callq 0x2e00
movq %rax, %rcx
movq -0xe0(%rbp), %rax
cmpq %rcx, %rax
je 0x1422
movl $0x8, %edi
callq 0x10a0
movq %rax, %rdi
leaq 0x4c31(%rip), %rax      # 0x6040
movq %rax, (%rdi)
movq 0x7baf(%rip), %rsi      # 0x8fc8
xorl %eax, %eax
movl %eax, %edx
callq 0x1170
movq -0x8(%rbp), %rdi
callq 0x2e00
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x28(%rbp)
movl $0x0, -0x1c(%rbp)
cmpq $0x1, -0x28(%rbp)
jbe 0x145b
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
movq -0x28(%rbp), %rax
shrq %rax
movq %rax, -0x28(%rbp)
jmp 0x143e
movl -0x1c(%rbp), %ecx
movl $0x1, %eax
shll %cl, %eax
movl %eax, %eax
cmpq -0x18(%rbp), %rax
je 0x1494
movl $0x8, %edi
callq 0x10a0
movq %rax, %rdi
leaq 0x4bd2(%rip), %rax      # 0x6053
movq %rax, (%rdi)
movq 0x7b3d(%rip), %rsi      # 0x8fc8
xorl %eax, %eax
movl %eax, %edx
callq 0x1170
movq -0x18(%rbp), %rax
shrq %rax
movq %rax, -0xf0(%rbp)
leaq -0x41(%rbp), %rdi
movq %rdi, -0xe8(%rbp)
callq 0x2e20
movq -0xf0(%rbp), %rsi
movq -0xe8(%rbp), %rdx
leaq -0x40(%rbp), %rdi
callq 0x2e40
jmp 0x14cb
leaq -0x41(%rbp), %rdi
callq 0x2eb0
movq -0x18(%rbp), %rax
shrq %rax
movq %rax, -0x100(%rbp)
leaq -0x71(%rbp), %rdi
movq %rdi, -0xf8(%rbp)
callq 0x2e20
movq -0x100(%rbp), %rsi
movq -0xf8(%rbp), %rdx
leaq -0x70(%rbp), %rdi
callq 0x2e40
jmp 0x150b
leaq -0x71(%rbp), %rdi
callq 0x2eb0
movq $0x0, -0x80(%rbp)
movq -0x80(%rbp), %rax
movq -0x18(%rbp), %rcx
shrq %rcx
cmpq %rcx, %rax
jae 0x1674
movq -0x80(%rbp), %xmm0
movaps 0x4ad4(%rip), %xmm3     # 0x6010
movaps %xmm3, -0x130(%rbp)
punpckldq %xmm3, %xmm0    # xmm0 = xmm0[0],xmm3[0],xmm0[1],xmm3[1]
movapd 0x4ad1(%rip), %xmm2     # 0x6020
movaps %xmm2, -0x120(%rbp)
subpd %xmm2, %xmm0
movaps %xmm0, %xmm1
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
addsd %xmm1, %xmm0
movsd 0x4ac3(%rip), %xmm1     # 0x6030
movsd %xmm1, -0x138(%rbp)
mulsd %xmm1, %xmm0
movq -0x18(%rbp), %xmm1
punpckldq %xmm3, %xmm1    # xmm1 = xmm1[0],xmm3[0],xmm1[1],xmm3[1]
subpd %xmm2, %xmm1
movaps %xmm1, %xmm2
unpckhpd %xmm1, %xmm1            # xmm1 = xmm1[1,1]
addsd %xmm2, %xmm1
divsd %xmm1, %xmm0
callq 0x11c0
movsd %xmm0, -0x140(%rbp)
movq -0x80(%rbp), %rsi
leaq -0x40(%rbp), %rdi
callq 0x2ed0
movsd -0x140(%rbp), %xmm0
movsd -0x138(%rbp), %xmm1
movaps -0x130(%rbp), %xmm3
movaps -0x120(%rbp), %xmm2
movsd %xmm0, (%rax)
movq -0x80(%rbp), %xmm0
punpckldq %xmm3, %xmm0    # xmm0 = xmm0[0],xmm3[0],xmm0[1],xmm3[1]
subpd %xmm2, %xmm0
movaps %xmm0, %xmm4
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
addsd %xmm4, %xmm0
mulsd %xmm1, %xmm0
movq -0x18(%rbp), %xmm1
punpckldq %xmm3, %xmm1    # xmm1 = xmm1[0],xmm3[0],xmm1[1],xmm3[1]
subpd %xmm2, %xmm1
movaps %xmm1, %xmm2
unpckhpd %xmm1, %xmm1            # xmm1 = xmm1[1,1]
addsd %xmm2, %xmm1
divsd %xmm1, %xmm0
callq 0x1040
movsd %xmm0, -0x108(%rbp)
movq -0x80(%rbp), %rsi
leaq -0x70(%rbp), %rdi
callq 0x2ed0
movsd -0x108(%rbp), %xmm0
movsd %xmm0, (%rax)
movq -0x80(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x80(%rbp)
jmp 0x151c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x41(%rbp), %rdi
callq 0x2eb0
jmp 0x1b69
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x71(%rbp), %rdi
callq 0x2eb0
jmp 0x1b60
movq $0x0, -0x88(%rbp)
movq -0x88(%rbp), %rax
cmpq -0x18(%rbp), %rax
jae 0x17ff
movq -0x88(%rbp), %rdi
movl -0x1c(%rbp), %esi
callq 0x2670
movq %rax, -0x148(%rbp)
jmp 0x16a8
movq -0x148(%rbp), %rax
movq %rax, -0x90(%rbp)
movq -0x90(%rbp), %rax
cmpq -0x88(%rbp), %rax
jbe 0x17e6
movq -0x8(%rbp), %rdi
movq -0x88(%rbp), %rsi
callq 0x2ed0
movsd (%rax), %xmm0
movsd %xmm0, -0x98(%rbp)
movq -0x8(%rbp), %rdi
movq -0x90(%rbp), %rsi
callq 0x2ed0
movsd (%rax), %xmm0
movsd %xmm0, -0x168(%rbp)
movq -0x8(%rbp), %rdi
movq -0x88(%rbp), %rsi
callq 0x2ed0
movsd -0x168(%rbp), %xmm0
movsd %xmm0, (%rax)
movsd -0x98(%rbp), %xmm0
movsd %xmm0, -0x160(%rbp)
movq -0x8(%rbp), %rdi
movq -0x90(%rbp), %rsi
callq 0x2ed0
movsd -0x160(%rbp), %xmm0
movsd %xmm0, (%rax)
movq -0x10(%rbp), %rdi
movq -0x88(%rbp), %rsi
callq 0x2ed0
movsd (%rax), %xmm0
movsd %xmm0, -0x98(%rbp)
movq -0x10(%rbp), %rdi
movq -0x90(%rbp), %rsi
callq 0x2ed0
movsd (%rax), %xmm0
movsd %xmm0, -0x158(%rbp)
movq -0x10(%rbp), %rdi
movq -0x88(%rbp), %rsi
callq 0x2ed0
movsd -0x158(%rbp), %xmm0
movsd %xmm0, (%rax)
movsd -0x98(%rbp), %xmm0
movsd %xmm0, -0x150(%rbp)
movq -0x10(%rbp), %rdi
movq -0x90(%rbp), %rsi
callq 0x2ed0
movsd -0x150(%rbp), %xmm0
movsd %xmm0, (%rax)
jmp 0x17e6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x70(%rbp), %rdi
callq 0x2ef0
jmp 0x1b60
jmp 0x17e8
movq -0x88(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x88(%rbp)
jmp 0x167f
movq $0x2, -0xa0(%rbp)
movq -0xa0(%rbp), %rax
cmpq -0x18(%rbp), %rax
ja 0x1b45
movq -0xa0(%rbp), %rax
shrq %rax
movq %rax, -0xa8(%rbp)
movq -0x18(%rbp), %rax
xorl %ecx, %ecx
movl %ecx, %edx
divq -0xa0(%rbp)
movq %rax, -0xb0(%rbp)
movq $0x0, -0xb8(%rbp)
movq -0xb8(%rbp), %rax
cmpq -0x18(%rbp), %rax
jae 0x1b1e
movq -0xb8(%rbp), %rax
movq %rax, -0xc0(%rbp)
movq $0x0, -0xc8(%rbp)
movq -0xc0(%rbp), %rax
movq -0xb8(%rbp), %rcx
addq -0xa8(%rbp), %rcx
cmpq %rcx, %rax
jae 0x1b02
movq -0x8(%rbp), %rdi
movq -0xc0(%rbp), %rsi
addq -0xa8(%rbp), %rsi
callq 0x2ed0
movsd (%rax), %xmm0
movsd %xmm0, -0x1b0(%rbp)
movq -0xc8(%rbp), %rsi
leaq -0x40(%rbp), %rdi
callq 0x2ed0
movsd (%rax), %xmm0
movsd %xmm0, -0x1a8(%rbp)
movq -0x10(%rbp), %rdi
movq -0xc0(%rbp), %rsi
addq -0xa8(%rbp), %rsi
callq 0x2ed0
movsd (%rax), %xmm0
movsd %xmm0, -0x1b8(%rbp)
movq -0xc8(%rbp), %rsi
leaq -0x70(%rbp), %rdi
callq 0x2ed0
movsd -0x1b8(%rbp), %xmm1
movsd -0x1b0(%rbp), %xmm0
movsd -0x1a8(%rbp), %xmm2
mulsd (%rax), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0xd0(%rbp)
movq -0x8(%rbp), %rdi
movq -0xc0(%rbp), %rsi
addq -0xa8(%rbp), %rsi
callq 0x2ed0
movsd (%rax), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
movsd %xmm0, -0x198(%rbp)
movq -0xc8(%rbp), %rsi
leaq -0x70(%rbp), %rdi
callq 0x2ed0
movsd (%rax), %xmm0
movsd %xmm0, -0x190(%rbp)
movq -0x10(%rbp), %rdi
movq -0xc0(%rbp), %rsi
addq -0xa8(%rbp), %rsi
callq 0x2ed0
movsd (%rax), %xmm0
movsd %xmm0, -0x1a0(%rbp)
movq -0xc8(%rbp), %rsi
leaq -0x40(%rbp), %rdi
callq 0x2ed0
movsd -0x1a0(%rbp), %xmm1
movsd -0x198(%rbp), %xmm0
movsd -0x190(%rbp), %xmm2
mulsd (%rax), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0xd8(%rbp)
movq -0x8(%rbp), %rdi
movq -0xc0(%rbp), %rsi
callq 0x2ed0
movsd (%rax), %xmm0
subsd -0xd0(%rbp), %xmm0
movsd %xmm0, -0x188(%rbp)
movq -0x8(%rbp), %rdi
movq -0xc0(%rbp), %rsi
addq -0xa8(%rbp), %rsi
callq 0x2ed0
movsd -0x188(%rbp), %xmm0
movsd %xmm0, (%rax)
movq -0x10(%rbp), %rdi
movq -0xc0(%rbp), %rsi
callq 0x2ed0
movsd (%rax), %xmm0
subsd -0xd8(%rbp), %xmm0
movsd %xmm0, -0x180(%rbp)
movq -0x10(%rbp), %rdi
movq -0xc0(%rbp), %rsi
addq -0xa8(%rbp), %rsi
callq 0x2ed0
movsd -0x180(%rbp), %xmm0
movsd %xmm0, (%rax)
movsd -0xd0(%rbp), %xmm0
movsd %xmm0, -0x178(%rbp)
movq -0x8(%rbp), %rdi
movq -0xc0(%rbp), %rsi
callq 0x2ed0
movsd -0x178(%rbp), %xmm0
addsd (%rax), %xmm0
movsd %xmm0, (%rax)
movsd -0xd8(%rbp), %xmm0
movsd %xmm0, -0x170(%rbp)
movq -0x10(%rbp), %rdi
movq -0xc0(%rbp), %rsi
callq 0x2ed0
movsd -0x170(%rbp), %xmm0
addsd (%rax), %xmm0
movsd %xmm0, (%rax)
movq -0xc0(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xc0(%rbp)
movq -0xb0(%rbp), %rax
addq -0xc8(%rbp), %rax
movq %rax, -0xc8(%rbp)
jmp 0x1877
jmp 0x1b04
movq -0xa0(%rbp), %rax
addq -0xb8(%rbp), %rax
movq %rax, -0xb8(%rbp)
jmp 0x184d
movq -0xa0(%rbp), %rax
cmpq -0x18(%rbp), %rax
jne 0x1b2d
jmp 0x1b45
jmp 0x1b2f
movq -0xa0(%rbp), %rax
shlq %rax
movq %rax, -0xa0(%rbp)
jmp 0x180a
leaq -0x70(%rbp), %rdi
callq 0x2ef0
leaq -0x40(%rbp), %rdi
callq 0x2ef0
addq $0x1c0, %rsp            # imm = 0x1C0
popq %rbp
retq
leaq -0x40(%rbp), %rdi
callq 0x2ef0
movq -0x50(%rbp), %rdi
callq 0x11a0
nopw %cs:(%rax,%rax)