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

  // Get some convenient variables
  int d[2],p[2],dd[2],s[2];
  for (int i = 0; i < 2; i++) {
    d[i]= p2[i] - p1[i];
    if(d[i]<0){
      dd[i] = -d[i];
      s[i] = -1;
    }
    else{
      dd[i] = d[i];
      s[i] = 1;
    }
    p[i] = p1[i];
  }

  // Choose dimensions
  int i1=0;
  if(dd[1]>dd[i1]){i1=1;}
  int i2=(i1+1)%2;

  // Check span extent
  if(dd[i1]==0){
    // Span is a point - rasterize it
    RNScalar value = 0.5 * (value1 + value2);
    RasterizeGridValue(p[0], p[1], value, operation);
  }
  else {
    // Step along span
    int off[2] = { 0, 0 };
    RNScalar value = value1;
    RNScalar dvalue = (value2 - value1) / dd[i1];
    for (int i = 0; i <= dd[i1]; i++) {
      RasterizeGridValue(p[0], p[1], value, operation);
      off[i2]+=dd[i2];
      p[i1]+=s[i1];
      p[i2]+=s[i2]*off[i2]/dd[i1];
      off[i2]%=dd[i1];
      value += dvalue;
    }
  }
}

// Assembly:
subq $0x98, %rsp
movq %rdi, 0x90(%rsp)
movq %rsi, 0x88(%rsp)
movq %rdx, 0x80(%rsp)
movsd %xmm0, 0x78(%rsp)
movsd %xmm1, 0x70(%rsp)
movl %ecx, 0x6c(%rsp)
movq 0x90(%rsp), %rax
movq %rax, 0x10(%rsp)
movsd 0x78(%rsp), %xmm0
movsd 0x834c6(%rip), %xmm1    # 0x133eb0
ucomisd %xmm1, %xmm0
jne 0xb09f7
jp 0xb09f7
jmp 0xb0c56
movsd 0x70(%rsp), %xmm0
movsd 0x834ab(%rip), %xmm1    # 0x133eb0
ucomisd %xmm1, %xmm0
jne 0xb0a12
jp 0xb0a12
jmp 0xb0c56
movl $0x0, 0x48(%rsp)
cmpl $0x2, 0x48(%rsp)
jge 0xb0ac5
movq 0x80(%rsp), %rax
movslq 0x48(%rsp), %rcx
movl (%rax,%rcx,4), %ecx
movq 0x88(%rsp), %rax
movslq 0x48(%rsp), %rdx
subl (%rax,%rdx,4), %ecx
movslq 0x48(%rsp), %rax
movl %ecx, 0x64(%rsp,%rax,4)
movslq 0x48(%rsp), %rax
cmpl $0x0, 0x64(%rsp,%rax,4)
jge 0xb0a7d
movslq 0x48(%rsp), %rax
xorl %ecx, %ecx
subl 0x64(%rsp,%rax,4), %ecx
movslq 0x48(%rsp), %rax
movl %ecx, 0x54(%rsp,%rax,4)
movslq 0x48(%rsp), %rax
movl $0xffffffff, 0x4c(%rsp,%rax,4) # imm = 0xFFFFFFFF
jmp 0xb0a9c
movslq 0x48(%rsp), %rax
movl 0x64(%rsp,%rax,4), %ecx
movslq 0x48(%rsp), %rax
movl %ecx, 0x54(%rsp,%rax,4)
movslq 0x48(%rsp), %rax
movl $0x1, 0x4c(%rsp,%rax,4)
movq 0x88(%rsp), %rax
movslq 0x48(%rsp), %rcx
movl (%rax,%rcx,4), %ecx
movslq 0x48(%rsp), %rax
movl %ecx, 0x5c(%rsp,%rax,4)
movl 0x48(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x48(%rsp)
jmp 0xb0a1a
movl $0x0, 0x44(%rsp)
movl 0x58(%rsp), %eax
movslq 0x44(%rsp), %rcx
cmpl 0x54(%rsp,%rcx,4), %eax
jle 0xb0ae4
movl $0x1, 0x44(%rsp)
movl 0x44(%rsp), %eax
addl $0x1, %eax
movl $0x2, %ecx
cltd
idivl %ecx
movl %edx, 0x40(%rsp)
movslq 0x44(%rsp), %rax
cmpl $0x0, 0x54(%rsp,%rax,4)
jne 0xb0b42
movq 0x10(%rsp), %rdi
movsd 0x78(%rsp), %xmm1
addsd 0x70(%rsp), %xmm1
movsd 0x824f4(%rip), %xmm0    # 0x133010
mulsd %xmm1, %xmm0
movsd %xmm0, 0x38(%rsp)
movl 0x5c(%rsp), %esi
movl 0x60(%rsp), %edx
movsd 0x38(%rsp), %xmm0
movl 0x6c(%rsp), %ecx
callq 0xb0550
jmp 0xb0c56
leaq 0x30(%rsp), %rdi
xorl %esi, %esi
movl $0x8, %edx
callq 0xf280
movsd 0x78(%rsp), %xmm0
movsd %xmm0, 0x28(%rsp)
movsd 0x70(%rsp), %xmm0
subsd 0x78(%rsp), %xmm0
movslq 0x44(%rsp), %rax
cvtsi2sdl 0x54(%rsp,%rax,4), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x20(%rsp)
movl $0x0, 0x1c(%rsp)
movl 0x1c(%rsp), %eax
movslq 0x44(%rsp), %rcx
cmpl 0x54(%rsp,%rcx,4), %eax
jg 0xb0c54
movq 0x10(%rsp), %rdi
movl 0x5c(%rsp), %esi
movl 0x60(%rsp), %edx
movsd 0x28(%rsp), %xmm0
movl 0x6c(%rsp), %ecx
callq 0xb0550
movslq 0x40(%rsp), %rax
movl 0x54(%rsp,%rax,4), %ecx
movslq 0x40(%rsp), %rax
addl 0x30(%rsp,%rax,4), %ecx
movl %ecx, 0x30(%rsp,%rax,4)
movslq 0x44(%rsp), %rax
movl 0x4c(%rsp,%rax,4), %ecx
movslq 0x44(%rsp), %rax
addl 0x5c(%rsp,%rax,4), %ecx
movl %ecx, 0x5c(%rsp,%rax,4)
movslq 0x40(%rsp), %rax
movl 0x4c(%rsp,%rax,4), %eax
movslq 0x40(%rsp), %rcx
imull 0x30(%rsp,%rcx,4), %eax
movslq 0x44(%rsp), %rcx
cltd
idivl 0x54(%rsp,%rcx,4)
movl %eax, %ecx
movslq 0x40(%rsp), %rax
addl 0x5c(%rsp,%rax,4), %ecx
movl %ecx, 0x5c(%rsp,%rax,4)
movslq 0x44(%rsp), %rax
movl 0x54(%rsp,%rax,4), %ecx
movslq 0x40(%rsp), %rax
movq %rax, 0x8(%rsp)
movl 0x30(%rsp,%rax,4), %eax
cltd
idivl %ecx
movq 0x8(%rsp), %rax
movl %edx, 0x30(%rsp,%rax,4)
movsd 0x20(%rsp), %xmm0
addsd 0x28(%rsp), %xmm0
movsd %xmm0, 0x28(%rsp)
movl 0x1c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1c(%rsp)
jmp 0xb0b88
jmp 0xb0c56
addq $0x98, %rsp
retq
nop