// File: /workspace/github_cpp_makefile_star2_O2/gvaumour[P]cache-simulator/src/Perceptron.cc
// Function: PerceptronPredictor::allocateInNVM(unsigned long, Access)
allocDecision
PerceptronPredictor::allocateInNVM(uint64_t set, Access element)
{
//	if(element.isInstFetch())
//		return ALLOCATE_IN_NVM;
	
	update_globalPChistory(element.m_pc);
	stats_nbMiss++;
	miss_counter++;
	if(miss_counter > 255)
		miss_counter = 255;
	
	// All the set is a learning/sampled set independantly of its way or SRAM/NVM alloc
	bool isLearning = m_tableSRAM[set][0]->isLearning; 
	
	if(!isLearning && m_enableBP)
	{
		//Predict if we should bypass
		int sum_prediction = 0;
		vector<double> samples;
		for(unsigned i = 0 ; i < m_BP_features.size(); i++)
		{
			int hash = m_BP_features_hash[i](element.m_address , element.m_pc);
			int pred = m_BP_features[i]->getConfidence(hash,true);
			samples.push_back(pred);
			sum_prediction += pred;
		}

		if(simu_parameters.perceptron_compute_variance)
		{	//Compute Variance of the sample
			double variance = 0, avg = (double)sum_prediction / (double) samples.size();
			for(unsigned i = 0 ; i < samples.size(); i++)
				variance += (samples[i] - avg) * (samples[i] - avg);

			variance = sqrt( variance / (double) samples.size());
			stats_variances_buffer.push_back(variance);
		}		
		
		if(sum_prediction > simu_parameters.perceptron_bypass_threshold)
		{
			stats_nbBPrequests[stats_nbBPrequests.size()-1]++;
			return BYPASS_CACHE;
		}
	}

	if(m_enableAllocation)
	{
		//Bypass decision is done, we do not bypass
		//Decide NVM/SRAM allocation
		int sum_prediction = 0;
		for(unsigned i = 0 ; i < m_Allocation_features.size(); i++)
		{
			int hash = m_Allocation_features_hash[i](element.m_address , element.m_pc);
			int pred = m_Allocation_features[i]->getConfidence(hash, true);
			sum_prediction += pred;
		}	
	
		if(sum_prediction > simu_parameters.perceptron_allocation_threshold)
			return ALLOCATE_IN_NVM;
		else
			return ALLOCATE_IN_SRAM;
	}
	else 
		return ALLOCATE_IN_SRAM;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rsi, %r15
movq %rdi, %rbx
leaq 0x60(%rsp), %r12
movq 0x10(%r12), %r14
movq %r14, %rsi
callq 0x1d9e2
incq 0x398(%rbx)
leaq 0x209f2(%rip), %rax     # 0x3e1a0
movl (%rax), %ecx
movl $0xfe, %edx
cmpl %edx, %ecx
cmovll %ecx, %edx
incl %edx
movl %edx, (%rax)
movq 0x8(%rbx), %rax
movq (%rax), %rax
imulq $0x18, %r15, %rcx
movq (%rax,%rcx), %rax
movq (%rax), %rax
cmpb $0x0, 0x148(%rax)
jne 0x1d946
cmpb $0x1, 0x348(%rbx)
jne 0x1d946
xorpd %xmm0, %xmm0
leaq 0x10(%rsp), %rax
movapd %xmm0, (%rax)
andq $0x0, 0x10(%rax)
movq (%r12), %r12
xorl %r13d, %r13d
xorl %r15d, %r15d
movl %r13d, %r13d
movq 0x2c0(%rbx), %rax
subq 0x2b8(%rbx), %rax
sarq $0x3, %rax
cmpq %r13, %rax
jbe 0x1d869
movq 0x2d0(%rbx), %rax
movq %r12, %rdi
movq %r14, %rsi
callq *(%rax,%r13,8)
movq 0x2b8(%rbx), %rcx
movq (%rcx,%r13,8), %rdi
movl %eax, %esi
pushq $0x1
popq %rdx
callq 0x11132
movl %eax, %ebp
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x8(%rsp)
leaq 0x10(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0xf960
addl %ebp, %r15d
incl %r13d
jmp 0x1d806
leaq 0x206e0(%rip), %r12     # 0x3df50
cmpb $0x0, 0x1cd(%r12)
je 0x1d910
cvtsi2sd %r15d, %xmm1
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
movq %rcx, %xmm0
punpckldq 0xce5e(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0xce66(%rip), %xmm0     # 0x2a710
movapd %xmm0, %xmm2
unpckhpd %xmm0, %xmm2            # xmm2 = xmm2[1],xmm0[1]
addsd %xmm0, %xmm2
divsd %xmm2, %xmm1
xorl %edx, %edx
xorpd %xmm0, %xmm0
movl %edx, %edx
cmpq %rdx, %rcx
jbe 0x1d8e2
movsd (%rax,%rdx,8), %xmm3
subsd %xmm1, %xmm3
mulsd %xmm3, %xmm3
addsd %xmm3, %xmm0
movsd %xmm0, 0x8(%rsp)
incl %edx
jmp 0x1d8c0
divsd %xmm2, %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x1d8f6
sqrtsd %xmm0, %xmm0
jmp 0x1d8fb
callq 0x5260
leaq 0x8(%rsp), %rsi
movsd %xmm0, (%rsi)
leaq 0x3a8(%rbx), %rdi
callq 0x9fa2
cmpl 0x1b8(%r12), %r15d
jle 0x1d937
movq 0x358(%rbx), %rax
incq -0x8(%rax)
leaq 0x10(%rsp), %rdi
callq 0xa7d8
pushq $0x2
popq %rax
jmp 0x1d9b8
leaq 0x10(%rsp), %rdi
callq 0xa7d8
leaq 0x60(%rsp), %r12
xorl %eax, %eax
cmpb $0x1, 0x349(%rbx)
jne 0x1d9b8
movq (%r12), %r15
xorl %r12d, %r12d
pushq $0x1
popq %rbp
xorl %eax, %eax
movl %eax, %r13d
movq 0x278(%rbx), %rax
subq 0x270(%rbx), %rax
sarq $0x3, %rax
cmpq %r13, %rax
jbe 0x1d9a5
movq 0x288(%rbx), %rax
movq %r15, %rdi
movq %r14, %rsi
callq *(%rax,%r13,8)
movq 0x270(%rbx), %rcx
movq (%rcx,%r13,8), %rdi
movl %eax, %esi
movl %ebp, %edx
callq 0x11132
addl %eax, %r12d
leal 0x1(%r13), %eax
jmp 0x1d95d
leaq 0x205a4(%rip), %rcx     # 0x3df50
xorl %eax, %eax
cmpl 0x1c0(%rcx), %r12d
setg %al
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x1d9cd
jmp 0x1d9cd
jmp 0x1d9cd
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0xa7d8
movq %rbx, %rdi
callq 0x5570