// File: FreeSlave[P]vhlt/hlbsp/surfaces.cpp
// Function: HashVec(double const*, int*, int*)
static int HashVec (const vec3_t vec, int *num_hashneighbors, int *hashneighbors)
	// returned value: the one bucket that a new vertex may "write" into
	// returned hashneighbors: the buckets that we should "read" to check for an existing vertex
{
	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 * POINT_EPSILON) ||
			x == 1 && slotdiff[0] < 1 - hash_scale[0] * (2 * POINT_EPSILON))
		{
			continue;
		}
		for (y = -1; y <= 1; y++)
		{
			if (y == -1 && slotdiff[1] > hash_scale[1] * (2 * POINT_EPSILON) ||
				y == 1 && slotdiff[1] < 1 - hash_scale[1] * (2 * POINT_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 0x3073a
movslq 0x48(%rsp), %rcx
leaq 0x36af45e(%rip), %rax   # 0x36dfab0
movsd (%rax,%rcx,8), %xmm0
movq 0x60(%rsp), %rax
movslq 0x48(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm1
movslq 0x48(%rsp), %rcx
leaq 0x36af45e(%rip), %rax   # 0x36dfad0
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 0x36af40b(%rip), %rcx   # 0x36dfae8
addl (%rcx,%rdx,4), %eax
movslq 0x48(%rsp), %rsi
leaq 0x36af3fc(%rip), %rcx   # 0x36dfae8
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 0x36af3da(%rip), %rcx   # 0x36dfae8
addl (%rcx,%rdx,4), %eax
movslq 0x48(%rsp), %rsi
leaq 0x36af3cb(%rip), %rcx   # 0x36dfae8
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 0x3063b
movl 0x38(%rsp), %eax
imull 0x36af3a7(%rip), %eax   # 0x36dfaec
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 0x308f2
cmpl $-0x1, 0x44(%rsp)
jne 0x3078e
movsd 0x10(%rsp), %xmm0
movsd 0x8e80(%rip), %xmm1     # 0x39600
mulsd 0x36af328(%rip), %xmm1  # 0x36dfab0
ucomisd %xmm1, %xmm0
ja 0x307d8
cmpl $0x1, 0x44(%rsp)
jne 0x307dd
movsd 0x10(%rsp), %xmm1
movsd 0x36af30d(%rip), %xmm0  # 0x36dfab0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
movsd 0x8e3e(%rip), %xmm2     # 0x39600
mulsd %xmm2, %xmm0
movsd 0x897a(%rip), %xmm2     # 0x39148
addsd %xmm2, %xmm0
ucomisd %xmm1, %xmm0
jbe 0x307dd
jmp 0x308e2
movl $0xffffffff, 0x40(%rsp) # imm = 0xFFFFFFFF
cmpl $0x1, 0x40(%rsp)
jg 0x308e0
cmpl $-0x1, 0x40(%rsp)
jne 0x30813
movsd 0x18(%rsp), %xmm0
movsd 0x8dfb(%rip), %xmm1     # 0x39600
mulsd 0x36af2ab(%rip), %xmm1  # 0x36dfab8
ucomisd %xmm1, %xmm0
ja 0x3085d
cmpl $0x1, 0x40(%rsp)
jne 0x3085f
movsd 0x18(%rsp), %xmm1
movsd 0x36af290(%rip), %xmm0  # 0x36dfab8
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
movsd 0x8db9(%rip), %xmm2     # 0x39600
mulsd %xmm2, %xmm0
movsd 0x88f5(%rip), %xmm2     # 0x39148
addsd %xmm2, %xmm0
ucomisd %xmm1, %xmm0
jbe 0x3085f
jmp 0x308d0
movq 0x58(%rsp), %rax
cmpl $0x4, (%rax)
jl 0x30877
leaq 0xb533(%rip), %rdi      # 0x3bda3
movb $0x0, %al
callq 0xb510
movl 0x38(%rsp), %eax
addl 0x44(%rsp), %eax
addl 0x36af263(%rip), %eax   # 0x36dfae8
cltd
idivl 0x36af25c(%rip)         # 0x36dfae8
imull 0x36af259(%rip), %edx   # 0x36dfaec
movl %edx, 0xc(%rsp)
movl 0x3c(%rsp), %eax
addl 0x40(%rsp), %eax
addl 0x36af247(%rip), %eax   # 0x36dfaec
cltd
idivl 0x36af240(%rip)         # 0x36dfaec
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 0x307e5
jmp 0x308e2
movl 0x44(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x44(%rsp)
jmp 0x30760
movl 0x4c(%rsp), %eax
addq $0x68, %rsp
retq
nopl (%rax,%rax)