// File: /workspace/github_cpp_makefile_star2_O3/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 0x181b8
movq 0x128(%rbx), %r15
movq %rbx, %rdi
xorl %esi, %esi
callq 0x15092
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 0x20eee
leaq 0xa8(%rsp), %rdi
callq 0x29bb0
mulss 0x250f2(%rip), %xmm0    # 0x39d88
movss %xmm0, 0xd0(%rbx)
leaq 0xa8(%rsp), %rdi
callq 0x1840e
leaq 0x8(%rsp), %rdi
callq 0x181b8
movq 0x130(%rbx), %r15
movq %rbx, %rdi
xorl %esi, %esi
callq 0x15092
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 0x2128a
movq 0x40(%rbx), %rax
cmpq %rax, 0x48(%rbx)
je 0x14e85
movq (%rax), %rsi
leaq 0x80(%rsp), %rdi
callq 0x194a6
leaq 0x30(%rbx), %r15
leaq 0x30(%rsp), %rdi
leaq 0x80(%rsp), %rdx
leaq 0x8(%rsp), %rcx
movq %r15, %rsi
callq 0x2eed4
leaq 0x30(%rsp), %rdx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x15242
leaq 0x30(%rsp), %rdi
callq 0x1840e
leaq 0x80(%rsp), %rdi
callq 0x1840e
cmpl $0x3, 0x28(%r14)
jne 0x14d9f
leaq 0xa8(%rsp), %rdi
callq 0x181b8
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 0x151aa
leaq 0xa8(%rsp), %rsi
movq %rax, %rdi
callq 0x2128a
leaq 0xa8(%rsp), %rdi
callq 0x1840e
leaq 0x100(%rbx), %rsi
leaq 0xa8(%rsp), %rdi
callq 0x194a6
leaq 0x30(%rsp), %rdi
leaq 0x8(%rsp), %rdx
leaq 0xa8(%rsp), %rcx
movq %r15, %rsi
callq 0x2eed4
leaq 0xa8(%rsp), %rdi
callq 0x1840e
cmpq $0x0, 0x18(%rbx)
je 0x14ed2
cmpq $0x0, 0x20(%rbx)
je 0x14ed2
movq 0x78(%rbx), %rax
movq (%rax), %rdi
leaq 0x30(%rsp), %rsi
callq 0x1fe8e
movq 0x90(%rbx), %rdi
leaq 0x8(%rsp), %rsi
callq 0x1fe8e
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x15360
movq %rax, %r14
cmpq $0x0, 0x18(%rbx)
je 0x14f1b
cmpq $0x0, 0x20(%rbx)
je 0x14f1b
movq 0x98(%rbx), %rax
movq (%rax), %rdi
movq %r14, %rsi
callq 0x20394
movq 0xb0(%rbx), %rdi
leaq 0x8(%rsp), %rsi
callq 0x20394
leaq 0x30(%rsp), %rdi
callq 0x1840e
leaq 0x8(%rsp), %rdi
callq 0x1840e
leaq 0x58(%rsp), %rdi
callq 0x1840e
addq $0x230, %rsp            # imm = 0x230
popq %rbx
popq %r14
popq %r15
retq
leaq 0x24fac(%rip), %rsi     # 0x39e38
leaq 0xa8(%rsp), %rdi
movl $0x56, %edx
movl $0x3, %ecx
callq 0x37222
leaq 0x25010(%rip), %rsi     # 0x39eba
leaq 0xa8(%rsp), %rdi
movl $0x24, %edx
callq 0x8350
leaq 0xa8(%rsp), %rdi
callq 0x3744a
movq 0x40(%rbx), %rax
jmp 0x14cf4
leaq 0x24f5f(%rip), %rsi     # 0x39e38
leaq 0xa8(%rsp), %rdi
movl $0x62, %edx
movl $0x3, %ecx
callq 0x37222
leaq 0x24fe8(%rip), %rsi     # 0x39edf
leaq 0xa8(%rsp), %rdi
movl $0x54, %edx
callq 0x8350
leaq 0xa8(%rsp), %rdi
callq 0x3744a
jmp 0x14df0
leaq 0x24f16(%rip), %rsi     # 0x39e38
leaq 0xa8(%rsp), %rdi
movl $0x6f, %edx
movl $0x3, %ecx
callq 0x37222
leaq 0x24f9f(%rip), %rsi     # 0x39edf
leaq 0xa8(%rsp), %rdi
movl $0x54, %edx
callq 0x8350
leaq 0xa8(%rsp), %rdi
callq 0x3744a
jmp 0x14e37
jmp 0x14f66
movq %rax, %rbx
leaq 0xa8(%rsp), %rdi
callq 0x3744a
jmp 0x14fd9
movq %rax, %rbx
leaq 0xa8(%rsp), %rdi
callq 0x3744a
jmp 0x14fe3
jmp 0x14fd1
jmp 0x14fbf
jmp 0x14fd1
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x1840e
jmp 0x14fa2
movq %rax, %rbx
leaq 0x80(%rsp), %rdi
jmp 0x14fde
jmp 0x14fcc
movq %rax, %rbx
leaq 0xa8(%rsp), %rdi
jmp 0x14fe8
jmp 0x14fcc
jmp 0x14fd1
movq %rax, %rbx
leaq 0xa8(%rsp), %rdi
jmp 0x14fde
movq %rax, %rbx
jmp 0x14fed
movq %rax, %rbx
jmp 0x14fe3
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x1840e
leaq 0x8(%rsp), %rdi
callq 0x1840e
leaq 0x58(%rsp), %rdi
callq 0x1840e
movq %rbx, %rdi
callq 0x8500
nop