// File: btbujiangjun[P]abcdl/src/cnn/Layer.cpp
// Function: abcdl::cnn::ConvolutionLayer::backward(abcdl::cnn::Layer*, abcdl::cnn::Layer*)
void ConvolutionLayer::backward(Layer* pre_layer, Layer* back_layer){
    if(back_layer->get_layer_type() == abcdl::framework::OUTPUT){
        size_t size = this->_rows * this->_cols;
        real*  data = back_layer->get_delta(0).data();
        abcdl::algebra::Mat delta(this->_rows, this->_cols);
        
        //recover multiply mat
        for(size_t i = 0; i != this->_out_channel_size; i++){
            memcpy(delta.data(), &data[i * size], sizeof(real) * size);
            this->set_delta(i, delta);
        }
    }else if(back_layer->get_layer_type() == abcdl::framework::SUBSAMPLING){
        SubSamplingLayer* sub_layer = (SubSamplingLayer*)back_layer;
        size_t scale = sub_layer->get_scale();
        abcdl::algebra::Mat delta;
        abcdl::algebra::Mat back_delta;
        for(size_t i = 0; i != this->_out_channel_size; i++){
            _activate_func->derivative(delta, this->get_activation(i));
            back_delta = back_layer->get_delta(i);
            //subsampling layer reduced matrix dim, so recover it by expand function
            back_delta.expand(scale, scale);
            //back layer error sharing
            back_delta /= scale * scale;
            //delta_l = derivative_sigmoid * delta_l+1(recover dim)
            delta *= back_delta;

            this->set_delta(i, delta);
	    }
    }

    for(size_t i = 0; i != this->_out_channel_size; i++){
        abcdl::algebra::Mat weight;
        for(size_t j = 0; j != pre_layer->get_out_channel_size(); j++){
            _helper.convn(weight, pre_layer->get_activation(j), this->get_delta(i), _stride, abcdl::algebra::VALID);
            this->set_delta_weight(j, i, weight);
            this->set_batch_weight(j, i, weight);
        }

        this->_delta_bias->set_data(this->get_delta(i).sum(), i, 0);
    }
    this->_batch_bias->operator+=(*this->_delta_bias);
}

