// File: FreeSlave[P]vhlt/hlbsp/tjunc.cpp
// Function: HashVec(double const*, int*, int*)
static int HashVec (const vec3_t vec, int *num_hashneighbors, int *hashneighbors)
{
	int h;
	int i;
	int x;
	int y;
	int slot[2];
	vec_t normalized[2];
	vec_t slotdiff[2];

	for (i = 0; i < 2; i++)
	{
		normalized[i] = hash_scale[i] * (vec[i] - hash_min[i]);
		slot[i] = (int)floor (normalized[i]);
		slotdiff[i] = normalized[i] - (vec_t)slot[i];

		slot[i] = (slot[i] + hash_numslots[i]) % hash_numslots[i];
		slot[i] = (slot[i] + hash_numslots[i]) % hash_numslots[i]; // do it twice to handle negative values
	}

	h = slot[0] * hash_numslots[1] + slot[1];

	*num_hashneighbors = 0;
	for (x = -1; x <= 1; x++)
	{
		if (x == -1 && slotdiff[0] > hash_scale[0] * (2 * ON_EPSILON) ||
			x == 1 && slotdiff[0] < 1 - hash_scale[0] * (2 * ON_EPSILON))
		{
			continue;
		}
		for (y = -1; y <= 1; y++)
		{
			if (y == -1 && slotdiff[1] > hash_scale[1] * (2 * ON_EPSILON) ||
				y == 1 && slotdiff[1] < 1 - hash_scale[1] * (2 * ON_EPSILON))
			{
				continue;
			}
			if (*num_hashneighbors >= MAX_HASH_NEIGHBORS)
			{
				Error ("HashVec: internal error.");
			}
			hashneighbors[*num_hashneighbors] =
				((slot[0] + x + hash_numslots[0]) % hash_numslots[0]) * hash_numslots[1] +
				(slot[1] + y + hash_numslots[1]) % hash_numslots[1];
			(*num_hashneighbors)++;
		}
	}

	return h;
}

