// File: tomfunkhouser[P]gaps/pkgs/RNMath/RNAlgebraic.cpp
// Function: gaps::RNAlgebraic::Evaluate(double const*) const
RNScalar RNAlgebraic::
Evaluate(const RNScalar *x) const
{
  // Return value of algebraic
  switch(operation) {
  case RN_ZERO_OPERATION:
    return 0;
  
  case RN_POLYNOMIAL_OPERATION: 
    return polynomial->Evaluate(x);

  case RN_ADD_OPERATION: 
    return operands[0]->Evaluate(x) + operands[1]->Evaluate(x);
  
  case RN_SUBTRACT_OPERATION: 
    return operands[0]->Evaluate(x) - operands[1]->Evaluate(x);
  
  case RN_MULTIPLY_OPERATION: 
    return operands[0]->Evaluate(x) * operands[1]->Evaluate(x);

  case RN_DIVIDE_OPERATION: {
    RNScalar v1 = operands[1]->Evaluate(x);
    if (RNIsZero(v1, RN_SMALL_EPSILON)) return RN_INFINITY;
    RNScalar v0 = operands[0]->Evaluate(x);
    return v0 / v1; }
  
  case RN_POW_OPERATION: {
    RNScalar v0 = operands[0]->Evaluate(x);
    if (v0 == 0) return 0;
    RNScalar v1 = operands[1]->Evaluate(x);
    if (v1 == 0) return 1;
    return pow(v0, v1); }
  }

  // Should never get here
  RNAbort("Invalid algebraic operation: %d", operation);
  return 0.0;
}

// Assembly:
subq $0x68, %rsp
movq %rdi, 0x58(%rsp)
movq %rsi, 0x50(%rsp)
movq 0x58(%rsp), %rax
movq %rax, 0x20(%rsp)
movl 0x8(%rax), %eax
movq %rax, 0x28(%rsp)
subq $0x6, %rax
ja 0x461d9
movq 0x28(%rsp), %rax
leaq 0x669a2(%rip), %rcx     # 0xac978
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
xorps %xmm0, %xmm0
movsd %xmm0, 0x60(%rsp)
jmp 0x461f8
movq 0x20(%rsp), %rax
movq 0x20(%rax), %rdi
movq 0x50(%rsp), %rsi
callq 0x3de00
movsd %xmm0, 0x60(%rsp)
jmp 0x461f8
movq 0x20(%rsp), %rax
movq 0x10(%rax), %rdi
movq 0x50(%rsp), %rsi
callq 0x45fa0
movq 0x20(%rsp), %rax
movsd %xmm0, 0x18(%rsp)
movq 0x18(%rax), %rdi
movq 0x50(%rsp), %rsi
callq 0x45fa0
movaps %xmm0, %xmm1
movsd 0x18(%rsp), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x60(%rsp)
jmp 0x461f8
movq 0x20(%rsp), %rax
movq 0x10(%rax), %rdi
movq 0x50(%rsp), %rsi
callq 0x45fa0
movq 0x20(%rsp), %rax
movsd %xmm0, 0x10(%rsp)
movq 0x18(%rax), %rdi
movq 0x50(%rsp), %rsi
callq 0x45fa0
movaps %xmm0, %xmm1
movsd 0x10(%rsp), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, 0x60(%rsp)
jmp 0x461f8
movq 0x20(%rsp), %rax
movq 0x10(%rax), %rdi
movq 0x50(%rsp), %rsi
callq 0x45fa0
movq 0x20(%rsp), %rax
movsd %xmm0, 0x8(%rsp)
movq 0x18(%rax), %rdi
movq 0x50(%rsp), %rsi
callq 0x45fa0
movaps %xmm0, %xmm1
movsd 0x8(%rsp), %xmm0
mulsd %xmm1, %xmm0
movsd %xmm0, 0x60(%rsp)
jmp 0x461f8
movq 0x20(%rsp), %rax
movq 0x18(%rax), %rdi
movq 0x50(%rsp), %rsi
callq 0x45fa0
movsd %xmm0, 0x48(%rsp)
movsd 0x48(%rsp), %xmm0
leaq 0x841d3(%rip), %rax     # 0xca2d0
movsd (%rax), %xmm1
callq 0xb660
cmpl $0x0, %eax
je 0x46121
leaq 0x841d6(%rip), %rax     # 0xca2e8
movsd (%rax), %xmm0
movsd %xmm0, 0x60(%rsp)
jmp 0x461f8
movq 0x20(%rsp), %rax
movq 0x10(%rax), %rdi
movq 0x50(%rsp), %rsi
callq 0x45fa0
movsd %xmm0, 0x40(%rsp)
movsd 0x40(%rsp), %xmm0
divsd 0x48(%rsp), %xmm0
movsd %xmm0, 0x60(%rsp)
jmp 0x461f8
movq 0x20(%rsp), %rax
movq 0x10(%rax), %rdi
movq 0x50(%rsp), %rsi
callq 0x45fa0
movsd %xmm0, 0x38(%rsp)
movsd 0x38(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x46186
jp 0x46186
xorps %xmm0, %xmm0
movsd %xmm0, 0x60(%rsp)
jmp 0x461f8
movq 0x20(%rsp), %rax
movq 0x18(%rax), %rdi
movq 0x50(%rsp), %rsi
callq 0x45fa0
movsd %xmm0, 0x30(%rsp)
movsd 0x30(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x461c0
jp 0x461c0
movsd 0x64e68(%rip), %xmm0    # 0xab020
movsd %xmm0, 0x60(%rsp)
jmp 0x461f8
movsd 0x38(%rsp), %xmm0
movsd 0x30(%rsp), %xmm1
callq 0x44a0
movsd %xmm0, 0x60(%rsp)
jmp 0x461f8
movq 0x20(%rsp), %rax
movl 0x8(%rax), %esi
leaq 0x66852(%rip), %rdi     # 0xaca3a
movb $0x0, %al
callq 0x4c6e0
xorps %xmm0, %xmm0
movsd %xmm0, 0x60(%rsp)
movsd 0x60(%rsp), %xmm0
addq $0x68, %rsp
retq
nopw %cs:(%rax,%rax)