// File: /workspace/github_cpp_makefile_star2_O1/gulshan216[P]Perceptron_Based_Reuse_Prediction/replacement_state.cpp
// Function: CACHE_REPLACEMENT_STATE::UpdateMyPolicy(unsigned int, unsigned int, int, LINE_STATE const*, unsigned long long, unsigned int, bool)
void CACHE_REPLACEMENT_STATE::UpdateMyPolicy(UINT32 tid, UINT32 setIndex, INT32 updateWayID, const LINE_STATE *currLine, Addr_t PC,UINT32 accessType, bool cacheHit) {
	// do nothing
	unsigned int hash[6];
	// int y_out;
	//Addr_t feature[6];
	
	if(!((accessType == 5) && (cacheHit)))
	{
		UpdateLRU(setIndex,updateWayID);

		Addr_t tag = currLine->tag;
		// feature[0]= (PC>>2)%256;
		// feature[1]= (PC_History[0]>>1)%256;
		// feature[2]= (PC_History[1]>>2)%256;
		// feature[3]= (PC_History[2]>>3)%256;
		// feature[4]= (tag>>4)%256;
		// feature[5]= (tag>>7)%256;

		// hash[0] = (unsigned char)((PC^feature[0])&0xFF);
		// hash[1] = (unsigned char)((PC^feature[1])&0xFF);
		// hash[2] = (unsigned char)((PC^feature[2])&0xFF);
		// hash[3] = (unsigned char)((PC^feature[3])&0xFF);
		// hash[4] = (unsigned char)((PC^feature[4])&0xFF);
		// hash[5] = (unsigned char)((PC^feature[5])&0xFF);

		// if(((PC^feature[0])&0xFF) == 0)
		// {
		// 	hash[0]=hash[0];
		// 	//Do nothing
		// }

		hash[0] = ((PC^(PC>>2))%PRED_TABLE_SIZE);
		hash[1] = ((PC^(PC_History[tid][0]>>1))%PRED_TABLE_SIZE);
		hash[2] = ((PC^(PC_History[tid][1]>>2))%PRED_TABLE_SIZE);
		hash[3] = ((PC^(PC_History[tid][2]>>3))%PRED_TABLE_SIZE);
		hash[4] = ((PC^(tag>>4))%PRED_TABLE_SIZE);
		hash[5] = ((PC^(tag))%PRED_TABLE_SIZE);

		repl[setIndex][updateWayID].pred_dead = ((int)pred_table1[hash[0]]+(int)pred_table2[hash[1]]+(int)pred_table3[hash[2]]+(int)pred_table4[hash[3]]+(int)pred_table5[hash[4]]+(int)pred_table6[hash[5]])>=Treplace;
		// if((repl[setIndex][updateWayID].pred_dead == false) && (cacheHit))
		// {
		// 	UpdateLRU(setIndex,updateWayID);
		// }

		if((setIndex%16)==0)
		{
			sampler_cache_access((setIndex/16),tag,PC,hash);
		}
	}

	//Implement PC History Update logic
	UpdatePCHistory(tid, PC);

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %r9, %rbx
cmpl $0x5, 0x50(%rsp)
sete %al
movl %esi, %r14d
testb %al, 0x58(%rsp)
jne 0x406a02
movq (%rdi), %rax
movl %edx, %esi
movq (%rax,%rsi,8), %rax
movslq %ecx, %rcx
movl 0xc(%rdi), %esi
testq %rsi, %rsi
je 0x4068e6
movl (%rax,%rcx,8), %edi
xorl %r9d, %r9d
movl (%rax,%r9,8), %r10d
cmpl %edi, %r10d
jae 0x4068de
incl %r10d
movl %r10d, (%rax,%r9,8)
incq %r9
cmpq %r9, %rsi
jne 0x4068ce
movl $0x0, (%rax,%rcx,8)
movq (%r8), %rsi
movq %rbx, %rdi
shrq $0x2, %rdi
xorq %rbx, %rdi
movl $0x1ff, %r9d            # imm = 0x1FF
movl %edi, %r8d
andl %r9d, %r8d
movl %r8d, (%rsp)
leaq (%r14,%r14,2), %r11
leaq 0x1fa85b(%rip), %r15    # 0x601170
movq (%r15,%r11,8), %r8
shrq %r8
xorq %rbx, %r8
movl %r8d, %r10d
andl %r9d, %r10d
movl %r10d, 0x4(%rsp)
movq 0x8(%r15,%r11,8), %r10
shrq $0x2, %r10
xorq %rbx, %r10
movl %r10d, %ebp
andl %r9d, %ebp
movl %ebp, 0x8(%rsp)
movq 0x10(%r15,%r11,8), %r11
shrq $0x3, %r11
xorq %rbx, %r11
movl %r11d, %ebp
andl %r9d, %ebp
movl %ebp, 0xc(%rsp)
movq %rsi, %r15
shrq $0x4, %r15
xorq %rbx, %r15
movl %r15d, %ebp
andl %r9d, %ebp
movl %ebp, 0x10(%rsp)
movq %rsi, %r12
xorq %rbx, %r12
andl %r12d, %r9d
movl %r9d, 0x14(%rsp)
movl $0x1ff, %r9d            # imm = 0x1FF
andq %r9, %rdi
leaq 0x1f9be8(%rip), %r13    # 0x600570
movsbl (%rdi,%r13), %edi
andq %r9, %r8
leaq 0x1f9dd9(%rip), %r13    # 0x600770
movsbl (%r8,%r13), %r8d
addl %edi, %r8d
andq %r9, %r10
leaq 0x1f9fc7(%rip), %rdi    # 0x600970
movsbl (%r10,%rdi), %edi
andq %r9, %r11
leaq 0x1fa1b8(%rip), %r10    # 0x600b70
movsbl (%r11,%r10), %r10d
addl %edi, %r10d
addl %r8d, %r10d
andq %r9, %r15
leaq 0x1fa3a3(%rip), %rdi    # 0x600d70
movsbl (%r15,%rdi), %edi
andq %r9, %r12
leaq 0x1fa594(%rip), %r8     # 0x600f70
movsbl (%r12,%r8), %r8d
addl %edi, %r8d
addl %r10d, %r8d
cmpl $0x7c, %r8d
setge 0x4(%rax,%rcx,8)
testb $0xf, %dl
jne 0x406a02
shrl $0x4, %edx
movq %rsp, %rcx
movl %edx, %edi
callq 0x406100
leaq (%r14,%r14,2), %rax
leaq 0x1fa763(%rip), %rcx    # 0x601170
movups (%rcx,%rax,8), %xmm0
movups %xmm0, 0x8(%rcx,%rax,8)
movq %rbx, (%rcx,%rax,8)
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq