// File: /workspace/github_cpp_makefile_star2_O1/FreeSlave[P]vhlt/hlrad/loadtextures.cpp
// Function: CQ_SelectPartition(cq_node_s*)
static void CQ_SelectPartition (cq_node_t *node)
{
	CQ_VectorClear (node->centerofpoints);
	for (int i = 0; i < node->numpoints; i++)
	{
		CQ_VectorAdd (node->centerofpoints, node->refpoints[i], node->centerofpoints);
	}
	CQ_VectorScale (node->centerofpoints, 1 / (double)node->numpoints, node->centerofpoints);

	node->needsplit = false;
	for (int k = 0; k < CQ_DIM; k++)
	{
		double count;
		double counts[256];
		double sum[CQ_DIM];
		double sums[256][CQ_DIM];

		double bucketsums[256][CQ_DIM];
		int bucketsizes[256];

		const unsigned char (*nodepoints)[CQ_DIM] = node->refpoints;
		const int nodenumpoints = node->numpoints;

		memset (bucketsums, 0, 256 * sizeof (double [CQ_DIM]));
		memset (bucketsizes, 0, 256 * sizeof (int));
		for (int i = 0; i < nodenumpoints; i++)
		{
			int j = nodepoints[i][k];
			bucketsizes[j]++;
			CQ_VectorAdd (bucketsums[j], nodepoints[i], bucketsums[j]);
		}
		
		int min = 256;
		int max = -1;
		count = 0;
		CQ_VectorClear (sum);
		for (int j = 0; j < 256; j++)
		{
			counts[j] = count;
			CQ_VectorCopy (sum, sums[j]);
			count += bucketsizes[j];
			CQ_VectorAdd (sum, bucketsums[j], sum);
			if (bucketsizes[j] > 0)
			{
				if (j < min)
				{
					min = j;
				}
				if (j > max)
				{
					max = j;
				}
			}
		}
		if (max < min)
		{
			Error ("CQ_SelectPartition: internal error");
		}
		// sweep along the axis and find the plane that maximize square error reduction
		for (int j = min + 1; j < max + 1; j++)
		{
			double priority = 0; // the decrease in total square deviation
			priority -= CQ_DotProduct (sum, sum) / count;
			priority += CQ_DotProduct (sums[j], sums[j]) / counts[j];
			double remain[CQ_DIM];
			CQ_VectorSubtract (sum, sums[j], remain); // sums and counts are precise (have no round-off error)
			priority += CQ_DotProduct (remain, remain) / (count - counts[j]);
			if (node->needsplit == false ||
				priority > node->splitpriority + 0.1 ||
				priority >= node->splitpriority - 0.1
				&& fabs (counts[j] - count / 2) < fabs (node->bestsplitter.numpoints[0] - count / 2))
			{
				node->needsplit = true;
				node->splitpriority = priority;
				node->bestsplitter.axis = k;
				node->bestsplitter.dist = j;
				node->bestsplitter.numpoints[0] = counts[j];
				node->bestsplitter.numpoints[1] = count - counts[j];
			}
		}
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x3c48, %rsp           # imm = 0x3C48
movq %rdi, %rbx
leaq 0x30(%rdi), %rax
xorpd %xmm0, %xmm0
movupd %xmm0, 0x30(%rdi)
movq $0x0, 0x40(%rdi)
movslq 0x20(%rdi), %rcx
testq %rcx, %rcx
jle 0x25285
movq 0x28(%rbx), %rdx
xorl %esi, %esi
xorl %edi, %edi
movzbl (%rdx,%rdi), %r8d
xorps %xmm0, %xmm0
cvtsi2sd %r8d, %xmm0
addsd (%rax,%rdi,8), %xmm0
movsd %xmm0, (%rax,%rdi,8)
incq %rdi
cmpq $0x3, %rdi
jne 0x25259
incq %rsi
addq $0x3, %rdx
cmpq %rcx, %rsi
jne 0x25257
cvtsi2sd %ecx, %xmm1
movsd 0x15ed7(%rip), %xmm0    # 0x3b168
divsd %xmm1, %xmm0
xorl %ecx, %ecx
movsd (%rax,%rcx,8), %xmm1
mulsd %xmm0, %xmm1
movsd %xmm1, (%rax,%rcx,8)
incq %rcx
cmpq $0x3, %rcx
jne 0x25297
movb $0x0, 0x48(%rbx)
leaq 0x2440(%rsp), %r14
leaq 0x40(%rsp), %r15
xorl %ebp, %ebp
movq 0x28(%rbx), %r12
movslq 0x20(%rbx), %r13
movl $0x1800, %edx           # imm = 0x1800
movq %r14, %rdi
xorl %esi, %esi
callq 0x4190
movl $0x400, %edx            # imm = 0x400
movq %r15, %rdi
xorl %esi, %esi
callq 0x4190
testq %r13, %r13
jle 0x2533c
movq %r12, %rax
xorl %ecx, %ecx
leaq (%rcx,%rcx,2), %rdx
addq %r12, %rdx
movzbl (%rbp,%rdx), %edx
incl 0x40(%rsp,%rdx,4)
leaq (%rdx,%rdx,2), %rdx
leaq (%rsp,%rdx,8), %rdx
addq $0x2440, %rdx           # imm = 0x2440
xorl %esi, %esi
movzbl (%rax,%rsi), %edi
xorps %xmm0, %xmm0
cvtsi2sd %edi, %xmm0
addsd (%rdx,%rsi,8), %xmm0
movsd %xmm0, (%rdx,%rsi,8)
incq %rsi
cmpq $0x3, %rsi
jne 0x25312
incq %rcx
addq $0x3, %rax
cmpq %r13, %rcx
jne 0x252f1
xorpd %xmm0, %xmm0
movapd %xmm0, (%rsp)
movq $0x0, 0x10(%rsp)
xorpd %xmm8, %xmm8
movl $0x100, %r12d           # imm = 0x100
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
movq %r14, %rax
xorl %ecx, %ecx
movsd 0x162ec(%rip), %xmm6    # 0x3b658
movsd 0x172ec(%rip), %xmm7    # 0x3c660
leaq (%rcx,%rcx,2), %rdx
movsd %xmm8, 0x440(%rsp,%rcx,8)
movq 0x10(%rsp), %rsi
movq %rsi, 0xc50(%rsp,%rdx,8)
movaps (%rsp), %xmm0
movups %xmm0, 0xc40(%rsp,%rdx,8)
movl 0x40(%rsp,%rcx,4), %edx
xorps %xmm0, %xmm0
cvtsi2sd %edx, %xmm0
xorl %esi, %esi
movsd (%rsp,%rsi,8), %xmm1
addsd (%rax,%rsi,8), %xmm1
movsd %xmm1, (%rsp,%rsi,8)
incq %rsi
cmpq $0x3, %rsi
jne 0x253a8
cmpl %r12d, %ecx
movl %r12d, %esi
cmovll %ecx, %esi
cmpl %r13d, %ecx
movl %r13d, %edi
cmovgl %ecx, %edi
testl %edx, %edx
cmovgl %esi, %r12d
cmovgl %edi, %r13d
addsd %xmm0, %xmm8
incq %rcx
addq $0x18, %rax
cmpq $0x100, %rcx            # imm = 0x100
jne 0x25374
cmpl %r12d, %r13d
jge 0x25422
leaq 0x1778a(%rip), %rdi     # 0x3cb87
xorl %eax, %eax
movsd %xmm8, 0x18(%rsp)
callq 0xa01c
movsd 0x18(%rsp), %xmm8
movsd 0x17246(%rip), %xmm7    # 0x3c660
movsd 0x16236(%rip), %xmm6    # 0x3b658
cmpl %r13d, %r12d
jge 0x25567
movapd %xmm8, %xmm0
mulsd 0x15ec8(%rip), %xmm0    # 0x3b300
movb 0x48(%rbx), %sil
movslq %r12d, %rax
movslq %r13d, %rcx
unpcklpd %xmm0, %xmm0            # xmm0 = xmm0[0,0]
leaq (%rax,%rax,2), %rdx
leaq 0xc58(%rsp), %rdi
leaq (%rdi,%rdx,8), %rdx
xorpd %xmm2, %xmm2
xorl %edi, %edi
movsd (%rsp,%rdi,8), %xmm1
mulsd %xmm1, %xmm1
addsd %xmm1, %xmm2
incq %rdi
cmpq $0x3, %rdi
jne 0x2545c
incq %rax
divsd %xmm8, %xmm2
xorpd %xmm3, %xmm3
xorl %edi, %edi
movsd (%rdx,%rdi,8), %xmm1
mulsd %xmm1, %xmm1
addsd %xmm1, %xmm3
incq %rdi
cmpq $0x3, %rdi
jne 0x25480
movsd 0x440(%rsp,%rax,8), %xmm1
divsd %xmm1, %xmm3
xorl %edi, %edi
movsd (%rsp,%rdi,8), %xmm4
subsd (%rdx,%rdi,8), %xmm4
movsd %xmm4, 0x20(%rsp,%rdi,8)
incq %rdi
cmpq $0x3, %rdi
jne 0x254a5
xorpd %xmm4, %xmm4
xorpd %xmm5, %xmm5
subsd %xmm2, %xmm5
xorl %edi, %edi
movsd 0x20(%rsp,%rdi,8), %xmm2
mulsd %xmm2, %xmm2
addsd %xmm2, %xmm4
incq %rdi
cmpq $0x3, %rdi
jne 0x254cc
addsd %xmm3, %xmm5
movapd %xmm8, %xmm2
subsd %xmm1, %xmm2
divsd %xmm2, %xmm4
addsd %xmm5, %xmm4
testb %sil, %sil
je 0x25510
movsd 0x68(%rbx), %xmm3
movapd %xmm3, %xmm5
addsd %xmm6, %xmm5
ucomisd %xmm5, %xmm4
jbe 0x2553b
movb $0x1, 0x48(%rbx)
movsd %xmm4, 0x68(%rbx)
movl %ebp, 0x50(%rbx)
movl %eax, 0x54(%rbx)
movsd %xmm1, 0x58(%rbx)
movsd %xmm2, 0x60(%rbx)
movb $0x1, %sil
addq $0x18, %rdx
cmpq %rcx, %rax
jne 0x25456
jmp 0x25567
addsd %xmm7, %xmm3
ucomisd %xmm3, %xmm4
jb 0x2552c
movapd %xmm1, %xmm3
movhpd 0x58(%rbx), %xmm3       # xmm3 = xmm3[0],mem[0]
subpd %xmm0, %xmm3
andpd 0x161f6(%rip), %xmm3    # 0x3b750
pshufd $0xee, %xmm3, %xmm5     # xmm5 = xmm3[2,3,2,3]
ucomisd %xmm3, %xmm5
ja 0x25510
jmp 0x2552c
incq %rbp
cmpq $0x3, %rbp
jne 0x252c1
addq $0x3c48, %rsp           # imm = 0x3C48
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq