// File: /workspace/github_cpp_makefile_star2_O1/btbujiangjun[P]abcdl/src/cnn/Layer.cpp
// Function: abcdl::cnn::OutputLayer::backward(abcdl::cnn::Layer*, abcdl::cnn::Layer*)
void OutputLayer::backward(Layer* pre_layer, Layer* back_layer){
    abcdl::algebra::Mat error;
    _cost->delta(error, this->get_activation(0), _y);

    _loss = (error * error).sum() / 2;

    //error * derivate_of_output
    abcdl::algebra::Mat derivative_output;
    _activate_func->derivative(derivative_output, this->get_activation(0));
    derivative_output *= error;

    //calc delta: weight.T * derivate_output
    this->set_delta(0, _helper.dot(this->get_weight(0, 0).Ts(), derivative_output));

    //if pre_layer is ConvolutionLayer, has sigmoid function
    if(pre_layer->get_layer_type() == abcdl::framework::CONVOLUTION){
        abcdl::algebra::Mat mat;
        _activate_func->derivative(mat, _pre_activation_array);
        this->get_delta(0) *= mat;
    }

    //derivate_weight = derivate_output * _pre_activation_array.T
    //derivate_bias = derivate_output
    auto delta_weight = _helper.dot(derivative_output, _pre_activation_array.Ts());
    this->set_delta_weight(0, 0, delta_weight);
    (*this->_delta_bias) = derivative_output;

    this->set_batch_weight(0, 0, this->get_delta_weight(0, 0)); 
    (*this->_batch_bias) += derivative_output;
}

// Assembly:
pushq %r15
pushq %r14
pushq %rbx
subq $0x230, %rsp            # imm = 0x230
movq %rsi, %r14
movq %rdi, %rbx
leaq 0x58(%rsp), %rdi
callq 0x18440
movq 0x128(%rbx), %r15
movq %rbx, %rdi
xorl %esi, %esi
callq 0x15328
leaq 0xd8(%rbx), %rcx
movq (%r15), %r8
leaq 0x58(%rsp), %rsi
movq %r15, %rdi
movq %rax, %rdx
callq *0x10(%r8)
leaq 0xa8(%rsp), %rdi
leaq 0x58(%rsp), %rdx
movq %rdx, %rsi
callq 0x21184
leaq 0xa8(%rsp), %rdi
callq 0x29d20
mulss 0x24e58(%rip), %xmm0    # 0x39d88
movss %xmm0, 0xd0(%rbx)
leaq 0xa8(%rsp), %rdi
callq 0x18696
leaq 0x8(%rsp), %rdi
callq 0x18440
movq 0x130(%rbx), %r15
movq %rbx, %rdi
xorl %esi, %esi
callq 0x15328
movq (%r15), %rcx
leaq 0x8(%rsp), %rsi
movq %r15, %rdi
movq %rax, %rdx
callq *0x18(%rcx)
leaq 0x8(%rsp), %rdi
leaq 0x58(%rsp), %rsi
callq 0x2151a
movq 0x48(%rbx), %rax
cmpq 0x40(%rbx), %rax
je 0x15123
movq 0x40(%rbx), %rax
movq (%rax), %rsi
leaq 0x80(%rsp), %rdi
callq 0x19730
leaq 0x30(%rbx), %r15
leaq 0x30(%rsp), %rdi
leaq 0x80(%rsp), %rdx
leaq 0x8(%rsp), %rcx
movq %r15, %rsi
callq 0x2f0be
leaq 0x30(%rsp), %rdx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x154d2
leaq 0x30(%rsp), %rdi
callq 0x18696
leaq 0x80(%rsp), %rdi
callq 0x18696
cmpl $0x3, 0x28(%r14)
jne 0x1503d
leaq 0xa8(%rsp), %rdi
callq 0x18440
movq 0x130(%rbx), %rdi
leaq 0x100(%rbx), %rdx
movq (%rdi), %rax
leaq 0xa8(%rsp), %rsi
callq *0x18(%rax)
movq %rbx, %rdi
xorl %esi, %esi
callq 0x1543e
leaq 0xa8(%rsp), %rsi
movq %rax, %rdi
callq 0x2151a
leaq 0xa8(%rsp), %rdi
callq 0x18696
leaq 0x100(%rbx), %rsi
leaq 0xa8(%rsp), %rdi
callq 0x19730
leaq 0x30(%rsp), %rdi
leaq 0x8(%rsp), %rdx
leaq 0xa8(%rsp), %rcx
movq %r15, %rsi
callq 0x2f0be
leaq 0xa8(%rsp), %rdi
callq 0x18696
cmpq $0x0, 0x18(%rbx)
je 0x1516c
cmpq $0x0, 0x20(%rbx)
je 0x1516c
movq 0x78(%rbx), %rax
movq (%rax), %rdi
leaq 0x30(%rsp), %rsi
callq 0x20172
movq 0x90(%rbx), %rdi
leaq 0x8(%rsp), %rsi
callq 0x20172
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x155ec
movq %rax, %r14
cmpq $0x0, 0x18(%rbx)
je 0x151b5
cmpq $0x0, 0x20(%rbx)
je 0x151b5
movq 0x98(%rbx), %rax
movq (%rax), %rdi
movq %r14, %rsi
callq 0x20660
movq 0xb0(%rbx), %rdi
leaq 0x8(%rsp), %rsi
callq 0x20660
leaq 0x30(%rsp), %rdi
callq 0x18696
leaq 0x8(%rsp), %rdi
callq 0x18696
leaq 0x58(%rsp), %rdi
callq 0x18696
addq $0x230, %rsp            # imm = 0x230
popq %rbx
popq %r14
popq %r15
retq
leaq 0x24d0e(%rip), %rsi     # 0x39e38
leaq 0xa8(%rsp), %rdi
movl $0x56, %edx
movl $0x3, %ecx
callq 0x373d6
leaq 0x24d72(%rip), %rsi     # 0x39eba
leaq 0xa8(%rsp), %rdi
movl $0x24, %edx
callq 0x8350
leaq 0xa8(%rsp), %rdi
callq 0x375fe
jmp 0x14f8e
leaq 0x24cc5(%rip), %rsi     # 0x39e38
leaq 0xa8(%rsp), %rdi
movl $0x62, %edx
movl $0x3, %ecx
callq 0x373d6
leaq 0x24d4e(%rip), %rsi     # 0x39edf
leaq 0xa8(%rsp), %rdi
movl $0x54, %edx
callq 0x8350
leaq 0xa8(%rsp), %rdi
callq 0x375fe
jmp 0x1508e
leaq 0x24c7c(%rip), %rsi     # 0x39e38
leaq 0xa8(%rsp), %rdi
movl $0x6f, %edx
movl $0x3, %ecx
callq 0x373d6
leaq 0x24d05(%rip), %rsi     # 0x39edf
leaq 0xa8(%rsp), %rdi
movl $0x54, %edx
callq 0x8350
leaq 0xa8(%rsp), %rdi
callq 0x375fe
jmp 0x150d5
jmp 0x15200
movq %rax, %rbx
leaq 0xa8(%rsp), %rdi
callq 0x375fe
jmp 0x15273
movq %rax, %rbx
leaq 0xa8(%rsp), %rdi
callq 0x375fe
jmp 0x1527d
jmp 0x1526b
jmp 0x15259
jmp 0x1526b
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x18696
jmp 0x1523c
movq %rax, %rbx
leaq 0x80(%rsp), %rdi
jmp 0x15278
jmp 0x15266
movq %rax, %rbx
leaq 0xa8(%rsp), %rdi
jmp 0x15282
jmp 0x15266
jmp 0x1526b
movq %rax, %rbx
leaq 0xa8(%rsp), %rdi
jmp 0x15278
movq %rax, %rbx
jmp 0x15287
movq %rax, %rbx
jmp 0x1527d
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x18696
leaq 0x8(%rsp), %rdi
callq 0x18696
leaq 0x58(%rsp), %rdi
callq 0x18696
movq %rbx, %rdi
callq 0x8500
nop