// Assembly:
subq $0x68, %rsp
movq %rdi, 0x60(%rsp)
movq %rsi, 0x58(%rsp)
movq %rdx, 0x50(%rsp)
movl $0x0, 0x48(%rsp)
cmpl $0x2, 0x48(%rsp)
jge 0x3177a
movslq 0x48(%rsp), %rcx
leaq 0x3a3647e(%rip), %rax   # 0x3a67b10
movsd (%rax,%rcx,8), %xmm0
movq 0x60(%rsp), %rax
movslq 0x48(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm1
movslq 0x48(%rsp), %rcx
leaq 0x3a3642e(%rip), %rax   # 0x3a67ae0
subsd (%rax,%rcx,8), %xmm1
mulsd %xmm1, %xmm0
movslq 0x48(%rsp), %rax
movsd %xmm0, 0x20(%rsp,%rax,8)
movslq 0x48(%rsp), %rax
movsd 0x20(%rsp,%rax,8), %xmm0
callq 0x4510
cvttsd2si %xmm0, %ecx
movslq 0x48(%rsp), %rax
movl %ecx, 0x38(%rsp,%rax,4)
movslq 0x48(%rsp), %rax
movsd 0x20(%rsp,%rax,8), %xmm0
movslq 0x48(%rsp), %rax
cvtsi2sdl 0x38(%rsp,%rax,4), %xmm1
subsd %xmm1, %xmm0
movslq 0x48(%rsp), %rax
movsd %xmm0, 0x10(%rsp,%rax,8)
movslq 0x48(%rsp), %rax
movl 0x38(%rsp,%rax,4), %eax
movslq 0x48(%rsp), %rdx
leaq 0x3a363db(%rip), %rcx   # 0x3a67af8
addl (%rcx,%rdx,4), %eax
movslq 0x48(%rsp), %rsi
leaq 0x3a363cc(%rip), %rcx   # 0x3a67af8
cltd
idivl (%rcx,%rsi,4)
movslq 0x48(%rsp), %rax
movl %edx, 0x38(%rsp,%rax,4)
movslq 0x48(%rsp), %rax
movl 0x38(%rsp,%rax,4), %eax
movslq 0x48(%rsp), %rdx
leaq 0x3a363aa(%rip), %rcx   # 0x3a67af8
addl (%rcx,%rdx,4), %eax
movslq 0x48(%rsp), %rsi
leaq 0x3a3639b(%rip), %rcx   # 0x3a67af8
cltd
idivl (%rcx,%rsi,4)
movslq 0x48(%rsp), %rax
movl %edx, 0x38(%rsp,%rax,4)
movl 0x48(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x48(%rsp)
jmp 0x3167b
movl 0x38(%rsp), %eax
imull 0x3a36377(%rip), %eax   # 0x3a67afc
addl 0x3c(%rsp), %eax
movl %eax, 0x4c(%rsp)
movq 0x58(%rsp), %rax
movl $0x0, (%rax)
movl $0xffffffff, 0x44(%rsp) # imm = 0xFFFFFFFF
cmpl $0x1, 0x44(%rsp)
jg 0x31932
cmpl $-0x1, 0x44(%rsp)
jne 0x317ce
movsd 0x10(%rsp), %xmm0
movsd 0x8e48(%rip), %xmm1     # 0x3a608
mulsd 0x3a36348(%rip), %xmm1  # 0x3a67b10
ucomisd %xmm1, %xmm0
ja 0x31818
cmpl $0x1, 0x44(%rsp)
jne 0x3181d
movsd 0x10(%rsp), %xmm1
movsd 0x3a3632d(%rip), %xmm0  # 0x3a67b10
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
movsd 0x8e06(%rip), %xmm2     # 0x3a608
mulsd %xmm2, %xmm0
movsd 0x793a(%rip), %xmm2     # 0x39148
addsd %xmm2, %xmm0
ucomisd %xmm1, %xmm0
jbe 0x3181d
jmp 0x31922
movl $0xffffffff, 0x40(%rsp) # imm = 0xFFFFFFFF
cmpl $0x1, 0x40(%rsp)
jg 0x31920
cmpl $-0x1, 0x40(%rsp)
jne 0x31853
movsd 0x18(%rsp), %xmm0
movsd 0x8dc3(%rip), %xmm1     # 0x3a608
mulsd 0x3a362cb(%rip), %xmm1  # 0x3a67b18
ucomisd %xmm1, %xmm0
ja 0x3189d
cmpl $0x1, 0x40(%rsp)
jne 0x3189f
movsd 0x18(%rsp), %xmm1
movsd 0x3a362b0(%rip), %xmm0  # 0x3a67b18
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
movsd 0x8d81(%rip), %xmm2     # 0x3a608
mulsd %xmm2, %xmm0
movsd 0x78b5(%rip), %xmm2     # 0x39148
addsd %xmm2, %xmm0
ucomisd %xmm1, %xmm0
jbe 0x3189f
jmp 0x31910
movq 0x58(%rsp), %rax
cmpl $0x4, (%rax)
jl 0x318b7
leaq 0xa4f3(%rip), %rdi      # 0x3bda3
movb $0x0, %al
callq 0xb510
movl 0x38(%rsp), %eax
addl 0x44(%rsp), %eax
addl 0x3a36233(%rip), %eax   # 0x3a67af8
cltd
idivl 0x3a3622c(%rip)         # 0x3a67af8
imull 0x3a36229(%rip), %edx   # 0x3a67afc
movl %edx, 0xc(%rsp)
movl 0x3c(%rsp), %eax
addl 0x40(%rsp), %eax
addl 0x3a36217(%rip), %eax   # 0x3a67afc
cltd
idivl 0x3a36210(%rip)         # 0x3a67afc
movl %edx, %eax
movl 0xc(%rsp), %edx
addl %eax, %edx
movq 0x50(%rsp), %rax
movq 0x58(%rsp), %rcx
movslq (%rcx), %rcx
movl %edx, (%rax,%rcx,4)
movq 0x58(%rsp), %rax
movl (%rax), %ecx
addl $0x1, %ecx
movl %ecx, (%rax)
movl 0x40(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x40(%rsp)
jmp 0x31825
jmp 0x31922
movl 0x44(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x44(%rsp)
jmp 0x317a0
movl 0x4c(%rsp), %eax
addq $0x68, %rsp
retq
nopl (%rax,%rax)