// File: /workspace/github_cpp_makefile_star2_O3/cjlin1[P]liblinear/newton.cpp
// Function: NEWTON::newton(double*)
void NEWTON::newton(double *w)
{
	int n = fun_obj->get_nr_variable();
	int i, cg_iter;
	double step_size;
	double f, fold, actred;
	double init_step_size = 1;
	int search = 1, iter = 1, inc = 1;
	double *s = new double[n];
	double *r = new double[n];
	double *g = new double[n];

	const double alpha_pcg = 0.01;
	double *M = new double[n];

	// calculate gradient norm at w=0 for stopping condition.
	double *w0 = new double[n];
	for (i=0; i<n; i++)
		w0[i] = 0;
	fun_obj->fun(w0);
	fun_obj->grad(w0, g);
	double gnorm0 = dnrm2_(&n, g, &inc);
	delete [] w0;

	f = fun_obj->fun(w);
	fun_obj->grad(w, g);
	double gnorm = dnrm2_(&n, g, &inc);
	info("init f %5.3e |g| %5.3e\n", f, gnorm);

	if (gnorm <= eps*gnorm0)
		search = 0;

	while (iter <= max_iter && search)
	{
		fun_obj->get_diag_preconditioner(M);
		for(i=0; i<n; i++)
			M[i] = (1-alpha_pcg) + alpha_pcg*M[i];
		cg_iter = pcg(g, M, s, r);

		fold = f;
		step_size = fun_obj->linesearch_and_update(w, s, &f, g, init_step_size);

		if (step_size == 0)
		{
			info("WARNING: line search fails\n");
			break;
		}

		fun_obj->grad(w, g);
		gnorm = dnrm2_(&n, g, &inc);

		info("iter %2d f %5.3e |g| %5.3e CG %3d step_size %4.2e \n", iter, f, gnorm, cg_iter, step_size);
		
		if (gnorm <= eps*gnorm0)
			break;
		if (f < -1.0e+32)
		{
			info("WARNING: f < -1.0e+32\n");
			break;
		}
		actred = fold - f;
		if (fabs(actred) <= 1.0e-12*fabs(f))
		{
			info("WARNING: actred too small\n");
			break;
		}

		iter++;
	}

	if(iter >= max_iter)
		info("\nWARNING: reaching max number of Newton iterations\n");

	delete[] g;
	delete[] r;
	delete[] s;
	delete[] M;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rsi, 0x30(%rsp)
movq %rdi, %rbx
movq 0x18(%rdi), %rdi
movq (%rdi), %rax
callq *0x18(%rax)
movl %eax, %ebp
movl %eax, 0x8(%rsp)
movl $0x1, 0xc(%rsp)
movl %eax, %r15d
shlq $0x3, %r15
testl %eax, %eax
movq $-0x1, %r14
cmovnsq %r15, %r14
movq %r14, %rdi
callq 0x2030
movq %rax, 0x40(%rsp)
movq %r14, %rdi
callq 0x2030
movq %rax, 0x38(%rsp)
movq %r14, %rdi
callq 0x2030
movq %rax, %r13
movq %r14, %rdi
callq 0x2030
movq %rax, %r12
movq %r14, %rdi
callq 0x2030
movq %rax, %r14
testl %ebp, %ebp
jle 0x384a
movq %r14, %rdi
xorl %esi, %esi
movq %r15, %rdx
callq 0x20c0
movq 0x18(%rbx), %rdi
movq (%rdi), %rax
movq %r14, %rsi
callq *(%rax)
movq 0x18(%rbx), %rdi
movq (%rdi), %rax
movq %r14, %rsi
movq %r13, %rdx
callq *0x8(%rax)
leaq 0x8(%rsp), %rbp
leaq 0xc(%rsp), %r15
movq %rbp, %rdi
movq %r13, %rsi
movq %r15, %rdx
callq 0xceb4
movsd %xmm0, 0x28(%rsp)
movq %r14, %rdi
callq 0x21e0
movq 0x18(%rbx), %rdi
movq (%rdi), %rax
movq 0x30(%rsp), %r14
movq %r14, %rsi
callq *(%rax)
movsd %xmm0, 0x10(%rsp)
movsd %xmm0, 0x18(%rsp)
movq 0x18(%rbx), %rdi
movq (%rdi), %rax
movq %r14, %rsi
movq %r13, %rdx
callq *0x8(%rax)
movq %rbp, %rdi
movq %r13, %rsi
movq %r15, %rdx
callq 0xceb4
movapd %xmm0, %xmm1
movsd %xmm0, 0x20(%rsp)
leaq 0xb3b8(%rip), %rsi      # 0xec90
movq %rbx, %rdi
movsd 0x10(%rsp), %xmm0
movb $0x2, %al
callq 0x36e6
movsd (%rbx), %xmm0
mulsd 0x28(%rsp), %xmm0
ucomisd 0x20(%rsp), %xmm0
movl $0x1, %eax
jae 0x3ab4
cmpl $0x0, 0x10(%rbx)
jle 0x3ab4
movl $0x1, %ecx
movq %rcx, 0x10(%rsp)
movq 0x18(%rbx), %rdi
movq (%rdi), %rax
movq %r12, %rsi
callq *0x20(%rax)
movslq 0x8(%rsp), %rax
testq %rax, %rax
movsd 0xb2f4(%rip), %xmm1     # 0xec28
movsd 0xb2fc(%rip), %xmm2     # 0xec38
jle 0x395c
xorl %ecx, %ecx
movsd (%r12,%rcx,8), %xmm0
mulsd %xmm1, %xmm0
addsd %xmm2, %xmm0
movsd %xmm0, (%r12,%rcx,8)
incq %rcx
cmpq %rcx, %rax
jne 0x3940
movq %rbx, %rdi
movq %r13, %rsi
movq %r12, %rdx
movq 0x40(%rsp), %rbp
movq %rbp, %rcx
movq 0x38(%rsp), %r8
callq 0x3afe
movl %eax, %r14d
movsd 0x18(%rsp), %xmm0
movaps %xmm0, 0x50(%rsp)
movq 0x18(%rbx), %rdi
movq (%rdi), %rax
movq %r13, %r15
movq 0x30(%rsp), %r13
movq %r13, %rsi
movq %rbp, %rdx
leaq 0x18(%rsp), %rcx
movq %r15, %r8
movsd 0xb296(%rip), %xmm0     # 0xec40
callq *0x38(%rax)
ucomisd 0xa683(%rip), %xmm0     # 0xe038
jne 0x39bd
jnp 0x3a7d
movq 0x18(%rbx), %rdi
movq (%rdi), %rax
movq %r13, %rsi
movq %r15, %rdx
movsd %xmm0, 0x20(%rsp)
callq *0x8(%rax)
leaq 0x8(%rsp), %rdi
movq %r15, %rsi
leaq 0xc(%rsp), %rdx
callq 0xceb4
movapd %xmm0, %xmm1
movsd %xmm0, 0x48(%rsp)
movsd 0x18(%rsp), %xmm0
movq %rbx, %rdi
leaq 0xb2c5(%rip), %rsi      # 0xecc4
movq 0x10(%rsp), %rbp
movl %ebp, %edx
movl %r14d, %ecx
movsd 0x20(%rsp), %xmm2
movb $0x3, %al
callq 0x36e6
movq %rbx, %rcx
movsd (%rbx), %xmm0
mulsd 0x28(%rsp), %xmm0
ucomisd 0x48(%rsp), %xmm0
movq %r15, %r13
jae 0x3a8e
movsd 0x18(%rsp), %xmm0
movsd 0xb20c(%rip), %xmm1     # 0xec48
ucomisd %xmm0, %xmm1
ja 0x3a95
movapd 0x50(%rsp), %xmm2
subsd %xmm0, %xmm2
movapd 0xb21c(%rip), %xmm1     # 0xec70
andpd %xmm1, %xmm2
andpd %xmm1, %xmm0
mulsd 0xb1ec(%rip), %xmm0     # 0xec50
ucomisd %xmm2, %xmm0
movq %rcx, %rbx
jae 0x3aa1
leal 0x1(%rbp), %eax
cmpl 0x10(%rbx), %ebp
movl %eax, %ecx
jl 0x3912
jmp 0x3ab4
movq %r15, %r13
movq 0x10(%rsp), %rbp
leaq 0xb21c(%rip), %rsi      # 0xeca8
jmp 0x3aa8
movl %ebp, %eax
movq %rcx, %rbx
jmp 0x3ab4
leaq 0xb25c(%rip), %rsi      # 0xecf8
movq %rcx, %rbx
jmp 0x3aa8
leaq 0xb267(%rip), %rsi      # 0xed0f
movq %rbx, %rdi
xorl %eax, %eax
callq 0x36e6
movl %ebp, %eax
cmpl 0x10(%rbx), %eax
jl 0x3aca
leaq 0xb26a(%rip), %rsi      # 0xed2a
movq %rbx, %rdi
xorl %eax, %eax
callq 0x36e6
movq %r13, %rdi
callq 0x21e0
movq 0x38(%rsp), %rdi
callq 0x21e0
movq 0x40(%rsp), %rdi
callq 0x21e0
movq %r12, %rdi
callq 0x21e0
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop