// File: /workspace/github_cpp_makefile_star2_O1/btbujiangjun[P]abcdl/src/cnn/Layer.cpp
// Function: abcdl::cnn::SubSamplingLayer::backward(abcdl::cnn::Layer*, abcdl::cnn::Layer*)
void SubSamplingLayer::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::CONVOLUTION){
        size_t stride = ((ConvolutionLayer*)back_layer)->get_stride();
        for(size_t i = 0 ; i != this->_out_channel_size; i++){
            abcdl::algebra::Mat delta;
            abcdl::algebra::Mat sub_delta;
            for(size_t j = 0; j != back_layer->get_out_channel_size(); j++){
                _helper.convn(sub_delta, back_layer->get_delta(j), back_layer->get_weight(i, j), stride, abcdl::algebra::FULL);
                delta += sub_delta;
            }
            this->set_delta(i, delta);
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1f8, %rsp            # imm = 0x1F8
movq %rdx, %r14
movl 0x28(%rdx), %eax
cmpl $0x3, %eax
movq %rdi, (%rsp)
je 0x13fa5
cmpl $0x4, %eax
jne 0x140d3
movq (%rsp), %r13
movq 0x10(%r13), %rbx
imulq 0x8(%r13), %rbx
movq %r14, %rdi
xorl %esi, %esi
callq 0x1543e
movq 0x10(%rax), %r14
movq 0x8(%r13), %rsi
movq 0x10(%r13), %rdx
leaq 0x70(%rsp), %rdi
callq 0x1845e
cmpq $0x0, 0x20(%r13)
je 0x13f96
shlq $0x2, %rbx
xorl %r15d, %r15d
leaq 0x70(%rsp), %r12
movq 0x80(%rsp), %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x8270
movq %r13, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x154d2
incq %r15
addq %rbx, %r14
cmpq 0x20(%r13), %r15
jne 0x13f69
leaq 0x70(%rsp), %rdi
callq 0x18696
jmp 0x140d3
cmpq $0x0, 0x20(%rdi)
je 0x140d3
movq 0xd0(%r14), %rax
movq %rax, 0x18(%rsp)
movq (%rsp), %rax
addq $0x30, %rax
movq %rax, 0x10(%rsp)
xorl %ebp, %ebp
leaq 0x48(%rsp), %rdi
leaq 0x20(%rsp), %r12
movq %rbp, 0x8(%rsp)
movq %rdi, %rbx
callq 0x18440
movq %r12, %r13
movq %r12, %rdi
callq 0x18440
cmpq $0x0, 0x20(%r14)
je 0x14099
xorl %ebp, %ebp
movq %r14, %rdi
movq %rbp, %rsi
callq 0x1543e
movq %rax, %r15
movq 0x20(%r14), %r12
imulq 0x8(%rsp), %r12
addq %rbp, %r12
movq 0x48(%r14), %rax
subq 0x40(%r14), %rax
sarq $0x3, %rax
cmpq %rax, %r12
jae 0x1405c
movq 0x40(%r14), %rax
movq (%rax,%r12,8), %rcx
movq 0x10(%rsp), %rdi
movq %r13, %rsi
movq %r15, %rdx
movq 0x18(%rsp), %r8
xorl %r9d, %r9d
callq 0x3067e
movq %rbx, %rdi
movq %r13, %rsi
callq 0x20660
incq %rbp
cmpq 0x20(%r14), %rbp
jne 0x13ffa
jmp 0x14099
leaq 0x70(%rsp), %rdi
leaq 0x25dd0(%rip), %rsi     # 0x39e38
movl $0x56, %edx
movl $0x3, %ecx
callq 0x373d6
movl $0x24, %edx
leaq 0x70(%rsp), %rdi
leaq 0x25e32(%rip), %rsi     # 0x39eba
callq 0x8350
leaq 0x70(%rsp), %rdi
callq 0x375fe
jmp 0x14026
movq (%rsp), %r15
movq %r15, %rdi
movq 0x8(%rsp), %rbp
movq %rbp, %rsi
movq %rbx, %rdx
callq 0x154d2
movq %r13, %r12
movq %r13, %rdi
callq 0x18696
movq %rbx, %rdi
callq 0x18696
incq %rbp
cmpq 0x20(%r15), %rbp
movq %rbx, %rdi
jne 0x13fd5
addq $0x1f8, %rsp            # imm = 0x1F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0x70(%rsp), %rdi
callq 0x375fe
jmp 0x14108
jmp 0x14105
movq %rax, %rbx
jmp 0x14112
movq %rax, %rbx
leaq 0x70(%rsp), %rdi
jmp 0x14117
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x18696
leaq 0x48(%rsp), %rdi
callq 0x18696
movq %rbx, %rdi
callq 0x8500