// File: /workspace/github_cpp_makefile_star2_O2/gvaumour[P]cache-simulator/src/Prefetcher.cc
// Function: SimplePrefetcher::getNextAddress(unsigned long)
vector<uint64_t>
SimplePrefetcher::getNextAddress(uint64_t current_address)
{

	uint64_t distance = INT64_MAX;
	int index = PAGE_SIZE;
	vector<uint64_t> prefetch_addresses;
	
	for(unsigned i = 0; i < m_prev_address.size() ; i++)
	{
		if( (current_address - m_prev_address[i].addr < distance) && m_prev_address[i].isValid)
		{
			index = i;
			distance = current_address - m_prev_address[i].addr;
		}
	}
	//DPRINTF("SimplePrefetcher::getNextAddress Distance %ld, index %d\n", distance , index);
	
	if( !(distance > 0 && distance < PAGE_SIZE) ) //No address within proper distances, create an entry 
	{
		//Select the older entry
		uint64_t age = INT64_MAX;
		int index_old = 0;
		bool done = false;
		for(unsigned i = 0 ; i < m_prev_address.size();i++)
		{
			if(!m_prev_address[i].isValid)
			{
				m_prev_address[i].addr = current_address;
				m_prev_address[i].isValid = true;
				m_prev_address[i].LRU_info = cpt_time;
				done = true;
			}
		}
		if(!done)
		{
		
			for(unsigned i = 0 ; i < m_prev_address.size();i++)
			{
				if(m_prev_address[i].LRU_info < age)
				{
					age = m_prev_address[i].LRU_info;
					index_old = i;
				}
			}
			m_prev_address[index_old].addr = current_address;
			m_prev_address[index_old].isValid = true;
			m_prev_address[index_old].LRU_info = cpt_time;
		}
		//DPRINTF("SimplePrefetcher::getNextAddress No Address Generated\n");
		return prefetch_addresses;
	}
	
	
	int stride = current_address - m_prev_address[index].addr;
	m_prev_address[index].addr = current_address;
	m_prev_address[index].LRU_info = cpt_time;

	if (stride != 0)
	{
		//DPRINTF("SimplePrefetcher::getNextAddress Generated prefecth For address %#lx\n", current_address);

		for(int i = 0; i < m_prefetchDegree; ++i)
		{
			 uint64_t prefetch_address = current_address + (i+1) * stride;
			 // But stay within the page if requested
			 if (!m_stop_at_page || ((prefetch_address & PAGE_MASK) == (current_address & PAGE_MASK)))
			    prefetch_addresses.push_back(prefetch_address);
			//DPRINTF("SimplePrefetcher::getNextAddress - %#lx\n",prefetch_address );
		}
		
	}

	return prefetch_addresses;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdx, %r14
movq %rsi, %r15
movabsq $0x7fffffffffffffff, %rsi # imm = 0x7FFFFFFFFFFFFFFF
xorps %xmm0, %xmm0
andq $0x0, 0x10(%rdi)
movq %rdi, 0x8(%rsp)
movups %xmm0, (%rdi)
movq 0x18(%r15), %rcx
movq 0x20(%r15), %rax
subq %rcx, %rax
pushq $0x18
popq %rdi
cqto
idivq %rdi
movl $0x1000, %edx           # imm = 0x1000
xorl %r8d, %r8d
movq %rsi, %rdi
movl %r8d, %r8d
cmpq %r8, %rax
jbe 0x214e9
imulq $0x18, %r8, %r10
movq %r14, %r9
subq (%rcx,%r10), %r9
cmpq %rdi, %r9
jae 0x214e4
addq %rcx, %r10
cmpb $0x0, 0x8(%r10)
cmovnel %r8d, %edx
cmovneq %r9, %rdi
incl %r8d
jmp 0x214bc
decq %rdi
cmpq $0xfff, %rdi            # imm = 0xFFF
jae 0x21568
movslq %edx, %rax
imulq $0x18, %rax, %rdx
movq %r14, %rax
subq (%rcx,%rdx), %rax
movq %r14, (%rcx,%rdx)
leaq 0x1ca0a(%rip), %rsi     # 0x3df18
movq (%rsi), %rsi
movq %rsi, 0x10(%rcx,%rdx)
testl %eax, %eax
je 0x215e4
movslq %eax, %r13
leaq (%r14,%r13), %rbp
xorl %ebx, %ebx
leaq 0x10(%rsp), %r12
movslq 0x8(%r15), %rax
cmpq %rax, %rbx
jge 0x215e4
movq %rbp, 0x10(%rsp)
cmpb $0x1, 0x15(%r15)
jne 0x21553
movq %rbp, %rax
xorq %r14, %rax
cmpq $0xfff, %rax            # imm = 0xFFF
ja 0x21560
movq 0x8(%rsp), %rdi
movq %r12, %rsi
callq 0x176d2
addq %r13, %rbp
incq %rbx
jmp 0x2152c
xorl %r8d, %r8d
leaq 0x1c9a6(%rip), %rdx     # 0x3df18
xorl %edi, %edi
movl %edi, %edi
cmpq %rdi, %rax
jbe 0x215a0
imulq $0x18, %rdi, %r9
cmpb $0x0, 0x8(%rcx,%r9)
jne 0x2159c
addq %rcx, %r9
movq %r14, (%r9)
movb $0x1, 0x8(%r9)
movq (%rdx), %r8
movq %r8, 0x10(%r9)
movb $0x1, %r8b
incl %edi
jmp 0x21574
testb $0x1, %r8b
jne 0x215e4
xorl %edi, %edi
xorl %r8d, %r8d
movl %r8d, %r8d
cmpq %r8, %rax
jbe 0x215cc
imulq $0x18, %r8, %r9
movq 0x10(%rcx,%r9), %r9
cmpq %rsi, %r9
cmovbq %r9, %rsi
cmovbl %r8d, %edi
incl %r8d
jmp 0x215ab
movslq %edi, %rax
imulq $0x18, %rax, %rax
movq %r14, (%rcx,%rax)
movb $0x1, 0x8(%rcx,%rax)
movq (%rdx), %rdx
movq %rdx, 0x10(%rcx,%rax)
movq 0x8(%rsp), %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r14
movq 0x8(%rsp), %rdi
callq 0xa4a8
movq %r14, %rdi
callq 0x5570
nop