// File: /workspace/github_cpp_makefile_star2_O2/ArashPartow[P]exprtk/exprtk_simple_example_10.cpp
// Function: void newton_sqrt<double>()
void newton_sqrt()
{
   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;

   T x = T(0);

   symbol_table_t symbol_table;

   symbol_table.add_constants();
   symbol_table.add_variable("x",x);

   compositor_t compositor(symbol_table);

   compositor.add(
      function_t("newton_sqrt")
      .var("x")
      .expression
      (
         " switch                                                   "
         " {                                                        "
         "    case x < 0  : null;                                   "
         "    case x == 0 : 0;                                      "
         "    case x == 1 : 1;                                      "
         "    default:                                              "
         "    {                                                     "
         "       var remaining_itrs := 100;                         "
         "       var sqrt_x := x / 2;                               "
         "       repeat                                             "
         "          if (equal(sqrt_x * sqrt_x, x))                  "
         "             break[sqrt_x];                               "
         "          else                                            "
         "             sqrt_x := (1 / 2) * (sqrt_x + (x / sqrt_x)); "
         "       until ((remaining_itrs -= 1) <= 0);                "
         "    };                                                    "
         " }                                                        "
      ));

   const std::string expression_str = "newton_sqrt(x)";

   expression_t expression;
   expression.register_symbol_table(symbol_table);

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

   for (x = T(0); x < T(500); x += T(0.5))
   {
      const T result = expression.value();
      const T real   = std::sqrt(x);
      const T error  = std::abs(result - real);

      const bool err_in_bound = error <= exprtk::details::numeric::constant::pi;

      printf("sqrt(%6.2f) - Result: %15.13f\tReal: %15.13f\tError: %18.16f EIB: %c\n",
             x,
             result,
             real,
             error,
             err_in_bound ? 'T' : 'F');
   }
}

