// File: /workspace/github_cpp_makefile_star2_O1/gvaumour[P]cache-simulator/src/DBAMBPredictor.cc
// Function: DBAMBPredictor::determineStatus(DHPEntry*)
void 
DBAMBPredictor::determineStatus(DHPEntry* entry)
{
	if(entry->des == BYPASS_CACHE)
	{
		//All the info gathered here are from learning cache line
		//If we see any any reuse we learn again
		/*if(entry->rw_history.size() != 0)
		{
			entry->des = ALLOCATE_PREEMPTIVELY;
			entry->state_rd = RD_NOT_ACCURATE;
			entry->state_rw = RW_NOT_ACCURATE;
			entry->dead_counter = 0;
		}*/
		return;		
	}


	/* Count the number of read and write during window */
	vector<int> cpts_rw = vector<int>(2,0);
	for(auto isWrite : entry->rw_history)
	{
		if(isWrite)
			cpts_rw[INDEX_WRITE]++;
		else
			cpts_rw[INDEX_READ]++;
	}

	if(cpts_rw[INDEX_READ] == 0 && cpts_rw[INDEX_WRITE] > 0)
		entry->state_rw = WO;
	else if(cpts_rw[INDEX_READ] > 0 && cpts_rw[INDEX_WRITE] == 0)
		entry->state_rw = RO;
	else if(cpts_rw[INDEX_READ] > 0 && cpts_rw[INDEX_WRITE] > 0)
		entry->state_rw = RW;
	else
		entry->state_rw = DEAD;
		
	if(entry->state_rw == DEAD)
	{
		entry->state_rd = RD_NOT_ACCURATE;
		return;	
	}

	
	/* Determination of the rd type */
//	int window_rd = entry->rd_history.size();
//	vector<int> cpts_rd = vector<int>(NUM_RD_TYPE,0);
//	
//	for(auto rd : entry->rd_history)
//		cpts_rd[rd]++;
//		
//	int max = 0, index_max = -1;
//	for(unsigned i = 0 ; i < cpts_rd.size() ; i++)
//	{
//		if(cpts_rd[i] > max)
//		{
//			max = cpts_rd[i];
//			index_max = i;
//		}
//	}

	double costSRAM = 0;
	double costNVM =0;
	for(unsigned i = 0 ; i < entry->rd_history.size() ; i++)
	{
		bool isWrite = entry->rw_history[i];
		if(entry->rd_history[i] == RD_NOT_ACCURATE)
		{			
				costSRAM += m_costParameters.costDRAM[isWrite] + m_costParameters.costSRAM[isWrite]; 
				costNVM += m_costParameters.costDRAM[isWrite] + m_costParameters.costNVM[isWrite];		
		}
		else if(entry->rd_history[i] == RD_SHORT)
		{
			costSRAM += m_costParameters.costSRAM[isWrite]; 
			costNVM += m_costParameters.costNVM[isWrite];
		}
		else
		{
			costSRAM += m_costParameters.costDRAM[isWrite]; 
			costNVM += m_costParameters.costNVM[isWrite];	
		}
	}

	if(costSRAM > costNVM)
		entry->state_rd = (RD_TYPE) RD_MEDIUM;
	else
		entry->state_rd = (RD_TYPE) RD_SHORT;
			
	
//	if( ((double)max / (double)window_rd) > RAP_INACURACY_TH)
//		entry->state_rd = (RD_TYPE) index_max;
//	else
//		entry->state_rd = RD_NOT_ACCURATE;

	

			
//	/** Debugging purpose */	
//	if(entry->state_rd == RD_NOT_ACCURATE && entry->state_rw == DEAD)
//	{
//		cout <<"RD history\t";
//		for(auto p : entry->rd_history)
//			cout <<p << "\t";
//		cout <<endl;
//		cout <<"RW history\tNBWrite=" << cpts_rw[INDEX_WRITE] << "\tNBRead=" << cpts_rw[INDEX_READ] << endl;
//		cout <<"New state is " << str_RD_status[entry->state_rd] << "\t" << str_RW_status[entry->state_rw] << endl;	
//	}


	/*
	if( ((double)max / (double) RAP_WINDOW_SIZE) > RAP_INACURACY_TH)
		entry->state_rw = (RW_TYPE)index_max;
	else
		entry->state_rw = RW_NOT_ACCURATE;
	*/
}

