// File: /workspace/github_c_makefile_star2_O3/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 $0xf8, %rsp
movq 0x10(%rsi), %rax
movq %rax, 0x18(%rsp)
movq 0x10(%rax), %rcx
testq %rcx, %rcx
je 0x5385
movq 0x8(%rsi), %r9
movq 0xa8(%rsi), %rax
movq 0x10(%rax), %r10
movq 0x18(%rsp), %r8
movq 0x28(%r8), %r11
movq 0x18(%rsi), %rdx
movq 0x8(%rdx), %rax
movq %rax, 0x68(%rsp)
movq 0x28(%rdx), %rbx
movq 0x28(%r10), %r13
movq 0x20(%rsi), %rdi
movq 0x8(%rdi), %rax
movq %rax, 0x90(%rsp)
movq 0x18(%r8), %rax
movq 0x28(%rdi), %r14
movq (%rsi), %rsi
movq %rsi, 0x8(%rsp)
movq 0x20(%rdx), %rsi
movq %rbx, 0xf0(%rsp)
imulq %rbx, %rsi
movq 0x18(%rdx), %rdx
movq %rsi, 0xb8(%rsp)
imulq %rsi, %rdx
movq %rdx, 0x58(%rsp)
movq 0x20(%r8), %rdx
movq %r11, 0x80(%rsp)
imulq %r11, %rdx
movq %rdx, 0x50(%rsp)
imulq %rax, %rdx
movq %rdx, 0x40(%rsp)
imulq 0x20(%rdi), %r14
movq %r14, 0x60(%rsp)
movq 0x20(%r10), %rdx
imulq %r13, %rdx
movq 0x18(%r10), %rsi
imulq %rdx, %rsi
shlq $0x3, %rsi
movq %rsi, 0x48(%rsp)
shlq $0x3, %r13
shlq $0x3, %rdx
movq %rdx, 0xd8(%rsp)
movl 0x28c(%r9), %edx
decl %edx
movl %edx, 0x34(%rsp)
movq 0x8(%r8), %rdx
movq %rdx, 0x38(%rsp)
movq %r10, 0x98(%rsp)
movq 0x8(%r10), %rdx
movq %rdx, 0x20(%rsp)
movq %r9, 0x28(%rsp)
movl 0x290(%r9), %edx
movl %edx, 0x30(%rsp)
movq $0x0, 0x10(%rsp)
movq %rax, %rdx
testq %rdx, %rdx
je 0x535e
movq 0x40(%rsp), %rcx
imulq 0x10(%rsp), %rcx
movq 0x38(%rsp), %rdx
leaq (%rdx,%rcx,8), %rcx
movq %rcx, 0x70(%rsp)
movq 0x8(%rsp), %rcx
movl 0x8(%rcx), %ecx
xorl %esi, %esi
testl %ecx, %ecx
jle 0x5344
movq 0x58(%rsp), %rax
imulq %rsi, %rax
movq 0x60(%rsp), %rdx
imulq %rsi, %rdx
movq %rdx, 0xa0(%rsp)
movq 0x50(%rsp), %rdx
movq %rsi, 0x78(%rsp)
imulq %rsi, %rdx
movq 0x68(%rsp), %rsi
leaq (%rsi,%rax,8), %rax
movq %rax, 0xc0(%rsp)
movq 0x70(%rsp), %rax
leaq (%rax,%rdx,8), %rax
movq %rax, 0x88(%rsp)
movq 0x8(%rsp), %rax
movl 0x4(%rax), %eax
movq $0x0, (%rsp)
testl %eax, %eax
jle 0x531f
movq 0x80(%rsp), %rax
imulq (%rsp), %rax
movq 0x88(%rsp), %rcx
leaq (%rcx,%rax,8), %rax
movq %rax, 0xa8(%rsp)
xorl %ecx, %ecx
movq 0x98(%rsp), %rax
movq 0x18(%rax), %rax
movq %rax, 0xe8(%rsp)
testq %rax, %rax
movq %rcx, 0xb0(%rsp)
je 0x52c7
movq 0x28(%rsp), %rax
movl 0x294(%rax), %r8d
imull %ecx, %r8d
movl 0x290(%rax), %ecx
movl 0x298(%rax), %eax
movl %eax, %esi
movq (%rsp), %rdx
imull %edx, %esi
movq %rcx, 0xe0(%rsp)
addl %esi, %ecx
movq %r8, 0xd0(%rsp)
movslq %r8d, %r12
movslq %esi, %rdi
movq %rdi, 0xc8(%rsp)
addl 0x30(%rsp), %esi
movslq %ecx, %r9
imull %edx, %eax
cltq
imulq %r13, %rax
movslq %r12d, %rcx
leaq (%rax,%rcx,8), %rbp
addq 0x20(%rsp), %rbp
xorpd %xmm0, %xmm0
xorl %r11d, %r11d
cmpl $0x0, 0xe0(%rsp)
jle 0x52ac
movq 0x28(%rsp), %rax
movl 0x28c(%rax), %r14d
movq 0xd0(%rsp), %rax
addl %r14d, %eax
movq 0xb8(%rsp), %rcx
imulq %r11, %rcx
movq 0xc0(%rsp), %rdx
leaq (%rdx,%rcx,8), %r15
movslq %eax, %rbx
movq %rbp, %r8
movq 0xc8(%rsp), %rcx
testl %r14d, %r14d
jle 0x52a1
movl %ecx, %eax
notl %eax
addl %esi, %eax
cltq
imulq 0xf0(%rsp), %rax
leaq (%r15,%rax,8), %rax
movl 0x34(%rsp), %edx
movq %r8, %r10
movq %r12, %rdi
movslq %edx, %rdx
movsd (%rax,%rdx,8), %xmm1
mulsd (%r10), %xmm1
addsd %xmm1, %xmm0
incq %rdi
addq $0x8, %r10
decl %edx
cmpq %rbx, %rdi
jl 0x5282
incq %rcx
addq %r13, %r8
cmpq %r9, %rcx
jl 0x525e
incq %r11
addq 0xd8(%rsp), %rbp
cmpq 0xe8(%rsp), %r11
jne 0x5213
jmp 0x52cb
xorpd %xmm0, %xmm0
movq 0x90(%rsp), %rax
movq 0xa0(%rsp), %rcx
addsd (%rax,%rcx,8), %xmm0
xorl %edi, %edi
movq 0x28(%rsp), %rax
callq *0x2a0(%rax)
movq 0xa8(%rsp), %rax
movq 0xb0(%rsp), %rcx
movsd %xmm0, (%rax,%rcx,8)
incq %rcx
movq 0x8(%rsp), %rax
movslq 0x4(%rax), %rax
cmpq %rax, %rcx
jl 0x5186
movq 0x8(%rsp), %rcx
movl 0x8(%rcx), %ecx
movq (%rsp), %rdi
incq %rdi
movslq %ecx, %rdx
movq %rdi, (%rsp)
cmpq %rdx, %rdi
jl 0x515b
movq 0x18(%rsp), %rax
movq 0x18(%rax), %rax
movq 0x78(%rsp), %rsi
incq %rsi
cmpq %rsi, %rax
ja 0x50f9
movq 0x18(%rsp), %rcx
movq 0x10(%rcx), %rcx
movq %rax, %rdx
jmp 0x5360
xorl %edx, %edx
movq 0x10(%rsp), %rdi
incq %rdi
movq 0x20(%rsp), %rsi
addq 0x48(%rsp), %rsi
movq %rsi, 0x20(%rsp)
movq %rdi, 0x10(%rsp)
cmpq %rdi, %rcx
ja 0x50cd
xorl %eax, %eax
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq