// File: /workspace/github_c_makefile_star2_O1/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 0x628d
cmpl $0x0, 0xa0(%rsi)
jle 0x6294
movq 0x60(%rsi), %rcx
movq 0x18(%rcx), %rax
testq %rax, %rax
je 0x629b
movq 0x28(%rcx), %r8
testq %r8, %r8
je 0x62a2
movq 0x28(%rsi), %r9
testq %r9, %r9
je 0x62a9
movq %rsi, 0x20(%rsp)
movq 0x18(%r9), %rcx
movq 0x20(%r9), %rdx
movq %rdx, 0x8(%rsp)
movq 0x28(%r9), %rdx
movq 0x8(%r9), %rdi
movq 0x10(%r9), %rsi
movq %rsi, 0x30(%rsp)
testq %rsi, %rsi
je 0x615a
movq 0x8(%rax), %rsi
movq %rsi, 0x28(%rsp)
movq 0x8(%r8), %rsi
movq 0x18(%r8), %r13
movq 0x20(%r8), %rbx
movq 0x28(%rax), %r14
movq %rdx, %r15
imulq 0x8(%rsp), %r15
movq %r15, %r9
imulq 0x28(%r8), %rbx
imulq %rcx, %r9
movq %r9, 0x10(%rsp)
movq %rbx, %r8
imulq %r13, %r8
shlq $0x3, %r8
movq %r8, 0x18(%rsp)
shlq $0x3, %rbx
imulq 0x20(%rax), %r14
leaq (,%r14,8), %rbp
imulq 0x18(%rax), %r14
shlq $0x3, %r14
movq $0x0, (%rsp)
testq %rcx, %rcx
je 0x6143
movq 0x10(%rsp), %rax
imulq (%rsp), %rax
leaq (%rdi,%rax,8), %r9
movq 0x28(%rsp), %rax
xorl %r8d, %r8d
xorpd %xmm0, %xmm0
testq %r13, %r13
je 0x612b
movq %r13, %r11
movq %rax, %r12
movq %rsi, %r10
movsd (%r12), %xmm1
mulsd (%r10), %xmm1
addsd %xmm1, %xmm0
addq %rbx, %r10
addq %r14, %r12
decq %r11
jne 0x6111
movq %r15, %r10
imulq %r8, %r10
movsd %xmm0, (%r9,%r10,8)
incq %r8
addq %rbp, %rax
cmpq %rcx, %r8
jne 0x60ff
movq (%rsp), %r8
incq %r8
addq 0x18(%rsp), %rsi
movq %r8, (%rsp)
cmpq 0x30(%rsp), %r8
jne 0x60e4
movq 0x20(%rsp), %rax
movq 0xa8(%rax), %rsi
movq 0x28(%rsi), %rax
testq %rax, %rax
je 0x62b0
movq 0x10(%rsi), %rsi
testq %rsi, %rsi
je 0x62b7
movq 0x10(%rsi), %r8
movq %r8, 0x20(%rsp)
testq %r8, %r8
je 0x62ec
movq 0x8(%rax), %r8
movq %r8, 0x10(%rsp)
movq 0x28(%rax), %r10
movq %rsi, 0x18(%rsp)
movq 0x18(%rsi), %rsi
movq %rsi, (%rsp)
imulq 0x8(%rsp), %rdx
imulq %rdx, %rcx
shlq $0x3, %rcx
shlq $0x3, %rdx
movq 0x20(%rax), %rsi
imulq %r10, %rsi
movq 0x18(%rax), %rax
imulq %rsi, %rax
shlq $0x3, %rax
movq %rax, 0x8(%rsp)
shlq $0x3, %rsi
movq %rsi, 0x30(%rsp)
shlq $0x3, %r10
xorl %esi, %esi
movq %rsi, 0x28(%rsp)
cmpq $0x0, (%rsp)
je 0x6269
movq 0x18(%rsp), %rax
movq 0x28(%rax), %rax
xorl %r12d, %r12d
movq 0x10(%rsp), %rbx
xorl %ebp, %ebp
testq %rax, %rax
je 0x625b
movq 0x18(%rsp), %rsi
movq 0x20(%rsi), %r14
movq %r14, %r8
negq %r8
movq %rbx, %r9
xorl %r15d, %r15d
testq %r14, %r14
je 0x624f
movslq %ebp, %rsi
imulq %rdx, %rsi
addq %rdi, %rsi
movq %r9, %r13
xorl %r11d, %r11d
movsd (%rsi), %xmm0
movsd %xmm0, (%r13)
addq %rdx, %rsi
decq %r11
addq %r10, %r13
cmpq %r11, %r8
jne 0x6234
subl %r11d, %ebp
incq %r15
addq $0x8, %r9
cmpq %rax, %r15
jne 0x621f
incq %r12
addq 0x30(%rsp), %rbx
cmpq (%rsp), %r12
jne 0x6205
movq 0x28(%rsp), %rsi
incq %rsi
addq %rcx, %rdi
movq 0x8(%rsp), %rax
addq %rax, 0x10(%rsp)
xorl %eax, %eax
cmpq 0x20(%rsp), %rsi
jne 0x61e6
jmp 0x62dd
movl $0x5a, %ecx
jmp 0x62bc
movl $0x5b, %ecx
jmp 0x62bc
movl $0x5d, %ecx
jmp 0x62bc
movl $0x60, %ecx
jmp 0x62bc
movl $0x63, %ecx
jmp 0x62bc
movl $0x7b, %ecx
jmp 0x62bc
movl $0x7c, %ecx
leaq 0x1d82(%rip), %rdi      # 0x8045
leaq 0x2533(%rip), %rsi      # 0x87fd
leaq 0x255e(%rip), %rdx      # 0x882f
xorl %eax, %eax
callq 0x1030
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %eax, %eax
jmp 0x62dd