// File: /workspace/github_cpp_makefile_star2_O3/tomfunkhouser[P]gaps/pkgs/R2Shapes/R2Grid.cpp
// Function: gaps::R2Grid::RasterizeGridTriangle(int const*, int const*, int const*, double, double, double, int)
void R2Grid::
RasterizeGridTriangle(const int p1[2], const int p2[2], const int p3[2], RNScalar value1, RNScalar value2, RNScalar value3, int operation)
{
  // Resolve values
  if (value1 == R2_GRID_UNKNOWN_VALUE) return;
  if (value2 == R2_GRID_UNKNOWN_VALUE) return;
  if (value3 == R2_GRID_UNKNOWN_VALUE) return;

  // Get convenient point variables
  R2Point points[3];
  points[0][0] = p1[0];
  points[0][1] = p1[1];
  points[1][0] = p2[0];
  points[1][1] = p2[1];
  points[2][0] = p3[0];
  points[2][1] = p3[1];

  // Get convenient value variables
  RNScalar values[3];
  values[0] = value1;
  values[1] = value2;
  values[2] = value3;

  // Sort vertex indices by Y coordinate
  int iv0, iv1, iv2;
  if (points[0].Y() < points[1].Y()) {
    if (points[0].Y() < points[2].Y()) { 
      if (points[1].Y() < points[2].Y()) { iv0 = 0; iv1 = 1; iv2 = 2; }
      else { iv0 = 0; iv1 = 2; iv2 = 1; }
    }
    else { iv0 = 2; iv1 = 0; iv2 = 1; }
  }
  else {
    if (points[1].Y() < points[2].Y()) { 
      if (points[0].Y() < points[2].Y()) { iv0 = 1; iv1 = 0; iv2 = 2; }
      else { iv0 = 1; iv1 = 2; iv2 = 0; }
    }
    else { iv0 = 2; iv1 = 1; iv2 = 0; }
  }
  
  // Sort vertex coordinates
  double dy = points[iv2].Y() - points[iv0].Y();
  double t1 = (dy > 0) ? (points[iv1].Y() - points[iv0].Y()) / dy : 0;
  double y0 = points[iv0].Y();
  double y1 = points[iv1].Y();
  double y2 = points[iv2].Y();
  double x0 = points[iv0].X();
  double x2 = points[iv2].X();
  double x1a = points[iv1].X();
  double x1b = (1-t1)*x0 + t1*x2;
  double val0 = values[iv0];
  double val2 = values[iv2];
  double val1a = values[iv1];
  double val1b = (1-t1)*val0 + t1*val2;
  if (x1a > x1b) { 
    double swap;
    swap = x1a; x1a = x1b; x1b = swap; 
    swap = val1a; val1a = val1b; val1b = swap; 
  }

  // Rasterize lower half of triangle
  int iy0 = (int) (y0 + 0.5);
  int iy1 = (int) (y1 + 0.5);
  int nysteps = (iy1 - iy0) + 1;
  double xa_step = (x1a - x0) / nysteps;
  double xb_step = (x1b - x0) / nysteps;
  double vala_step = (val1a  - val0) / nysteps;
  double valb_step = (val1b  - val0) / nysteps;
  double xa = x0;
  double xb = x0;
  double vala = val0;
  double valb = val0;
  for (int iy = iy0; iy <= iy1; iy++) {
    int ixa = (int) (xa + 0.5);
    int ixb = (int) (xb + 0.5);
    int nxsteps = (ixb - ixa) + 1;
    double val_step = (valb - vala) / nxsteps;
    double val = vala;
    for (int ix = ixa; ix <= ixb; ix++) {
      RasterizeGridValue(ix, iy, val, operation);
      val += val_step;
    }
    vala += vala_step;
    valb += valb_step;
    xa += xa_step;
    xb += xb_step;
  }
  
  // Rasterize upper half of triangle
  int iy2 = (int) (y2 + 0.5);
  nysteps = (iy2 - iy1) + 1;
  xa_step = (x2 - x1a) / nysteps;
  xb_step = (x2 - x1b) / nysteps;
  vala_step = (val2  - val1a) / nysteps;
  valb_step = (val2  - val1b) / nysteps;
  xa = x1a;
  xb = x1b;
  vala = val1a;
  valb = val1b;
  for (int iy = iy1; iy <= iy2; iy++) {
    int ixa = (int) (xa + 0.5);
    int ixb = (int) (xb + 0.5);
    int nxsteps = (ixb - ixa) + 1;
    double val_step = (valb - vala) / nxsteps;
    double val = vala;
    for (int ix = ixa; ix <= ixb; ix++) {
      RasterizeGridValue(ix, iy, val, operation);
      val += val_step;
    }
    vala += vala_step;
    valb += valb_step;
    xa += xa_step;
    xb += xb_step;
  }
}

