// File: /workspace/github_c_makefile_star2_O2/xtanitfy[P]Murphy/src/layers/conv_layer.c
// Function: ConvLayerBackward
int ConvLayerBackward(NET_PARA_s *pNetPara,LAYER_s *pLayer)
{
	CHECK_EXPR_RET(pLayer == NULL,-1);
	CONV_LAYER_PARA_s *pPara = &pLayer->para->select.convLayer;
	//CONV_LAYER_s *pConvLayer = (CONV_LAYER_s *)pLayer->currLayer;
	
	BLOB_s *pDataCurr = pLayer->pData;
	BLOB_s *pDeltaCurr = pLayer->pDelta;
	CHECK_EXPR_RET(pDataCurr == NULL,-1);
	CHECK_EXPR_RET(pDeltaCurr == NULL,-1);

	CHECK_EXPR_RET(pLayer->nNext <= 0,-1);
	BLOB_s *pDeltaNext = pLayer->pNext[0]->pDelta;
	CHECK_EXPR_RET(pDeltaNext == NULL,-1);
	int sampScale = pLayer->pNext[0]->sampScale;
	int sampScaleSquare = pLayer->pNext[0]->sampScale * pLayer->pNext[0]->sampScale;

	BLOB_DATA_PTR(pDeltaCurr,ptrDeltaCurr);
	BLOB_DATA_PTR(pDeltaNext,ptrDeltaNext);
	BLOB_DATA_PTR(pDataCurr,ptrDataCurr);
	FUNC function = pPara->function;

	int padx = pDeltaCurr->padx;
	int pady = pDeltaCurr->pady;

	for (int i_batch = 0;i_batch < pDataCurr->numSize;i_batch++) {
		for (int i_out = 0;i_out < pDataCurr->channelSize;i_out++) {
			for (int h = 0;h < pDataCurr->heightSize;h++) {
				for (int w = 0;w < pDataCurr->widthSize;w++) {
					ptrDeltaCurr[i_batch][i_out][h+pady][w+padx] = \
						function(ptrDataCurr[i_batch][i_out][h][w],FUNC_DERIVA) *\
						ptrDeltaNext[i_batch][i_out][h/sampScale][w/sampScale]/sampScaleSquare;
				}
			}
		}
	}
	
	#if 0
	if (strcmp(pLayer->name,"conv1") == 0) {
		LOG_writeBlobToTxtFile("conv1_delta.txt",pDeltaCurr);
	} else if (strcmp(pLayer->name,"conv0") == 0) {
		LOG_writeBlobToTxtFile("conv0_delta.txt",pDeltaCurr);
	}
	#endif


	CHECK_EXPR_RET(pLayer->pDB == NULL,-1);
	BLOB_DATA_PTR(pLayer->pDB,ptrDB);
	
	/*����Ȩ�غ�ƫ������*/
//	OPTS_s *pOpts = NET_GetOpts();
//	double alpha = pOpts->alpha;
	//CHECK_EXPR_RET(pLayer->b == NULL,-1);
	//BLOB_DATA_PTR(pLayer->b,ptrB);
	double sum = 0;
	
	for (int i_out = 0;i_out < pDataCurr->channelSize;i_out++) {
		sum = 0;
		for (int i_batch = 0;i_batch < pDataCurr->numSize;i_batch++) {
			for (int h = 0;h < pDataCurr->heightSize;h++) {
				for (int w = 0;w < pDataCurr->widthSize;w++) {
					sum += ptrDeltaCurr[i_batch][i_out][h+pady][w+padx];
				}
			}
		}
		ptrDB[0][i_out][0][0] = sum/pDataCurr->numSize;
	}	
	

#if 0
	if (strcmp(pLayer->name,"conv0") == 0) {
		LOG_writeBlobToTxtFile("conv0_db.txt",pLayer->pDB);
		
	} else if(strcmp(pLayer->name,"conv1") == 0) {
		LOG_writeBlobToTxtFile("conv1_db.txt",pLayer->pDB);
	}
#endif

	CHECK_EXPR_RET(pLayer->nPrev <= 0,-1);
	BLOB_s *pDataPrev = pLayer->pPrev[0]->pData;
	CHECK_EXPR_RET(pDataPrev == NULL,-1);

	BLOB_s *pDK = pLayer->pDK;
	CHECK_EXPR_RET(pDK == NULL,-1);
	BLOB_DATA_PTR(pDK,ptrDK);
	BLOB_DATA_PTR(pDataPrev,ptrDataPrev);

	int blockMinX = 0,blockMinY = 0;
	double tmp = 0;
	int ytmp = pDataPrev->heightSize-1;
	int xtmp = pDataPrev->widthSize-1;
	int y1tmp = pDataCurr->heightSize-1+pady;
	int x1tmp = pDataCurr->widthSize-1+padx;

	//CHECK_EXPR_RET(pLayer->k == NULL,-1);
	//BLOB_DATA_PTR(pLayer->k,ptrK);
	
	for (int i_out = 0;i_out < pDataCurr->channelSize;i_out++) {	
		for (int h = 0;h < pDK->heightSize;h++) {
			for (int w = 0;w < pDK->widthSize;w++) {
				for (int i_in = 0;i_in < pDataPrev->channelSize;i_in++) {
					blockMinY = h;
					blockMinX = w;
					tmp = 0;
					for (int i_batch = 0;i_batch < pDataCurr->numSize;i_batch++) {
						for (int offsetY = blockMinY;offsetY < (blockMinY + pDataCurr->heightSize);offsetY++) {
							for (int offsetX = blockMinX;offsetX < (blockMinX + pDataCurr->widthSize);offsetX++) {	
								tmp += ptrDataPrev[i_batch][i_in][ytmp-offsetY][xtmp-offsetX] * \
										ptrDeltaCurr[i_batch][i_out][y1tmp-offsetY+blockMinY][x1tmp-offsetX+blockMinX];
							}
						}
					}
					ptrDK[i_out][i_in][h][w] = tmp / pDataCurr->numSize;
				}
			}
		}
	}

	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x108, %rsp            # imm = 0x108
testq %rsi, %rsi
je 0x51d2
movq 0x10(%rsi), %rax
movq %rax, 0x8(%rsp)
testq %rax, %rax
je 0x51dc
movq 0x28(%rsi), %rax
testq %rax, %rax
je 0x51e6
cmpl $0x0, 0xa0(%rsi)
jle 0x529b
movq 0x60(%rsi), %rdx
movq 0x28(%rdx), %rcx
testq %rcx, %rcx
je 0x55d4
movq %rsi, 0x20(%rsp)
movq 0x8(%rsi), %rsi
movl 0xec(%rdx), %edx
movl %edx, 0xb8(%rsp)
imull %edx, %edx
movq 0x8(%rax), %rdi
movq 0x28(%rax), %r8
movq 0x8(%rcx), %r9
movq %r9, 0x70(%rsp)
movq 0x28(%rcx), %rbx
movq 0x8(%rsp), %r10
movq 0x20(%r10), %r15
movq 0x28(%r10), %r11
movq 0x2a0(%rsi), %rsi
movq %rsi, 0xb0(%rsp)
movslq 0x3c(%rax), %rsi
movslq 0x38(%rax), %r9
movq 0x20(%rcx), %r14
movq %rbx, %r13
imulq %rbx, %r14
movq 0x18(%rcx), %rcx
cvtsi2sd %edx, %xmm0
movsd %xmm0, 0xa8(%rsp)
movq %r14, 0x80(%rsp)
imulq %r14, %rcx
movq %rcx, 0x68(%rsp)
movq %r8, %rcx
movq %r9, 0x50(%rsp)
imulq %r9, %rcx
movq %rsi, 0x58(%rsp)
leaq (,%rsi,8), %rdx
leaq (%rdx,%rcx,8), %rsi
movq 0x20(%rax), %rcx
imulq %r8, %rcx
movq 0x18(%rax), %rax
imulq %rcx, %rax
movq %r11, %r9
imulq %r15, %r9
movq 0x18(%r10), %rdx
movq %r9, %rbx
imulq %rdx, %rbx
shlq $0x3, %rbx
movq %rbx, 0x60(%rsp)
shlq $0x3, %r9
movq %r9, 0x78(%rsp)
movq 0x8(%r10), %r9
movq %r9, 0x18(%rsp)
movq %rdi, 0xc0(%rsp)
movq %rsi, (%rsp)
addq %rdi, %rsi
movq %rsi, 0x10(%rsp)
movq %rax, 0xf8(%rsp)
leaq (,%rax,8), %rax
movq %rax, 0x30(%rsp)
movq %rcx, 0xe0(%rsp)
leaq (,%rcx,8), %rax
movq %rax, 0x38(%rsp)
movq %r8, 0x100(%rsp)
leaq (,%r8,8), %rax
movq %rax, 0x48(%rsp)
leaq (,%r11,8), %rax
movq %rax, 0x40(%rsp)
xorl %ecx, %ecx
movq 0x8(%rsp), %rax
movq 0x10(%rax), %rbx
cmpq %rcx, %rbx
jbe 0x51f0
movq 0x68(%rsp), %rax
movq %rcx, 0x28(%rsp)
imulq %rcx, %rax
movq 0x70(%rsp), %rcx
leaq (%rcx,%rax,8), %rax
movq %rax, 0x88(%rsp)
movq 0x18(%rsp), %rbx
movq 0x10(%rsp), %rbp
xorl %ecx, %ecx
cmpq %rcx, %rdx
jbe 0x51a7
movq 0x80(%rsp), %rax
movq %rcx, 0x90(%rsp)
imulq %rcx, %rax
movq 0x88(%rsp), %rcx
leaq (%rcx,%rax,8), %r12
movq %rbx, 0xa0(%rsp)
movq %rbp, 0x98(%rsp)
xorl %r14d, %r14d
cmpq %r14, %r15
jbe 0x5174
xorl %r15d, %r15d
cmpq %r15, %r11
jbe 0x515c
movsd (%rbx,%r15,8), %xmm0
pushq $0x1
popq %rdi
callq *0xb0(%rsp)
movl %r14d, %eax
cltd
movl 0xb8(%rsp), %esi
idivl %esi
cltq
imulq %r13, %rax
leaq (%r12,%rax,8), %rcx
movl %r15d, %eax
cltd
idivl %esi
cltq
mulsd (%rcx,%rax,8), %xmm0
divsd 0xa8(%rsp), %xmm0
movsd %xmm0, (%rbp,%r15,8)
incq %r15
movq 0x8(%rsp), %rax
movq 0x28(%rax), %r11
jmp 0x5105
incq %r14
movq 0x8(%rsp), %rax
movq 0x20(%rax), %r15
addq 0x48(%rsp), %rbp
addq 0x40(%rsp), %rbx
jmp 0x50fd
movq 0x90(%rsp), %rcx
incq %rcx
movq 0x8(%rsp), %rax
movq 0x18(%rax), %rdx
movq 0x98(%rsp), %rbp
addq 0x38(%rsp), %rbp
movq 0xa0(%rsp), %rbx
addq 0x78(%rsp), %rbx
jmp 0x50c1
movq 0x28(%rsp), %rcx
incq %rcx
movq 0x10(%rsp), %rax
addq 0x30(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0x18(%rsp), %rax
addq 0x60(%rsp), %rax
movq %rax, 0x18(%rsp)
jmp 0x5084
movl $0x160, %ecx            # imm = 0x160
jmp 0x55f9
movl $0x166, %ecx            # imm = 0x166
jmp 0x55f9
movl $0x167, %ecx            # imm = 0x167
jmp 0x55f9
movq %rdx, 0xc8(%rsp)
movq 0x20(%rsp), %rax
movq 0x38(%rax), %rdx
testq %rdx, %rdx
je 0x55db
movq 0x8(%rdx), %rax
movq 0x28(%rdx), %rcx
cvtsi2sd %rbx, %xmm0
imulq 0x20(%rdx), %rcx
movq (%rsp), %rdx
addq 0xc0(%rsp), %rdx
movq %rdx, (%rsp)
xorl %edx, %edx
cmpq 0xc8(%rsp), %rdx
je 0x52a5
xorpd %xmm1, %xmm1
movq (%rsp), %rsi
xorl %edi, %edi
cmpq %rbx, %rdi
je 0x5279
movq %rsi, %r8
xorl %r9d, %r9d
cmpq %r15, %r9
je 0x526f
xorl %r10d, %r10d
cmpq %r10, %r11
je 0x5265
addsd (%r8,%r10,8), %xmm1
incq %r10
jmp 0x5255
incq %r9
addq 0x48(%rsp), %r8
jmp 0x524d
incq %rdi
addq 0x30(%rsp), %rsi
jmp 0x5242
divsd %xmm0, %xmm1
movq %rcx, %rsi
imulq %rdx, %rsi
movsd %xmm1, (%rax,%rsi,8)
incq %rdx
movq (%rsp), %rsi
addq 0x38(%rsp), %rsi
movq %rsi, (%rsp)
jmp 0x522e
movl $0x169, %ecx            # imm = 0x169
jmp 0x55f9
movq 0x20(%rsp), %rdx
cmpl $0x0, 0xe8(%rdx)
jle 0x55e2
movq 0xa8(%rdx), %rax
movq 0x10(%rax), %rcx
testq %rcx, %rcx
je 0x55ed
movq 0x30(%rdx), %rax
testq %rax, %rax
je 0x55f4
movabsq $-0x100000000, %r8      # imm = 0xFFFFFFFF00000000
movq 0x8(%rax), %rdx
movq %rdx, 0xd8(%rsp)
movq 0x20(%rax), %rdx
movq 0x28(%rax), %rsi
movq 0x8(%rcx), %rdi
movq %rdi, 0x78(%rsp)
movq 0x18(%rcx), %rdi
movq 0x20(%rcx), %r9
movq 0x28(%rcx), %r10
movq %r10, %r14
movq %r9, 0x8(%rsp)
imulq %r9, %r14
movq %rsi, 0x28(%rsp)
movq %rdx, 0x68(%rsp)
imulq %rdx, %rsi
movq 0x18(%rax), %rax
movq %rsi, 0x18(%rsp)
imulq %rsi, %rax
movq %rax, 0xd0(%rsp)
movq %r14, 0x30(%rsp)
movq %rdi, 0x38(%rsp)
imulq %rdi, %r14
movq %r14, 0x90(%rsp)
movq 0x50(%rsp), %rax
addl %r15d, %eax
decl %eax
movq %rax, 0x20(%rsp)
movq 0x58(%rsp), %rax
addl %r11d, %eax
decl %eax
movl %eax, 0xb0(%rsp)
movq %r10, 0xb8(%rsp)
shlq $0x20, %r10
addq %r8, %r10
movq %r10, 0x58(%rsp)
xorl %edx, %edx
movq %r11, 0xf0(%rsp)
movq %r15, 0xe8(%rsp)
movq %rbx, 0x98(%rsp)
cmpq 0xc8(%rsp), %rdx
je 0x55e9
movq 0xd0(%rsp), %rax
imulq %rdx, %rax
movq 0xe0(%rsp), %rcx
movq %rdx, 0x50(%rsp)
imulq %rdx, %rcx
movq 0xc0(%rsp), %rdx
leaq (%rdx,%rcx,8), %rcx
movq %rcx, 0xa0(%rsp)
movq 0xd8(%rsp), %rcx
leaq (%rcx,%rax,8), %rax
movq %rax, 0x60(%rsp)
movq %r15, %rax
xorl %esi, %esi
cmpq %rsi, %rax
movq %rsi, %rdx
cmovaq %rax, %rdx
cmpq 0x68(%rsp), %rsi
je 0x55c7
movq %rax, 0x70(%rsp)
movq 0x20(%rsp), %rax
addl %esi, %eax
movq 0x28(%rsp), %rcx
movq %rsi, 0x40(%rsp)
imulq %rsi, %rcx
movq 0x60(%rsp), %rsi
leaq (%rsi,%rcx,8), %rcx
movq %rcx, (%rsp)
movq 0x58(%rsp), %rsi
movq %r11, %rcx
xorl %r11d, %r11d
cmpq %r11, %rcx
movq %r11, %r15
cmovaq %rcx, %r15
cmpq 0x28(%rsp), %r11
je 0x55a2
movq %rcx, 0x10(%rsp)
movq (%rsp), %rcx
leaq (%rcx,%r11,8), %rcx
movq %rcx, 0x80(%rsp)
xorl %edi, %edi
cmpq 0x38(%rsp), %rdi
je 0x558f
movq 0x30(%rsp), %rcx
movq %rdi, 0x88(%rsp)
imulq %rdi, %rcx
movq 0x78(%rsp), %rdi
leaq (%rdi,%rcx,8), %rcx
movq %rcx, 0x48(%rsp)
xorpd %xmm1, %xmm1
xorl %r9d, %r9d
cmpq %rbx, %r9
je 0x5562
movq 0x90(%rsp), %rcx
imulq %r9, %rcx
movq 0xf8(%rsp), %rdi
movq %r9, 0xa8(%rsp)
imulq %r9, %rdi
movq 0x48(%rsp), %r9
leaq (%r9,%rcx,8), %r9
movq 0xa0(%rsp), %rcx
leaq (%rcx,%rdi,8), %r10
movq 0x40(%rsp), %rbx
cmpq %rdx, %rbx
je 0x554a
movl %eax, %ecx
subl %ebx, %ecx
movl %ebx, %edi
notl %edi
addl 0x8(%rsp), %edi
movslq %edi, %rdi
imulq 0xb8(%rsp), %rdi
leaq (%r9,%rdi,8), %r12
movslq %ecx, %rcx
imulq 0x100(%rsp), %rcx
leaq (%r10,%rcx,8), %rdi
movl 0xb0(%rsp), %ecx
movl %ecx, %r13d
movq %r15, %rcx
movq %rsi, %rbp
cmpq %rcx, %r11
je 0x5545
movq %rbp, %r14
sarq $0x1d, %r14
movsd (%r12,%r14), %xmm2
movslq %r13d, %r13
mulsd (%rdi,%r13,8), %xmm2
addsd %xmm2, %xmm1
addq %r8, %rbp
decq %rcx
decl %r13d
jmp 0x551b
incq %rbx
jmp 0x54da
movq 0xa8(%rsp), %r9
incq %r9
movq 0x98(%rsp), %rbx
jmp 0x5497
divsd %xmm0, %xmm1
movq 0x18(%rsp), %rcx
movq 0x88(%rsp), %r9
imulq %r9, %rcx
movq 0x80(%rsp), %rdi
movsd %xmm1, (%rdi,%rcx,8)
movq %r9, %rdi
incq %rdi
jmp 0x5466
incq %r11
movq 0x10(%rsp), %rcx
incq %rcx
addq %r8, %rsi
jmp 0x543a
movq 0x40(%rsp), %rsi
incq %rsi
movq 0x70(%rsp), %rax
incq %rax
movq 0xf0(%rsp), %r11
movq 0xe8(%rsp), %r15
jmp 0x53f3
movq 0x50(%rsp), %rdx
incq %rdx
jmp 0x539e
movl $0x16b, %ecx            # imm = 0x16B
jmp 0x55f9
movl $0x18c, %ecx            # imm = 0x18C
jmp 0x55f9
movl $0x1ac, %ecx            # imm = 0x1AC
jmp 0x55f9
xorl %eax, %eax
jmp 0x5618
movl $0x1ae, %ecx            # imm = 0x1AE
jmp 0x55f9
movl $0x1b1, %ecx            # imm = 0x1B1
leaq 0x1a45(%rip), %rdi      # 0x7045
leaq 0x219b(%rip), %rsi      # 0x77a2
leaq 0x21af(%rip), %rdx      # 0x77bd
xorl %eax, %eax
callq 0x1030
pushq $-0x1
popq %rax
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop