// File: /workspace/github_cpp_makefile_star2_O2/hdlguy[P]fft_demo/conv.cpp
// Function: main
int main(int argc, char **argv) {
    const int Nsig  = 256;
    const int Nt    =  32;
    const int Off   =   5;
    const int Nfft  =  (int)pow(2.0, ceil(log2(2.0*Nt-1))); // 2^(ceil(log2(2*Nt-1)));
    cout << "Nfft = " << Nfft << "\n";


    // random signal
    vector<double> s_real(Nsig);
    vector<double> s_imag(Nsig);
    for(int i=0; i<Nsig; i++) {
        s_real[i] = ((double)rand())/((double)RAND_MAX) - 0.5;
        s_imag[i] = ((double)rand())/((double)RAND_MAX) - 0.5;
    }

    // take the reverse order complex conjugate of a portion of the signal to be the convolution template.
    vector<double> h_real(Nt);
    vector<double> h_imag(Nt);
    for(int i=0; i<Nt; i++) {
        h_imag[i] = -s_imag[Nt-1-i+Off];
        h_real[i] = +s_real[Nt-1-i+Off];
    }

    vector<double> h_pad_imag(Nfft, 0.0);
    vector<double> h_pad_real(Nfft, 0.0);
    vector<double> s_pad_imag(Nfft, 0.0);
    vector<double> s_pad_real(Nfft, 0.0);
    for(int i=0; i<Nt; i++) {
        h_pad_imag[i] = h_imag[i];
        h_pad_real[i] = h_real[i];
        s_pad_imag[i] = s_imag[i];
        s_pad_real[i] = s_real[i];
    }
    
    vector<double> H_imag(Nfft); H_imag = h_pad_imag;
    vector<double> H_real(Nfft); H_real = h_pad_real;
    Fft::transformRadix2(H_real, H_imag);

    vector<double> S_imag(Nfft); S_imag = s_pad_imag;
    vector<double> S_real(Nfft); S_real = s_pad_real;
    Fft::transformRadix2(S_real, S_imag);

    vector<double> Y_imag(Nfft);
    vector<double> Y_real(Nfft);
    for(int i=0; i<Nfft; i++) {
        Y_imag[i] = (H_real[i]*S_real[i] - H_imag[i]*S_imag[i]);
        Y_real[i] = (H_real[i]*S_imag[i] + H_imag[i]*S_real[i]);
    }

    vector<double> y_imag(Nfft); y_imag = Y_imag;
    vector<double> y_real(Nfft); y_real = Y_real;
    Fft::transformRadix2(y_imag, y_real);  // real <-> imag swapped to implement inverse fft (without scaling)

    // plot time domain data
    FILE* gnuplotPipe = popen ("gnuplot --persist", "w");
    fprintf(gnuplotPipe, "plot '-' ps 0.1 lt 6\n");
    for (int i=0; i<Nfft; i++) fprintf(gnuplotPipe, "%lf\n", h_pad_real[i]);
    fprintf(gnuplotPipe, "e");
    pclose(gnuplotPipe);


    return EXIT_SUCCESS;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0x188, %rsp            # imm = 0x188
movq 0x3092(%rip), %rdi      # 0x4fd8
leaq 0x1193(%rip), %rsi      # 0x30e0
callq 0x10c0
pushq $0x40
popq %rsi
movq %rax, %rdi
callq 0x1140
leaq 0x11ac(%rip), %rsi      # 0x3110
movq %rax, %rdi
callq 0x10c0
leaq -0x1a0(%rbp), %rdi
leaq -0xe0(%rbp), %rdx
movl $0x100, %esi            # imm = 0x100
callq 0x1cde
leaq -0xe0(%rbp), %rdi
leaq -0x188(%rbp), %rdx
movl $0x100, %esi            # imm = 0x100
callq 0x1cde
xorl %ebx, %ebx
cmpq $0x100, %rbx            # imm = 0x100
je 0x1ffe
callq 0x1040
cvtsi2sd %eax, %xmm0
movsd 0x1118(%rip), %xmm1     # 0x30d0
divsd %xmm1, %xmm0
movsd 0x1114(%rip), %xmm1     # 0x30d8
addsd %xmm1, %xmm0
movq -0x1a0(%rbp), %rax
movsd %xmm0, (%rax,%rbx,8)
callq 0x1040
cvtsi2sd %eax, %xmm0
divsd 0x10eb(%rip), %xmm0     # 0x30d0
addsd 0x10eb(%rip), %xmm0     # 0x30d8
movq -0xe0(%rbp), %rax
movsd %xmm0, (%rax,%rbx,8)
incq %rbx
jmp 0x1f9e
leaq -0x188(%rbp), %rdi
pushq $0x20
popq %rsi
leaq -0x170(%rbp), %rdx
callq 0x1cde
leaq -0x170(%rbp), %rdi
pushq $0x20
popq %rsi
leaq -0x158(%rbp), %rdx
callq 0x1cde
movq -0xe0(%rbp), %rax
movq -0x170(%rbp), %rcx
movq -0x1a0(%rbp), %rdx
movl $0x120, %esi            # imm = 0x120
movq -0x188(%rbp), %rdi
xorl %r8d, %r8d
movapd 0x105a(%rip), %xmm0     # 0x30b0
cmpq $0x20, %r8
je 0x207f
movsd (%rax,%rsi), %xmm1
xorpd %xmm0, %xmm1
movlpd %xmm1, (%rcx,%r8,8)
movsd (%rdx,%rsi), %xmm1
movsd %xmm1, (%rdi,%r8,8)
incq %r8
addq $-0x8, %rsi
jmp 0x2056
leaq -0xc8(%rbp), %rdx
andq $0x0, (%rdx)
leaq -0x158(%rbp), %rdi
pushq $0x40
popq %rsi
leaq -0xb0(%rbp), %rcx
callq 0x2582
leaq -0xb0(%rbp), %rdx
andq $0x0, (%rdx)
leaq -0xc8(%rbp), %rdi
pushq $0x40
popq %rsi
leaq -0x98(%rbp), %rcx
callq 0x2582
leaq -0x98(%rbp), %rdx
andq $0x0, (%rdx)
leaq -0xb0(%rbp), %rdi
pushq $0x40
popq %rsi
leaq -0x50(%rbp), %rcx
callq 0x2582
leaq -0x50(%rbp), %rdx
andq $0x0, (%rdx)
leaq -0x98(%rbp), %rdi
pushq $0x40
popq %rsi
leaq -0x38(%rbp), %rcx
callq 0x2582
movq -0x170(%rbp), %rax
movq -0x158(%rbp), %rcx
movq -0x188(%rbp), %rdx
movq -0xc8(%rbp), %rsi
movq -0xe0(%rbp), %rdi
movq -0xb0(%rbp), %r8
movq -0x1a0(%rbp), %r9
movq -0x98(%rbp), %r10
xorl %r11d, %r11d
cmpq $0x100, %r11            # imm = 0x100
je 0x2174
movsd (%rax,%r11), %xmm0
movsd %xmm0, (%rcx,%r11)
movsd (%rdx,%r11), %xmm0
movsd %xmm0, (%rsi,%r11)
movsd (%rdi,%r11), %xmm0
movsd %xmm0, (%r8,%r11)
movsd (%r9,%r11), %xmm0
movsd %xmm0, (%r10,%r11)
addq $0x8, %r11
jmp 0x2135
leaq -0x50(%rbp), %rdi
pushq $0x40
popq %rsi
leaq -0x38(%rbp), %rdx
callq 0x1cde
leaq -0x50(%rbp), %rdi
leaq -0x158(%rbp), %rsi
callq 0x25e2
leaq -0x38(%rbp), %rdi
pushq $0x40
popq %rsi
leaq -0x80(%rbp), %rdx
callq 0x1cde
leaq -0x38(%rbp), %rdi
leaq -0xc8(%rbp), %rsi
callq 0x25e2
leaq -0x38(%rbp), %rdi
leaq -0x50(%rbp), %rsi
callq 0x1318
leaq -0x80(%rbp), %rdi
pushq $0x40
popq %rsi
leaq -0x68(%rbp), %rdx
callq 0x1cde
leaq -0x80(%rbp), %rdi
leaq -0xb0(%rbp), %rsi
callq 0x25e2
leaq -0x68(%rbp), %rdi
pushq $0x40
popq %rsi
leaq -0x140(%rbp), %rdx
callq 0x1cde
leaq -0x68(%rbp), %rdi
leaq -0x98(%rbp), %rsi
callq 0x25e2
leaq -0x68(%rbp), %rdi
leaq -0x80(%rbp), %rsi
callq 0x1318
leaq -0x140(%rbp), %rdi
pushq $0x40
popq %rsi
leaq -0x128(%rbp), %rdx
callq 0x1cde
leaq -0x128(%rbp), %rdi
pushq $0x40
popq %rsi
leaq -0x110(%rbp), %rdx
callq 0x1cde
movq -0x38(%rbp), %rax
movq -0x68(%rbp), %rcx
movq -0x50(%rbp), %rdx
movq -0x80(%rbp), %rsi
movq -0x140(%rbp), %rdi
movq -0x128(%rbp), %r8
xorl %r9d, %r9d
cmpq $0x200, %r9             # imm = 0x200
je 0x22b1
movsd (%rax,%r9), %xmm0
movsd (%rsi,%r9), %xmm1
mulsd (%rdx,%r9), %xmm1
mulsd (%rcx,%r9), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, (%rdi,%r9)
movsd (%rax,%r9), %xmm0
movsd (%rdx,%r9), %xmm1
mulsd (%rcx,%r9), %xmm1
mulsd (%rsi,%r9), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, (%r8,%r9)
addq $0x8, %r9
jmp 0x225e
leaq -0x110(%rbp), %rdi
pushq $0x40
popq %rsi
leaq -0xf8(%rbp), %rdx
callq 0x1cde
leaq -0x110(%rbp), %rdi
leaq -0x140(%rbp), %rsi
callq 0x25e2
leaq -0xf8(%rbp), %rdi
pushq $0x40
popq %rsi
leaq -0x19(%rbp), %rdx
callq 0x1cde
leaq -0xf8(%rbp), %rdi
leaq -0x128(%rbp), %rsi
callq 0x25e2
leaq -0x110(%rbp), %rdi
leaq -0xf8(%rbp), %rsi
callq 0x1318
leaq 0xdce(%rip), %rdi       # 0x30e8
leaq 0xdd9(%rip), %rsi       # 0x30fa
callq 0x1050
movq %rax, %rbx
leaq 0xdcc(%rip), %rdi       # 0x30fc
pushq $0x15
popq %rsi
pushq $0x1
popq %rdx
movq %rax, %rcx
callq 0x1170
xorl %r15d, %r15d
leaq 0xdca(%rip), %r14       # 0x3112
cmpq $0x40, %r15
je 0x236d
movq -0xc8(%rbp), %rax
movsd (%rax,%r15,8), %xmm0
movq %rbx, %rdi
movq %r14, %rsi
movb $0x1, %al
callq 0x1100
incq %r15
jmp 0x2348
pushq $0x65
popq %rdi
movq %rbx, %rsi
callq 0x10e0
movq %rbx, %rdi
callq 0x10f0
leaq -0xf8(%rbp), %rdi
callq 0x1dc2
leaq -0x110(%rbp), %rdi
callq 0x1dc2
leaq -0x128(%rbp), %rdi
callq 0x1dc2
leaq -0x140(%rbp), %rdi
callq 0x1dc2
leaq -0x68(%rbp), %rdi
callq 0x1dc2
leaq -0x80(%rbp), %rdi
callq 0x1dc2
leaq -0x38(%rbp), %rdi
callq 0x1dc2
leaq -0x50(%rbp), %rdi
callq 0x1dc2
leaq -0x98(%rbp), %rdi
callq 0x1dc2
leaq -0xb0(%rbp), %rdi
callq 0x1dc2
leaq -0xc8(%rbp), %rdi
callq 0x1dc2
leaq -0x158(%rbp), %rdi
callq 0x1dc2
leaq -0x170(%rbp), %rdi
callq 0x1dc2
leaq -0x188(%rbp), %rdi
callq 0x1dc2
leaq -0xe0(%rbp), %rdi
callq 0x1dc2
leaq -0x1a0(%rbp), %rdi
callq 0x1dc2
xorl %eax, %eax
addq $0x188, %rsp            # imm = 0x188
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
jmp 0x2446
movq %rax, %rbx
jmp 0x24c8
movq %rax, %rbx
jmp 0x24d4
movq %rax, %rbx
jmp 0x24e0
jmp 0x24ee
jmp 0x2462
movq %rax, %rbx
jmp 0x24fa
jmp 0x2505
jmp 0x2471
movq %rax, %rbx
jmp 0x2511
movq %rax, %rbx
jmp 0x251a
movq %rax, %rbx
jmp 0x2526
movq %rax, %rbx
jmp 0x2532
movq %rax, %rbx
jmp 0x253e
movq %rax, %rbx
jmp 0x254a
movq %rax, %rbx
jmp 0x2556
movq %rax, %rbx
jmp 0x2562
movq %rax, %rbx
jmp 0x256e
movq %rax, %rbx
leaq -0xf8(%rbp), %rdi
callq 0x1dc2
leaq -0x110(%rbp), %rdi
callq 0x1dc2
leaq -0x128(%rbp), %rdi
callq 0x1dc2
leaq -0x140(%rbp), %rdi
callq 0x1dc2
jmp 0x24f1
movq %rax, %rbx
leaq -0x68(%rbp), %rdi
callq 0x1dc2
leaq -0x80(%rbp), %rdi
callq 0x1dc2
jmp 0x2508
movq %rax, %rbx
leaq -0x38(%rbp), %rdi
callq 0x1dc2
leaq -0x50(%rbp), %rdi
callq 0x1dc2
leaq -0x98(%rbp), %rdi
callq 0x1dc2
leaq -0xb0(%rbp), %rdi
callq 0x1dc2
leaq -0xc8(%rbp), %rdi
callq 0x1dc2
leaq -0x158(%rbp), %rdi
callq 0x1dc2
leaq -0x170(%rbp), %rdi
callq 0x1dc2
leaq -0x188(%rbp), %rdi
callq 0x1dc2
leaq -0xe0(%rbp), %rdi
callq 0x1dc2
leaq -0x1a0(%rbp), %rdi
callq 0x1dc2
movq %rbx, %rdi
callq 0x1150