// File: /workspace/github_c_makefile_star2_O3/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 $0xf8, %rsp
testq %rsi, %rsi
je 0x56d3
movq 0x10(%rsi), %r11
testq %r11, %r11
je 0x56dd
movq 0x28(%rsi), %rax
testq %rax, %rax
je 0x56e7
cmpl $0x0, 0xa0(%rsi)
jle 0x56f1
movq 0x60(%rsi), %rdx
movq 0x28(%rdx), %rcx
testq %rcx, %rcx
je 0x56fb
movq %rsi, 0x28(%rsp)
movq 0x8(%rax), %r14
movq 0x18(%rax), %rsi
movq 0x20(%rax), %r8
movq 0x28(%rax), %r9
movq 0x28(%r11), %r10
movq 0x10(%r11), %r12
movslq 0x3c(%rax), %rbp
movslq 0x38(%rax), %rbx
testq %r12, %r12
movq %r9, 0xd0(%rsp)
movq %r11, 0x8(%rsp)
movq %rbx, (%rsp)
movq %r14, 0xe0(%rsp)
movq %rsi, 0x38(%rsp)
movq %r8, 0x20(%rsp)
je 0x5705
movq 0x28(%rsp), %rax
movq 0x8(%rax), %rax
movl 0xec(%rdx), %edx
movq 0x8(%rcx), %rsi
movq %rsi, 0x78(%rsp)
movq 0x28(%rcx), %r15
movq 0x8(%r11), %rsi
movq %rsi, 0x50(%rsp)
movq 0x2a0(%rax), %r13
movq 0x18(%r11), %rsi
movq 0x20(%r11), %rax
movl %edx, %r11d
imull %edx, %edx
movq 0x20(%rcx), %rdi
imulq %r15, %rdi
movq 0x18(%rcx), %rcx
cvtsi2sd %edx, %xmm1
movq %rdi, 0x80(%rsp)
imulq %rdi, %rcx
movq %rcx, 0x70(%rsp)
movq %r9, %rcx
imulq %rbx, %rcx
leaq (,%rbp,8), %rdx
leaq (%rdx,%rcx,8), %rcx
addq %r14, %rcx
movq %rcx, 0x10(%rsp)
movq %r9, %rcx
imulq %r8, %rcx
movq %rcx, %rdx
imulq 0x38(%rsp), %rdx
shlq $0x3, %rdx
movq %rdx, 0x48(%rsp)
shlq $0x3, %rcx
movq %rcx, 0x90(%rsp)
leaq (,%r9,8), %rcx
movq %rcx, 0xb8(%rsp)
movq %r10, %rcx
imulq %rax, %rcx
movq %rcx, %rdx
imulq %rsi, %rdx
shlq $0x3, %rdx
movq %rdx, 0x40(%rsp)
shlq $0x3, %rcx
movq %rcx, 0x58(%rsp)
leaq (,%r10,8), %rcx
movq %rcx, 0xb0(%rsp)
movq $0x0, 0x30(%rsp)
movq %r10, %rdi
movq %rax, %rdx
movq %rsi, %r8
movq %r15, 0xc8(%rsp)
movq %r13, 0xc0(%rsp)
movsd %xmm1, 0x60(%rsp)
movq 0x8(%rsp), %r14
testq %r8, %r8
je 0x56a4
movq 0x70(%rsp), %rcx
imulq 0x30(%rsp), %rcx
movq 0x78(%rsp), %r8
leaq (%r8,%rcx,8), %rcx
movq %rcx, 0x88(%rsp)
movq 0x50(%rsp), %r8
movq 0x10(%rsp), %r9
xorl %ecx, %ecx
testq %rdx, %rdx
je 0x5680
movq %rcx, %rdx
movq 0x80(%rsp), %rcx
movq %rdx, 0x98(%rsp)
imulq %rdx, %rcx
movq 0x88(%rsp), %rdx
leaq (%rdx,%rcx,8), %rsi
movq %r8, 0xa8(%rsp)
movq %r9, 0xa0(%rsp)
xorl %r13d, %r13d
testq %rdi, %rdi
je 0x5641
xorl %r15d, %r15d
movq %r8, 0x18(%rsp)
movq %r9, 0xd8(%rsp)
movsd (%r8,%r15,8), %xmm0
movl $0x1, %edi
movl %r11d, %r12d
movq %rsi, %rbx
callq *0xc0(%rsp)
movq %rbx, %rsi
movsd 0x60(%rsp), %xmm1
movl %r12d, %r11d
movl %r13d, %eax
cltd
idivl %r12d
cltq
imulq 0xc8(%rsp), %rax
leaq (%rbx,%rax,8), %rcx
movl %r15d, %eax
cltd
idivl %r12d
cltq
mulsd (%rcx,%rax,8), %xmm0
movq 0xd8(%rsp), %r9
movq 0x18(%rsp), %r8
divsd %xmm1, %xmm0
movsd %xmm0, (%r9,%r15,8)
incq %r15
movq 0x28(%r14), %r10
cmpq %r15, %r10
ja 0x55cd
movq 0x20(%r14), %rax
movq %r10, %rdi
jmp 0x5643
xorl %edi, %edi
incq %r13
addq 0xb8(%rsp), %r9
addq 0xb0(%rsp), %r8
cmpq %r13, %rax
ja 0x55b4
movq 0x18(%r14), %rsi
movq %rax, %rdx
movq 0xa8(%rsp), %r8
movq 0xa0(%rsp), %r9
movq 0x98(%rsp), %rcx
jmp 0x5682
xorl %edx, %edx
incq %rcx
addq 0x90(%rsp), %r9
addq 0x58(%rsp), %r8
cmpq %rcx, %rsi
ja 0x5575
movq 0x10(%r14), %r12
movq %rsi, %r8
jmp 0x56a7
xorl %r8d, %r8d
movq 0x30(%rsp), %r9
incq %r9
movq 0x48(%rsp), %rcx
addq %rcx, 0x10(%rsp)
movq 0x40(%rsp), %rcx
addq %rcx, 0x50(%rsp)
movq %r9, 0x30(%rsp)
cmpq %r9, %r12
ja 0x5544
jmp 0x5708
movl $0x160, %ecx            # imm = 0x160
jmp 0x5b88
movl $0x166, %ecx            # imm = 0x166
jmp 0x5b88
movl $0x167, %ecx            # imm = 0x167
jmp 0x5b88
movl $0x169, %ecx            # imm = 0x169
jmp 0x5b88
movl $0x16b, %ecx            # imm = 0x16B
jmp 0x5b88
xorl %r12d, %r12d
movq 0x28(%rsp), %rax
movq 0x38(%rax), %rcx
testq %rcx, %rcx
je 0x5b6e
movq %r10, 0x18(%rsp)
movq 0x8(%rsp), %rax
movq 0x18(%rax), %rdx
movq 0x20(%rsp), %rax
imulq 0xd0(%rsp), %rax
movq %rax, 0x20(%rsp)
movq %rdx, 0x68(%rsp)
testq %rdx, %rdx
movq %r12, 0x60(%rsp)
je 0x5827
movq 0x28(%rcx), %rax
xorps %xmm0, %xmm0
cvtsi2sd %r12, %xmm0
imulq 0x20(%rcx), %rax
movq 0x8(%rcx), %rdx
movq 0xd0(%rsp), %r9
movq %r9, %rcx
imulq (%rsp), %rcx
leaq (,%rbp,8), %rsi
leaq (%rsi,%rcx,8), %rsi
addq 0xe0(%rsp), %rsi
movq 0x20(%rsp), %r8
leaq (,%r8,8), %rdi
imulq 0x38(%rsp), %r8
shlq $0x3, %r8
leaq (,%r9,8), %r9
xorl %r10d, %r10d
testq %r12, %r12
je 0x5806
movq 0x8(%rsp), %rcx
movq 0x20(%rcx), %r11
xorpd %xmm1, %xmm1
movq %rsi, %rbx
xorl %r14d, %r14d
testq %r11, %r11
je 0x57f4
movq %rbx, %r15
xorl %r13d, %r13d
cmpq $0x0, 0x18(%rsp)
je 0x57e9
xorl %ecx, %ecx
movq 0x18(%rsp), %r12
addsd (%r15,%rcx,8), %xmm1
incq %rcx
cmpq %rcx, %r12
jne 0x57db
incq %r13
addq %r9, %r15
cmpq %r11, %r13
jne 0x57cc
incq %r14
addq %r8, %rbx
movq 0x60(%rsp), %r12
cmpq %r12, %r14
jne 0x57c1
jmp 0x580a
xorpd %xmm1, %xmm1
divsd %xmm0, %xmm1
movq %rax, %rcx
imulq %r10, %rcx
movsd %xmm1, (%rdx,%rcx,8)
incq %r10
addq %rdi, %rsi
cmpq 0x68(%rsp), %r10
jne 0x57a9
movq 0x28(%rsp), %rdx
cmpl $0x0, 0xe8(%rdx)
jle 0x5b75
movq 0xa8(%rdx), %rax
movq 0x10(%rax), %rcx
testq %rcx, %rcx
je 0x5b7c
movq 0x30(%rdx), %rax
testq %rax, %rax
je 0x5b83
cmpq $0x0, 0x68(%rsp)
je 0x5bbb
movq 0x8(%rax), %rdx
movq %rdx, 0xf0(%rsp)
movq 0x20(%rax), %rdx
movq 0x28(%rax), %rsi
movq 0x8(%rcx), %rdi
movq %rdi, 0x30(%rsp)
movq 0x18(%rcx), %rdi
movq 0x20(%rcx), %r8
movq 0x28(%rcx), %r9
movq 0x8(%rsp), %rcx
movq 0x20(%rcx), %r10
movabsq $-0x100000000, %r13     # imm = 0xFFFFFFFF00000000
movq %r9, %r11
movq %r8, 0xc8(%rsp)
imulq %r8, %r11
movq %rsi, 0x10(%rsp)
movq %rdx, 0x48(%rsp)
imulq %rdx, %rsi
movq 0x18(%rax), %rax
movq %rsi, 0x80(%rsp)
imulq %rsi, %rax
movq %rax, 0xe8(%rsp)
movq %r11, 0x50(%rsp)
movq %rdi, 0x58(%rsp)
imulq %rdi, %r11
movq %r11, 0x98(%rsp)
movq 0x38(%rsp), %rax
imulq 0x20(%rsp), %rax
movq %rax, 0x38(%rsp)
movq (%rsp), %rax
movq %r10, 0x40(%rsp)
addl %r10d, %eax
decl %eax
movq %rax, 0x70(%rsp)
decl %ebp
cvtsi2sd %r12, %xmm0
movq %r9, 0xc0(%rsp)
shlq $0x20, %r9
addq %r13, %r9
movq %r9, 0x28(%rsp)
movq $0x0, (%rsp)
cmpq $0x0, 0x48(%rsp)
je 0x5b54
movq 0xe8(%rsp), %rax
movq (%rsp), %rdx
imulq %rdx, %rax
movq 0x20(%rsp), %rcx
imulq %rdx, %rcx
movq 0xe0(%rsp), %rdx
leaq (%rdx,%rcx,8), %rcx
movq %rcx, 0xa0(%rsp)
movq 0xf0(%rsp), %rcx
leaq (%rcx,%rax,8), %rax
movq %rax, 0x78(%rsp)
movq 0x40(%rsp), %r8
xorl %r11d, %r11d
cmpq $0x0, 0x10(%rsp)
je 0x5b43
movq 0x40(%rsp), %rax
addq %r11, %rax
movq %rax, 0xb0(%rsp)
movq 0x70(%rsp), %rax
addl %r11d, %eax
movl %eax, 0xd8(%rsp)
movq %r11, %rax
imulq 0x10(%rsp), %rax
movq 0x78(%rsp), %rcx
leaq (%rcx,%rax,8), %rax
movq %rax, 0x8(%rsp)
movq 0x28(%rsp), %r15
xorl %r9d, %r9d
movq %r11, 0xb8(%rsp)
cmpq $0x0, 0x58(%rsp)
je 0x5b32
movq 0x18(%rsp), %rax
leaq (%r9,%rax), %rdx
movq 0x8(%rsp), %rax
leaq (%rax,%r9,8), %rax
movq %rax, 0x88(%rsp)
xorl %esi, %esi
testq %r12, %r12
movq %rsi, 0x90(%rsp)
je 0x5afb
movq 0x50(%rsp), %rax
imulq %rsi, %rax
movq 0x30(%rsp), %rcx
leaq (%rcx,%rax,8), %rax
movq %rax, 0xa8(%rsp)
xorpd %xmm1, %xmm1
xorl %edi, %edi
cmpq %r11, 0xb0(%rsp)
jbe 0x5ae0
movq 0x38(%rsp), %rcx
imulq %rdi, %rcx
movq 0x98(%rsp), %rax
imulq %rdi, %rax
movq 0xa8(%rsp), %rsi
leaq (%rsi,%rax,8), %rax
movq 0xa0(%rsp), %rsi
leaq (%rsi,%rcx,8), %r10
movq %r11, %rsi
cmpq %r9, %rdx
jbe 0x5ad8
movl 0xd8(%rsp), %ecx
subl %esi, %ecx
movl %esi, %r11d
notl %r11d
addl 0xc8(%rsp), %r11d
movslq %r11d, %r11
imulq 0xc0(%rsp), %r11
leaq (%rax,%r11,8), %r12
movslq %ecx, %rcx
imulq 0xd0(%rsp), %rcx
leaq (%r10,%rcx,8), %rcx
movq 0x18(%rsp), %rbx
movq %r15, %r14
movq %r14, %r11
sarq $0x1d, %r11
movsd (%r12,%r11), %xmm2
leal (%rbx,%rbp), %r11d
movslq %r11d, %r11
mulsd (%rcx,%r11,8), %xmm2
addsd %xmm2, %xmm1
addq %r13, %r14
decq %rbx
jne 0x5ab2
incq %rsi
cmpq %r8, %rsi
jne 0x5a6e
incq %rdi
movq 0x60(%rsp), %r12
cmpq %r12, %rdi
movq 0xb8(%rsp), %r11
jne 0x5a30
jmp 0x5aff
xorpd %xmm1, %xmm1
divsd %xmm0, %xmm1
movq 0x80(%rsp), %rax
movq 0x90(%rsp), %rsi
imulq %rsi, %rax
movq 0x88(%rsp), %rcx
movsd %xmm1, (%rcx,%rax,8)
incq %rsi
cmpq 0x58(%rsp), %rsi
jne 0x59ff
incq %r9
addq %r13, %r15
cmpq 0x10(%rsp), %r9
jne 0x59d7
incq %r11
incq %r8
cmpq 0x48(%rsp), %r11
jne 0x5985
movq (%rsp), %rdx
incq %rdx
xorl %eax, %eax
movq %rdx, (%rsp)
cmpq 0x68(%rsp), %rdx
jne 0x5933
jmp 0x5ba9
movl $0x18c, %ecx            # imm = 0x18C
jmp 0x5b88
movl $0x1ac, %ecx            # imm = 0x1AC
jmp 0x5b88
movl $0x1ae, %ecx            # imm = 0x1AE
jmp 0x5b88
movl $0x1b1, %ecx            # imm = 0x1B1
leaq 0x14b6(%rip), %rdi      # 0x7045
leaq 0x1c0c(%rip), %rsi      # 0x77a2
leaq 0x1c20(%rip), %rdx      # 0x77bd
xorl %eax, %eax
callq 0x1030
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %eax, %eax
jmp 0x5ba9
nop