// File: 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:
subq $0x1c8, %rsp            # imm = 0x1C8
movq %rdi, 0x1c0(%rsp)
movq %rsi, 0x1b8(%rsp)
movq %rdx, 0x1b0(%rsp)
movq 0x1c0(%rsp), %rax
movq %rax, 0x48(%rsp)
leaq 0x188(%rsp), %rdi
callq 0x30500
movq 0x48(%rsp), %rdi
movq 0x128(%rdi), %rax
movq %rax, 0x50(%rsp)
xorl %eax, %eax
movl %eax, %esi
callq 0x29aa0
movq %rax, 0x58(%rsp)
jmp 0x294fa
movq 0x58(%rsp), %rdx
movq 0x50(%rsp), %rdi
movq 0x48(%rsp), %rcx
addq $0xd8, %rcx
movq (%rdi), %rax
movq 0x10(%rax), %rax
leaq 0x188(%rsp), %rsi
callq *%rax
jmp 0x29523
leaq 0x150(%rsp), %rdi
leaq 0x188(%rsp), %rdx
movq %rdx, %rsi
callq 0x40da0
jmp 0x2953d
leaq 0x150(%rsp), %rdi
callq 0x5a880
movss %xmm0, 0x44(%rsp)
jmp 0x29552
movq 0x48(%rsp), %rax
movss 0x44(%rsp), %xmm0
movss 0x637ff(%rip), %xmm1    # 0x8cd64
mulss %xmm1, %xmm0
movss %xmm0, 0xd0(%rax)
leaq 0x150(%rsp), %rdi
callq 0x30940
leaq 0x128(%rsp), %rdi
callq 0x30500
jmp 0x2958d
movq 0x48(%rsp), %rdi
movq 0x130(%rdi), %rax
movq %rax, 0x30(%rsp)
xorl %eax, %eax
movl %eax, %esi
callq 0x29aa0
movq %rax, 0x38(%rsp)
jmp 0x295ae
movq 0x38(%rsp), %rdx
movq 0x30(%rsp), %rdi
movq (%rdi), %rax
movq 0x18(%rax), %rax
leaq 0x128(%rsp), %rsi
callq *%rax
jmp 0x295cb
leaq 0x128(%rsp), %rdi
leaq 0x188(%rsp), %rsi
callq 0x413b0
jmp 0x295e2
movq 0x48(%rsp), %rdi
movq %rdi, %rax
addq $0x30, %rax
movq %rax, 0x20(%rsp)
xorl %eax, %eax
movl %eax, %edx
movq %rdx, %rsi
callq 0x29e50
movq %rax, 0x28(%rsp)
jmp 0x29606
movq 0x28(%rsp), %rsi
leaq 0xd8(%rsp), %rdi
callq 0x321b0
jmp 0x2961a
movq 0x20(%rsp), %rsi
leaq 0x100(%rsp), %rdi
leaq 0xd8(%rsp), %rdx
leaq 0x128(%rsp), %rcx
callq 0x66c60
jmp 0x2963e
movq 0x48(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
leaq 0x100(%rsp), %rdx
callq 0x29d50
jmp 0x29656
leaq 0x100(%rsp), %rdi
callq 0x30940
leaq 0xd8(%rsp), %rdi
callq 0x30940
movq 0x1b8(%rsp), %rdi
callq 0x29c60
movl %eax, 0x1c(%rsp)
jmp 0x29683
movl 0x1c(%rsp), %eax
cmpl $0x3, %eax
jne 0x297c7
leaq 0xb0(%rsp), %rdi
callq 0x30500
jmp 0x2969f
movq 0x48(%rsp), %rdx
movq 0x130(%rdx), %rdi
addq $0x100, %rdx            # imm = 0x100
movq (%rdi), %rax
movq 0x18(%rax), %rax
leaq 0xb0(%rsp), %rsi
callq *%rax
jmp 0x296c5
movq 0x48(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x29c70
movq %rax, 0x10(%rsp)
jmp 0x296da
movq 0x10(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
callq 0x413b0
jmp 0x296ee
leaq 0xb0(%rsp), %rdi
callq 0x30940
jmp 0x297c7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x180(%rsp)
movl %eax, 0x17c(%rsp)
jmp 0x29912
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x180(%rsp)
movl %eax, 0x17c(%rsp)
leaq 0x150(%rsp), %rdi
callq 0x30940
jmp 0x29912
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x180(%rsp)
movl %eax, 0x17c(%rsp)
jmp 0x29905
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x180(%rsp)
movl %eax, 0x17c(%rsp)
jmp 0x2978f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x180(%rsp)
movl %eax, 0x17c(%rsp)
leaq 0x100(%rsp), %rdi
callq 0x30940
leaq 0xd8(%rsp), %rdi
callq 0x30940
jmp 0x29905
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x180(%rsp)
movl %eax, 0x17c(%rsp)
leaq 0xb0(%rsp), %rdi
callq 0x30940
jmp 0x29905
movq 0x48(%rsp), %rsi
movq %rsi, %rax
addq $0x30, %rax
movq %rax, 0x8(%rsp)
addq $0x100, %rsi            # imm = 0x100
leaq 0x60(%rsp), %rdi
callq 0x321b0
jmp 0x297eb
movq 0x8(%rsp), %rsi
leaq 0x88(%rsp), %rdi
leaq 0x128(%rsp), %rdx
leaq 0x60(%rsp), %rcx
callq 0x66c60
jmp 0x2980c
leaq 0x60(%rsp), %rdi
callq 0x30940
movq 0x48(%rsp), %rdi
xorl %eax, %eax
movl %eax, %edx
leaq 0x88(%rsp), %rcx
movq %rdx, %rsi
callq 0x2a1e0
jmp 0x29831
movq 0x48(%rsp), %rax
movq 0x90(%rax), %rdi
leaq 0x128(%rsp), %rsi
callq 0x3f350
jmp 0x2984c
movq 0x48(%rsp), %rdi
xorl %eax, %eax
movl %eax, %edx
movq %rdx, %rsi
callq 0x2a530
movq %rax, (%rsp)
jmp 0x29863
movq (%rsp), %rcx
movq 0x48(%rsp), %rdi
xorl %eax, %eax
movl %eax, %edx
movq %rdx, %rsi
callq 0x2a2f0
jmp 0x2987a
movq 0x48(%rsp), %rax
movq 0xb0(%rax), %rdi
leaq 0x128(%rsp), %rsi
callq 0x3fc60
jmp 0x29895
leaq 0x88(%rsp), %rdi
callq 0x30940
leaq 0x128(%rsp), %rdi
callq 0x30940
leaq 0x188(%rsp), %rdi
callq 0x30940
addq $0x1c8, %rsp            # imm = 0x1C8
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x180(%rsp)
movl %eax, 0x17c(%rsp)
leaq 0x60(%rsp), %rdi
callq 0x30940
jmp 0x29905
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x180(%rsp)
movl %eax, 0x17c(%rsp)
leaq 0x88(%rsp), %rdi
callq 0x30940
leaq 0x128(%rsp), %rdi
callq 0x30940
leaq 0x188(%rsp), %rdi
callq 0x30940
movq 0x180(%rsp), %rdi
callq 0x95d0
nopl (%rax)