// File: /workspace/github_cpp_makefile_star2_O1/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 0x3a48
movl %esi, %ebp
movq %rdi, %r14
movslq %esi, %rbx
movq 0x13b8(%rdi), %r12
leaq (%r12,%rbx,8), %r15
movss 0x13d0(%rdi), %xmm1
movss 0x6d96(%rip), %xmm0     # 0xa48c
movss %xmm1, 0x4(%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 0x31e4
movl %r13d, 0x8(%rsp)
testl %r13d, %r13d
js 0x3a39
movss 0x6d3b(%rip), %xmm5     # 0xa488
movss 0x4(%rsp), %xmm0
mulss %xmm0, %xmm5
mulss %xmm0, %xmm5
movl 0x2c(%rsp), %ecx
movss 0x13b4(%r14), %xmm0
xorps %xmm1, %xmm1
cvtss2sd %xmm0, %xmm1
mulsd 0x68d9(%rip), %xmm1     # 0xa050
movl 0x28(%rsp), %edx
movsd (%r12,%rbx,8), %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 0x6d08(%rip), %xmm0     # 0xa4b0
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 0x6c9c(%rip), %xmm3     # 0xa47c
movl %ecx, 0x10(%rsp)
movl %ecx, %edx
movss %xmm5, 0x24(%rsp)
movl 0x14(%rsp), %ecx
testl %edx, %edx
je 0x37ff
cmpl $0x1, %edx
jne 0x37fd
xorl %ecx, %ecx
jmp 0x37ff
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 0x3847
cmpl $0x1, %r13d
jne 0x3845
xorl %ecx, %ecx
jmp 0x3847
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 %xmm3, %xmm0
movss (%r15), %xmm2
subss %xmm0, %xmm2
movss 0x4(%r15), %xmm0
mulss 0x1c(%rsp), %xmm1
addss %xmm3, %xmm1
subss %xmm1, %xmm0
mulss %xmm0, %xmm0
mulss %xmm2, %xmm2
addss %xmm0, %xmm2
ucomiss %xmm2, %xmm5
jbe 0x3a09
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 0x3a09
cmpl %ebp, %eax
je 0x39fc
movq 0x13b8(%r14), %rcx
movsd (%r15), %xmm0
movss (%rcx,%rax,8), %xmm1
movss 0x4(%rcx,%rax,8), %xmm4
subss %xmm0, %xmm1
shufps $0x55, %xmm0, %xmm0     # xmm0 = xmm0[1,1,1,1]
subss %xmm0, %xmm4
cmpb $0x1, 0x13d4(%r14)
jne 0x3956
ucomiss %xmm1, %xmm3
jbe 0x3925
addss 0x6b55(%rip), %xmm1     # 0xa478
jmp 0x3936
ucomiss 0x6b54(%rip), %xmm1     # 0xa480
jbe 0x3936
addss 0x6b4e(%rip), %xmm1     # 0xa484
ucomiss %xmm4, %xmm3
jbe 0x3945
addss 0x6b35(%rip), %xmm4     # 0xa478
jmp 0x3956
ucomiss 0x6b34(%rip), %xmm4     # 0xa480
jbe 0x3956
addss 0x6b2e(%rip), %xmm4     # 0xa484
movaps %xmm4, %xmm2
mulss %xmm4, %xmm2
movaps %xmm1, %xmm0
mulss %xmm1, %xmm0
addss %xmm2, %xmm0
ucomiss %xmm0, %xmm5
jbe 0x39fc
ucomiss 0x6b1c(%rip), %xmm0     # 0xa494
jb 0x3980
sqrtss %xmm0, %xmm0
jmp 0x399d
movss %xmm1, 0xc(%rsp)
movss %xmm4, 0x20(%rsp)
callq 0x2320
movss 0x20(%rsp), %xmm4
movss 0xc(%rsp), %xmm1
movss %xmm0, 0x4(%rsp)
movaps %xmm4, %xmm0
callq 0x2230
movss %xmm0, 0xc(%rsp)
movss 0x4(%rsp), %xmm0
mulss 0x6ad1(%rip), %xmm0     # 0xa490
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 0x73d8
movss 0x24(%rsp), %xmm5
movss 0x6a80(%rip), %xmm3     # 0xa47c
incq %r12
cmpq $0x9, %r12
jne 0x38d3
leal 0x1(%r13), %eax
cmpl 0x8(%rsp), %r13d
movl %eax, %r13d
movq 0x60(%rsp), %rcx
jne 0x3830
movq 0x40(%rsp), %rdx
leal 0x1(%rdx), %ecx
movq 0x38(%rsp), %rax
cmpl 0x8(%rsp), %edx
movl %ecx, %edx
jne 0x37ec
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x8a7f(%rip), %rdi      # 0xc4ce
callq 0x2290
movl $0x1, %edi
callq 0x2200