// File: /workspace/github_cpp_makefile_star2_O2/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, 0x18(%rsp)
movq %rdi, %rbx
movq 0x18(%rdi), %rdi
movq (%rdi), %rax
callq *0x18(%rax)
movl %eax, %r15d
movl %eax, (%rsp)
movl $0x1, 0x4(%rsp)
movl %eax, %eax
shlq $0x3, %rax
xorl %r13d, %r13d
testl %r15d, %r15d
pushq $-0x1
popq %r14
cmovnsq %rax, %r14
cmovlel %r13d, %r15d
movq %r14, %rdi
callq 0x2030
movq %rax, 0x30(%rsp)
movq %r14, %rdi
callq 0x2030
movq %rax, 0x28(%rsp)
movq %r14, %rdi
callq 0x2030
movq %rax, %rbp
movq %r14, %rdi
callq 0x2030
movq %rax, %r12
movq %r14, %rdi
callq 0x2030
movq %rax, %r14
cmpq %r13, %r15
je 0x32a4
andq $0x0, (%r14,%r13,8)
incq %r13
jmp 0x3295
movq 0x18(%rbx), %rdi
movq (%rdi), %rax
movq %r14, %rsi
callq *(%rax)
movq 0x18(%rbx), %rdi
movq (%rdi), %rax
movq %r14, %rsi
movq %rbp, %rdx
callq *0x8(%rax)
movq %rsp, %r15
leaq 0x4(%rsp), %r13
movq %r15, %rdi
movq %rbp, %rsi
movq %r13, %rdx
callq 0xb518
movsd %xmm0, 0x20(%rsp)
movq %r14, %rdi
callq 0x21e0
movq 0x18(%rbx), %rdi
movq (%rdi), %rax
movq 0x18(%rsp), %r14
movq %r14, %rsi
callq *(%rax)
movsd %xmm0, 0x10(%rsp)
leaq 0x8(%rsp), %rax
movsd %xmm0, (%rax)
movq 0x18(%rbx), %rdi
movq (%rdi), %rax
movq %r14, %rsi
movq %rbp, %rdx
callq *0x8(%rax)
movq %r15, %rdi
movq %rbp, %rsi
movq %r13, %rdx
callq 0xb518
movapd %xmm0, %xmm1
leaq 0x9963(%rip), %rsi      # 0xcc90
movq %rbx, %rdi
movsd 0x10(%rsp), %xmm0
movsd %xmm1, 0x48(%rsp)
movb $0x2, %al
callq 0x313e
movsd 0x20(%rsp), %xmm0
mulsd (%rbx), %xmm0
movsd %xmm0, 0x40(%rsp)
pushq $0x1
popq %r15
movq %rbp, 0x38(%rsp)
movsd 0x40(%rsp), %xmm0
ucomisd 0x48(%rsp), %xmm0
jae 0x34ec
cmpl 0x10(%rbx), %r15d
jg 0x34ec
movq 0x18(%rbx), %rdi
movq (%rdi), %rax
movq %r12, %rsi
callq *0x20(%rax)
movsd 0x98ab(%rip), %xmm2     # 0xcc38
movl (%rsp), %eax
testl %eax, %eax
movl $0x0, %ecx
cmovlel %ecx, %eax
xorl %ecx, %ecx
movsd 0x9884(%rip), %xmm1     # 0xcc28
cmpq %rcx, %rax
je 0x33c2
movsd (%r12,%rcx,8), %xmm0
mulsd %xmm1, %xmm0
addsd %xmm2, %xmm0
movsd %xmm0, (%r12,%rcx,8)
incq %rcx
jmp 0x33a4
movq %rbx, %rdi
movq %rbp, %rsi
movq %r12, %rdx
movq 0x30(%rsp), %rbp
movq %rbp, %rcx
movq 0x28(%rsp), %r8
callq 0x3536
movl %eax, %r13d
movsd 0x8(%rsp), %xmm0
movaps %xmm0, 0x50(%rsp)
movq 0x18(%rbx), %rdi
movq (%rdi), %rax
movq %r14, %rsi
movq %rbp, %rdx
movq 0x38(%rsp), %rbp
leaq 0x8(%rsp), %rcx
movq %rbp, %r8
movsd 0x9833(%rip), %xmm0     # 0xcc40
callq *0x38(%rax)
ucomisd 0x8c20(%rip), %xmm0     # 0xc038
jne 0x3420
jnp 0x34c9
movq 0x18(%rbx), %rdi
movq (%rdi), %rax
movq %r14, %rsi
movq %rbp, %rdx
movsd %xmm0, 0x18(%rsp)
callq *0x8(%rax)
movq %rsp, %rdi
movq %rbp, %rsi
leaq 0x4(%rsp), %rdx
callq 0xb518
movapd %xmm0, %xmm1
movsd %xmm0, 0x10(%rsp)
movsd 0x8(%rsp), %xmm0
movq %rbx, %rdi
leaq 0x9864(%rip), %rsi      # 0xccc4
movl %r15d, %edx
movl %r13d, %ecx
movsd 0x18(%rsp), %xmm2
movb $0x3, %al
callq 0x313e
movsd (%rbx), %xmm0
mulsd 0x20(%rsp), %xmm0
ucomisd 0x10(%rsp), %xmm0
jae 0x34ec
movsd 0x8(%rsp), %xmm0
movsd 0x97b5(%rip), %xmm1     # 0xcc48
ucomisd %xmm0, %xmm1
ja 0x34d2
movapd 0x50(%rsp), %xmm2
subsd %xmm0, %xmm2
movapd 0x97c5(%rip), %xmm1     # 0xcc70
andpd %xmm1, %xmm2
andpd %xmm1, %xmm0
mulsd 0x9795(%rip), %xmm0     # 0xcc50
ucomisd %xmm2, %xmm0
jae 0x34db
incl %r15d
jmp 0x335c
leaq 0x97d8(%rip), %rsi      # 0xcca8
jmp 0x34e2
leaq 0x981f(%rip), %rsi      # 0xccf8
jmp 0x34e2
leaq 0x982d(%rip), %rsi      # 0xcd0f
movq %rbx, %rdi
xorl %eax, %eax
callq 0x313e
cmpl 0x10(%rbx), %r15d
jl 0x3503
leaq 0x9831(%rip), %rsi      # 0xcd2a
movq %rbx, %rdi
xorl %eax, %eax
callq 0x313e
movq %rbp, %rdi
callq 0x21e0
movq 0x28(%rsp), %rdi
callq 0x21e0
movq 0x30(%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