// File: ArashPartow[P]exprtk-extras/exprtk_prime_sieve_vectorized.cpp
// Function: void sieve_of_eratosthenes_vectorized<double>()
void sieve_of_eratosthenes_vectorized()
{
   typedef exprtk::symbol_table<T> symbol_table_t;
   typedef exprtk::expression<T>   expression_t;
   typedef exprtk::parser<T>       parser_t;

   const std::string sieve_of_eratosthenes_program =
      " var sieve[10^8] := [false];                     "
      " var m := trunc(sqrt(sieve[]));                  "
      "                                                 "
      " sieve[0] := true;                               "
      " sieve[1] := true;                               "
      "                                                 "
      " for (var i := 0; i <= m; i += 1)                "
      " {                                               "
      "    if (false == sieve[i])                       "
      "    {                                            "
      "       assign(sieve, true, i^2, sieve[] - 1, i); "
      "    }                                            "
      " };                                              "
      "                                                 "
      " var prime_count := sieve[] - sum(sieve);        "
      "                                                 "
      " prime_count == 5761455;                         ";

   symbol_table_t symbol_table;
   exprtk::rtl::vecops::package<T> vector_package;

   symbol_table.add_package(vector_package);

   expression_t expression;
   expression.register_symbol_table(symbol_table);

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

   exprtk::timer timer;
   timer.start();

   const T result = expression.value();

   timer.stop();

   printf("Result: %8.3f\n",result);

   printf("Total time: %8.3fsec\n",timer.time());
}

// Assembly:
subq $0x1308, %rsp           # imm = 0x1308
leaq 0x12e7(%rsp), %rdi
movq %rdi, 0x18(%rsp)
callq 0x113650
movq 0x18(%rsp), %rdx
leaq 0x33bcf9(%rip), %rsi    # 0x45653e
leaq 0x12e8(%rsp), %rdi
callq 0x11a760
jmp 0x11a854
leaq 0x12e7(%rsp), %rdi
callq 0x113780
leaq 0x12c8(%rsp), %rdi
movl $0x1, %esi
callq 0x11ad90
jmp 0x11a875
leaq 0xae8(%rsp), %rdi
callq 0x11add0
jmp 0x11a884
leaq 0x12c8(%rsp), %rdi
leaq 0xae8(%rsp), %rsi
callq 0x11b4d0
jmp 0x11a89b
leaq 0xac8(%rsp), %rdi
callq 0x11b500
jmp 0x11a8aa
leaq 0xac8(%rsp), %rdi
leaq 0x12c8(%rsp), %rsi
callq 0x11b5b0
jmp 0x11a8c1
leaq 0x50(%rsp), %rdi
movl $0x7f, %esi
callq 0x11b660
jmp 0x11a8d2
leaq 0x1a0(%rsp), %rdi
leaq 0x50(%rsp), %rsi
callq 0x11b790
jmp 0x11a8e6
leaq 0x50(%rsp), %rdi
callq 0x11bfc0
leaq 0x1a0(%rsp), %rdi
leaq 0x12e8(%rsp), %rsi
leaq 0xac8(%rsp), %rdx
callq 0x11c030
jmp 0x11a90f
leaq 0x28(%rsp), %rdi
callq 0x11c8c0
jmp 0x11a91b
leaq 0x28(%rsp), %rdi
callq 0x11c8f0
jmp 0x11a927
leaq 0xac8(%rsp), %rdi
callq 0x11c910
movsd %xmm0, 0x10(%rsp)
jmp 0x11a93c
movsd 0x10(%rsp), %xmm0
movsd %xmm0, 0x20(%rsp)
leaq 0x28(%rsp), %rdi
callq 0x11c930
jmp 0x11a954
movsd 0x20(%rsp), %xmm0
leaq 0x33bf1f(%rip), %rdi    # 0x456880
movb $0x1, %al
callq 0x113080
jmp 0x11a96a
leaq 0x28(%rsp), %rdi
callq 0x11c960
movsd %xmm0, 0x8(%rsp)
jmp 0x11a97c
movsd 0x8(%rsp), %xmm0
leaq 0x33bf06(%rip), %rdi    # 0x45688f
movb $0x1, %al
callq 0x113080
jmp 0x11a992
leaq 0x1a0(%rsp), %rdi
callq 0x11c9b0
leaq 0xac8(%rsp), %rdi
callq 0x11cbb0
leaq 0xae8(%rsp), %rdi
callq 0x11cbf0
leaq 0x12c8(%rsp), %rdi
callq 0x11cde0
leaq 0x12e8(%rsp), %rdi
callq 0x113798
addq $0x1308, %rsp           # imm = 0x1308
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x12d8(%rsp)
movl %eax, 0x12d4(%rsp)
leaq 0x12e7(%rsp), %rdi
callq 0x113780
jmp 0x11aad4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x12d8(%rsp)
movl %eax, 0x12d4(%rsp)
jmp 0x11aac7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x12d8(%rsp)
movl %eax, 0x12d4(%rsp)
jmp 0x11aaba
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x12d8(%rsp)
movl %eax, 0x12d4(%rsp)
jmp 0x11aaad
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x12d8(%rsp)
movl %eax, 0x12d4(%rsp)
jmp 0x11aaa0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x12d8(%rsp)
movl %eax, 0x12d4(%rsp)
leaq 0x50(%rsp), %rdi
callq 0x11bfc0
jmp 0x11aaa0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x12d8(%rsp)
movl %eax, 0x12d4(%rsp)
leaq 0x1a0(%rsp), %rdi
callq 0x11c9b0
leaq 0xac8(%rsp), %rdi
callq 0x11cbb0
leaq 0xae8(%rsp), %rdi
callq 0x11cbf0
leaq 0x12c8(%rsp), %rdi
callq 0x11cde0
leaq 0x12e8(%rsp), %rdi
callq 0x113798
movq 0x12d8(%rsp), %rdi
callq 0x113450
nopw %cs:(%rax,%rax)
nopl (%rax,%rax)