// Assembly:
pushq %rbp
pushq %r14
pushq %rbx
subq $0x14c0, %rsp           # imm = 0x14C0
andq $0x0, 0x10(%rsp)
leaq 0x8(%rsp), %rbx
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0x1126fc
movq %rbx, %rdi
callq 0x112720
leaq 0x130d52(%rip), %rsi    # 0x243085
leaq 0xad8(%rsp), %rdi
leaq 0x1b0(%rsp), %rdx
callq 0x1122c8
leaq 0x8(%rsp), %rdi
leaq 0xad8(%rsp), %rsi
leaq 0x10(%rsp), %rdx
xorl %ecx, %ecx
callq 0x112746
leaq 0xad8(%rsp), %rdi
callq 0x1105c8
leaq 0xad8(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x1127b2
leaq 0x130da1(%rip), %rsi    # 0x243128
leaq 0x60(%rsp), %rdi
leaq 0x7(%rsp), %rdx
callq 0x1122c8
leaq 0x1b0(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0x1128d0
leaq 0x130cd6(%rip), %rsi    # 0x243085
leaq 0x40(%rsp), %rdi
leaq 0x6(%rsp), %rdx
callq 0x1122c8
leaq 0x1f0(%rsp), %rdi
leaq 0x40(%rsp), %rsi
callq 0x11d386
leaq 0x130d5d(%rip), %rsi    # 0x243134
leaq 0x18(%rsp), %rdi
leaq 0x5(%rsp), %rdx
callq 0x1122c8
leaq 0x1d0(%rsp), %rdi
leaq 0x18(%rsp), %rsi
callq 0x110550
leaq 0xad8(%rsp), %rdi
leaq 0x1b0(%rsp), %rsi
xorl %edx, %edx
callq 0x1128c0
leaq 0x18(%rsp), %rdi
callq 0x1105c8
leaq 0x40(%rsp), %rdi
callq 0x1105c8
leaq 0x1b0(%rsp), %rdi
callq 0x11291c
leaq 0x60(%rsp), %rdi
callq 0x1105c8
leaq 0x1310ce(%rip), %rsi    # 0x24350f
leaq 0x40(%rsp), %rdi
leaq 0x1b0(%rsp), %rdx
callq 0x1122c8
leaq 0x18(%rsp), %rdi
callq 0x11293c
leaq 0x18(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x11299c
leaq 0x60(%rsp), %rdi
pushq $0x7f
popq %rsi
callq 0x1129f8
leaq 0x1b0(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0x112b08
leaq 0x60(%rsp), %rdi
callq 0x113120
leaq 0x1b0(%rsp), %rdi
leaq 0x40(%rsp), %rsi
leaq 0x18(%rsp), %rdx
callq 0x113168
pushq $0x54
popq %rbp
pushq $0x46
popq %r14
leaq 0x131064(%rip), %rbx    # 0x24351e
xorpd %xmm0, %xmm0
movsd %xmm0, 0x10(%rsp)
movsd 0x12bb3c(%rip), %xmm1   # 0x23e008
ucomisd %xmm0, %xmm1
jbe 0x112559
movq 0x18(%rsp), %rax
movq 0x8(%rax), %rdi
movq (%rdi), %rax
callq *0x18(%rax)
movapd %xmm0, %xmm1
movsd 0x10(%rsp), %xmm0
ucomisd 0x12bb29(%rip), %xmm0   # 0x23e020
jb 0x1124ff
sqrtsd %xmm0, %xmm2
jmp 0x112514
movsd %xmm1, 0x38(%rsp)
callq 0x110250
movsd 0x38(%rsp), %xmm1
movapd %xmm0, %xmm2
movapd %xmm1, %xmm3
subsd %xmm2, %xmm3
andpd 0x12bbbc(%rip), %xmm3   # 0x23e0e0
movsd 0x12bae4(%rip), %xmm0   # 0x23e010
ucomisd %xmm3, %xmm0
movsd 0x10(%rsp), %xmm0
movl %r14d, %esi
cmovael %ebp, %esi
movq %rbx, %rdi
movb $0x4, %al
callq 0x110060
movsd 0x10(%rsp), %xmm0
addsd 0x12bac4(%rip), %xmm0   # 0x23e018
jmp 0x1124be
leaq 0x1b0(%rsp), %rdi
callq 0x1135ea
leaq 0x18(%rsp), %rdi
callq 0x113724
leaq 0x40(%rsp), %rdi
callq 0x1105c8
leaq 0xad8(%rsp), %rdi
callq 0x11373a
leaq 0x8(%rsp), %rdi
callq 0x11378e
addq $0x14c0, %rsp           # imm = 0x14C0
popq %rbx
popq %r14
popq %rbp
retq
jmp 0x112618
movq %rax, %rbx
leaq 0x60(%rsp), %rdi
callq 0x113120
jmp 0x112628
jmp 0x1125b0
movq %rax, %rbx
jmp 0x112628
movq %rax, %rbx
jmp 0x112632
jmp 0x1125cd
jmp 0x1125be
movq %rax, %rbx
jmp 0x1125fa
movq %rax, %rbx
jmp 0x112604
movq %rax, %rbx
jmp 0x112611
movq %rax, %rbx
jmp 0x11263c
jmp 0x1125e8
movq %rax, %rbx
leaq 0xad8(%rsp), %rdi
callq 0x1105c8
jmp 0x112649
jmp 0x1125e8
movq %rax, %rbx
jmp 0x112649
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x1105c8
leaq 0x40(%rsp), %rdi
callq 0x1105c8
leaq 0x1b0(%rsp), %rdi
callq 0x11291c
leaq 0x60(%rsp), %rdi
jmp 0x112637
movq %rax, %rbx
leaq 0x1b0(%rsp), %rdi
callq 0x1135ea
leaq 0x18(%rsp), %rdi
callq 0x113724
leaq 0x40(%rsp), %rdi
callq 0x1105c8
leaq 0xad8(%rsp), %rdi
callq 0x11373a
leaq 0x8(%rsp), %rdi
callq 0x11378e
movq %rbx, %rdi
callq 0x110310