// File: /workspace/github_c_makefile_star2_O2/xtanitfy[P]Murphy/src/layers/samp_layer.c
// Function: SampLayerBackward
int SampLayerBackward(NET_PARA_s *pNetPara,LAYER_s *pLayer)
{
	CHECK_EXPR_RET(pLayer == NULL,-1);

	/*��ʱdelta�Ѿ���linear��������*/
	if (strcmp(pLayer->pNext[0]->para->typeName,"linear") == 0) {
		//LOG_writeBlobToTxtFile("sample1_delta.txt",pLayer->pDelta);		
	} else if (strcmp(pLayer->pNext[0]->para->typeName,"conv") == 0) {
		CHECK_EXPR_RET(pLayer->nNext <= 0,-1);
		BLOB_s *pDeltaNext = pLayer->pNext[0]->pDelta;
		CHECK_EXPR_RET(pDeltaNext == NULL,-1);
		BLOB_s *pDeltaCurr = pLayer->pDelta;
		CHECK_EXPR_RET(pDeltaCurr == NULL,-1);
		BLOB_s *pKNext = pLayer->pNext[0]->k;

		BLOB_DATA_PTR(pDeltaNext,ptrDeltaNext);
		BLOB_DATA_PTR(pDeltaCurr,ptrDeltaCurr);
		BLOB_DATA_PTR(pKNext,ptrKNext);

		int  blockMinY = 0;
		int  blockMinX = 0;
		double tmp = 0;
		int ky = 0;
		int kx = 0;
		
		int strideX = pLayer->pNext[0]->strideX;
		int strideY = pLayer->pNext[0]->strideY;
		/*[h,w]+pad[kh-1,kw-1] conv [kh,kw]-->[(h+(kh-1)*2-kh+1),(w+(kw-1)*2-kw+1)]-->[h+kh-1,w+kw-1]*/
 		
		for (int i_batch = 0;i_batch < pDeltaCurr->numSize;i_batch++) {
			for (int i_out = 0;i_out < pDeltaCurr->channelSize;i_out++) {
				for (int h = 0;h < pDeltaCurr->heightSize;h++) {
					for (int w = 0;w < pDeltaCurr->widthSize;w++) {
						tmp = 0;
						blockMinY = h * strideY;
						blockMinX = w * strideX;
						for (int i_outNext = 0;i_outNext < pDeltaNext->channelSize;i_outNext++) {
							for (int offsetY = blockMinY;offsetY < (blockMinY + pKNext->heightSize);offsetY++) {
								for (int offsetX = blockMinX;offsetX < (blockMinX + pKNext->widthSize);offsetX++) {	
									ky = offsetY - blockMinY;
									kx = offsetX - blockMinX;		
									tmp += ptrKNext[i_outNext][i_out][ky][kx] * \
											ptrDeltaNext[i_batch][i_outNext][offsetY][offsetX];
									
								}
							}
						}
						ptrDeltaCurr[i_batch][i_out][h][w] = tmp;
					}		
				}
				
			}
		}

		#if 0
		LOG_writeBlobToTxtFile("sample0_delta.txt",pDeltaCurr);	
		#endif
	}

	
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xf8, %rsp
testq %rsi, %rsi
je 0x67ba
movq %rsi, %r14
movq 0x60(%rsi), %rbx
movq 0x8(%rbx), %r15
addq $0x4, %r15
leaq 0xc4e(%rip), %rsi       # 0x709e
movq %r15, %rdi
callq 0x1100
xorl %r12d, %r12d
testl %eax, %eax
je 0x67dd
leaq 0xc12(%rip), %rsi       # 0x707c
movq %r15, %rdi
callq 0x1100
testl %eax, %eax
jne 0x67dd
cmpl $0x0, 0xa0(%r14)
jle 0x67f2
movq 0x28(%rbx), %rax
testq %rax, %rax
je 0x67fb
movq 0x28(%r14), %rcx
testq %rcx, %rcx
je 0x67ff
movq 0x18(%rbx), %rdx
movq 0x18(%rcx), %rsi
movq 0x20(%rcx), %rdi
movq 0x28(%rcx), %r8
movq 0x8(%rcx), %r9
movq %r9, 0x48(%rsp)
movq 0x10(%rcx), %rcx
movq %rcx, 0x50(%rsp)
movq 0x8(%rdx), %rcx
movq %rcx, 0x40(%rsp)
movq 0x20(%rdx), %rcx
movq 0x28(%rdx), %r9
movslq 0xf0(%rbx), %r14
movslq 0xf4(%rbx), %r10
movq %r10, 0x20(%rsp)
movq %r8, 0x28(%rsp)
movq %rdi, 0x98(%rsp)
imulq %rdi, %r8
movq %r9, %rdi
movq %rcx, 0x88(%rsp)
imulq %rcx, %rdi
leaq (,%rdi,8), %rcx
movq %rcx, 0x60(%rsp)
imulq 0x18(%rdx), %rdi
movq %r8, 0x68(%rsp)
movq %rsi, 0x78(%rsp)
imulq %rsi, %r8
movq %r8, 0x38(%rsp)
leaq (,%r14,8), %rcx
negq %rcx
movq %rcx, 0xb0(%rsp)
shlq $0x3, %rdi
movq %rdi, 0xf0(%rsp)
movq 0x8(%rax), %rcx
movq %rcx, 0x18(%rsp)
movq 0x18(%rax), %rcx
movq 0x28(%rax), %rbp
movq 0x20(%rax), %rax
imulq %rbp, %rax
movq %rax, %rdx
movq %rcx, %rdi
imulq %rcx, %rdx
shlq $0x3, %rdx
movq %rdx, 0x30(%rsp)
shlq $0x3, %rbp
shlq $0x3, %rax
movq %rax, 0xe8(%rsp)
movq %r9, 0xc0(%rsp)
leaq (,%r9,8), %rax
xorl %edx, %edx
movq %r14, 0xb8(%rsp)
cmpq 0x50(%rsp), %rdx
je 0x67f6
movq 0x38(%rsp), %rcx
movq %rdx, 0x58(%rsp)
imulq %rdx, %rcx
movq 0x48(%rsp), %rdx
leaq (%rdx,%rcx,8), %rcx
movq %rcx, 0x70(%rsp)
movq 0x40(%rsp), %rcx
movq %rcx, 0x8(%rsp)
xorl %edx, %edx
cmpq 0x78(%rsp), %rdx
je 0x67a3
movq 0x68(%rsp), %rcx
movq %rdx, 0x80(%rsp)
imulq %rdx, %rcx
movq 0x70(%rsp), %rdx
leaq (%rdx,%rcx,8), %rcx
movq %rcx, 0x90(%rsp)
xorl %ecx, %ecx
xorl %esi, %esi
xorl %r8d, %r8d
cmpq 0x98(%rsp), %rsi
je 0x6784
movl %r8d, 0x10(%rsp)
movslq %r8d, %r11
movq %rcx, 0xa8(%rsp)
leaq (%rcx,%r11), %r8
imulq %rax, %r8
addq 0x8(%rsp), %r8
movq %rbp, %r9
imulq %r11, %r9
movq %rsi, %rcx
imulq 0x20(%rsp), %rcx
addq 0x88(%rsp), %rcx
movq 0x28(%rsp), %rdx
movq %rsi, 0xa0(%rsp)
imulq %rsi, %rdx
movq 0x90(%rsp), %rsi
leaq (%rsi,%rdx,8), %rdx
movq %rdx, 0xc8(%rsp)
addq 0x18(%rsp), %r9
movq %r9, 0xd0(%rsp)
xorl %r9d, %r9d
xorl %edx, %edx
cmpq 0x28(%rsp), %r9
je 0x6759
movl %edx, 0x14(%rsp)
movslq %edx, %rdx
movq %r8, 0xe0(%rsp)
leaq (%r8,%rdx,8), %r10
movq 0xd0(%rsp), %rsi
leaq (%rsi,%rdx,8), %r15
movq %r9, 0xd8(%rsp)
imulq %r14, %r9
addq 0xc0(%rsp), %r9
xorpd %xmm0, %xmm0
xorl %r14d, %r14d
cmpq %rdi, %r14
je 0x671c
movq %r15, %r8
movq %r10, %rsi
movq %r11, %r13
cmpq %r13, %rcx
jbe 0x6707
xorl %r12d, %r12d
leaq (%rdx,%r12), %rbx
cmpq %rbx, %r9
jbe 0x66fc
movsd (%rsi,%r12,8), %xmm1
mulsd (%r8,%r12,8), %xmm1
addsd %xmm1, %xmm0
incq %r12
jmp 0x66de
incq %r13
addq %rax, %rsi
addq %rbp, %r8
jmp 0x66d6
incq %r14
addq 0xf0(%rsp), %r10
addq 0xe8(%rsp), %r15
jmp 0x66c8
movq 0xc8(%rsp), %rdx
movq 0xd8(%rsp), %r9
movsd %xmm0, (%rdx,%r9,8)
incq %r9
movq 0xb8(%rsp), %r14
movl 0x14(%rsp), %edx
addl %r14d, %edx
movq 0xe0(%rsp), %r8
addq 0xb0(%rsp), %r8
jmp 0x6683
movq 0xa0(%rsp), %rsi
incq %rsi
movq 0x20(%rsp), %rcx
movl 0x10(%rsp), %r8d
addl %ecx, %r8d
movq 0xa8(%rsp), %rdx
subq %rcx, %rdx
movq %rdx, %rcx
jmp 0x6609
movq 0x80(%rsp), %rdx
incq %rdx
movq 0x8(%rsp), %rcx
addq 0x60(%rsp), %rcx
movq %rcx, 0x8(%rsp)
jmp 0x65d5
movq 0x58(%rsp), %rdx
incq %rdx
movq 0x30(%rsp), %rcx
addq %rcx, 0x18(%rsp)
jmp 0x65a2
pushq $0x66
popq %rcx
leaq 0x881(%rip), %rdi       # 0x7045
leaq 0x10b2(%rip), %rsi      # 0x787d
leaq 0x10c6(%rip), %rdx      # 0x7898
xorl %eax, %eax
callq 0x1030
pushq $-0x1
popq %r12
movl %r12d, %eax
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x6c
jmp 0x67bc
xorl %r12d, %r12d
jmp 0x67dd
pushq $0x6e
jmp 0x67bc
pushq $0x70
jmp 0x67bc