// Assembly:
ucomisd 0x43efe(%rip), %xmm0    # 0xa2fb8
jne 0x5f0c2
jnp 0x5f50e
ucomisd 0x43eee(%rip), %xmm1    # 0xa2fb8
jne 0x5f0d2
jnp 0x5f50e
ucomisd 0x43ede(%rip), %xmm2    # 0xa2fb8
jne 0x5f0e2
jnp 0x5f50e
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xf8, %rsp
movl %r8d, %ebx
movq %rcx, %r15
movq %rdx, %r12
movq %rsi, %r13
movq %rdi, %r14
movsd %xmm0, (%rsp)
movsd %xmm1, 0x30(%rsp)
movsd %xmm2, 0x20(%rsp)
xorl %ebp, %ebp
leaq (%rsp,%rbp), %rdi
addq $0x80, %rdi
callq 0x680a4
addq $0x10, %rbp
cmpq $0x30, %rbp
jne 0x5f115
cvtdq2pd (%r13), %xmm1
movapd %xmm1, 0x80(%rsp)
cvtdq2pd (%r12), %xmm2
movapd %xmm2, 0x90(%rsp)
cvtdq2pd (%r15), %xmm0
movapd %xmm0, 0xa0(%rsp)
movsd (%rsp), %xmm3
movsd %xmm3, 0x40(%rsp)
movsd 0x30(%rsp), %xmm3
movsd %xmm3, 0x48(%rsp)
movsd 0x20(%rsp), %xmm3
movsd %xmm3, 0x50(%rsp)
unpckhpd %xmm1, %xmm1            # xmm1 = xmm1[1,1]
unpckhpd %xmm2, %xmm2            # xmm2 = xmm2[1,1]
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
ucomisd %xmm1, %xmm2
jbe 0x5f1b9
ucomisd %xmm1, %xmm0
jbe 0x5f1e0
xorl %ecx, %ecx
ucomisd %xmm2, %xmm0
seta %cl
movl $0x2, %eax
subq %rcx, %rax
incq %rcx
leaq 0x80(%rsp), %r8
leaq 0x40(%rsp), %rdi
jmp 0x5f1fd
ucomisd %xmm2, %xmm0
jbe 0x5f1e9
xorl %eax, %eax
xorl %ecx, %ecx
ucomisd %xmm1, %xmm0
setbe %al
seta %cl
addl %ecx, %ecx
addl %eax, %eax
leaq 0x48(%rsp), %rdi
leaq 0x90(%rsp), %r8
jmp 0x5f1fd
movl $0x1, %ecx
xorl %eax, %eax
jmp 0x5f1f0
movl $0x1, %eax
xorl %ecx, %ecx
leaq 0x50(%rsp), %rdi
leaq 0xa0(%rsp), %r8
movl %ecx, %edx
shll $0x4, %edx
movsd 0x88(%rsp,%rdx), %xmm3
movsd 0x8(%r8), %xmm0
movsd %xmm3, 0xb8(%rsp)
subsd %xmm0, %xmm3
movl %eax, %esi
shll $0x4, %esi
movsd 0x80(%rsp,%rsi), %xmm2
movsd 0x88(%rsp,%rsi), %xmm1
movapd %xmm1, %xmm4
subsd %xmm0, %xmm4
divsd %xmm3, %xmm4
xorpd %xmm5, %xmm5
cmpltsd %xmm3, %xmm5
andpd %xmm4, %xmm5
movsd 0x42de2(%rip), %xmm3    # 0xa2038
subsd %xmm5, %xmm3
unpcklpd %xmm5, %xmm5            # xmm5 = xmm5[0,0]
movsd 0x40(%rsp,%rcx,8), %xmm4
movhpd 0x80(%rsp,%rdx), %xmm4  # xmm4 = xmm4[0],mem[0]
movapd %xmm4, 0x70(%rsp)
mulpd %xmm4, %xmm5
unpcklpd %xmm3, %xmm3            # xmm3 = xmm3[0,0]
movsd (%rdi), %xmm6
movhpd (%r8), %xmm6            # xmm6 = xmm6[0],mem[0]
mulpd %xmm6, %xmm3
addpd %xmm5, %xmm3
movapd %xmm3, %xmm4
unpckhpd %xmm3, %xmm4            # xmm4 = xmm4[1],xmm3[1]
ucomisd %xmm4, %xmm2
movsd 0x40(%rsp,%rax,8), %xmm4
unpcklpd %xmm2, %xmm4            # xmm4 = xmm4[0],xmm2[0]
ja 0x5f2b1
movapd %xmm4, (%rsp)
movapd %xmm3, 0x10(%rsp)
jmp 0x5f2bc
movapd %xmm4, 0x10(%rsp)
movapd %xmm3, (%rsp)
unpcklpd %xmm0, %xmm1            # xmm1 = xmm1[0],xmm0[0]
addpd 0x42d48(%rip), %xmm1    # 0xa2010
cvttpd2dq %xmm1, %xmm0
movd %xmm0, %r15d
pshufd $0x55, %xmm0, %xmm0     # xmm0 = xmm0[1,1,1,1]
movd %xmm0, %r12d
movl %r15d, %eax
subl %r12d, %eax
jl 0x5f3ef
incl %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movapd (%rsp), %xmm1
subpd %xmm6, %xmm1
movapd 0x10(%rsp), %xmm2
subpd %xmm6, %xmm2
unpcklpd %xmm0, %xmm0            # xmm0 = xmm0[0,0]
divpd %xmm0, %xmm2
movapd %xmm2, 0xd0(%rsp)
divpd %xmm0, %xmm1
shufpd $0x1, %xmm1, %xmm1      # xmm1 = xmm1[1,0]
movapd %xmm1, 0xe0(%rsp)
movapd %xmm6, %xmm1
shufpd $0x1, %xmm6, %xmm1      # xmm1 = xmm1[1],xmm6[0]
movapd %xmm6, 0x60(%rsp)
movapd %xmm6, %xmm0
movapd %xmm1, 0xc0(%rsp)
shufpd $0x1, %xmm1, %xmm0      # xmm0 = xmm0[1],xmm1[0]
addpd 0x42cc1(%rip), %xmm0    # 0xa2010
cvttpd2dq %xmm0, %xmm0
movd %xmm0, %r13d
pshufd $0x55, %xmm0, %xmm0     # xmm0 = xmm0[1,1,1,1]
movd %xmm0, %ebp
movl %r13d, %eax
subl %ebp, %eax
jl 0x5f3bd
incl %eax
movapd 0xc0(%rsp), %xmm0
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
movapd 0x60(%rsp), %xmm2
subsd %xmm0, %xmm2
xorps %xmm1, %xmm1
cvtsi2sd %eax, %xmm1
divsd %xmm1, %xmm2
movapd %xmm2, 0x30(%rsp)
incl %r13d
movapd %xmm0, 0x20(%rsp)
movq %r14, %rdi
movl %ebp, %esi
movl %r12d, %edx
movl %ebx, %ecx
callq 0x5eb14
movapd 0x20(%rsp), %xmm0
addsd 0x30(%rsp), %xmm0
incl %ebp
cmpl %ebp, %r13d
jne 0x5f395
movapd 0x60(%rsp), %xmm6
addpd 0xd0(%rsp), %xmm6
movapd 0xc0(%rsp), %xmm1
addpd 0xe0(%rsp), %xmm1
leal 0x1(%r12), %eax
cmpl %r15d, %r12d
movl %eax, %r12d
jne 0x5f32f
movsd 0xb8(%rsp), %xmm0
addsd 0x42c30(%rip), %xmm0    # 0xa2030
cvttsd2si %xmm0, %r12d
movl %r12d, %eax
subl %r15d, %eax
movapd (%rsp), %xmm2
movapd 0x10(%rsp), %xmm3
jl 0x5f4fd
incl %eax
movapd 0x70(%rsp), %xmm1
movapd %xmm1, %xmm4
subpd %xmm2, %xmm4
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
subpd %xmm3, %xmm1
unpcklpd %xmm0, %xmm0            # xmm0 = xmm0[0,0]
divpd %xmm0, %xmm4
movapd %xmm4, 0x60(%rsp)
divpd %xmm0, %xmm1
movapd %xmm1, 0x70(%rsp)
movapd %xmm2, (%rsp)
movapd %xmm2, %xmm0
movapd %xmm3, 0x10(%rsp)
unpckhpd %xmm3, %xmm0            # xmm0 = xmm0[1],xmm3[1]
addpd 0x42ba6(%rip), %xmm0    # 0xa2010
cvttpd2dq %xmm0, %xmm0
movd %xmm0, %ebp
pshufd $0x55, %xmm0, %xmm0     # xmm0 = xmm0[1,1,1,1]
movd %xmm0, %r13d
movl %r13d, %eax
subl %ebp, %eax
jl 0x5f4d6
incl %eax
movapd 0x10(%rsp), %xmm2
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movapd (%rsp), %xmm1
subsd %xmm1, %xmm2
divsd %xmm0, %xmm2
movapd %xmm2, 0x30(%rsp)
incl %r13d
movsd %xmm1, 0x20(%rsp)
movq %r14, %rdi
movl %ebp, %esi
movl %r15d, %edx
movq 0x20(%rsp), %xmm0
movl %ebx, %ecx
callq 0x5eb14
movsd 0x20(%rsp), %xmm1
addsd 0x30(%rsp), %xmm1
incl %ebp
cmpl %ebp, %r13d
jne 0x5f4a8
movapd (%rsp), %xmm2
addpd 0x60(%rsp), %xmm2
movapd 0x10(%rsp), %xmm3
addpd 0x70(%rsp), %xmm3
leal 0x1(%r15), %eax
cmpl %r12d, %r15d
movl %eax, %r15d
jne 0x5f44f
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop