// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xd0, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
cmpq $0x0, -0x18(%rbp)
jne 0xa407
leaq 0xc34(%rip), %rdi       # 0xb015
leaq 0x1475(%rip), %rsi      # 0xb85d
leaq 0x1489(%rip), %rdx      # 0xb878
movl $0x66, %ecx
movb $0x0, %al
callq 0x1030
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xa8ab
movq -0x18(%rbp), %rax
movq 0x60(%rax), %rax
movq 0x8(%rax), %rdi
addq $0x4, %rdi
leaq 0xc50(%rip), %rsi       # 0xb06e
callq 0x10f0
cmpl $0x0, %eax
jne 0xa42d
jmp 0xa8a4
movq -0x18(%rbp), %rax
movq 0x60(%rax), %rax
movq 0x8(%rax), %rdi
addq $0x4, %rdi
leaq 0xc08(%rip), %rsi       # 0xb04c
callq 0x10f0
cmpl $0x0, %eax
jne 0xa8a2
movq -0x18(%rbp), %rax
cmpl $0x0, 0xa0(%rax)
jg 0xa48c
leaq 0xbaf(%rip), %rdi       # 0xb015
leaq 0x13f0(%rip), %rsi      # 0xb85d
leaq 0x1404(%rip), %rdx      # 0xb878
movl $0x6c, %ecx
movb $0x0, %al
callq 0x1030
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xa8ab
movq -0x18(%rbp), %rax
movq 0x60(%rax), %rax
movq 0x28(%rax), %rax
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
jne 0xa4d0
leaq 0xb6b(%rip), %rdi       # 0xb015
leaq 0x13ac(%rip), %rsi      # 0xb85d
leaq 0x13c0(%rip), %rdx      # 0xb878
movl $0x6e, %ecx
movb $0x0, %al
callq 0x1030
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xa8ab
movq -0x18(%rbp), %rax
movq 0x28(%rax), %rax
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0xa510
leaq 0xb2b(%rip), %rdi       # 0xb015
leaq 0x136c(%rip), %rsi      # 0xb85d
leaq 0x1380(%rip), %rdx      # 0xb878
movl $0x70, %ecx
movb $0x0, %al
callq 0x1030
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xa8ab
movq -0x18(%rbp), %rax
movq 0x60(%rax), %rax
movq 0x18(%rax), %rax
movq %rax, -0x30(%rbp)
movq -0x20(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0xd0(%rbp)
movq -0x20(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0xc8(%rbp)
movq -0x20(%rbp), %rax
movq 0x28(%rax), %rax
movq %rax, -0xc0(%rbp)
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x28(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0xb8(%rbp)
movq -0x28(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0xb0(%rbp)
movq -0x28(%rbp), %rax
movq 0x28(%rax), %rax
movq %rax, -0xa8(%rbp)
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x40(%rbp)
movq -0x30(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0xa0(%rbp)
movq -0x30(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x98(%rbp)
movq -0x30(%rbp), %rax
movq 0x28(%rax), %rax
movq %rax, -0x90(%rbp)
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x48(%rbp)
movl $0x0, -0x4c(%rbp)
movl $0x0, -0x50(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x58(%rbp)
movl $0x0, -0x5c(%rbp)
movl $0x0, -0x60(%rbp)
movq -0x18(%rbp), %rax
movq 0x60(%rax), %rax
movl 0xf0(%rax), %eax
movl %eax, -0x64(%rbp)
movq -0x18(%rbp), %rax
movq 0x60(%rax), %rax
movl 0xf4(%rax), %eax
movl %eax, -0x68(%rbp)
movl $0x0, -0x6c(%rbp)
movslq -0x6c(%rbp), %rax
movq -0x28(%rbp), %rcx
cmpq 0x10(%rcx), %rax
jae 0xa8a0
movl $0x0, -0x70(%rbp)
movslq -0x70(%rbp), %rax
movq -0x28(%rbp), %rcx
cmpq 0x18(%rcx), %rax
jae 0xa890
movl $0x0, -0x74(%rbp)
movslq -0x74(%rbp), %rax
movq -0x28(%rbp), %rcx
cmpq 0x20(%rcx), %rax
jae 0xa880
movl $0x0, -0x78(%rbp)
movslq -0x78(%rbp), %rax
movq -0x28(%rbp), %rcx
cmpq 0x28(%rcx), %rax
jae 0xa870
xorps %xmm0, %xmm0
movsd %xmm0, -0x58(%rbp)
movl -0x74(%rbp), %eax
imull -0x68(%rbp), %eax
movl %eax, -0x4c(%rbp)
movl -0x78(%rbp), %eax
imull -0x64(%rbp), %eax
movl %eax, -0x50(%rbp)
movl $0x0, -0x7c(%rbp)
movslq -0x7c(%rbp), %rax
movq -0x20(%rbp), %rcx
cmpq 0x18(%rcx), %rax
jae 0xa802
movl -0x4c(%rbp), %eax
movl %eax, -0x80(%rbp)
movslq -0x80(%rbp), %rax
movslq -0x4c(%rbp), %rcx
movq -0x30(%rbp), %rdx
addq 0x20(%rdx), %rcx
cmpq %rcx, %rax
jae 0xa7f2
movl -0x50(%rbp), %eax
movl %eax, -0x84(%rbp)
movslq -0x84(%rbp), %rax
movslq -0x50(%rbp), %rcx
movq -0x30(%rbp), %rdx
addq 0x28(%rdx), %rcx
cmpq %rcx, %rax
jae 0xa7e2
movq -0xc0(%rbp), %rdx
movq -0xc8(%rbp), %rsi
movq -0xd0(%rbp), %rdi
movq -0x90(%rbp), %r8
movq -0x98(%rbp), %r9
movq -0xa0(%rbp), %r10
movl -0x80(%rbp), %eax
subl -0x4c(%rbp), %eax
movl %eax, -0x5c(%rbp)
movl -0x84(%rbp), %eax
subl -0x50(%rbp), %eax
movl %eax, -0x60(%rbp)
movq -0x48(%rbp), %rax
movslq -0x7c(%rbp), %rcx
imulq %r9, %r10
imulq %r8, %r10
imulq %r10, %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movslq -0x70(%rbp), %rcx
imulq %r8, %r9
imulq %r9, %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movslq -0x5c(%rbp), %rcx
imulq %r8, %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movslq -0x60(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq -0x38(%rbp), %rax
movslq -0x6c(%rbp), %rcx
imulq %rsi, %rdi
imulq %rdx, %rdi
imulq %rdi, %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movslq -0x7c(%rbp), %rcx
imulq %rdx, %rsi
imulq %rsi, %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movslq -0x80(%rbp), %rcx
imulq %rdx, %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movslq -0x84(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm2
movsd -0x58(%rbp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x58(%rbp)
movl -0x84(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x84(%rbp)
jmp 0xa6d2
jmp 0xa7e4
movl -0x80(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x80(%rbp)
jmp 0xa6b0
jmp 0xa7f4
movl -0x7c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x7c(%rbp)
jmp 0xa698
movq -0xa8(%rbp), %rdx
movq -0xb0(%rbp), %rsi
movq -0xb8(%rbp), %rdi
movsd -0x58(%rbp), %xmm0
movq -0x40(%rbp), %rax
movslq -0x6c(%rbp), %rcx
imulq %rsi, %rdi
imulq %rdx, %rdi
imulq %rdi, %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movslq -0x70(%rbp), %rcx
imulq %rdx, %rsi
imulq %rsi, %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movslq -0x74(%rbp), %rcx
imulq %rdx, %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movslq -0x78(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl -0x78(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x78(%rbp)
jmp 0xa663
jmp 0xa872
movl -0x74(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x74(%rbp)
jmp 0xa64a
jmp 0xa882
movl -0x70(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x70(%rbp)
jmp 0xa631
jmp 0xa892
movl -0x6c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x6c(%rbp)
jmp 0xa618
jmp 0xa8a2
jmp 0xa8a4
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xd0, %rsp
popq %rbp
retq