// File: matthewarcus[P]mmps/image.cpp
// Function: Image::PlotLineAux(double, double, LinePlotter const&, Rgb const&, int)
void Image::PlotLineAux(double t0, double t1, 
			const LinePlotter& lineplotter,
			const Rgb& color,
			int depth)
{
  if (depth == 0) {
    return;
  }
  double x0 = 0.0, y0 = 0.0;
  double x1 = 0.0, y1 = 0.0;
  // The bools tell us if the inverse mapping is even defined
  bool defined0 = lineplotter.GetXY(t0, x0, y0);
  bool defined1 = lineplotter.GetXY(t1, x1, y1);
  // This really ought to take note of the curvature.
  if (defined0 && defined1 && fabs (x0-x1) + fabs(y0-y1) < 10) {
    long ix0 = long(ROUND(x0));
    long iy0 = long(ROUND(y0));
    long ix1 = long(ROUND(x1));
    long iy1 = long(ROUND(y1));
    DrawLine(ix0,iy0,ix1,iy1,color);
    //CheckSetRgb(ix0,iy0,Rgb(255,255,255));
    //CheckSetRgb(ix1,iy1,Rgb(255,255,255));
  } else {
    int margin = 50;
    // Consider whether to recurse
    // If both points are off to one side or above or below the image,
    // don't recurse, we assume that the line doesn't pass through.
    long sidex0 = (x0 > width + margin) ? 1 : (x0 < -margin) ? -1 : 0;
    long sidey0 = (y0 > height + margin) ? 1 : (y0 < -margin) ? -1 : 0;
    long sidex1 = (x1 > width + margin) ? 1 : (x1 < -margin) ? -1 : 0;
    long sidey1 = (y1 > height + margin) ? 1 : (y1 < -margin) ? 1 : 0;
    if ((defined0 || defined1) && sidex0 * sidex1 + sidey0 * sidey1 <= 0) {
#if 0
      double t2 = (t0 + t0 + t1) / 3;
      PlotLineAux(t0, t2, lineplotter, color, depth-1);
      double t3 = (t0 + t1 + t1) / 3;
      PlotLineAux(t2, t3, lineplotter, color, depth-1);
      PlotLineAux(t3, t1, lineplotter, color, depth-1);
#else
      double t2 = (t0 + t1) / 2;
      PlotLineAux(t0, t2, lineplotter, color, depth-1);
      PlotLineAux(t2, t1, lineplotter, color, depth-1);
#endif
    } else {
#if 0
      fprintf(stderr, "Giving up: %f %f %f %f %f %f\n",
              t0, t1, x0, y0, x1, y1);
#endif
    }
    
  }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xc0, %rsp
movq %rdi, -0x8(%rbp)
movsd %xmm0, -0x10(%rbp)
movsd %xmm1, -0x18(%rbp)
movq %rsi, -0x20(%rbp)
movq %rdx, -0x28(%rbp)
movl %ecx, -0x2c(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0xb0(%rbp)
cmpl $0x0, -0x2c(%rbp)
jne 0x13f3a
jmp 0x14282
xorps %xmm0, %xmm0
movsd %xmm0, -0x38(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x40(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x48(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x50(%rbp)
movq -0x20(%rbp), %rdi
movsd -0x10(%rbp), %xmm0
movq (%rdi), %rax
leaq -0x38(%rbp), %rsi
leaq -0x40(%rbp), %rdx
callq *(%rax)
andb $0x1, %al
movb %al, -0x51(%rbp)
movq -0x20(%rbp), %rdi
movsd -0x18(%rbp), %xmm0
movq (%rdi), %rax
leaq -0x48(%rbp), %rsi
leaq -0x50(%rbp), %rdx
callq *(%rax)
andb $0x1, %al
movb %al, -0x52(%rbp)
testb $0x1, -0x51(%rbp)
je 0x14051
testb $0x1, -0x52(%rbp)
je 0x14051
movsd -0x38(%rbp), %xmm1
subsd -0x48(%rbp), %xmm1
movaps 0x14eb(%rip), %xmm0     # 0x154a0
pand %xmm0, %xmm1
movsd -0x40(%rbp), %xmm0
subsd -0x50(%rbp), %xmm0
movaps 0x14d6(%rip), %xmm2     # 0x154a0
pand %xmm2, %xmm0
addsd %xmm0, %xmm1
movsd 0x1976(%rip), %xmm0     # 0x15950
ucomisd %xmm1, %xmm0
jbe 0x14051
movsd -0x38(%rbp), %xmm0
callq 0x13930
cvttsd2si %xmm0, %rax
movq %rax, -0x60(%rbp)
movsd -0x40(%rbp), %xmm0
callq 0x13930
cvttsd2si %xmm0, %rax
movq %rax, -0x68(%rbp)
movsd -0x48(%rbp), %xmm0
callq 0x13930
cvttsd2si %xmm0, %rax
movq %rax, -0x70(%rbp)
movsd -0x50(%rbp), %xmm0
callq 0x13930
movq -0xb0(%rbp), %rdi
cvttsd2si %xmm0, %rax
movq %rax, -0x78(%rbp)
movq -0x60(%rbp), %rsi
movq -0x68(%rbp), %rdx
movq -0x70(%rbp), %rcx
movq -0x78(%rbp), %r8
movq -0x28(%rbp), %r9
callq 0x13ae0
jmp 0x14282
movq -0xb0(%rbp), %rax
movl $0x32, -0x7c(%rbp)
movsd -0x38(%rbp), %xmm0
movq (%rax), %rax
movslq -0x7c(%rbp), %rcx
addq %rcx, %rax
cvtsi2sd %rax, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x14086
movl $0x1, %eax
movl %eax, -0xb4(%rbp)
jmp 0x140a8
movsd -0x38(%rbp), %xmm1
xorl %eax, %eax
subl -0x7c(%rbp), %eax
cvtsi2sd %eax, %xmm0
xorl %eax, %eax
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
ucomisd %xmm1, %xmm0
cmoval %ecx, %eax
movl %eax, -0xb4(%rbp)
movq -0xb0(%rbp), %rax
movl -0xb4(%rbp), %ecx
movslq %ecx, %rcx
movq %rcx, -0x88(%rbp)
movsd -0x40(%rbp), %xmm0
movq 0x8(%rax), %rax
movslq -0x7c(%rbp), %rcx
addq %rcx, %rax
cvtsi2sd %rax, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x140e7
movl $0x1, %eax
movl %eax, -0xb8(%rbp)
jmp 0x14109
movsd -0x40(%rbp), %xmm1
xorl %eax, %eax
subl -0x7c(%rbp), %eax
cvtsi2sd %eax, %xmm0
xorl %eax, %eax
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
ucomisd %xmm1, %xmm0
cmoval %ecx, %eax
movl %eax, -0xb8(%rbp)
movq -0xb0(%rbp), %rax
movl -0xb8(%rbp), %ecx
movslq %ecx, %rcx
movq %rcx, -0x90(%rbp)
movsd -0x48(%rbp), %xmm0
movq (%rax), %rax
movslq -0x7c(%rbp), %rcx
addq %rcx, %rax
cvtsi2sd %rax, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x14147
movl $0x1, %eax
movl %eax, -0xbc(%rbp)
jmp 0x14169
movsd -0x48(%rbp), %xmm1
xorl %eax, %eax
subl -0x7c(%rbp), %eax
cvtsi2sd %eax, %xmm0
xorl %eax, %eax
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
ucomisd %xmm1, %xmm0
cmoval %ecx, %eax
movl %eax, -0xbc(%rbp)
movq -0xb0(%rbp), %rax
movl -0xbc(%rbp), %ecx
movslq %ecx, %rcx
movq %rcx, -0x98(%rbp)
movsd -0x50(%rbp), %xmm0
movq 0x8(%rax), %rax
movslq -0x7c(%rbp), %rcx
addq %rcx, %rax
cvtsi2sd %rax, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x141a8
movl $0x1, %eax
movl %eax, -0xc0(%rbp)
jmp 0x141ca
movsd -0x50(%rbp), %xmm1
xorl %eax, %eax
subl -0x7c(%rbp), %eax
cvtsi2sd %eax, %xmm0
xorl %eax, %eax
movl $0x1, %ecx
ucomisd %xmm1, %xmm0
cmoval %ecx, %eax
movl %eax, -0xc0(%rbp)
movl -0xc0(%rbp), %eax
cltq
movq %rax, -0xa0(%rbp)
testb $0x1, -0x51(%rbp)
jne 0x141e9
testb $0x1, -0x52(%rbp)
je 0x1427e
movq -0x88(%rbp), %rax
imulq -0x98(%rbp), %rax
movq -0x90(%rbp), %rcx
imulq -0xa0(%rbp), %rcx
addq %rcx, %rax
cmpq $0x0, %rax
jg 0x1427e
movq -0xb0(%rbp), %rdi
movsd -0x10(%rbp), %xmm0
addsd -0x18(%rbp), %xmm0
movsd 0x11f7(%rip), %xmm1     # 0x15420
divsd %xmm1, %xmm0
movsd %xmm0, -0xa8(%rbp)
movsd -0x10(%rbp), %xmm0
movsd -0xa8(%rbp), %xmm1
movq -0x20(%rbp), %rsi
movq -0x28(%rbp), %rdx
movl -0x2c(%rbp), %ecx
subl $0x1, %ecx
callq 0x13f00
movq -0xb0(%rbp), %rdi
movsd -0xa8(%rbp), %xmm0
movsd -0x18(%rbp), %xmm1
movq -0x20(%rbp), %rsi
movq -0x28(%rbp), %rdx
movl -0x2c(%rbp), %ecx
subl $0x1, %ecx
callq 0x13f00
jmp 0x14280
jmp 0x14280
jmp 0x14282
addq $0xc0, %rsp
popq %rbp
retq
nopl (%rax,%rax)