// File: /workspace/github_cpp_makefile_star2_O3/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 0x13d0d
cmpl $0x4, %eax
jne 0x13e3e
movq (%rsp), %r13
movq 0x10(%r13), %rbx
imulq 0x8(%r13), %rbx
movq %r14, %rdi
xorl %esi, %esi
callq 0x151aa
movq 0x10(%rax), %r14
movq 0x8(%r13), %rsi
movq 0x10(%r13), %rdx
leaq 0x70(%rsp), %rdi
callq 0x181d6
cmpq $0x0, 0x20(%r13)
je 0x13cfe
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 0x15242
incq %r15
addq %rbx, %r14
cmpq 0x20(%r13), %r15
jne 0x13cd1
leaq 0x70(%rsp), %rdi
callq 0x1840e
jmp 0x13e3e
cmpq $0x0, 0x20(%rdi)
je 0x13e3e
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 0x181b8
movq %r12, %r13
movq %r12, %rdi
callq 0x181b8
cmpq $0x0, 0x20(%r14)
je 0x13e04
xorl %ebp, %ebp
movq %r14, %rdi
movq %rbp, %rsi
callq 0x151aa
movq %rax, %r15
movq 0x20(%r14), %r12
imulq 0x8(%rsp), %r12
addq %rbp, %r12
movq 0x40(%r14), %rax
movq 0x48(%r14), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
cmpq %rcx, %r12
jae 0x13dc3
movq (%rax,%r12,8), %rcx
movq 0x10(%rsp), %rdi
movq %r13, %rsi
movq %r15, %rdx
movq 0x18(%rsp), %r8
xorl %r9d, %r9d
callq 0x3047a
movq %rbx, %rdi
movq %r13, %rsi
callq 0x20394
incq %rbp
cmpq 0x20(%r14), %rbp
jne 0x13d62
jmp 0x13e04
leaq 0x70(%rsp), %rdi
leaq 0x26069(%rip), %rsi     # 0x39e38
movl $0x56, %edx
movl $0x3, %ecx
callq 0x37222
movl $0x24, %edx
leaq 0x70(%rsp), %rdi
leaq 0x260cb(%rip), %rsi     # 0x39eba
callq 0x8350
leaq 0x70(%rsp), %rdi
callq 0x3744a
movq 0x40(%r14), %rax
jmp 0x13d91
movq (%rsp), %r15
movq %r15, %rdi
movq 0x8(%rsp), %rbp
movq %rbp, %rsi
movq %rbx, %rdx
callq 0x15242
movq %r13, %r12
movq %r13, %rdi
callq 0x1840e
movq %rbx, %rdi
callq 0x1840e
incq %rbp
cmpq 0x20(%r15), %rbp
movq %rbx, %rdi
jne 0x13d3d
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 0x3744a
jmp 0x13e73
jmp 0x13e70
movq %rax, %rbx
jmp 0x13e7d
movq %rax, %rbx
leaq 0x70(%rsp), %rdi
jmp 0x13e82
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x1840e
leaq 0x48(%rsp), %rdi
callq 0x1840e
movq %rbx, %rdi
callq 0x8500
nop