// Assembly:
cmpl $0x2, 0x28(%rsi)
jne 0x10523
retq
pushq %rbp
pushq %r14
pushq %rbx
movq %rsi, %rbx
movq %rdi, %r14
movl $0x8, %edi
callq 0x52e0
movq $0x0, (%rax)
movq 0x58(%rbx), %rcx
movq 0x68(%rbx), %rdx
movl 0x70(%rbx), %esi
cmpq %rdx, %rcx
setne %dil
testl %esi, %esi
setne %r8b
orb %dil, %r8b
cmpb $0x1, %r8b
jne 0x105a3
xorl %edi, %edi
xorl %r9d, %r9d
movq %rcx, %r8
movq (%r8), %r10
xorl %r11d, %r11d
btq %r9, %r10
setb %r11b
incl (%rax,%r11,4)
xorl %r10d, %r10d
cmpl $0x3f, %r9d
leal 0x1(%r9), %r9d
sete %r10b
leaq (%r8,%r10,8), %r8
cmovel %edi, %r9d
cmpq %rdx, %r8
setne %r10b
cmpl %esi, %r9d
setne %r11b
orb %r10b, %r11b
jne 0x10567
movl (%rax), %edx
testl %edx, %edx
je 0x105d8
testl %edx, %edx
jle 0x105c2
cmpl $0x0, 0x4(%rax)
je 0x105e5
testl %edx, %edx
jle 0x105c2
movl $0x3, %edi
cmpl $0x0, 0x4(%rax)
jg 0x105ea
movabsq $0x200000000, %rcx      # imm = 0x200000000
movq %rcx, 0x98(%rbx)
jmp 0x106b3
movl $0x1, %edi
cmpl $0x0, 0x4(%rax)
jg 0x105ea
jmp 0x105a9
movl $0x2, %edi
movq 0x40(%rbx), %rdx
movq 0x48(%rbx), %rsi
movl %edi, 0x98(%rbx)
subq %rdx, %rsi
je 0x1068f
sarq $0x2, %rsi
movq 0x2a0(%r14), %rdi
movq 0x2b8(%r14), %r8
movq 0x2d0(%r14), %r9
xorpd %xmm0, %xmm0
xorl %r10d, %r10d
xorpd %xmm1, %xmm1
movq %r10, %r11
shrq $0x6, %r11
movq (%rcx,%r11,8), %r14
movl (%rdx,%r10,4), %ebp
xorl %r11d, %r11d
btq %r10, %r14
setb %r11b
shll $0x2, %r11d
testl %ebp, %ebp
je 0x1065d
cmpl $0x2, %ebp
jne 0x10663
movl (%r9,%r11), %r14d
movl (%r8,%r11), %ebp
addl %r14d, %ebp
addl (%rdi,%r11), %r14d
jmp 0x1066b
movl (%r8,%r11), %ebp
jmp 0x10667
movl (%r9,%r11), %ebp
movl (%rdi,%r11), %r14d
xorps %xmm2, %xmm2
cvtsi2sd %r14d, %xmm2
addsd %xmm2, %xmm0
xorps %xmm2, %xmm2
cvtsi2sd %ebp, %xmm2
addsd %xmm2, %xmm1
incq %r10
movl %r10d, %r11d
cmpq %r11, %rsi
ja 0x10625
jmp 0x10697
xorpd %xmm1, %xmm1
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm1
jbe 0x106a9
movl $0x1, 0x9c(%rbx)
jmp 0x106b3
movl $0x0, 0x9c(%rbx)
movq %rax, %rdi
popq %rbx
popq %r14
popq %rbp
jmp 0x52a0
nop