// File: /workspace/github_cpp_makefile_star2_O1/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 $0x78, %rsp
movq %rsi, 0x28(%rsp)
movq %rdi, %rbx
movq 0x18(%rdi), %rdi
movq (%rdi), %rax
callq *0x18(%rax)
movl %eax, %r14d
movl %eax, 0x4(%rsp)
movl $0x1, 0x14(%rsp)
movslq %eax, %rbp
leaq (,%rbp,8), %rax
testl %ebp, %ebp
movq $-0x1, %r15
cmovnsq %rax, %r15
movq %r15, %rdi
callq 0x2030
movq %rax, 0x38(%rsp)
movq %r15, %rdi
callq 0x2030
movq %rax, 0x30(%rsp)
movq %r15, %rdi
callq 0x2030
movq %rax, %r13
movq %r15, %rdi
callq 0x2030
movq %rax, %r12
movq %r15, %rdi
callq 0x2030
movq %rax, %r15
testl %ebp, %ebp
jle 0x38b9
movl %r14d, %edx
shlq $0x3, %rdx
movq %r15, %rdi
xorl %esi, %esi
callq 0x20c0
movq 0x18(%rbx), %rdi
movq (%rdi), %rax
movq %r15, %rsi
callq *(%rax)
movq 0x18(%rbx), %rdi
movq (%rdi), %rax
movq %r15, %rsi
movq %r13, %rdx
callq *0x8(%rax)
leaq 0x4(%rsp), %r14
leaq 0x14(%rsp), %rbp
movq %r14, %rdi
movq %r13, %rsi
movq %rbp, %rdx
callq 0xc330
movsd %xmm0, 0x20(%rsp)
movq %r15, %rdi
callq 0x21e0
movq 0x18(%rbx), %rdi
movq (%rdi), %rax
movq 0x28(%rsp), %r15
movq %r15, %rsi
callq *(%rax)
movsd %xmm0, 0x8(%rsp)
movq 0x18(%rbx), %rdi
movq (%rdi), %rax
movq %r15, %rsi
movq %r13, %rdx
callq *0x8(%rax)
movq %r14, %rdi
movq %r13, %rsi
movq %rbp, %rdx
callq 0xc330
movapd %xmm0, %xmm1
movsd %xmm0, 0x18(%rsp)
movsd 0x8(%rsp), %xmm0
leaq 0xa319(%rip), %rsi      # 0xdc60
movq %rbx, %rdi
movb $0x2, %al
callq 0x374c
movsd (%rbx), %xmm0
mulsd 0x20(%rsp), %xmm0
movsd %xmm0, 0x48(%rsp)
ucomisd 0x18(%rsp), %xmm0
movl $0x1, %eax
jae 0x3b26
cmpl $0x0, 0x10(%rbx)
jle 0x3b26
movl $0x1, %ecx
movq 0x28(%rsp), %rbp
movq %rcx, 0x40(%rsp)
movq 0x18(%rbx), %rdi
movq (%rdi), %rax
movq %r12, %rsi
callq *0x20(%rax)
movslq 0x4(%rsp), %rax
testq %rax, %rax
movsd 0xa258(%rip), %xmm1     # 0xdc00
movsd 0xa260(%rip), %xmm2     # 0xdc10
jle 0x39d0
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 0x39b4
movq %rbx, %rdi
movq %r13, %rsi
movq %r12, %rdx
movq %r13, %r15
movq 0x38(%rsp), %r13
movq %r13, %rcx
movq 0x30(%rsp), %r8
callq 0x3b70
movl %eax, %r14d
movsd 0x8(%rsp), %xmm0
movaps %xmm0, 0x60(%rsp)
movq 0x18(%rbx), %rdi
movq (%rdi), %rax
movq %rbp, %rsi
movq %r13, %rdx
movq %r15, %r13
leaq 0x8(%rsp), %rcx
movq %r15, %r8
movsd 0xa1fc(%rip), %xmm0     # 0xdc18
callq *0x38(%rax)
ucomisd 0x95e9(%rip), %xmm0     # 0xd010
jne 0x3a2f
jnp 0x3afb
movq 0x18(%rbx), %rdi
movq (%rdi), %rax
movq %rbp, %rsi
movq %r13, %rdx
movsd %xmm0, 0x58(%rsp)
callq *0x8(%rax)
leaq 0x4(%rsp), %rdi
movq %r13, %rsi
leaq 0x14(%rsp), %rdx
callq 0xc330
movapd %xmm0, %xmm1
movsd %xmm0, 0x50(%rsp)
movsd 0x8(%rsp), %xmm0
movq %rbx, %rdi
leaq 0xa223(%rip), %rsi      # 0xdc94
movq 0x40(%rsp), %r15
movl %r15d, %edx
movl %r14d, %ecx
movsd 0x58(%rsp), %xmm2
movb $0x3, %al
callq 0x374c
movsd (%rbx), %xmm0
mulsd 0x20(%rsp), %xmm0
ucomisd 0x50(%rsp), %xmm0
jae 0x3b23
movsd 0x8(%rsp), %xmm0
movsd 0xa173(%rip), %xmm1     # 0xdc20
ucomisd %xmm0, %xmm1
ja 0x3b09
movapd 0x60(%rsp), %xmm2
subsd %xmm0, %xmm2
movapd 0xa17b(%rip), %xmm1     # 0xdc40
andpd %xmm1, %xmm2
andpd %xmm1, %xmm0
mulsd 0xa153(%rip), %xmm0     # 0xdc28
ucomisd %xmm2, %xmm0
jae 0x3b12
leal 0x1(%r15), %eax
movsd 0x48(%rsp), %xmm0
ucomisd 0x18(%rsp), %xmm0
jae 0x3b26
cmpl 0x10(%rbx), %r15d
movl %eax, %ecx
jl 0x3986
jmp 0x3b26
movq 0x40(%rsp), %r15
leaq 0xa171(%rip), %rsi      # 0xdc78
jmp 0x3b19
leaq 0xa1b8(%rip), %rsi      # 0xdcc8
jmp 0x3b19
leaq 0xa1c6(%rip), %rsi      # 0xdcdf
movq %rbx, %rdi
xorl %eax, %eax
callq 0x374c
movl %r15d, %eax
cmpl 0x10(%rbx), %eax
jl 0x3b3c
leaq 0xa1c8(%rip), %rsi      # 0xdcfa
movq %rbx, %rdi
xorl %eax, %eax
callq 0x374c
movq %r13, %rdi
callq 0x21e0
movq 0x30(%rsp), %rdi
callq 0x21e0
movq 0x38(%rsp), %rdi
callq 0x21e0
movq %r12, %rdi
callq 0x21e0
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop