// File: /workspace/github_c_makefile_star2_O2/xtanitfy[P]Murphy/src/layers/linear_layer.c
// Function: LinearLayerBackward
int LinearLayerBackward(NET_PARA_s *pNetPara,LAYER_s *pLayer)
{
	/*���Բ�����һ����չ������Ϊ�������к���������*/
	CHECK_EXPR_RET(pLayer == NULL,-1);
	CHECK_EXPR_RET(pLayer->nNext <= 0,-1);
	BLOB_s *pNextK = pLayer->pNext[0]->k;
	CHECK_EXPR_RET(pNextK == NULL,-1);
	
	BLOB_s *pNextDelta = pLayer->pNext[0]->pDelta;
	CHECK_EXPR_RET(pNextDelta == NULL,-1);
	
	BLOB_s *pDelta = pLayer->pDelta;
	CHECK_EXPR_RET(pDelta == NULL,-1);
	
	BLOB_DATA_PTR(pNextK,ptrNextK);
	BLOB_DATA_PTR(pNextDelta,ptrNextDelta);
	BLOB_DATA_PTR(pDelta,ptrCurrDelta);

	double tmp = 0;
	
	for (int i_batch = 0;i_batch < pDelta->numSize;i_batch++) {
		for (int i_in = 0;i_in < pDelta->channelSize;i_in++) {
			tmp = 0;
			for (int i_out = 0;i_out < pNextDelta->channelSize;i_out++) {
				tmp += ptrNextK[i_out][i_in][0][0] * \
							ptrNextDelta[i_batch][i_out][0][0];
			}
			ptrCurrDelta[i_batch][i_in][0][0] = tmp;
		}
	}
	
	//LOG_writeBlobToTxtFile("linearDelta.txt",pDelta);
	
	/*���Ի�ԭ,������ʱ�����Ƕ����ĺϲ���������ҪLAYER_TYPE_MERGEȥ��*/
	BLOB_s *pPrevDelta = pLayer->pPrev[0]->pDelta;
	BLOB_s *pPrevData = pLayer->pPrev[0]->pData;
	CHECK_EXPR_RET(pPrevDelta == NULL,-1);
	CHECK_EXPR_RET(pPrevData == NULL,-1);
	BLOB_DATA_PTR(pPrevDelta,ptrPrevDelta);
//	BLOB_DATA_PTR(pPrevData,ptrPrevData);

	int cnt = 0;
	
	for (int i_batch = 0;i_batch < pPrevData->numSize;i_batch++) {
		cnt = 0;
		for (int i_in = 0;i_in < pPrevData->channelSize;i_in++) {
			for (int i_w = 0; i_w < pPrevData->widthSize;i_w++) {
				for (int i_h = 0; i_h < pPrevData->heightSize;i_h++) {
					ptrPrevDelta[i_batch][i_in][i_h][i_w] = \
						ptrCurrDelta[i_batch][cnt++][0][0]; 
				}
			}
		}
	}

	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
testq %rsi, %rsi
je 0x5998
cmpl $0x0, 0xa0(%rsi)
jle 0x599f
movq 0x60(%rsi), %rcx
movq 0x18(%rcx), %rax
testq %rax, %rax
je 0x59a6
movq 0x28(%rcx), %rdi
testq %rdi, %rdi
je 0x5aa9
movq 0x28(%rsi), %r9
testq %r9, %r9
je 0x5aad
movq %rsi, 0x8(%rsp)
movq 0x8(%rax), %rcx
movq %rcx, 0x18(%rsp)
movq 0x28(%rax), %r8
movq 0x8(%rdi), %rsi
movq 0x18(%rdi), %r12
movq 0x28(%rdi), %r11
movq 0x18(%r9), %rbx
movq 0x20(%r9), %rcx
movq 0x8(%r9), %rdx
imulq 0x20(%rax), %r8
movq 0x10(%r9), %r10
movq %r10, 0x28(%rsp)
imulq 0x28(%r9), %rcx
movq %rcx, %r9
imulq %rbx, %r9
movq %r9, 0x20(%rsp)
leaq (,%r8,8), %r15
imulq 0x18(%rax), %r8
shlq $0x3, %r8
imulq 0x20(%rdi), %r11
movq %r11, %rax
imulq %r12, %rax
shlq $0x3, %rax
movq %rax, 0x10(%rsp)
shlq $0x3, %r11
xorl %r13d, %r13d
cmpq 0x28(%rsp), %r13
je 0x59ad
movq 0x20(%rsp), %rax
imulq %r13, %rax
leaq (%rdx,%rax,8), %rdi
movq 0x18(%rsp), %r14
xorl %eax, %eax
cmpq %rbx, %rax
je 0x598e
xorpd %xmm0, %xmm0
movq %rsi, %r9
movq %r14, %rbp
movq %r12, %r10
subq $0x1, %r10
jb 0x5979
movsd (%rbp), %xmm1
mulsd (%r9), %xmm1
addsd %xmm1, %xmm0
addq %r8, %rbp
addq %r11, %r9
jmp 0x595d
movq %rcx, %r9
imulq %rax, %r9
movsd %xmm0, (%rdi,%r9,8)
incq %rax
addq %r15, %r14
jmp 0x594b
incq %r13
addq 0x10(%rsp), %rsi
jmp 0x5930
pushq $0x5a
jmp 0x5abb
pushq $0x5b
jmp 0x5abb
pushq $0x5d
jmp 0x5abb
movq 0x8(%rsp), %rax
movq 0xa8(%rax), %rsi
movq 0x28(%rsi), %rax
testq %rax, %rax
je 0x5ab5
movq 0x10(%rsi), %rsi
testq %rsi, %rsi
je 0x5ab9
movq 0x8(%rax), %r11
movq 0x28(%rax), %r9
movq 0x10(%rsi), %rdi
movq %rdi, 0x8(%rsp)
movq 0x20(%rax), %rdi
imulq %r9, %rdi
movq 0x18(%rax), %rax
imulq %rdi, %rax
shlq $0x3, %rax
movq %rax, 0x30(%rsp)
shlq $0x3, %rdi
movq %rdi, 0x28(%rsp)
shlq $0x3, %r9
shlq $0x3, 0x20(%rsp)
shlq $0x3, %rcx
xorl %eax, %eax
cmpq 0x8(%rsp), %rax
je 0x5ab1
movq %rax, 0x10(%rsp)
movq 0x18(%rsi), %r15
xorl %r12d, %r12d
movq %r11, 0x18(%rsp)
xorl %ebp, %ebp
cmpq %r15, %r12
je 0x5a8d
movq 0x28(%rsi), %r10
movq %r11, %r8
xorl %edi, %edi
cmpq %r10, %rdi
je 0x5a83
movslq %ebp, %rbx
xorl %eax, %eax
subq 0x20(%rsi), %rax
imulq %rcx, %rbx
addq %rdx, %rbx
movq %r8, %r13
xorl %r14d, %r14d
cmpq %r14, %rax
je 0x5a77
movsd (%rbx), %xmm0
movsd %xmm0, (%r13)
decq %r14
addq %r9, %r13
addq %rcx, %rbx
jmp 0x5a5d
subl %r14d, %ebp
incq %rdi
addq $0x8, %r8
jmp 0x5a42
incq %r12
addq 0x28(%rsp), %r11
jmp 0x5a34
movq 0x10(%rsp), %rax
incq %rax
movq 0x18(%rsp), %r11
addq 0x30(%rsp), %r11
addq 0x20(%rsp), %rdx
jmp 0x5a16
pushq $0x60
jmp 0x5abb
pushq $0x63
jmp 0x5abb
xorl %eax, %eax
jmp 0x5adb
pushq $0x7b
jmp 0x5abb
pushq $0x7c
popq %rcx
leaq 0x1582(%rip), %rdi      # 0x7045
leaq 0x1d20(%rip), %rsi      # 0x77ea
leaq 0x1d4b(%rip), %rdx      # 0x781c
xorl %eax, %eax
callq 0x1030
pushq $-0x1
popq %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop