// File: /workspace/github_c_makefile_star2_O2/xtanitfy[P]Murphy/src/layers/conv_layer.c
// Function: ConvLayerForward
int ConvLayerForward(NET_PARA_s *pNetPara,LAYER_s *pLayer)
{
	CONV_LAYER_s *pConvLayer = (CONV_LAYER_s *)pLayer->currLayer;
	BLOB_s *pData = pLayer->pData;
	
	CONV_LAYER_PARA_s *pPara = &pLayer->para->select.convLayer;

	BLOB_s *pPrevData = pLayer->pPrev[0]->pData;
	
	BLOB_DATA_PTR(pLayer->pData,ptrCurr);
	BLOB_DATA_PTR(pLayer->k,ptrK);
	BLOB_DATA_PTR(pPrevData,ptrPrevData);
	BLOB_DATA_PTR(pLayer->b,ptrB);

	//CalTimeStart();
	int blockMinX = 0,blockMinY = 0;
	int ytmp = pPara->kernelY-1;
	int xtmp = pPara->kernelX-1;
	double tmp = 0;
	
	for (int i_batch = 0;i_batch < pData->numSize;i_batch++) {
		for (int i_out = 0;i_out < pData->channelSize;i_out++) {
			for (int h = 0;h < pConvLayer->imgheight;h++) {
				for (int w = 0;w < pConvLayer->imgWidth;w++) {
					tmp = 0;
					blockMinY = h*pPara->strideY;
					blockMinX = w*pPara->strideX;
					for (int i_in = 0;i_in < pPrevData->channelSize;i_in++) {
						for (int offsetY = blockMinY;offsetY < (blockMinY + pPara->kernelY);offsetY++) {
							for (int offsetX = blockMinX;offsetX < (blockMinX + pPara->kernelX);offsetX++) {	
								tmp += ptrK[i_out][i_in][ytmp-offsetY+blockMinY][xtmp-offsetX+blockMinX] * \
										ptrPrevData[i_batch][i_in][offsetY][offsetX];
								
							}
						}
					}	
					ptrCurr[i_batch][i_out][h][w]  = pPara->function(tmp + \
								ptrB[0][i_out][0][0],FUNC_VALUE);

				}
		
			}
			
		}
	}
	
	//CalTimeEnd("conv");

#if 0
	static int cnt = 0;
	cnt++;
	if (cnt == 1) {
		LOG_writeBlobToTxtFile("murphy_conv1.txt",pData);
		LOG_writeBlobToBinFile("murphy_conv1.bin",pData);
		LOG_compBinFile("murphy_conv1.bin","a1.bin",sizeof(BLOB_DATA_TYPE)*pData->size);
	}
#endif
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xe8, %rsp
movq (%rsi), %rax
movq %rax, 0x18(%rsp)
movq 0x8(%rsi), %r14
movq 0x10(%rsi), %rdi
movq 0xa8(%rsi), %rax
movq 0x10(%rax), %r8
movq 0x18(%rdi), %rax
movq 0x28(%rdi), %r9
movq 0x18(%rsi), %rcx
movq 0x8(%rcx), %rdx
movq %rdx, 0x58(%rsp)
movq 0x28(%rcx), %rbp
movq 0x20(%rsi), %rdx
movq 0x8(%rdx), %rsi
movq %rsi, 0x80(%rsp)
movq 0x28(%rdx), %rsi
movl 0x28c(%r14), %r13d
movq 0x20(%rcx), %r10
imulq %rbp, %r10
movq 0x18(%rcx), %rcx
movq %r10, 0xb8(%rsp)
imulq %r10, %rcx
movq %rcx, 0x48(%rsp)
movq 0x20(%rdi), %rcx
imulq 0x20(%rdx), %rsi
movq %rsi, 0x50(%rsp)
movq %r9, 0x70(%rsp)
imulq %r9, %rcx
movq %rcx, 0x40(%rsp)
imulq %rax, %rcx
movq %rcx, 0x30(%rsp)
decl %r13d
movq 0x28(%r8), %r15
movq 0x20(%r8), %rcx
imulq %r15, %rcx
movq 0x18(%r8), %rdx
imulq %rcx, %rdx
shlq $0x3, %rdx
movq %rdx, 0x28(%rsp)
shlq $0x3, %r15
shlq $0x3, %rcx
movq %rcx, 0xb0(%rsp)
movq %rdi, 0x10(%rsp)
movq 0x8(%rdi), %rcx
movq %rcx, 0x20(%rsp)
movq %r8, 0x88(%rsp)
movq 0x8(%r8), %rcx
movq %rcx, (%rsp)
movl 0x290(%r14), %ecx
movl %ecx, 0xc(%rsp)
xorl %edx, %edx
movq 0x10(%rsp), %rcx
cmpq %rdx, 0x10(%rcx)
jbe 0x4ede
movq 0x30(%rsp), %rcx
movq %rdx, 0x38(%rsp)
imulq %rdx, %rcx
movq 0x20(%rsp), %rdx
leaq (%rdx,%rcx,8), %rcx
movq %rcx, 0x60(%rsp)
xorl %edx, %edx
cmpq %rdx, %rax
jbe 0x4ec4
movq 0x48(%rsp), %rax
imulq %rdx, %rax
movq 0x50(%rsp), %rcx
imulq %rdx, %rcx
movq %rcx, 0x90(%rsp)
movq 0x40(%rsp), %rcx
movq %rdx, 0x68(%rsp)
imulq %rdx, %rcx
movq 0x58(%rsp), %rdx
leaq (%rdx,%rax,8), %rax
movq %rax, 0xc0(%rsp)
movq 0x60(%rsp), %rax
leaq (%rax,%rcx,8), %rax
movq %rax, 0x78(%rsp)
xorl %r9d, %r9d
movq 0x18(%rsp), %rax
movslq 0x8(%rax), %rax
cmpq %rax, %r9
jge 0x4eae
movq 0x70(%rsp), %rax
imulq %r9, %rax
movq 0x78(%rsp), %rcx
leaq (%rcx,%rax,8), %rax
movq %rax, 0x98(%rsp)
xorl %r10d, %r10d
movq %r9, 0xa0(%rsp)
movq 0x18(%rsp), %rax
movslq 0x4(%rax), %rax
cmpq %rax, %r10
jge 0x4ea6
movl 0x294(%r14), %ecx
movl 0x298(%r14), %edx
movl %edx, %eax
imull %r9d, %eax
movl %ecx, %esi
imull %r10d, %esi
movq 0x88(%rsp), %rdi
movq 0x18(%rdi), %rdi
movq %rdi, 0xd0(%rsp)
movslq %esi, %r8
movslq %eax, %rsi
movq %rsi, 0xc8(%rsp)
addl 0xc(%rsp), %eax
imull %r9d, %edx
movslq %edx, %rdx
imulq %r15, %rdx
movq %r10, 0xa8(%rsp)
imull %r10d, %ecx
movslq %ecx, %rcx
leaq (%rdx,%rcx,8), %rdi
addq (%rsp), %rdi
xorpd %xmm0, %xmm0
xorl %edx, %edx
cmpq 0xd0(%rsp), %rdx
je 0x4e62
movslq 0x290(%r14), %r9
movq 0xc8(%rsp), %rsi
addq %rsi, %r9
movq 0xb8(%rsp), %rcx
movq %rdx, 0xd8(%rsp)
imulq %rdx, %rcx
movq 0xc0(%rsp), %rdx
leaq (%rdx,%rcx,8), %r10
movq %rdi, 0xe0(%rsp)
cmpq %r9, %rsi
jge 0x4e42
movslq 0x28c(%r14), %r12
addq %r8, %r12
movl %esi, %ecx
notl %ecx
addl %eax, %ecx
movslq %ecx, %rcx
imulq %rbp, %rcx
leaq (%r10,%rcx,8), %rcx
movq %rdi, %r11
movl %r13d, %ebx
movq %r8, %rdx
cmpq %r12, %rdx
jge 0x4e3a
movslq %ebx, %rbx
movsd (%rcx,%rbx,8), %xmm1
mulsd (%r11), %xmm1
addsd %xmm1, %xmm0
incq %rdx
decl %ebx
addq $0x8, %r11
jmp 0x4e19
incq %rsi
addq %r15, %rdi
jmp 0x4df0
movq 0xd8(%rsp), %rdx
incq %rdx
movq 0xe0(%rsp), %rdi
addq 0xb0(%rsp), %rdi
jmp 0x4da8
movq 0x80(%rsp), %rax
movq 0x90(%rsp), %rcx
addsd (%rax,%rcx,8), %xmm0
xorl %edi, %edi
callq *0x2a0(%r14)
movq 0x98(%rsp), %rax
movq 0xa8(%rsp), %r10
movsd %xmm0, (%rax,%r10,8)
incq %r10
movq 0xa0(%rsp), %r9
jmp 0x4d2e
incq %r9
jmp 0x4cf7
movq 0x68(%rsp), %rdx
incq %rdx
movq 0x10(%rsp), %rax
movq 0x18(%rax), %rax
jmp 0x4ca4
movq 0x38(%rsp), %rdx
incq %rdx
movq (%rsp), %rcx
addq 0x28(%rsp), %rcx
movq %rcx, (%rsp)
jmp 0x4c77
xorl %eax, %eax
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq