// File: /workspace/github_cpp_makefile_star2_O3/cjlin1[P]liblinear/newton.cpp
// Function: NEWTON::pcg(double*, double*, double*, double*)
int NEWTON::pcg(double *g, double *M, double *s, double *r)
{
	int i, inc = 1;
	int n = fun_obj->get_nr_variable();
	double one = 1;
	double *d = new double[n];
	double *Hd = new double[n];
	double zTr, znewTrnew, alpha, beta, cgtol, dHd;
	double *z = new double[n];
	double Q = 0, newQ, Qdiff;

	for (i=0; i<n; i++)
	{
		s[i] = 0;
		r[i] = -g[i];
		z[i] = r[i] / M[i];
		d[i] = z[i];
	}

	zTr = ddot_(&n, z, &inc, r, &inc);
	double gMinv_norm = sqrt(zTr);
	cgtol = min(eps_cg, sqrt(gMinv_norm));
	int cg_iter = 0;
	int max_cg_iter = max(n, 5);

	while (cg_iter < max_cg_iter)
	{
		cg_iter++;

		fun_obj->Hv(d, Hd);
		dHd = ddot_(&n, d, &inc, Hd, &inc);
		// avoid 0/0 in getting alpha
		if (dHd <= 1.0e-16)
			break;
		
		alpha = zTr/dHd;
		daxpy_(&n, &alpha, d, &inc, s, &inc);
		alpha = -alpha;
		daxpy_(&n, &alpha, Hd, &inc, r, &inc);

		// Using quadratic approximation as CG stopping criterion
		newQ = -0.5*(ddot_(&n, s, &inc, r, &inc) - ddot_(&n, s, &inc, g, &inc));
		Qdiff = newQ - Q;
		if (newQ <= 0 && Qdiff <= 0)
		{
			if (cg_iter * Qdiff >= cgtol * newQ)
				break;
		}
		else
		{
			info("WARNING: quadratic approximation > 0 or increasing in CG\n");
			break;
		}
		Q = newQ;

		for (i=0; i<n; i++)
			z[i] = r[i] / M[i];
		znewTrnew = ddot_(&n, z, &inc, r, &inc);
		beta = znewTrnew/zTr;
		dscal_(&n, &beta, d, &inc);
		daxpy_(&n, &one, z, &inc, d, &inc);
		zTr = znewTrnew;
	}

	if (cg_iter == max_cg_iter)
		info("WARNING: reaching maximal number of CG steps\n");

	delete[] d;
	delete[] Hd;
	delete[] z;

	return cg_iter;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %r8, %r13
movq %rcx, 0x58(%rsp)
movq %rdx, %rbx
movq %rsi, %r12
movl $0x1, 0x14(%rsp)
movq %rdi, 0x18(%rsp)
movq 0x18(%rdi), %rdi
movq (%rdi), %rax
callq *0x18(%rax)
movl %eax, %r14d
movl %eax, (%rsp)
movabsq $0x3ff0000000000000, %rcx # imm = 0x3FF0000000000000
movq %rcx, 0x70(%rsp)
movl %eax, %ebp
leaq (,%rbp,8), %rcx
testl %eax, %eax
movq $-0x1, %r15
cmovnsq %rcx, %r15
movq %r15, %rdi
callq 0x2030
movq %rax, 0x8(%rsp)
movq %r15, %rdi
callq 0x2030
movq %rax, 0x38(%rsp)
movq %r15, %rdi
movq 0x58(%rsp), %r15
callq 0x2030
movq %r12, %rcx
movq %rax, %r12
testl %r14d, %r14d
jle 0x3bd2
xorl %eax, %eax
movapd 0xb0e7(%rip), %xmm0     # 0xec80
movq 0x8(%rsp), %rdx
movq $0x0, (%r15,%rax,8)
movsd (%rcx,%rax,8), %xmm1
xorpd %xmm0, %xmm1
movlpd %xmm1, (%r13,%rax,8)
movsd (%rbx,%rax,8), %xmm2
divsd %xmm2, %xmm1
movsd %xmm1, (%r12,%rax,8)
movsd %xmm1, (%rdx,%rax,8)
incq %rax
cmpq %rax, %rbp
jne 0x3b9e
movq %rcx, 0x60(%rsp)
movq %rsp, %rdi
leaq 0x14(%rsp), %rdx
movq %r12, %rsi
movq %r13, %rcx
movq %rdx, %r8
callq 0xd0a4
xorpd %xmm2, %xmm2
ucomisd %xmm2, %xmm0
movsd %xmm0, 0x40(%rsp)
jb 0x3c03
sqrtsd %xmm0, %xmm0
jmp 0x3c0c
callq 0x20e0
xorpd %xmm2, %xmm2
movq 0x18(%rsp), %rax
movsd 0x8(%rax), %xmm1
movsd %xmm1, 0x28(%rsp)
ucomisd %xmm2, %xmm0
jb 0x3c28
sqrtsd %xmm0, %xmm0
jmp 0x3c2d
callq 0x20e0
movsd 0x28(%rsp), %xmm1
minsd %xmm0, %xmm1
movsd %xmm1, 0x28(%rsp)
movq %rsp, %r15
movl (%r15), %eax
cmpl $0x6, %eax
movl $0x5, %ecx
cmovgel %eax, %ecx
movl %ecx, 0x4(%rsp)
xorl %eax, %eax
xorpd %xmm0, %xmm0
movsd %xmm0, 0x30(%rsp)
incl %eax
movq %rax, 0x48(%rsp)
movq 0x18(%rsp), %rax
movq 0x18(%rax), %rdi
movq (%rdi), %rax
movq 0x8(%rsp), %rbp
movq %rbp, %rsi
movq 0x38(%rsp), %r14
movq %r14, %rdx
callq *0x10(%rax)
movq %r15, %rdi
movq %rbp, %rsi
leaq 0x14(%rsp), %rbp
movq %rbp, %rdx
movq %r14, %rcx
movq %rbp, %r8
callq 0xd0a4
movsd 0xafb3(%rip), %xmm1     # 0xec58
ucomisd %xmm0, %xmm1
movq 0x58(%rsp), %r15
jae 0x3e5b
movsd 0x40(%rsp), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, 0x20(%rsp)
movq %rsp, %r14
movq %r14, %rdi
leaq 0x20(%rsp), %rsi
movq 0x8(%rsp), %rdx
movq %rbp, %rcx
movq %r15, %r8
movq %rbp, %r9
callq 0xcf80
movsd 0x20(%rsp), %xmm0
xorpd 0xaf90(%rip), %xmm0     # 0xec80
movlpd %xmm0, 0x20(%rsp)
movq %r14, %rdi
leaq 0x20(%rsp), %rsi
movq 0x38(%rsp), %rdx
movq %rbp, %rcx
movq %r13, %r8
movq %rbp, %r9
callq 0xcf80
movq %r14, %rdi
movq %r15, %rsi
movq %rbp, %rdx
movq %r13, %rcx
movq %rbp, %r8
callq 0xd0a4
movsd %xmm0, 0x50(%rsp)
movq %r14, %rdi
movq %r15, %rsi
movq %rbp, %rdx
movq 0x60(%rsp), %rcx
movq %rbp, %r8
callq 0xd0a4
movsd 0x50(%rsp), %xmm2
subsd %xmm0, %xmm2
mulsd 0xaf0d(%rip), %xmm2     # 0xec60
xorpd %xmm1, %xmm1
ucomisd %xmm2, %xmm1
jb 0x3e48
movapd %xmm2, %xmm0
subsd 0x30(%rsp), %xmm0
ucomisd %xmm0, %xmm1
jb 0x3e48
xorps %xmm1, %xmm1
cvtsi2sdl 0x48(%rsp), %xmm1
mulsd %xmm1, %xmm0
movsd 0x28(%rsp), %xmm1
mulsd %xmm2, %xmm1
ucomisd %xmm1, %xmm0
jae 0x3e5b
movsd %xmm2, 0x50(%rsp)
movslq (%rsp), %rax
testq %rax, %rax
jle 0x3dc1
xorl %ecx, %ecx
movsd (%r13,%rcx,8), %xmm0
divsd (%rbx,%rcx,8), %xmm0
movsd %xmm0, (%r12,%rcx,8)
incq %rcx
cmpq %rcx, %rax
jne 0x3da7
movq %rsp, %r15
movq %r15, %rdi
movq %r12, %rsi
movq %rbp, %rdx
movq %r13, %rcx
movq %rbp, %r8
callq 0xd0a4
movsd %xmm0, 0x30(%rsp)
movapd %xmm0, %xmm1
divsd 0x40(%rsp), %xmm1
movsd %xmm1, 0x68(%rsp)
movq %r15, %rdi
leaq 0x68(%rsp), %rsi
movq 0x8(%rsp), %r14
movq %r14, %rdx
movq %rbp, %rcx
callq 0xd1ac
movq %r15, %rdi
leaq 0x70(%rsp), %rsi
movq %r12, %rdx
movq %rbp, %rcx
movq %r14, %r8
movq %rbp, %r9
callq 0xcf80
movsd 0x30(%rsp), %xmm0
movsd %xmm0, 0x40(%rsp)
movsd 0x50(%rsp), %xmm0
movq 0x48(%rsp), %rax
cmpl 0x4(%rsp), %eax
jne 0x3c58
movl 0x4(%rsp), %eax
movl %eax, %ebx
jmp 0x3e66
leaq 0xaf0f(%rip), %rsi      # 0xed5e
movq 0x18(%rsp), %rdi
xorl %eax, %eax
callq 0x36e6
movq 0x48(%rsp), %rbx
cmpl 0x4(%rsp), %ebx
jne 0x3e79
leaq 0xaf2b(%rip), %rsi      # 0xed98
movq 0x18(%rsp), %rdi
xorl %eax, %eax
callq 0x36e6
movq 0x8(%rsp), %rdi
callq 0x21e0
movq 0x38(%rsp), %rdi
callq 0x21e0
movq %r12, %rdi
callq 0x21e0
movl %ebx, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq