// File: /workspace/github_cpp_makefile_star2_O3/tomfunkhouser[P]gaps/pkgs/R3Shapes/R3Grid.cpp
// Function: gaps::R3Grid::RasterizeGridTriangle(int const*, int const*, int const*, double, int)
void R3Grid::
RasterizeGridTriangle(const int p1[3], const int p2[3], const int p3[3], RNScalar value, int operation)
{
  int i,j;

  // Figure out the min, max, and delta in each dimension
  int mn[3], mx[3], delta[3];
  for (i = 0; i < 3; i++) {
    mx[i]=mn[i]=p1[i];
    if (p2[i] < mn[i]) mn[i]=p2[i];
    if (p3[i] < mn[i]) mn[i]=p3[i];
    if (p2[i] > mx[i]) mx[i]=p2[i];
    if (p3[i] > mx[i]) mx[i]=p3[i];
    delta[i] = mx[i] - mn[i];
  }

  // Determine direction of maximal delta
  int d = 0;
  if ((delta[1] > delta[0]) && (delta[1] > delta[2])) d = 1;
  else if (delta[2] > delta[0]) d = 2;

  // Sort by d-value
  const int *q1,*q2,*q3;
  if(p1[d]>=p2[d] && p1[d]>=p3[d]){
    q1=p1;
    if(p2[d]>=p3[d]){
      q2=p2;
      q3=p3;
    }
    else{
      q2=p3;
      q3=p2;
    }
  }
  else if(p2[d]>=p1[d] && p2[d]>=p3[d]){
    q1=p2;
    if(p1[d]>=p3[d]){
      q2=p1;
      q3=p3;
    }
    else{
      q2=p3;
      q3=p1;
    }
  }
  else{
    q1=p3;
    if(p1[d]>=p2[d]){
      q2=p1;
      q3=p2;
    }
    else{
      q2=p2;
      q3=p1;
    }
  }

  // Init state
  int dx,dx1,dx2,ddx;
  dx=q1[d]-q2[d];
  dx1=q1[d]-q2[d];
  dx2=q1[d]-q3[d];
  ddx=dx1*dx2;

  int r1[3],r2[3];
  int last1[3],last2[3];
  int off1[3],off2[3];
  for(i=0;i<3;i++){
    last1[i]=q1[i];
    last2[i]=q1[i];

    off1[i]=0;
    off2[i]=0;

    r1[i]=(-q1[i]+q2[i])*dx2;
    r2[i]=(-q1[i]+q3[i])*dx1;
  }

  // Draw Top triangle
  if(dx==0){
    for(i=0;i<3;i++){
      last1[i]=q1[i];
      last2[i]=q2[i];
    }
  }
  else{
    for(i=0;i<dx;i++){
      RasterizeGridSpan(last1,last2,value, operation);
      for(j=0;j<3;j++){
        off1[j]+=r1[j];
        off2[j]+=r2[j];

        last1[j]+=off1[j]/ddx;
        if(off1[j]<0){off1[j]=-((-off1[j])%ddx);}
        else{off1[j]%=ddx;}

        last2[j]+=off2[j]/ddx;
        if(off2[j]<0){off2[j]=-((-off2[j])%ddx);}
        else{off2[j]%=ddx;}
      }
    }
  }

  // Init
  dx=q2[d]-q3[d];
  dx1=last1[d]-q3[d];
  dx2=last2[d]-q3[d];
  ddx=dx1*dx2;
  if(dx==0){
    RasterizeGridSpan(q2,q3,value,operation);
    return;
  }

  for(i=0;i<3;i++){
    off1[i]=0;
    off2[i]=0;
    r1[i]=(-last1[i]+q3[i])*dx2;
    r2[i]=(-last2[i]+q3[i])*dx1;
  }

  // Draw Bottom parrallelogram
  for(i=0;i<=dx;i++){
    RasterizeGridSpan(last1,last2,value,operation);
    for(j=0;j<3;j++){
      off1[j]+=r1[j];
      off2[j]+=r2[j];

      last1[j]+=off1[j]/ddx;
      if(off1[j]<0){off1[j]=-((-off1[j])%ddx);}
      else{off1[j]%=ddx;}

      last2[j]+=off2[j]/ddx;
      if(off2[j]<0){off2[j]=-((-off2[j])%ddx);}
      else{off2[j]%=ddx;}
    }
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movl %r8d, 0x4(%rsp)
movsd %xmm0, 0x50(%rsp)
movq %rdx, %r15
movq %rdi, 0x48(%rsp)
xorl %eax, %eax
movl (%rsi,%rax,4), %edx
movl (%r15,%rax,4), %edi
cmpl %edx, %edi
movl %edx, %r8d
cmovll %edi, %r8d
cmovgl %edi, %edx
movl (%rcx,%rax,4), %edi
cmpl %r8d, %edi
cmovll %edi, %r8d
cmpl %edx, %edi
cmovgl %edi, %edx
subl %r8d, %edx
movl %edx, 0x74(%rsp,%rax,4)
incq %rax
cmpq $0x3, %rax
jne 0x34a6a
movl 0x74(%rsp), %eax
movl 0x78(%rsp), %edx
movl 0x7c(%rsp), %edi
xorl %r8d, %r8d
cmpl %eax, %edi
setg %r8b
addl %r8d, %r8d
cmpl %edi, %edx
movl $0x1, %r9d
cmovleq %r8, %r9
cmpl %eax, %edx
cmovleq %r8, %r9
leal (,%r9,4), %edx
movl (%rsi,%rdx), %eax
movl (%r15,%rdx), %edx
cmpl %edx, %eax
jl 0x34ae1
movl (%rcx,%r9,4), %edi
cmpl %edi, %eax
jge 0x34afc
cmpl %eax, %edx
jl 0x34aed
movl (%rcx,%r9,4), %edi
cmpl %edi, %edx
jge 0x34b0e
cmpl %edx, %eax
movq %rsi, %r12
cmovlq %r15, %r12
cmovlq %rsi, %r15
jmp 0x34b24
cmpl %edi, %edx
movq %r15, %r12
cmovlq %rcx, %r12
cmovgeq %rcx, %r15
movq %rsi, %rcx
jmp 0x34b24
cmpl %edi, %eax
movq %rsi, %r12
cmovlq %rcx, %r12
movq %rcx, %rax
cmovlq %rsi, %rax
movq %r15, %rcx
movq %rax, %r15
leal (,%r9,4), %eax
movl (%rcx,%rax), %edx
movl (%r12,%rax), %ebp
movl %edx, %ebx
subl %ebp, %ebx
movl (%r15,%rax), %eax
movl %edx, %r14d
subl %eax, %r14d
xorl %esi, %esi
movq %rsi, 0x38(%rsp)
movl %esi, 0x40(%rsp)
movl %esi, 0x30(%rsp)
movq %rsi, 0x28(%rsp)
movq (%rcx), %rdi
movq %rdi, 0x18(%rsp)
movl 0x8(%rcx), %edi
movl %edi, 0x20(%rsp)
movq (%rcx), %rdi
movq %rdi, 0x8(%rsp)
movl 0x8(%rcx), %edi
movl %edi, 0x10(%rsp)
movl (%rcx,%rsi,4), %edi
movl (%r12,%rsi,4), %r8d
subl %edi, %r8d
imull %r14d, %r8d
movl %r8d, 0x68(%rsp,%rsi,4)
movl (%r15,%rsi,4), %r8d
subl %edi, %r8d
imull %ebx, %r8d
movl %r8d, 0x5c(%rsp,%rsi,4)
incq %rsi
cmpq $0x3, %rsi
jne 0x34b73
cmpl %ebp, %edx
jne 0x34bcb
movl 0x8(%rcx), %esi
movl %esi, 0x20(%rsp)
movq (%rcx), %rcx
movq %rcx, 0x18(%rsp)
movq (%r12), %rcx
movq %rcx, 0x8(%rsp)
movl 0x8(%r12), %ecx
movl %ecx, 0x10(%rsp)
movl %edx, %ebp
jmp 0x34c59
testl %ebx, %ebx
jle 0x34c59
movq %r9, 0x80(%rsp)
imull %ebx, %r14d
xorl %r13d, %r13d
leaq 0x8(%rsp), %rbp
movq 0x48(%rsp), %rdi
leaq 0x18(%rsp), %rsi
movq %rbp, %rdx
movsd 0x50(%rsp), %xmm0
movl 0x4(%rsp), %ecx
callq 0x3485e
xorl %esi, %esi
movl 0x38(%rsp,%rsi), %eax
addl 0x68(%rsp,%rsi), %eax
movl 0x28(%rsp,%rsi), %ecx
addl 0x5c(%rsp,%rsi), %ecx
cltd
idivl %r14d
addl %eax, 0x18(%rsp,%rsi)
movl %edx, 0x38(%rsp,%rsi)
movl %ecx, %eax
cltd
idivl %r14d
addl %eax, 0x8(%rsp,%rsi)
movl %edx, 0x28(%rsp,%rsi)
addq $0x4, %rsi
cmpq $0xc, %rsi
jne 0x34c05
incl %r13d
cmpl %ebx, %r13d
jne 0x34be7
movq 0x80(%rsp), %r9
leal (,%r9,4), %eax
movl (%r12,%rax), %ebp
movl (%r15,%rax), %eax
cmpl %eax, %ebp
jne 0x34c88
movq 0x48(%rsp), %rdi
movq %r12, %rsi
movq %r15, %rdx
movsd 0x50(%rsp), %xmm0
movl 0x4(%rsp), %ecx
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3485e
shll $0x2, %r9d
movl 0x18(%rsp,%r9), %ecx
subl %eax, %ecx
movl 0x8(%rsp,%r9), %edx
subl %eax, %edx
movl %edx, %ebx
imull %ecx, %ebx
xorl %esi, %esi
movl %esi, 0x40(%rsp)
movq %rsi, 0x38(%rsp)
movl %esi, 0x30(%rsp)
movq %rsi, 0x28(%rsp)
movl (%r15,%rsi,4), %edi
movl %edi, %r8d
subl 0x18(%rsp,%rsi,4), %r8d
imull %edx, %r8d
movl %r8d, 0x68(%rsp,%rsi,4)
subl 0x8(%rsp,%rsi,4), %edi
imull %ecx, %edi
movl %edi, 0x5c(%rsp,%rsi,4)
incq %rsi
cmpq $0x3, %rsi
jne 0x34cb3
subl %eax, %ebp
jl 0x34d45
incl %ebp
xorl %r14d, %r14d
leaq 0x18(%rsp), %r15
leaq 0x8(%rsp), %r12
movq 0x48(%rsp), %rdi
movq %r15, %rsi
movq %r12, %rdx
movsd 0x50(%rsp), %xmm0
movl 0x4(%rsp), %ecx
callq 0x3485e
xorl %esi, %esi
movl 0x38(%rsp,%rsi), %eax
addl 0x68(%rsp,%rsi), %eax
movl 0x28(%rsp,%rsi), %ecx
addl 0x5c(%rsp,%rsi), %ecx
cltd
idivl %ebx
addl %eax, 0x18(%rsp,%rsi)
movl %edx, 0x38(%rsp,%rsi)
movl %ecx, %eax
cltd
idivl %ebx
addl %eax, 0x8(%rsp,%rsi)
movl %edx, 0x28(%rsp,%rsi)
addq $0x4, %rsi
cmpq $0xc, %rsi
jne 0x34d0b
incl %r14d
cmpl %ebp, %r14d
jne 0x34cef
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop