// File: tomfunkhouser[P]gaps/pkgs/RNMath/RNAlgebraic.cpp
// Function: gaps::RNAlgebraic::Construct(int, double, double, int)
void RNAlgebraic::
Construct(int op, RNScalar operand1, RNScalar operand2, RNBoolean force)
{
  // Condense scalars based on operation
  switch(op) {
  case RN_ZERO_OPERATION:
    break;

  case RN_ADD_OPERATION: {
    RNScalar sum = operand1 + operand2;
    if (sum == 0) break;
    operation = RN_POLYNOMIAL_OPERATION;
    polynomial = new RNPolynomial(sum, 0);
    break; }

  case RN_SUBTRACT_OPERATION: {
    RNScalar difference = operand1 - operand2;
    if (difference == 0) break;
    operation = RN_POLYNOMIAL_OPERATION;
    polynomial = new RNPolynomial(difference, 0);
    break; }

  case RN_MULTIPLY_OPERATION: {
    RNScalar product = operand1 * operand2;
    if (product == 0) break;
    operation = RN_POLYNOMIAL_OPERATION;
    polynomial = new RNPolynomial(product, 0);
    break; }

  case RN_DIVIDE_OPERATION:
    if (operand2 == 0) {
      operation = RN_POLYNOMIAL_OPERATION;
      polynomial = new RNPolynomial(RN_INFINITY, 0);
    }
    else {
      RNScalar quotient = operand1 / operand2;
      if (quotient == 0) break;
      operation = RN_POLYNOMIAL_OPERATION;
      polynomial = new RNPolynomial(quotient, 0);
    }
    break;

  case RN_POW_OPERATION:
    if (operand2 == 0) {
      operation = RN_POLYNOMIAL_OPERATION;
      polynomial = new RNPolynomial(1.0, 0);
    }
    else {
      RNScalar result = pow(operand1, operand2);
      if (result == 0) break;
      operation = RN_POLYNOMIAL_OPERATION;
      polynomial = new RNPolynomial(result, 0);
    }
    break;

  default:
    RNAbort("Invalid operation %d\n", op);
    break;
  }

  // Just checking
  assert(IsValid());
}