// Assembly:
subq $0x188, %rsp            # imm = 0x188
movq %rdi, 0x180(%rsp)
movq %rsi, 0x178(%rsp)
movq %rdx, 0x170(%rsp)
movq 0x180(%rsp), %rax
movq %rax, 0x68(%rsp)
movq 0x170(%rsp), %rdi
callq 0x29c60
cmpl $0x4, %eax
jne 0x2894a
movq 0x68(%rsp), %rcx
movq 0x8(%rcx), %rax
imulq 0x10(%rcx), %rax
movq %rax, 0x168(%rsp)
movq 0x170(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x29c70
movq %rax, %rdi
callq 0x26c60
movq %rax, %rcx
movq 0x68(%rsp), %rax
movq %rcx, 0x160(%rsp)
movq 0x8(%rax), %rsi
movq 0x10(%rax), %rdx
leaq 0x138(%rsp), %rdi
callq 0x30540
movq $0x0, 0x130(%rsp)
movq 0x68(%rsp), %rcx
movq 0x130(%rsp), %rax
cmpq 0x20(%rcx), %rax
je 0x28938
leaq 0x138(%rsp), %rdi
callq 0x26c60
movq %rax, 0x60(%rsp)
jmp 0x288ad
movq 0x60(%rsp), %rdi
movq 0x160(%rsp), %rax
movq 0x130(%rsp), %rcx
movq 0x168(%rsp), %rdx
imulq %rdx, %rcx
leaq (%rax,%rcx,4), %rsi
shlq $0x2, %rdx
callq 0x92e0
movq 0x68(%rsp), %rdi
movq 0x130(%rsp), %rsi
leaq 0x138(%rsp), %rdx
callq 0x29d50
jmp 0x288f7
jmp 0x288f9
movq 0x130(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x130(%rsp)
jmp 0x28882
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x128(%rsp)
movl %eax, 0x124(%rsp)
leaq 0x138(%rsp), %rdi
callq 0x30940
jmp 0x28d6f
leaq 0x138(%rsp), %rdi
callq 0x30940
jmp 0x28b72
movq 0x170(%rsp), %rdi
callq 0x29c60
cmpl $0x2, %eax
jne 0x28b70
movq 0x170(%rsp), %rax
movq %rax, 0x118(%rsp)
movq 0x118(%rsp), %rdi
callq 0x2a1c0
movq %rax, 0x110(%rsp)
leaq 0xe8(%rsp), %rdi
callq 0x30500
leaq 0xc0(%rsp), %rdi
callq 0x30500
jmp 0x289a1
movq $0x0, 0xb8(%rsp)
movq 0x68(%rsp), %rcx
movq 0xb8(%rsp), %rax
cmpq 0x20(%rcx), %rax
je 0x28b42
movq 0x68(%rsp), %rdi
movq 0xe0(%rdi), %rax
movq %rax, 0x50(%rsp)
movq 0xb8(%rsp), %rsi
callq 0x29aa0
movq %rax, 0x58(%rsp)
jmp 0x289e9
movq 0x58(%rsp), %rdx
movq 0x50(%rsp), %rdi
movq (%rdi), %rax
movq 0x18(%rax), %rax
leaq 0xe8(%rsp), %rsi
callq *%rax
jmp 0x28a06
movq 0x170(%rsp), %rdi
movq 0xb8(%rsp), %rsi
callq 0x29c70
movq %rax, 0x48(%rsp)
jmp 0x28a22
movq 0x48(%rsp), %rsi
leaq 0xc0(%rsp), %rdi
callq 0x3f350
jmp 0x28a36
movq 0x110(%rsp), %rdx
leaq 0xc0(%rsp), %rdi
movq %rdx, %rsi
callq 0x59c90
jmp 0x28a50
movq 0x110(%rsp), %rax
imulq %rax, %rax
movq %rax, %rdx
shrq %rdx
movl %eax, %ecx
andl $0x1, %ecx
orq %rdx, %rcx
cvtsi2ss %rcx, %xmm0
addss %xmm0, %xmm0
cvtsi2ss %rax, %xmm1
movss %xmm1, 0x40(%rsp)
testq %rax, %rax
movss %xmm0, 0x44(%rsp)
js 0x28a95
movss 0x40(%rsp), %xmm0
movss %xmm0, 0x44(%rsp)
movss 0x44(%rsp), %xmm0
movss %xmm0, 0xb4(%rsp)
leaq 0xc0(%rsp), %rdi
leaq 0xb4(%rsp), %rsi
callq 0x41f10
jmp 0x28abb
leaq 0xe8(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
callq 0x413b0
jmp 0x28ad2
movq 0x68(%rsp), %rdi
movq 0xb8(%rsp), %rsi
leaq 0xe8(%rsp), %rdx
callq 0x29d50
jmp 0x28aee
jmp 0x28af0
movq 0xb8(%rsp), %rax
addq $0x1, %rax
movq %rax, 0xb8(%rsp)
jmp 0x289ad
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x128(%rsp)
movl %eax, 0x124(%rsp)
jmp 0x28b5e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x128(%rsp)
movl %eax, 0x124(%rsp)
leaq 0xc0(%rsp), %rdi
callq 0x30940
jmp 0x28b5e
leaq 0xc0(%rsp), %rdi
callq 0x30940
leaq 0xe8(%rsp), %rdi
callq 0x30940
jmp 0x28b70
leaq 0xe8(%rsp), %rdi
callq 0x30940
jmp 0x28d6f
jmp 0x28b72
movq $0x0, 0xa8(%rsp)
movq 0x68(%rsp), %rcx
movq 0xa8(%rsp), %rax
cmpq 0x20(%rcx), %rax
je 0x28d4f
leaq 0x80(%rsp), %rdi
callq 0x30500
movq $0x0, 0x78(%rsp)
movq 0x78(%rsp), %rax
movq %rax, 0x30(%rsp)
movq 0x178(%rsp), %rdi
callq 0x29980
movq %rax, 0x38(%rsp)
jmp 0x28bc9
movq 0x30(%rsp), %rax
movq 0x38(%rsp), %rcx
cmpq %rcx, %rax
je 0x28cc9
movq 0x68(%rsp), %rax
addq $0x30, %rax
movq %rax, 0x20(%rsp)
movq 0x178(%rsp), %rdi
movq 0x78(%rsp), %rsi
callq 0x29aa0
movq %rax, 0x28(%rsp)
jmp 0x28c03
movq 0x68(%rsp), %rdi
movq 0xa8(%rsp), %rsi
callq 0x29c70
movq %rax, 0x18(%rsp)
jmp 0x28c1c
movq 0x18(%rsp), %rcx
movq 0x28(%rsp), %rdx
movq 0x20(%rsp), %rdi
movq 0x68(%rsp), %rax
movq 0xd0(%rax), %r8
leaq 0x80(%rsp), %rsi
movl $0x1, %r9d
callq 0x69960
jmp 0x28c4c
movq 0x68(%rsp), %rdi
movq 0x78(%rsp), %rsi
movq 0xa8(%rsp), %rdx
leaq 0x80(%rsp), %rcx
callq 0x2a1e0
jmp 0x28c6d
movq 0x68(%rsp), %rdi
movq 0x78(%rsp), %rsi
movq 0xa8(%rsp), %rdx
leaq 0x80(%rsp), %rcx
callq 0x2a2f0
jmp 0x28c8e
jmp 0x28c90
movq 0x78(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x78(%rsp)
jmp 0x28bab
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x128(%rsp)
movl %eax, 0x124(%rsp)
leaq 0x80(%rsp), %rdi
callq 0x30940
jmp 0x28d6f
movq 0x68(%rsp), %rdi
movq 0x90(%rdi), %rax
movq %rax, 0x8(%rsp)
movq 0xa8(%rsp), %rsi
callq 0x29c70
movq %rax, 0x10(%rsp)
jmp 0x28cee
movq 0x10(%rsp), %rdi
callq 0x5a880
movss %xmm0, 0x4(%rsp)
jmp 0x28d00
movq 0x8(%rsp), %rdi
movss 0x4(%rsp), %xmm0
movss %xmm0, 0x74(%rsp)
movq 0xa8(%rsp), %rdx
xorl %eax, %eax
movl %eax, %ecx
leaq 0x74(%rsp), %rsi
callq 0x2a400
jmp 0x28d29
leaq 0x80(%rsp), %rdi
callq 0x30940
movq 0xa8(%rsp), %rax
addq $0x1, %rax
movq %rax, 0xa8(%rsp)
jmp 0x28b7e
movq 0x68(%rsp), %rax
movq 0xb0(%rax), %rdi
movq 0x90(%rax), %rsi
callq 0x3fc60
addq $0x188, %rsp            # imm = 0x188
retq
movq 0x128(%rsp), %rdi
callq 0x95d0
nopl (%rax)