// File: 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:
subq $0x108, %rsp            # imm = 0x108
movq %rdi, 0x100(%rsp)
movq %rsi, 0xf8(%rsp)
movq %rdx, 0xf0(%rsp)
movq 0x100(%rsp), %rax
movq %rax, 0x30(%rsp)
movq 0xf0(%rsp), %rdi
callq 0x29c60
cmpl $0x4, %eax
jne 0x27dea
movq 0x30(%rsp), %rcx
movq 0x8(%rcx), %rax
imulq 0x10(%rcx), %rax
movq %rax, 0xe8(%rsp)
movq 0xf0(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x29c70
movq %rax, %rdi
callq 0x26c60
movq %rax, %rcx
movq 0x30(%rsp), %rax
movq %rcx, 0xe0(%rsp)
movq 0x8(%rax), %rsi
movq 0x10(%rax), %rdx
leaq 0xb8(%rsp), %rdi
callq 0x30540
movq $0x0, 0xb0(%rsp)
movq 0x30(%rsp), %rcx
movq 0xb0(%rsp), %rax
cmpq 0x20(%rcx), %rax
je 0x27dd8
leaq 0xb8(%rsp), %rdi
callq 0x26c60
movq %rax, 0x28(%rsp)
jmp 0x27d4d
movq 0x28(%rsp), %rdi
movq 0xe0(%rsp), %rax
movq 0xb0(%rsp), %rcx
movq 0xe8(%rsp), %rdx
imulq %rdx, %rcx
leaq (%rax,%rcx,4), %rsi
shlq $0x2, %rdx
callq 0x92e0
movq 0x30(%rsp), %rdi
movq 0xb0(%rsp), %rsi
leaq 0xb8(%rsp), %rdx
callq 0x29d50
jmp 0x27d97
jmp 0x27d99
movq 0xb0(%rsp), %rax
addq $0x1, %rax
movq %rax, 0xb0(%rsp)
jmp 0x27d22
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xa8(%rsp)
movl %eax, 0xa4(%rsp)
leaq 0xb8(%rsp), %rdi
callq 0x30940
jmp 0x27fae
leaq 0xb8(%rsp), %rdi
callq 0x30940
jmp 0x27fa6
movq 0xf0(%rsp), %rdi
callq 0x29c60
cmpl $0x3, %eax
jne 0x27fa4
movq 0xf0(%rsp), %rdi
callq 0x29e30
movq %rax, 0x98(%rsp)
movq $0x0, 0x90(%rsp)
movq 0x30(%rsp), %rcx
movq 0x90(%rsp), %rax
cmpq 0x20(%rcx), %rax
je 0x27fa2
leaq 0x68(%rsp), %rdi
callq 0x30500
leaq 0x40(%rsp), %rdi
callq 0x30500
jmp 0x27e4e
movq $0x0, 0x38(%rsp)
movq 0x38(%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0xf0(%rsp), %rdi
callq 0x29980
movq %rax, 0x20(%rsp)
jmp 0x27e75
movq 0x18(%rsp), %rax
movq 0x20(%rsp), %rcx
cmpq %rcx, %rax
je 0x27f50
movq 0x30(%rsp), %rax
addq $0x30, %rax
movq %rax, 0x8(%rsp)
movq 0xf0(%rsp), %rdi
movq 0x38(%rsp), %rsi
callq 0x29c70
movq %rax, 0x10(%rsp)
jmp 0x27eaf
movq 0xf0(%rsp), %rdi
movq 0x90(%rsp), %rsi
movq 0x38(%rsp), %rdx
callq 0x29e50
movq %rax, (%rsp)
jmp 0x27ecf
movq (%rsp), %rcx
movq 0x10(%rsp), %rdx
movq 0x8(%rsp), %rdi
movq 0x98(%rsp), %r8
leaq 0x40(%rsp), %rsi
xorl %r9d, %r9d
callq 0x69960
jmp 0x27ef4
leaq 0x68(%rsp), %rdi
leaq 0x40(%rsp), %rsi
callq 0x3fc60
jmp 0x27f05
jmp 0x27f07
movq 0x38(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x38(%rsp)
jmp 0x27e57
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xa8(%rsp)
movl %eax, 0xa4(%rsp)
jmp 0x27f96
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xa8(%rsp)
movl %eax, 0xa4(%rsp)
leaq 0x40(%rsp), %rdi
callq 0x30940
jmp 0x27f96
movq 0x30(%rsp), %rdi
movq 0x90(%rsp), %rsi
leaq 0x68(%rsp), %rdx
callq 0x29d50
jmp 0x27f69
leaq 0x40(%rsp), %rdi
callq 0x30940
leaq 0x68(%rsp), %rdi
callq 0x30940
movq 0x90(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x90(%rsp)
jmp 0x27e21
leaq 0x68(%rsp), %rdi
callq 0x30940
jmp 0x27fae
jmp 0x27fa4
jmp 0x27fa6
addq $0x108, %rsp            # imm = 0x108
retq
movq 0xa8(%rsp), %rdi
callq 0x95d0
nopl (%rax,%rax)