// Assembly:
subq $0xa8, %rsp
movq %rdi, 0xa0(%rsp)
movl %esi, 0x9c(%rsp)
movsd %xmm0, 0x90(%rsp)
movsd %xmm1, 0x88(%rsp)
movl %edx, 0x84(%rsp)
movq 0xa0(%rsp), %rax
movq %rax, 0x38(%rsp)
movl 0x9c(%rsp), %eax
movq %rax, 0x40(%rsp)
subq $0x6, %rax
ja 0x40507
movq 0x40(%rsp), %rax
leaq 0x6c766(%rip), %rcx     # 0xac844
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
jmp 0x4051c
movsd 0x90(%rsp), %xmm0
addsd 0x88(%rsp), %xmm0
movsd %xmm0, 0x78(%rsp)
movsd 0x78(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x4011a
jp 0x4011a
jmp 0x4051c
movq 0x38(%rsp), %rax
movl $0x1, 0x8(%rax)
movl $0x10, %edi
callq 0x4280
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x30(%rsp)
movsd 0x78(%rsp), %xmm0
xorl %r8d, %r8d
movl %r8d, %ecx
movl %r8d, %esi
movq %rcx, %rdx
callq 0x3c6e0
jmp 0x40154
movq 0x38(%rsp), %rax
movq 0x30(%rsp), %rcx
movq %rcx, 0x20(%rax)
jmp 0x4051c
movq 0x30(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x70(%rsp)
movl %eax, 0x6c(%rsp)
movl $0x10, %esi
callq 0x4290
jmp 0x40524
movsd 0x90(%rsp), %xmm0
subsd 0x88(%rsp), %xmm0
movsd %xmm0, 0x60(%rsp)
movsd 0x60(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x401b7
jp 0x401b7
jmp 0x4051c
movq 0x38(%rsp), %rax
movl $0x1, 0x8(%rax)
movl $0x10, %edi
callq 0x4280
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x28(%rsp)
movsd 0x60(%rsp), %xmm0
xorl %r8d, %r8d
movl %r8d, %ecx
movl %r8d, %esi
movq %rcx, %rdx
callq 0x3c6e0
jmp 0x401f1
movq 0x38(%rsp), %rax
movq 0x28(%rsp), %rcx
movq %rcx, 0x20(%rax)
jmp 0x4051c
movq 0x28(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x70(%rsp)
movl %eax, 0x6c(%rsp)
movl $0x10, %esi
callq 0x4290
jmp 0x40524
movsd 0x90(%rsp), %xmm0
mulsd 0x88(%rsp), %xmm0
movsd %xmm0, 0x58(%rsp)
movsd 0x58(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x40254
jp 0x40254
jmp 0x4051c
movq 0x38(%rsp), %rax
movl $0x1, 0x8(%rax)
movl $0x10, %edi
callq 0x4280
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x20(%rsp)
movsd 0x58(%rsp), %xmm0
xorl %r8d, %r8d
movl %r8d, %ecx
movl %r8d, %esi
movq %rcx, %rdx
callq 0x3c6e0
jmp 0x4028e
movq 0x38(%rsp), %rax
movq 0x20(%rsp), %rcx
movq %rcx, 0x20(%rax)
jmp 0x4051c
movq 0x20(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x70(%rsp)
movl %eax, 0x6c(%rsp)
movl $0x10, %esi
callq 0x4290
jmp 0x40524
movsd 0x88(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x4034b
jp 0x4034b
movq 0x38(%rsp), %rax
movl $0x1, 0x8(%rax)
movl $0x10, %edi
callq 0x4280
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x18(%rsp)
leaq 0x89fe9(%rip), %rax     # 0xca2e8
movsd (%rax), %xmm0
xorl %r8d, %r8d
movl %r8d, %ecx
movl %r8d, %esi
movq %rcx, %rdx
callq 0x3c6e0
jmp 0x40316
movq 0x38(%rsp), %rax
movq 0x18(%rsp), %rcx
movq %rcx, 0x20(%rax)
jmp 0x403e5
movq 0x18(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x70(%rsp)
movl %eax, 0x6c(%rsp)
movl $0x10, %esi
callq 0x4290
jmp 0x40524
movsd 0x90(%rsp), %xmm0
divsd 0x88(%rsp), %xmm0
movsd %xmm0, 0x50(%rsp)
movsd 0x50(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x40379
jp 0x40379
jmp 0x4051c
movq 0x38(%rsp), %rax
movl $0x1, 0x8(%rax)
movl $0x10, %edi
callq 0x4280
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x10(%rsp)
movsd 0x50(%rsp), %xmm0
xorl %r8d, %r8d
movl %r8d, %ecx
movl %r8d, %esi
movq %rcx, %rdx
callq 0x3c6e0
jmp 0x403b3
movq 0x38(%rsp), %rax
movq 0x10(%rsp), %rcx
movq %rcx, 0x20(%rax)
jmp 0x403e5
movq 0x10(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x70(%rsp)
movl %eax, 0x6c(%rsp)
movl $0x10, %esi
callq 0x4290
jmp 0x40524
jmp 0x4051c
movsd 0x88(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x4046f
jp 0x4046f
movq 0x38(%rsp), %rax
movl $0x1, 0x8(%rax)
movl $0x10, %edi
callq 0x4280
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x8(%rsp)
xorl %r8d, %r8d
movl %r8d, %ecx
movsd 0x6abf3(%rip), %xmm0    # 0xab020
movl %r8d, %esi
movq %rcx, %rdx
callq 0x3c6e0
jmp 0x4043a
movq 0x38(%rsp), %rax
movq 0x8(%rsp), %rcx
movq %rcx, 0x20(%rax)
jmp 0x40505
movq 0x8(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x70(%rsp)
movl %eax, 0x6c(%rsp)
movl $0x10, %esi
callq 0x4290
jmp 0x40524
movsd 0x90(%rsp), %xmm0
movsd 0x88(%rsp), %xmm1
callq 0x44a0
movsd %xmm0, 0x48(%rsp)
movsd 0x48(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x4049f
jp 0x4049f
jmp 0x4051c
movq 0x38(%rsp), %rax
movl $0x1, 0x8(%rax)
movl $0x10, %edi
callq 0x4280
movq %rax, %rdi
movq %rdi, %rax
movq %rax, (%rsp)
movsd 0x48(%rsp), %xmm0
xorl %r8d, %r8d
movl %r8d, %ecx
movl %r8d, %esi
movq %rcx, %rdx
callq 0x3c6e0
jmp 0x404d8
movq 0x38(%rsp), %rax
movq (%rsp), %rcx
movq %rcx, 0x20(%rax)
jmp 0x40505
movq (%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x70(%rsp)
movl %eax, 0x6c(%rsp)
movl $0x10, %esi
callq 0x4290
jmp 0x40524
jmp 0x4051c
movl 0x9c(%rsp), %esi
leaq 0x6c4e8(%rip), %rdi     # 0xac9fd
movb $0x0, %al
callq 0x4c6e0
addq $0xa8, %rsp
retq
movq 0x70(%rsp), %rdi
callq 0x44b0
nop