// File: /workspace/github_c_makefile_star2_O1/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 0x5d05
movq 0x10(%rsi), %r13
testq %r13, %r13
je 0x5d0c
movq 0x28(%rsi), %rcx
testq %rcx, %rcx
je 0x5d13
cmpl $0x0, 0xa0(%rsi)
jle 0x5d1a
movq 0x60(%rsi), %rdx
movq 0x28(%rdx), %rax
testq %rax, %rax
je 0x5d21
movq %rsi, 0x20(%rsp)
movq 0x8(%rcx), %rsi
movq %rsi, 0x80(%rsp)
movq 0x18(%rcx), %rsi
movq %rsi, 0x40(%rsp)
movq 0x20(%rcx), %rsi
movq %rsi, 0x10(%rsp)
movq 0x28(%rcx), %rsi
movq %rsi, 0x78(%rsp)
movq 0x10(%r13), %r14
movslq 0x3c(%rcx), %rbp
movslq 0x38(%rcx), %rcx
movq %rcx, 0x18(%rsp)
testq %r14, %r14
je 0x584d
movq 0x20(%rsp), %rcx
movq 0x8(%rcx), %rcx
movl 0xec(%rdx), %esi
movq 0x8(%rax), %rdx
movq %rdx, 0x50(%rsp)
movq 0x28(%rax), %rdi
movq 0x28(%r13), %r8
movq 0x8(%r13), %rdx
movq %rdx, 0x60(%rsp)
movq 0x2a0(%rcx), %r9
movl %esi, %ecx
imull %esi, %ecx
movq 0x20(%rax), %rdx
imulq %rdi, %rdx
movq 0x18(%rax), %rax
movq %rdx, 0xa8(%rsp)
imulq %rdx, %rax
movq %rax, 0x98(%rsp)
cvtsi2sd %ecx, %xmm1
movq 0x78(%rsp), %rdx
movq %rdx, %rax
imulq 0x18(%rsp), %rax
leaq (,%rbp,8), %rcx
leaq (%rcx,%rax,8), %rax
addq 0x80(%rsp), %rax
movq %rax, (%rsp)
movq %rdx, %rax
imulq 0x10(%rsp), %rax
movq %rax, %rcx
imulq 0x40(%rsp), %rcx
shlq $0x3, %rcx
movq %rcx, 0xa0(%rsp)
shlq $0x3, %rax
movq %rax, 0xb8(%rsp)
leaq (,%rdx,8), %rax
movq %rax, 0xc0(%rsp)
movq 0x20(%r13), %rax
imulq %r8, %rax
movq 0x18(%r13), %rcx
imulq %rax, %rcx
shlq $0x3, %rcx
movq %rcx, 0x58(%rsp)
shlq $0x3, %rax
movq %rax, 0x68(%rsp)
shlq $0x3, %r8
movq %r8, 0xc8(%rsp)
movq $0x0, 0x28(%rsp)
movsd %xmm1, 0xd0(%rsp)
movq %rdi, 0x70(%rsp)
cmpq $0x0, 0x18(%r13)
je 0x581d
movq 0x98(%rsp), %rax
imulq 0x28(%rsp), %rax
movq 0x50(%rsp), %rcx
leaq (%rcx,%rax,8), %rax
movq %rax, 0xb0(%rsp)
movq 0x60(%rsp), %rax
movq %rax, 0x48(%rsp)
movq (%rsp), %rax
movq %rax, 0x38(%rsp)
movq $0x0, 0x30(%rsp)
cmpq $0x0, 0x20(%r13)
je 0x57e5
movq 0xa8(%rsp), %rax
imulq 0x30(%rsp), %rax
movq 0xb0(%rsp), %rcx
leaq (%rcx,%rax,8), %rbx
movq 0x48(%rsp), %rax
movq 0x38(%rsp), %rcx
xorl %r10d, %r10d
cmpq $0x0, 0x28(%r13)
je 0x57c8
xorl %r15d, %r15d
movq %rax, 0x8(%rsp)
movq %rcx, 0xe0(%rsp)
movq %r10, 0xd8(%rsp)
movsd (%rax,%r15,8), %xmm0
movl $0x1, %edi
movl %esi, %r12d
movq %r9, %r14
callq *%r9
movq 0xd8(%rsp), %r10
movsd 0xd0(%rsp), %xmm1
movq %r14, %r9
movq 0x70(%rsp), %rdi
movl %r12d, %esi
movl %r10d, %eax
cltd
idivl %r12d
cltq
imulq %rdi, %rax
leaq (%rbx,%rax,8), %rcx
movl %r15d, %eax
cltd
idivl %r12d
cltq
mulsd (%rcx,%rax,8), %xmm0
movq 0xe0(%rsp), %rcx
movq 0x8(%rsp), %rax
divsd %xmm1, %xmm0
movsd %xmm0, (%rcx,%r15,8)
incq %r15
cmpq %r15, 0x28(%r13)
ja 0x5759
incq %r10
addq 0xc0(%rsp), %rcx
addq 0xc8(%rsp), %rax
cmpq %r10, 0x20(%r13)
ja 0x5736
movq 0x30(%rsp), %rcx
incq %rcx
movq 0x38(%rsp), %rax
addq 0xb8(%rsp), %rax
movq %rax, 0x38(%rsp)
movq 0x48(%rsp), %rax
addq 0x68(%rsp), %rax
movq %rax, 0x48(%rsp)
movq %rcx, 0x30(%rsp)
cmpq %rcx, 0x18(%r13)
ja 0x5704
movq 0x28(%rsp), %rcx
incq %rcx
movq 0x10(%r13), %r14
movq 0xa0(%rsp), %rax
addq %rax, (%rsp)
movq 0x58(%rsp), %rax
addq %rax, 0x60(%rsp)
movq %rcx, 0x28(%rsp)
cmpq %rcx, %r14
ja 0x56be
movq 0x20(%rsp), %rax
movq 0x38(%rax), %rcx
testq %rcx, %rcx
je 0x5d28
movq 0x18(%r13), %rax
movq %rax, 0x90(%rsp)
testq %rax, %rax
movq %r14, 0x70(%rsp)
je 0x597c
movq 0x8(%rcx), %rax
movq %rax, 0x8(%rsp)
movq %r14, %xmm1
punpckldq 0x2a71(%rip), %xmm1 # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
movq 0x28(%rcx), %rdx
subpd 0x2a75(%rip), %xmm1     # 0x8310
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
addsd %xmm1, %xmm0
imulq 0x20(%rcx), %rdx
movq 0x78(%rsp), %r8
movq %r8, %rcx
imulq 0x18(%rsp), %rcx
leaq (,%rbp,8), %rsi
leaq (%rsi,%rcx,8), %rcx
addq 0x80(%rsp), %rcx
movq %r8, %rsi
imulq 0x10(%rsp), %rsi
leaq (,%rsi,8), %rdi
imulq 0x40(%rsp), %rsi
shlq $0x3, %rsi
leaq (,%r8,8), %r8
xorl %r9d, %r9d
movq %r13, %rax
testq %r14, %r14
je 0x594e
movq 0x20(%r13), %r10
xorpd %xmm1, %xmm1
movq %rcx, %r11
xorl %ebx, %ebx
testq %r10, %r10
je 0x5939
movq 0x28(%r13), %r14
movq %r11, %r15
xorl %r13d, %r13d
testq %r14, %r14
je 0x592e
xorl %r12d, %r12d
addsd (%r15,%r12,8), %xmm1
incq %r12
cmpq %r12, %r14
jne 0x5920
incq %r13
addq %r8, %r15
cmpq %r10, %r13
jne 0x5918
incq %rbx
addq %rsi, %r11
movq 0x70(%rsp), %r14
cmpq %r14, %rbx
movq %rax, %r13
jne 0x5909
jmp 0x5952
xorpd %xmm1, %xmm1
divsd %xmm0, %xmm1
movq %rdx, %r10
imulq %r9, %r10
movq 0x8(%rsp), %r11
movsd %xmm1, (%r11,%r10,8)
incq %r9
addq %rdi, %rcx
cmpq 0x90(%rsp), %r9
jne 0x58f7
movq 0x20(%rsp), %rdx
cmpl $0x0, 0xe8(%rdx)
jle 0x5d2f
movq 0xa8(%rdx), %rax
movq 0x10(%rax), %rcx
testq %rcx, %rcx
je 0x5d36
movq 0x30(%rdx), %rax
testq %rax, %rax
je 0x5d3d
cmpq $0x0, 0x90(%rsp)
je 0x5d75
movq 0x8(%rax), %rdx
movq %rdx, 0xf0(%rsp)
movq 0x20(%rax), %rdx
movq 0x28(%rax), %rsi
movq 0x8(%rcx), %rdi
movq %rdi, 0x28(%rsp)
movq 0x18(%rcx), %rdi
movq 0x20(%rcx), %r9
movq 0x28(%rcx), %r10
movq 0x20(%r13), %r11
movq 0x28(%r13), %rcx
movq %rcx, 0x8(%rsp)
movq 0x10(%rsp), %rcx
imulq 0x78(%rsp), %rcx
movq %r10, %rbx
movq %r9, 0xd8(%rsp)
imulq %r9, %rbx
movq %rsi, (%rsp)
movq %rdx, 0x58(%rsp)
imulq %rdx, %rsi
movq 0x18(%rax), %rax
movq %rsi, 0xa8(%rsp)
imulq %rsi, %rax
movq %rax, 0xe8(%rsp)
movq %rbx, 0x60(%rsp)
movq %rdi, 0x68(%rsp)
imulq %rdi, %rbx
movq %rbx, 0x30(%rsp)
movq 0x40(%rsp), %rax
movq %rcx, 0x10(%rsp)
imulq %rcx, %rax
movq %rax, 0x40(%rsp)
movq 0x18(%rsp), %rax
movq %r11, 0x50(%rsp)
addl %r11d, %eax
decl %eax
movq %rax, 0x20(%rsp)
decl %ebp
movq %r14, %xmm1
punpckldq 0x2884(%rip), %xmm1 # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0x288c(%rip), %xmm1     # 0x8310
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
addsd %xmm1, %xmm0
movq %r10, 0xd0(%rsp)
shlq $0x20, %r10
movabsq $-0x100000000, %rdi     # imm = 0xFFFFFFFF00000000
addq %rdi, %r10
movq %r10, 0x18(%rsp)
movq $0x0, 0x88(%rsp)
cmpq $0x0, 0x58(%rsp)
je 0x5ce0
movq 0xe8(%rsp), %rax
movq 0x88(%rsp), %rdx
imulq %rdx, %rax
movq 0x10(%rsp), %rcx
imulq %rdx, %rcx
movq 0x80(%rsp), %rdx
leaq (%rdx,%rcx,8), %rcx
movq %rcx, 0x38(%rsp)
movq 0xf0(%rsp), %rcx
leaq (%rcx,%rax,8), %rax
movq %rax, 0x98(%rsp)
movq 0x50(%rsp), %rdx
xorl %r11d, %r11d
cmpq $0x0, (%rsp)
je 0x5ccf
movq 0x50(%rsp), %rax
addq %r11, %rax
movq %rax, 0xc0(%rsp)
movq 0x20(%rsp), %rax
addl %r11d, %eax
movl %eax, 0xe0(%rsp)
movq %r11, %rax
imulq (%rsp), %rax
movq 0x98(%rsp), %rcx
leaq (%rcx,%rax,8), %rax
movq %rax, 0xa0(%rsp)
movq 0x18(%rsp), %r15
xorl %r8d, %r8d
movq %r11, 0xc8(%rsp)
cmpq $0x0, 0x68(%rsp)
je 0x5cbf
movq 0x8(%rsp), %rax
leaq (%r8,%rax), %rsi
movq 0xa0(%rsp), %rax
leaq (%rax,%r8,8), %rax
movq %rax, 0xb0(%rsp)
xorl %r9d, %r9d
testq %r14, %r14
movq %r9, 0xb8(%rsp)
je 0x5c88
movq 0x60(%rsp), %rax
imulq %r9, %rax
movq 0x28(%rsp), %rcx
leaq (%rcx,%rax,8), %rax
movq %rax, 0x48(%rsp)
xorpd %xmm1, %xmm1
xorl %ecx, %ecx
cmpq %r11, 0xc0(%rsp)
jbe 0x5c6d
movq 0x40(%rsp), %r9
imulq %rcx, %r9
movq 0x30(%rsp), %rax
imulq %rcx, %rax
movq 0x48(%rsp), %r10
leaq (%r10,%rax,8), %rax
movq 0x38(%rsp), %r10
leaq (%r10,%r9,8), %r13
movq %r11, %r9
cmpq %r8, %rsi
jbe 0x5c65
movl 0xe0(%rsp), %r10d
subl %r9d, %r10d
movl %r9d, %r11d
notl %r11d
addl 0xd8(%rsp), %r11d
movslq %r11d, %r11
imulq 0xd0(%rsp), %r11
leaq (%rax,%r11,8), %r12
movslq %r10d, %r10
imulq 0x78(%rsp), %r10
leaq (,%r10,8), %r11
addq %r13, %r11
movq 0x8(%rsp), %rbx
movq %r15, %r14
movq %r14, %r10
sarq $0x1d, %r10
movsd (%r12,%r10), %xmm2
leal (%rbx,%rbp), %r10d
movslq %r10d, %r10
mulsd (%r11,%r10,8), %xmm2
addsd %xmm2, %xmm1
addq %rdi, %r14
decq %rbx
jne 0x5c3f
incq %r9
cmpq %rdx, %r9
jne 0x5bf5
incq %rcx
movq 0x70(%rsp), %r14
cmpq %r14, %rcx
movq 0xc8(%rsp), %r11
jne 0x5bc0
jmp 0x5c8c
xorpd %xmm1, %xmm1
divsd %xmm0, %xmm1
movq 0xa8(%rsp), %rax
movq 0xb8(%rsp), %r9
imulq %r9, %rax
movq 0xb0(%rsp), %rcx
movsd %xmm1, (%rcx,%rax,8)
incq %r9
cmpq 0x68(%rsp), %r9
jne 0x5b92
incq %r8
addq %rdi, %r15
cmpq (%rsp), %r8
jne 0x5b66
incq %r11
incq %rdx
cmpq 0x58(%rsp), %r11
jne 0x5b10
movq 0x88(%rsp), %rdx
incq %rdx
xorl %eax, %eax
movq %rdx, 0x88(%rsp)
cmpq 0x90(%rsp), %rdx
jne 0x5aba
jmp 0x5d63
movl $0x160, %ecx            # imm = 0x160
jmp 0x5d42
movl $0x166, %ecx            # imm = 0x166
jmp 0x5d42
movl $0x167, %ecx            # imm = 0x167
jmp 0x5d42
movl $0x169, %ecx            # imm = 0x169
jmp 0x5d42
movl $0x16b, %ecx            # imm = 0x16B
jmp 0x5d42
movl $0x18c, %ecx            # imm = 0x18C
jmp 0x5d42
movl $0x1ac, %ecx            # imm = 0x1AC
jmp 0x5d42
movl $0x1ae, %ecx            # imm = 0x1AE
jmp 0x5d42
movl $0x1b1, %ecx            # imm = 0x1B1
leaq 0x22fc(%rip), %rdi      # 0x8045
leaq 0x2a65(%rip), %rsi      # 0x87b5
leaq 0x2a79(%rip), %rdx      # 0x87d0
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 0x5d63
nopl (%rax)