// File: /workspace/github_cpp_makefile_star2_O2/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 $0x78, %rsp
movl %r8d, %r13d
movsd %xmm0, 0x28(%rsp)
movq %rdx, %r15
movq %rdi, 0x20(%rsp)
xorl %eax, %eax
cmpq $0x3, %rax
je 0x30b1b
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, 0x6c(%rsp,%rax,4)
incq %rax
jmp 0x30ae7
movl 0x6c(%rsp), %eax
movl 0x70(%rsp), %edx
movl 0x74(%rsp), %edi
xorl %r8d, %r8d
cmpl %eax, %edi
setg %r8b
addl %r8d, %r8d
cmpl %edi, %edx
pushq $0x1
popq %r9
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 0x30b5e
movl (%rcx,%r9,4), %edi
cmpl %edi, %eax
jge 0x30b79
cmpl %eax, %edx
jl 0x30b6a
movl (%rcx,%r9,4), %edi
cmpl %edi, %edx
jge 0x30b8b
cmpl %edx, %eax
movq %rsi, %r12
cmovlq %r15, %r12
cmovlq %rsi, %r15
jmp 0x30ba1
cmpl %edi, %edx
movq %r15, %r12
cmovlq %rcx, %r12
cmovgeq %rcx, %r15
movq %rsi, %rcx
jmp 0x30ba1
cmpl %edi, %eax
movq %rsi, %r12
cmovlq %rcx, %r12
movq %rcx, %rax
cmovlq %rsi, %rax
movq %r15, %rcx
movq %rax, %r15
movq %r9, 0x48(%rsp)
leal (,%r9,4), %eax
movl (%rcx,%rax), %edx
movl (%r12,%rax), %esi
movl %edx, %r14d
subl %esi, %r14d
movl (%r15,%rax), %eax
movl %edx, %ebx
subl %eax, %ebx
xorl %edi, %edi
cmpq $0xc, %rdi
je 0x30c09
movl (%rcx,%rdi), %r8d
movl %r8d, 0x14(%rsp,%rdi)
movl %r8d, 0x8(%rsp,%rdi)
andl $0x0, 0x3c(%rsp,%rdi)
andl $0x0, 0x30(%rsp,%rdi)
movl (%r12,%rdi), %r9d
subl %r8d, %r9d
imull %ebx, %r9d
movl %r9d, 0x60(%rsp,%rdi)
movl (%r15,%rdi), %r9d
subl %r8d, %r9d
imull %r14d, %r9d
movl %r9d, 0x54(%rsp,%rdi)
addq $0x4, %rdi
jmp 0x30bc5
cmpl %esi, %edx
jne 0x30c32
xorl %esi, %esi
movq 0x48(%rsp), %r8
cmpq $0x3, %rsi
je 0x30cbb
movl (%rcx,%rsi,4), %edi
movl %edi, 0x14(%rsp,%rsi,4)
movl (%r12,%rsi,4), %edi
movl %edi, 0x8(%rsp,%rsi,4)
incq %rsi
jmp 0x30c14
movl %r13d, %ebp
imull %r14d, %ebx
xorl %r13d, %r13d
testl %r14d, %r14d
cmovlel %r13d, %r14d
cmpl %r14d, %r13d
je 0x30c9f
movq 0x20(%rsp), %rdi
leaq 0x14(%rsp), %rsi
leaq 0x8(%rsp), %rdx
movsd 0x28(%rsp), %xmm0
movl %ebp, %ecx
callq 0x308fc
xorl %esi, %esi
cmpq $0xc, %rsi
je 0x30c9a
movl 0x3c(%rsp,%rsi), %eax
addl 0x60(%rsp,%rsi), %eax
movl 0x30(%rsp,%rsi), %ecx
addl 0x54(%rsp,%rsi), %ecx
cltd
idivl %ebx
addl %eax, 0x14(%rsp,%rsi)
movl %edx, 0x3c(%rsp,%rsi)
movl %ecx, %eax
cltd
idivl %ebx
addl %eax, 0x8(%rsp,%rsi)
movl %edx, 0x30(%rsp,%rsi)
addq $0x4, %rsi
jmp 0x30c66
incl %r13d
jmp 0x30c43
movq 0x48(%rsp), %r8
leal (,%r8,4), %eax
movl (%r12,%rax), %edx
movl (%r15,%rax), %eax
movl %edx, %ebx
subl %eax, %ebx
movl %ebp, %r13d
cmpl %eax, %edx
jne 0x30ce6
movq 0x20(%rsp), %rdi
movq %r12, %rsi
movq %r15, %rdx
movsd 0x28(%rsp), %xmm0
movl %r13d, %ecx
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x308fc
shll $0x2, %r8d
movl 0x14(%rsp,%r8), %ecx
subl %eax, %ecx
movl 0x8(%rsp,%r8), %edx
subl %eax, %edx
movl %edx, %ebp
imull %ecx, %ebp
xorl %eax, %eax
cmpq $0xc, %rax
je 0x30d31
andl $0x0, 0x3c(%rsp,%rax)
andl $0x0, 0x30(%rsp,%rax)
movl (%r15,%rax), %esi
movl %esi, %edi
subl 0x14(%rsp,%rax), %edi
imull %edx, %edi
subl 0x8(%rsp,%rax), %esi
movl %edi, 0x60(%rsp,%rax)
imull %ecx, %esi
movl %esi, 0x54(%rsp,%rax)
addq $0x4, %rax
jmp 0x30cff
xorl %r14d, %r14d
leaq 0x14(%rsp), %r15
leaq 0x8(%rsp), %r12
cmpl %ebx, %r14d
jg 0x30d97
movq 0x20(%rsp), %rdi
movq %r15, %rsi
movq %r12, %rdx
movsd 0x28(%rsp), %xmm0
movl %r13d, %ecx
callq 0x308fc
xorl %esi, %esi
cmpq $0xc, %rsi
je 0x30d92
movl 0x3c(%rsp,%rsi), %eax
addl 0x60(%rsp,%rsi), %eax
movl 0x30(%rsp,%rsi), %ecx
addl 0x54(%rsp,%rsi), %ecx
cltd
idivl %ebp
addl %eax, 0x14(%rsp,%rsi)
movl %edx, 0x3c(%rsp,%rsi)
movl %ecx, %eax
cltd
idivl %ebp
addl %eax, 0x8(%rsp,%rsi)
movl %edx, 0x30(%rsp,%rsi)
addq $0x4, %rsi
jmp 0x30d5e
incl %r14d
jmp 0x30d3e
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq