// File: /workspace/github_cpp_makefile_star2_O3/ArashPartow[P]exprtk/exprtk_simple_example_23.cpp
// Function: void real_1d_discrete_fourier_transform<double>()
void real_1d_discrete_fourier_transform()
{
   typedef exprtk::symbol_table<T>         symbol_table_t;
   typedef exprtk::expression<T>           expression_t;
   typedef exprtk::parser<T>               parser_t;
   typedef exprtk::function_compositor<T>  compositor_t;
   typedef typename compositor_t::function function_t;

   const T sampling_rate = 1024.0;            // ~1KHz
   const T N             = 8 * sampling_rate; // 8 seconds worth of samples

   std::vector<T> input (static_cast<std::size_t>(N),0.0);
   std::vector<T> output(static_cast<std::size_t>(N),0.0);

   exprtk::rtl::io::println<T> println;

   symbol_table_t symbol_table;
   symbol_table.add_vector   ("input"        , input        );
   symbol_table.add_vector   ("output"       , output       );
   symbol_table.add_function ("println"      , println      );
   symbol_table.add_constant ("N"            , N            );
   symbol_table.add_constant ("sampling_rate", sampling_rate);
   symbol_table.add_pi();

   compositor_t compositor(symbol_table);
   compositor.load_vectors(true);

   compositor.add(
      function_t("dft_1d_real")
      .var("N")
      .expression
      (
         " for (var k := 0; k < N; k += 1)        "
         " {                                      "
         "    var k_real := 0.0;                  "
         "    var k_imag := 0.0;                  "
         "                                        "
         "    for (var i := 0; i < N; i += 1)     "
         "    {                                   "
         "       var theta := 2pi * k * i / N;    "
         "       k_real += input[i] * cos(theta); "
         "       k_imag -= input[i] * sin(theta); "
         "    };                                  "
         "                                        "
         "    output[k] := hypot(k_real,k_imag);  "
         " }                                      "
      ));

   const std::string dft_program =
      "                                                                          "
      " /*                                                                       "
      "    Generate an aggregate waveform comprised of three                     "
      "    sine waves of varying frequencies and amplitudes.                     "
      " */                                                                       "
      " const var num_waves := 3;                                                "
      "                                                                          "
      " var frequencies[num_waves] := { 111.125, 222.250, 333.375 }; /* Hz    */ "
      " var amplitudes [num_waves] := {  11.111,  22.222,  33.333 }; /* Power */ "
      "                                                                          "
      " for (var i := 0; i < N; i += 1)                                          "
      " {                                                                        "
      "    var time := i / sampling_rate;                                        "
      "                                                                          "
      "    for (var j := 0; j < frequencies[]; j += 1)                           "
      "    {                                                                     "
      "       var frequency := frequencies[j];                                   "
      "       var amplitude := amplitudes [j];                                   "
      "       var theta     := 2 * pi * frequency * time;                        "
      "                                                                          "
      "       input[i] += amplitude * sin(theta);                                "
      "    }                                                                     "
      " };                                                                       "
      "                                                                          "
      " dft_1d_real(input[]);                                                    "
      "                                                                          "
      " var freq_bin_size   := sampling_rate / N;                                "
      " var max_bin         := ceil(N / 2);                                      "
      " var max_noise_level := 1e-6;                                             "
      "                                                                          "
      " /* Normalise amplitudes */                                               "
      " output /= max_bin;                                                       "
      "                                                                          "
      " println('1D Real DFT Frequencies');                                      "
      "                                                                          "
      " for (var k := 0; k < max_bin; k += 1)                                    "
      " {                                                                        "
      "    if (output[k] > max_noise_level)                                      "
      "    {                                                                     "
      "       var freq_begin := k * freq_bin_size;                               "
      "       var freq_end   := freq_begin + freq_bin_size;                      "
      "                                                                          "
      "       println('Index: ', k,' ',                                          "
      "               'Freq. range: [', freq_begin, 'Hz, ', freq_end, 'Hz) ',    "
      "               'Amplitude: ', output[k]);                                 "
      "    }                                                                     "
      " }                                                                        "
      "                                                                          ";

   expression_t expression;
   expression.register_symbol_table(symbol_table);

   parser_t parser;
   parser.compile(dft_program,expression);

   expression.value();
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1558, %rsp           # imm = 0x1558
movabsq $0x4090000000000000, %rax # imm = 0x4090000000000000
movq %rax, 0x88(%rsp)
movabsq $0x40c0000000000000, %rax # imm = 0x40C0000000000000
movq %rax, 0x80(%rsp)
movl $0x10000, %edi          # imm = 0x10000
callq 0x1160c0
movq %rax, 0x68(%rsp)
leaq 0x10000(%rax), %rbx
movq %rbx, 0x78(%rsp)
movl $0x10000, %edx          # imm = 0x10000
movq %rax, %rdi
xorl %esi, %esi
callq 0x116150
movq %rbx, 0x70(%rsp)
movl $0x10000, %edi          # imm = 0x10000
callq 0x1160c0
movq %rax, 0x50(%rsp)
movq %rax, %rbx
addq $0x10000, %rbx          # imm = 0x10000
movq %rbx, 0x60(%rsp)
movl $0x10000, %edx          # imm = 0x10000
movq %rax, %rdi
xorl %esi, %esi
callq 0x116150
movq %rbx, 0x58(%rsp)
leaq 0x258(%rsp), %r14
movq %r14, -0x10(%r14)
leaq 0x1963e7(%rip), %rsi    # 0x2af955
leaq 0x1963e6(%rip), %rdx    # 0x2af95b
leaq 0x248(%rsp), %rdi
callq 0x119d94
leaq 0x90(%rsp), %rdi
leaq 0x248(%rsp), %rsi
callq 0x119f4c
movq 0x248(%rsp), %rdi
cmpq %r14, %rdi
je 0x1195b4
movq 0x258(%rsp), %rsi
incq %rsi
callq 0x116090
leaq 0x8(%rsp), %rdi
movl $0x1, %esi
callq 0x11a054
leaq 0x248(%rsp), %rdi
movq %r14, (%rdi)
leaq 0x196387(%rip), %rsi    # 0x2af95c
leaq 0x196385(%rip), %rdx    # 0x2af961
callq 0x119d94
leaq 0x8(%rsp), %rdi
leaq 0x248(%rsp), %rsi
leaq 0x68(%rsp), %rdx
callq 0x11a09c
movq 0x248(%rsp), %rdi
cmpq %r14, %rdi
je 0x119615
movq 0x258(%rsp), %rsi
incq %rsi
callq 0x116090
leaq 0x248(%rsp), %rdi
movq %r14, (%rdi)
leaq 0x19633b(%rip), %rsi    # 0x2af962
leaq 0x19633a(%rip), %rdx    # 0x2af968
callq 0x119d94
leaq 0x8(%rsp), %rdi
leaq 0x248(%rsp), %rsi
leaq 0x50(%rsp), %rdx
callq 0x11a09c
movq 0x248(%rsp), %rdi
cmpq %r14, %rdi
je 0x119667
movq 0x258(%rsp), %rsi
incq %rsi
callq 0x116090
leaq 0x248(%rsp), %rdi
movq %r14, (%rdi)
leaq 0x1962f0(%rip), %rsi    # 0x2af969
leaq 0x1962f0(%rip), %rdx    # 0x2af970
callq 0x119d94
leaq 0x8(%rsp), %rdi
leaq 0x248(%rsp), %rsi
leaq 0x90(%rsp), %rdx
callq 0x11a27c
movq 0x248(%rsp), %rdi
cmpq %r14, %rdi
je 0x1196bc
movq 0x258(%rsp), %rsi
incq %rsi
callq 0x116090
leaq 0x248(%rsp), %rdi
movq %r14, (%rdi)
leaq 0x197a0f(%rip), %rsi    # 0x2b10dd
leaq 0x197a09(%rip), %rdx    # 0x2b10de
callq 0x119d94
leaq 0x8(%rsp), %rdi
leaq 0x248(%rsp), %rsi
leaq 0x80(%rsp), %rdx
callq 0x11a3e6
movq 0x248(%rsp), %rdi
cmpq %r14, %rdi
je 0x119711
movq 0x258(%rsp), %rsi
incq %rsi
callq 0x116090
leaq 0x248(%rsp), %rdi
movq %r14, (%rdi)
leaq 0x19624e(%rip), %rsi    # 0x2af971
leaq 0x196254(%rip), %rdx    # 0x2af97e
callq 0x119d94
leaq 0x8(%rsp), %rdi
leaq 0x248(%rsp), %rsi
leaq 0x88(%rsp), %rdx
callq 0x11a3e6
movq 0x248(%rsp), %rdi
cmpq %r14, %rdi
je 0x119766
movq 0x258(%rsp), %rsi
incq %rsi
callq 0x116090
movb 0x2f968c(%rip), %al     # 0x412df8
testb %al, %al
je 0x119bc4
leaq 0x248(%rsp), %rsi
movq %r14, (%rsi)
movw $0x6970, 0x10(%rsi)     # imm = 0x6970
movq $0x2, 0x8(%rsi)
movb $0x0, 0x12(%rsi)
leaq 0x2f9658(%rip), %rdx    # 0x412df0
leaq 0x8(%rsp), %rdi
callq 0x11a3e6
movq 0x248(%rsp), %rdi
cmpq %r14, %rdi
je 0x1197bf
movq 0x258(%rsp), %rsi
incq %rsi
callq 0x116090
leaq 0x248(%rsp), %rdi
leaq 0x8(%rsp), %rbx
movq %rbx, %rsi
callq 0x11a510
movb $0x1, 0xc29(%rsp)
leaq 0x108(%rsp), %r13
movq %r13, -0x10(%r13)
leaq 0x196190(%rip), %rsi    # 0x2af97f
leaq 0x196194(%rip), %rdx    # 0x2af98a
leaq 0xf8(%rsp), %rdi
callq 0x119d94
leaq 0xc30(%rsp), %rdi
leaq 0xf8(%rsp), %rsi
callq 0x11a698
leaq 0x20(%rsp), %rax
movq %rax, -0x10(%rax)
leaq 0x1978b5(%rip), %rsi    # 0x2b10dd
leaq 0x1978af(%rip), %rdx    # 0x2b10de
leaq 0x10(%rsp), %rdi
callq 0x119d94
leaq 0xc70(%rsp), %r14
leaq 0x10(%rsp), %rsi
movq %r14, %rdi
callq 0x1278c0
leaq 0x40(%rsp), %rbp
movq %rbp, -0x10(%rbp)
leaq 0x19612d(%rip), %rsi    # 0x2af98b
leaq 0x196356(%rip), %rdx    # 0x2afbbb
leaq 0x30(%rsp), %rdi
callq 0x119d94
leaq 0xc50(%rsp), %r12
leaq 0x30(%rsp), %rsi
movq %r12, %rdi
callq 0x116520
leaq 0xb78(%rsp), %r15
leaq 0xc30(%rsp), %rsi
movq %r15, %rdi
callq 0x12097c
leaq 0xb80(%rsp), %rcx
cmpq %rax, %rcx
jne 0x119956
leaq 0x248(%rsp), %rdi
leaq 0xc30(%rsp), %rsi
movq %r12, %rdx
movq %r14, %rcx
xorl %r8d, %r8d
callq 0x11f6e2
testb %al, %al
je 0x119956
leaq 0xc30(%rsp), %rsi
movq 0x88(%rsi), %rax
movq %rax, %rcx
subq 0x68(%rsi), %rcx
shrq $0x3, %rcx
cmpq $0x1, %rax
adcq $-0x1, %rcx
movq 0x60(%rsi), %rax
movq 0x70(%rsi), %rdx
subq 0x78(%rsi), %rdx
subq 0x50(%rsi), %rax
sarq $0x5, %rdx
sarq $0x5, %rax
leaq (%rcx,%rcx,2), %rcx
shlq $0x8, %rcx
addq 0xba8(%rsp), %rcx
leaq (%rdx,%rdx,2), %rdx
shlq $0x4, %rdx
leaq (%rax,%rax,2), %rdi
shlq $0x4, %rdi
addq %rdx, %rdi
addq %rcx, %rdi
callq 0x1205a0
movq (%rax), %r12
leaq 0xc30(%rsp), %rsi
movq %r15, %rdi
callq 0x120900
movq %r12, %rdi
movq %rax, %rsi
callq 0x12061c
movq 0x30(%rsp), %rdi
cmpq %rbp, %rdi
je 0x11996d
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x116090
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %r15
cmpq %r15, %rdi
je 0x119989
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x116090
movq %r14, %rdi
callq 0x127b5a
leaq 0xc60(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x1199b2
movq 0xc60(%rsp), %rsi
incq %rsi
callq 0x116090
leaq 0xc40(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x1199d3
movq 0xc40(%rsp), %rsi
incq %rsi
callq 0x116090
movq 0xf8(%rsp), %rdi
cmpq %r13, %rdi
je 0x1199f0
movq 0x108(%rsp), %rsi
incq %rsi
callq 0x116090
leaq 0x10(%rsp), %rdi
movq %r15, (%rdi)
leaq 0x1961bd(%rip), %rsi    # 0x2afbbc
leaq 0x196f96(%rip), %rdx    # 0x2b099c
callq 0x119d94
leaq 0x30(%rsp), %rdi
callq 0x11a772
movq 0x38(%rsp), %rax
movq 0x40(%rsp), %rcx
subq %rax, %rcx
je 0x119a48
sarq $0x3, %rcx
movq 0x8(%rsp), %rdx
cmpq $0x1, %rcx
adcq $0x0, %rcx
cmpq %rbx, %rax
je 0x119a57
cmpq (%rax), %rdx
je 0x119a57
addq $0x8, %rax
decq %rcx
jne 0x119a35
leaq 0x38(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x127de0
leaq 0xf8(%rsp), %rdi
movl $0x7f, %esi
callq 0x11a7fe
leaq 0xc30(%rsp), %rdi
leaq 0xf8(%rsp), %rsi
callq 0x11a8fe
leaq 0x1f8(%rsp), %rdi
callq 0x127fc6
leaq 0x1c8(%rsp), %rdi
callq 0x127fc6
leaq 0x198(%rsp), %rdi
callq 0x127fc6
leaq 0x168(%rsp), %rdi
callq 0x127fc6
leaq 0x138(%rsp), %rdi
callq 0x127fc6
leaq 0x108(%rsp), %rdi
callq 0x127fc6
leaq 0xc30(%rsp), %rdi
leaq 0x10(%rsp), %rsi
leaq 0x30(%rsp), %rdx
callq 0x11b122
movq 0x30(%rsp), %rax
movq 0x8(%rax), %rdi
movq (%rdi), %rax
callq *0x18(%rax)
leaq 0xc30(%rsp), %rdi
callq 0x11bcbe
leaq 0x30(%rsp), %rdi
callq 0x11bf80
movq 0x10(%rsp), %rdi
cmpq %r15, %rdi
je 0x119b20
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x116090
leaq 0x248(%rsp), %rdi
callq 0x11bfce
leaq 0x8(%rsp), %rdi
callq 0x11c030
leaq 0xe8(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x119b58
movq 0xe8(%rsp), %rsi
incq %rsi
callq 0x116090
leaq 0x28fc81(%rip), %rax    # 0x3a97e0
leaq 0xc0(%rsp), %rcx
movq %rax, -0x30(%rcx)
movq -0x10(%rcx), %rdi
cmpq %rcx, %rdi
je 0x119b84
movq 0xc0(%rsp), %rsi
incq %rsi
callq 0x116090
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x119b9b
movq 0x60(%rsp), %rsi
subq %rdi, %rsi
callq 0x116090
movq 0x68(%rsp), %rdi
testq %rdi, %rdi
je 0x119bb2
movq 0x78(%rsp), %rsi
subq %rdi, %rsi
callq 0x116090
addq $0x1558, %rsp           # imm = 0x1558
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x1165a0
jmp 0x119774
jmp 0x119be5
movq %rax, %rbx
leaq 0xf8(%rsp), %rdi
callq 0x11b0da
jmp 0x119caf
movq %rax, %rbx
jmp 0x119caf
movq %rax, %rbx
jmp 0x119cb9
jmp 0x119c11
jmp 0x119bf9
movq %rax, %rbx
jmp 0x119ce9
movq %rax, %rbx
jmp 0x119d05
movq %rax, %rbx
jmp 0x119d12
movq %rax, %rbx
jmp 0x119d2f
jmp 0x119c56
jmp 0x119c2d
jmp 0x119c2d
jmp 0x119c56
jmp 0x119c2d
jmp 0x119c56
jmp 0x119c2d
jmp 0x119c56
jmp 0x119c2d
jmp 0x119c56
movq %rax, %rbx
movq 0x248(%rsp), %rdi
cmpq %r14, %rdi
je 0x119d3c
movq 0x258(%rsp), %rsi
incq %rsi
callq 0x116090
jmp 0x119d3c
movq %rax, %rbx
jmp 0x119d3c
movq %rax, %rbx
jmp 0x119d46
movq %rax, %rbx
movq 0x248(%rsp), %rdi
cmpq %r14, %rdi
je 0x119d53
movq 0x258(%rsp), %rsi
incq %rsi
callq 0x116090
jmp 0x119d53
movq %rax, %rbx
jmp 0x119d53
movq %rax, %rbx
jmp 0x119d6a
movq %rax, %rbx
leaq 0xc30(%rsp), %rdi
callq 0x11bcbe
leaq 0x30(%rsp), %rdi
callq 0x11bf80
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x119d2f
movq 0x20(%rsp), %rsi
jmp 0x119d27
movq %rax, %rbx
movq 0x30(%rsp), %rdi
cmpq %rbp, %rdi
je 0x119ce9
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x116090
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x119d05
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x116090
leaq 0xc30(%rsp), %rdi
callq 0x11a732
movq 0xf8(%rsp), %rdi
cmpq %r13, %rdi
je 0x119d2f
movq 0x108(%rsp), %rsi
incq %rsi
callq 0x116090
leaq 0x248(%rsp), %rdi
callq 0x11bfce
leaq 0x8(%rsp), %rdi
callq 0x11c030
leaq 0x90(%rsp), %rdi
callq 0x11c0c6
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x119d6a
movq 0x60(%rsp), %rsi
subq %rdi, %rsi
callq 0x116090
movq 0x68(%rsp), %rdi
testq %rdi, %rdi
je 0x119d81
movq 0x78(%rsp), %rsi
subq %rdi, %rsi
callq 0x116090
movq %rbx, %rdi
callq 0x1162e0