// File: /workspace/github_cpp_makefile_star2_O2/ddunbar[P]PDSample/PDSampling.cpp
// Function: PDSampler::findNeighborRanges(int, RangeList&)
void PDSampler::findNeighborRanges(int index, RangeList &rl)
{
	if (!m_grid) {
		printf("Internal error, sampler cannot search without grid.\n");
		exit(1);
	}

	Vec2 &candidate = points[index];
	float rangeSqrd = 4*4*radius*radius;
	int i, j, k, gx, gy, N = (int) ceil(4*radius/m_gridCellSize);
	if (N>(m_gridSize>>1)) N = m_gridSize>>1;
	
	getGridXY(candidate, &gx, &gy);

	int xSide = (candidate.x - (-1 + gx*m_gridCellSize))>m_gridCellSize*.5;
	int ySide = (candidate.y - (-1 + gy*m_gridCellSize))>m_gridCellSize*.5;
	int iy = 1;
	for (j=-N; j<=N; j++) {
		int ix = 1;

		if (j==0) iy = ySide;
		else if (j==1) iy = 0;

		for (i=-N; i<=N; i++) {
			if (i==0) ix = xSide;
			else if (i==1) ix = 0;

				// offset to closest cell point
			float dx = candidate.x - (-1 + (gx+i+ix)*m_gridCellSize);
			float dy = candidate.y - (-1 + (gy+j+iy)*m_gridCellSize);

			if (dx*dx+dy*dy<rangeSqrd) {
				int cx = (gx+i+m_gridSize)%m_gridSize;
				int cy = (gy+j+m_gridSize)%m_gridSize;
				int *cell = m_grid[cy*m_gridSize + cx];

				for (k=0; k<kMaxPointsPerCell; k++) {
					if (cell[k]==-1) {
						break;
					} else if (cell[k]!=index) {
						Vec2 &pt = points[cell[k]];
						Vec2 v = getTiled(pt-candidate);
						float distSqrd = v.x*v.x + v.y*v.y;

						if (distSqrd<rangeSqrd) {
							float dist = sqrt(distSqrd);
							float angle = atan2(v.y,v.x);
							float theta = acos(.25f*dist/radius);

							rl.subtract(angle-theta, angle+theta);
						}
					}
				}
			}
		}
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rdx, 0x58(%rsp)
cmpq $0x0, 0x13a8(%rdi)
je 0x3774
movl %esi, %ebp
movq %rdi, %r14
movslq %esi, %rax
movq 0x13b8(%rdi), %rcx
leaq (%rcx,%rax,8), %rbx
movss 0x13d0(%rdi), %xmm0
movss 0x5fc6(%rip), %xmm1     # 0x9478
mulss %xmm0, %xmm1
mulss %xmm0, %xmm1
movss %xmm1, 0xc(%rsp)
mulss 0x5fb4(%rip), %xmm0     # 0x947c
divss 0x13b4(%rdi), %xmm0
callq 0x20c0
cvttss2si %xmm0, %eax
movl 0x13b0(%r14), %r15d
sarl %r15d
cmpl %eax, %r15d
cmovgel %eax, %r15d
leaq 0x64(%rsp), %r12
leaq 0x60(%rsp), %r13
movq %r14, %rdi
movq %rbx, %rsi
movq %r12, %rdx
movq %r13, %rcx
callq 0x3016
movss 0x13b4(%r14), %xmm0
cvtss2sd %xmm0, %xmm1
mulsd 0x5b36(%rip), %xmm1     # 0x9050
movl (%r12), %ecx
movl (%r13), %edx
movq %rbx, 0x28(%rsp)
movsd (%rbx), %xmm2
movq %rdx, 0x30(%rsp)
movd %edx, %xmm3
movq %rcx, 0x50(%rsp)
movd %ecx, %xmm4
punpckldq %xmm3, %xmm4    # xmm4 = xmm4[0],xmm3[0],xmm4[1],xmm3[1]
cvtdq2ps %xmm4, %xmm3
shufps $0x0, %xmm0, %xmm0      # xmm0 = xmm0[0,0,0,0]
mulps %xmm3, %xmm0
addps 0x5f4e(%rip), %xmm0     # 0x94a0
subps %xmm0, %xmm2
cvtps2pd %xmm2, %xmm0
unpcklpd %xmm1, %xmm1            # xmm1 = xmm1[0,0]
cmpltpd %xmm0, %xmm1
movmskpd %xmm1, %ecx
movl %ecx, %eax
shrb %al
andl $0x1, %ecx
movl %ecx, 0x18(%rsp)
movzbl %al, %eax
movl %eax, 0x14(%rsp)
movl %r15d, 0x8(%rsp)
negl %r15d
pushq $0x1
popq %rax
movl %r15d, 0x10(%rsp)
movl %r15d, %edx
cmpl 0x8(%rsp), %edx
jg 0x3765
movl 0x14(%rsp), %ecx
testl %edx, %edx
je 0x35a7
cmpl $0x1, %edx
jne 0x35a5
xorl %ecx, %ecx
jmp 0x35a7
movl %eax, %ecx
movq 0x30(%rsp), %rax
movq %rdx, 0x40(%rsp)
addl %edx, %eax
movq %rcx, 0x38(%rsp)
movq %rax, 0x48(%rsp)
addl %ecx, %eax
cvtsi2ss %eax, %xmm0
movss %xmm0, 0x1c(%rsp)
pushq $0x1
popq %rax
movl 0x10(%rsp), %ecx
movl %ecx, %r12d
cmpl 0x8(%rsp), %r12d
jg 0x3754
movl 0x18(%rsp), %ecx
movl %ecx, %r13d
testl %r12d, %r12d
je 0x35f8
cmpl $0x1, %r12d
jne 0x35f5
xorl %r13d, %r13d
jmp 0x35f8
movl %eax, %r13d
movq 0x50(%rsp), %rax
addl %r12d, %eax
leal (%r13,%rax), %ecx
cvtsi2ss %ecx, %xmm0
movss 0x13b4(%r14), %xmm1
mulss %xmm1, %xmm0
movss 0x5e4e(%rip), %xmm3     # 0x946c
addss %xmm3, %xmm0
movq 0x28(%rsp), %rcx
movss (%rcx), %xmm2
subss %xmm0, %xmm2
movss 0x4(%rcx), %xmm0
mulss 0x1c(%rsp), %xmm1
addss %xmm3, %xmm1
subss %xmm1, %xmm0
mulss %xmm0, %xmm0
mulss %xmm2, %xmm2
addss %xmm0, %xmm2
movss 0xc(%rsp), %xmm0
ucomiss %xmm2, %xmm0
jbe 0x3749
movl 0x13b0(%r14), %esi
addl %esi, %eax
cltd
idivl %esi
movl %edx, %ecx
movq 0x48(%rsp), %rax
addl %esi, %eax
cltd
idivl %esi
imull %esi, %edx
addl %ecx, %edx
movslq %edx, %rax
imulq $0x24, %rax, %rbx
addq 0x13a8(%r14), %rbx
xorl %r15d, %r15d
cmpq $0x9, %r15
je 0x3749
movslq (%rbx,%r15,4), %rax
cmpq $-0x1, %rax
je 0x3749
cmpl %ebp, %eax
je 0x3741
movq 0x13b8(%r14), %rcx
movq 0x28(%rsp), %rdx
movsd (%rdx), %xmm1
movsd (%rcx,%rax,8), %xmm0
subps %xmm1, %xmm0
movq %r14, %rdi
callq 0x2fb6
movaps %xmm0, %xmm1
shufps $0x55, %xmm0, %xmm0     # xmm0 = xmm0[1,1,1,1]
movaps %xmm0, %xmm3
mulss %xmm0, %xmm3
movaps %xmm1, %xmm2
mulss %xmm1, %xmm2
addss %xmm3, %xmm2
movss 0xc(%rsp), %xmm3
ucomiss %xmm2, %xmm3
jbe 0x3741
sqrtss %xmm2, %xmm2
movss %xmm2, 0x20(%rsp)
callq 0x2220
movss %xmm0, 0x24(%rsp)
movss 0x20(%rsp), %xmm0
mulss 0x5d6e(%rip), %xmm0     # 0x9480
divss 0x13d0(%r14), %xmm0
callq 0x21c0
movss 0x24(%rsp), %xmm2
movaps %xmm2, %xmm1
subss %xmm0, %xmm1
addss %xmm0, %xmm2
movq 0x58(%rsp), %rdi
movaps %xmm1, %xmm0
movaps %xmm2, %xmm1
callq 0x6a44
incq %r15
jmp 0x368b
incl %r12d
movl %r13d, %eax
jmp 0x35d3
movq 0x40(%rsp), %rdx
incl %edx
movq 0x38(%rsp), %rax
jmp 0x358a
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x7d43(%rip), %rdi      # 0xb4be
callq 0x2280
pushq $0x1
popq %rdi
callq 0x21f0