// File: /workspace/github_cpp_makefile_star2_O3/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 0x3963
movl %esi, %ebp
movq %rdi, %r14
movslq %esi, %rbx
movq 0x13b8(%rdi), %r12
leaq (%r12,%rbx,8), %r15
movss 0x13d0(%rdi), %xmm1
movss 0x6e3c(%rip), %xmm0     # 0xa47c
movss %xmm1, 0xc(%rsp)
mulss %xmm1, %xmm0
divss 0x13b4(%rdi), %xmm0
callq 0x20d0
cvttss2si %xmm0, %eax
movl 0x13b0(%r14), %r13d
sarl %r13d
cmpl %eax, %r13d
cmovgel %eax, %r13d
leaq 0x2c(%rsp), %rdx
leaq 0x28(%rsp), %rcx
movq %r14, %rdi
movq %r15, %rsi
callq 0x313c
movl %r13d, 0x8(%rsp)
testl %r13d, %r13d
js 0x3954
movss 0x6de1(%rip), %xmm4     # 0xa478
movss 0xc(%rsp), %xmm0
mulss %xmm0, %xmm4
mulss %xmm0, %xmm4
movl 0x2c(%rsp), %ecx
movss 0x13b4(%r14), %xmm0
xorps %xmm1, %xmm1
cvtss2sd %xmm0, %xmm1
mulsd 0x698f(%rip), %xmm1     # 0xa050
movl 0x28(%rsp), %edx
movq %rdx, 0x30(%rsp)
movd %edx, %xmm2
movq %rcx, 0x50(%rsp)
movd %ecx, %xmm3
punpckldq %xmm2, %xmm3    # xmm3 = xmm3[0],xmm2[0],xmm3[1],xmm2[1]
cvtdq2ps %xmm3, %xmm2
shufps $0x0, %xmm0, %xmm0      # xmm0 = xmm0[0,0,0,0]
mulps %xmm2, %xmm0
addps 0x6db4(%rip), %xmm0     # 0xa4a0
movsd (%r12,%rbx,8), %xmm2
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 0x8(%rsp), %ecx
negl %ecx
movl $0x1, %eax
movss 0x6d42(%rip), %xmm5     # 0xa46c
movl %ecx, 0x10(%rsp)
movl %ecx, %edx
movss %xmm4, 0x20(%rsp)
movl 0x14(%rsp), %ecx
testl %edx, %edx
je 0x3749
cmpl $0x1, %edx
jne 0x3747
xorl %ecx, %ecx
jmp 0x3749
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
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
movss %xmm0, 0x1c(%rsp)
movl $0x1, %ecx
movl 0x10(%rsp), %eax
movl %eax, %r13d
movl %ecx, %eax
movl 0x18(%rsp), %ecx
testl %r13d, %r13d
je 0x3791
cmpl $0x1, %r13d
jne 0x378f
xorl %ecx, %ecx
jmp 0x3791
movl %eax, %ecx
movq 0x50(%rsp), %rax
addl %r13d, %eax
movq %rcx, 0x60(%rsp)
leal (%rcx,%rax), %ecx
xorps %xmm0, %xmm0
cvtsi2ss %ecx, %xmm0
movss 0x13b4(%r14), %xmm1
mulss %xmm1, %xmm0
addss %xmm5, %xmm0
movss (%r15), %xmm2
subss %xmm0, %xmm2
movss 0x4(%r15), %xmm0
mulss 0x1c(%rsp), %xmm1
addss %xmm5, %xmm1
subss %xmm1, %xmm0
mulss %xmm0, %xmm0
mulss %xmm2, %xmm2
addss %xmm0, %xmm2
ucomiss %xmm2, %xmm4
jbe 0x3924
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
leaq (%rax,%rax,8), %rbx
shlq $0x2, %rbx
addq 0x13a8(%r14), %rbx
xorl %r12d, %r12d
movslq (%rbx,%r12,4), %rax
cmpq $-0x1, %rax
je 0x3924
cmpl %ebp, %eax
je 0x3917
movq 0x13b8(%r14), %rcx
movsd (%r15), %xmm2
movss (%rcx,%rax,8), %xmm1
movss 0x4(%rcx,%rax,8), %xmm0
subss %xmm2, %xmm1
shufps $0x55, %xmm2, %xmm2     # xmm2 = xmm2[1,1,1,1]
subss %xmm2, %xmm0
cmpb $0x1, 0x13d4(%r14)
jne 0x38a0
ucomiss %xmm1, %xmm5
jbe 0x386f
addss 0x6bfb(%rip), %xmm1     # 0xa468
jmp 0x3880
ucomiss 0x6bfa(%rip), %xmm1     # 0xa470
jbe 0x3880
addss 0x6bf4(%rip), %xmm1     # 0xa474
ucomiss %xmm0, %xmm5
jbe 0x388f
addss 0x6bdb(%rip), %xmm0     # 0xa468
jmp 0x38a0
ucomiss 0x6bda(%rip), %xmm0     # 0xa470
jbe 0x38a0
addss 0x6bd4(%rip), %xmm0     # 0xa474
movaps %xmm0, %xmm3
mulss %xmm0, %xmm3
movaps %xmm1, %xmm2
mulss %xmm1, %xmm2
addss %xmm3, %xmm2
ucomiss %xmm2, %xmm4
jbe 0x3917
sqrtss %xmm2, %xmm2
movss %xmm2, 0x24(%rsp)
callq 0x2230
movss %xmm0, 0xc(%rsp)
movss 0x24(%rsp), %xmm0
mulss 0x6ba6(%rip), %xmm0     # 0xa480
divss 0x13d0(%r14), %xmm0
callq 0x21d0
movss 0xc(%rsp), %xmm2
movaps %xmm2, %xmm1
subss %xmm0, %xmm1
addss %xmm0, %xmm2
movq 0x58(%rsp), %rdi
movaps %xmm1, %xmm0
movaps %xmm2, %xmm1
callq 0x7292
movss 0x6b5b(%rip), %xmm5     # 0xa46c
movss 0x20(%rsp), %xmm4
incq %r12
cmpq $0x9, %r12
jne 0x381d
leal 0x1(%r13), %eax
cmpl 0x8(%rsp), %r13d
movl %eax, %r13d
movq 0x60(%rsp), %rcx
jne 0x377a
movq 0x40(%rsp), %rdx
leal 0x1(%rdx), %ecx
movq 0x38(%rsp), %rax
cmpl 0x8(%rsp), %edx
movl %ecx, %edx
jne 0x3736
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x8b54(%rip), %rdi      # 0xc4be
callq 0x2290
movl $0x1, %edi
callq 0x2200
nop