// File: /workspace/github_c_makefile_star2_O1/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 $0x108, %rsp            # imm = 0x108
testq %rsi, %rsi
je 0x71ca
movq %rsi, %r14
movq 0x60(%rsi), %r12
movq 0x8(%r12), %r15
addq $0x4, %r15
leaq 0x12ad(%rip), %rsi      # 0x809e
movq %r15, %rdi
callq 0x10f0
xorl %ebx, %ebx
testl %eax, %eax
je 0x71f0
leaq 0x1272(%rip), %rsi      # 0x807c
movq %r15, %rdi
callq 0x10f0
testl %eax, %eax
jne 0x71f0
cmpl $0x0, 0xa0(%r14)
jle 0x7204
movq 0x28(%r12), %rax
testq %rax, %rax
je 0x720b
movq 0x28(%r14), %rdx
testq %rdx, %rdx
je 0x7212
movq 0x10(%rdx), %rcx
movq %rcx, 0x90(%rsp)
testq %rcx, %rcx
je 0x71f0
movq 0x18(%r12), %rcx
movq 0x8(%rax), %rsi
movq %rsi, 0x40(%rsp)
movq 0x18(%rdx), %rsi
movq 0x20(%rdx), %rdi
movq 0x28(%rdx), %r8
movq 0x8(%rdx), %rdx
movq %rdx, 0x80(%rsp)
movq 0x8(%rcx), %rdx
movq %rdx, 0x78(%rsp)
movq 0x20(%rcx), %rdx
movq 0x28(%rcx), %r9
movslq 0xf0(%r12), %r10
movslq 0xf4(%r12), %r11
movq %r11, 0x50(%rsp)
movq %r8, 0x28(%rsp)
movq %rdi, 0x48(%rsp)
imulq %rdi, %r8
movq %r9, %rdi
movq %rdx, 0xb0(%rsp)
imulq %rdx, %rdi
leaq (,%rdi,8), %rdx
movq %rdx, 0xa8(%rsp)
imulq 0x18(%rcx), %rdi
movq %r8, 0x98(%rsp)
movq %rsi, 0x38(%rsp)
imulq %rsi, %r8
movq %r8, 0x70(%rsp)
movq 0x18(%rax), %rcx
movq 0x28(%rax), %r8
movq 0x20(%rax), %rax
imulq %r8, %rax
movq %rax, %rdx
movq %rcx, 0x68(%rsp)
imulq %rcx, %rdx
shlq $0x3, %rdx
movq %rdx, 0x88(%rsp)
shlq $0x3, %r8
shlq $0x3, %rax
movq %rax, 0xf0(%rsp)
movq %r10, 0x60(%rsp)
leaq (,%r10,8), %rax
negq %rax
movq %rax, 0xd0(%rsp)
shlq $0x3, %rdi
movq %rdi, 0xf8(%rsp)
movq %r9, 0xc0(%rsp)
leaq (,%r9,8), %rdx
movq $0x0, 0x8(%rsp)
cmpq $0x0, 0x38(%rsp)
je 0x719e
movq 0x70(%rsp), %rax
imulq 0x8(%rsp), %rax
movq 0x80(%rsp), %rcx
leaq (%rcx,%rax,8), %rax
movq %rax, 0xa0(%rsp)
movq 0x78(%rsp), %rax
movq %rax, 0x18(%rsp)
movq $0x0, 0x10(%rsp)
cmpq $0x0, 0x48(%rsp)
je 0x7174
movq 0x98(%rsp), %rax
imulq 0x10(%rsp), %rax
movq 0xa0(%rsp), %rcx
leaq (%rcx,%rax,8), %rax
movq %rax, 0xb8(%rsp)
movq $0x0, 0x58(%rsp)
movq $0x0, 0x20(%rsp)
movl $0x0, 0x4(%rsp)
cmpq $0x0, 0x28(%rsp)
je 0x714e
movslq 0x4(%rsp), %rcx
movq %r8, %rsi
imulq %rcx, %rsi
movq 0x58(%rsp), %rax
movq %rcx, 0x100(%rsp)
leaq (%rax,%rcx), %rdi
imulq %rdx, %rdi
addq 0x18(%rsp), %rdi
movq 0x20(%rsp), %rcx
movq %rcx, %r10
imulq 0x50(%rsp), %r10
movq 0xb0(%rsp), %rax
leaq (%rax,%r10), %rbp
imulq 0x28(%rsp), %rcx
movq 0xb8(%rsp), %rax
leaq (%rax,%rcx,8), %rax
movq %rax, 0xd8(%rsp)
addq 0x40(%rsp), %rsi
movq %rsi, 0xc8(%rsp)
xorl %r11d, %r11d
xorl %eax, %eax
cmpq $0x0, 0x68(%rsp)
movq %rdi, 0xe8(%rsp)
movq %r11, 0xe0(%rsp)
movl %eax, 0x34(%rsp)
je 0x710e
imulq 0x60(%rsp), %r11
movslq %eax, %rbx
movq 0xc8(%rsp), %rax
leaq (%rax,%rbx,8), %r13
leaq (%rdi,%rbx,8), %r12
movq 0xc0(%rsp), %rax
leaq (%rax,%r11), %r15
xorpd %xmm0, %xmm0
xorl %edi, %edi
movq %r12, %rcx
movq %r13, %r14
movq 0x100(%rsp), %rsi
cmpq %r10, %rbp
jbe 0x70f2
cmpq %r11, %r15
jbe 0x70e4
xorl %r9d, %r9d
movsd (%rcx,%r9,8), %xmm1
mulsd (%r14,%r9,8), %xmm1
addsd %xmm1, %xmm0
leaq (%rbx,%r9), %rax
incq %rax
incq %r9
cmpq %rax, %r15
ja 0x70c5
incq %rsi
addq %r8, %r14
addq %rdx, %rcx
cmpq %rsi, %rbp
ja 0x70bd
incq %rdi
addq 0xf0(%rsp), %r13
addq 0xf8(%rsp), %r12
cmpq 0x68(%rsp), %rdi
jne 0x70aa
jmp 0x7112
xorpd %xmm0, %xmm0
movq 0xd8(%rsp), %rax
movq 0xe0(%rsp), %r11
movsd %xmm0, (%rax,%r11,8)
incq %r11
movl 0x34(%rsp), %eax
addl 0x60(%rsp), %eax
movq 0xe8(%rsp), %rdi
addq 0xd0(%rsp), %rdi
cmpq 0x28(%rsp), %r11
jne 0x705f
movq 0x20(%rsp), %rcx
incq %rcx
movq 0x50(%rsp), %rax
addl %eax, 0x4(%rsp)
subq %rax, 0x58(%rsp)
movq %rcx, 0x20(%rsp)
cmpq 0x48(%rsp), %rcx
jne 0x6fe7
movq 0x10(%rsp), %rcx
incq %rcx
movq 0x18(%rsp), %rax
addq 0xa8(%rsp), %rax
movq %rax, 0x18(%rsp)
movq %rcx, 0x10(%rsp)
cmpq 0x38(%rsp), %rcx
jne 0x6f9f
movq 0x8(%rsp), %rcx
incq %rcx
movq 0x88(%rsp), %rax
addq %rax, 0x40(%rsp)
xorl %ebx, %ebx
movq %rcx, 0x8(%rsp)
cmpq 0x90(%rsp), %rcx
jne 0x6f61
jmp 0x71f0
movl $0x66, %ecx
leaq 0xe6f(%rip), %rdi       # 0x8045
leaq 0x16b3(%rip), %rsi      # 0x8890
leaq 0x16c7(%rip), %rdx      # 0x88ab
xorl %eax, %eax
callq 0x1030
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movl %ebx, %eax
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x6c, %ecx
jmp 0x71cf
movl $0x6e, %ecx
jmp 0x71cf
movl $0x70, %ecx
jmp 0x71cf