// File: /workspace/github_cpp_makefile_star2_O1/xbai0624[P]dnn/src/ConstructLayer.cpp
// Function: ConstructLayer::BatchNormalization_UpdateGammaBeta()
void ConstructLayer::BatchNormalization_UpdateGammaBeta()
{
    // for batch normalization, update beta and gamma during back propagation

    // only apply for cnn, fc and output layers
    if(__type != LayerType::cnn && __type != LayerType::fullyConnected && __type != LayerType::output)
        return;

    size_t nK = 1;
    if(GetLayerDimension() == LayerDimension::_2D)
        nK = __n_kernels_cnn;

    assert(__gamma_BN.size() == nK);
    assert(__gamma_BN.size() == __v_partial_C_over_partial_gamma.size());
    assert(__beta_BN.size() == __v_partial_C_over_partial_beta.size());

    size_t batch_size = __p_data_interface->GetBatchSize();

    for(size_t i=0;i<nK;i++)
    {
        Matrix & gradient_gamma = __v_partial_C_over_partial_gamma[i];
        Matrix & gradient_beta = __v_partial_C_over_partial_beta[i];

        Matrix d_gamma =  gradient_gamma *  (__learningRate/(double)batch_size);
        __gamma_BN[i] = __gamma_BN[i] - d_gamma;
        Matrix d_beta =  gradient_beta * (__learningRate/(double)batch_size);
        __beta_BN[i] = __beta_BN[i] - d_beta;
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movl 0xc(%rdi), %eax
cmpl $0x4, %eax
ja 0x10132
movl $0x13, %ecx
btl %eax, %ecx
jae 0x10132
movq %rdi, %rbp
movq (%rdi), %rax
callq *0x258(%rax)
movl $0x1, %esi
cmpl $0x1, %eax
jne 0xffb6
movq 0x18(%rbp), %rsi
movq 0x310(%rbp), %rax
subq 0x308(%rbp), %rax
movq %rax, %rcx
sarq $0x3, %rcx
movabsq $-0x5555555555555555, %rdx # imm = 0xAAAAAAAAAAAAAAAB
imulq %rcx, %rdx
cmpq %rsi, %rdx
jne 0x10141
movq 0x370(%rbp), %rcx
subq 0x368(%rbp), %rcx
cmpq %rcx, %rax
jne 0x10160
movq 0x328(%rbp), %rax
subq 0x320(%rbp), %rax
movq 0x388(%rbp), %rcx
subq 0x380(%rbp), %rcx
cmpq %rcx, %rax
jne 0x1017f
testq %rsi, %rsi
je 0x10132
movq 0x400(%rbp), %rax
movslq (%rax), %rax
movq %rax, %xmm0
punpckldq 0x17002(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x1700a(%rip), %xmm0    # 0x27050
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movapd %xmm1, 0x10(%rsp)
xorl %r14d, %r14d
leaq 0x50(%rsp), %rbx
leaq 0x20(%rsp), %r12
movq %rsi, 0x8(%rsp)
movq 0x368(%rbp), %rsi
addq %r14, %rsi
movq 0x380(%rbp), %r13
addq %r14, %r13
movsd 0xc0(%rbp), %xmm0
divsd 0x10(%rsp), %xmm0
movq %rbx, %rdi
callq 0x1b7d0
movq 0x308(%rbp), %rsi
addq %r14, %rsi
movq %r12, %rdi
movq %rbx, %rdx
callq 0x1b1b0
movq 0x308(%rbp), %rdi
addq %r14, %rdi
movq %r12, %rsi
callq 0x14f80
movq %r12, %rdi
callq 0x1acb6
movsd 0xc0(%rbp), %xmm0
divsd 0x10(%rsp), %xmm0
movq %r12, %rdi
movq %r13, %rsi
callq 0x1b7d0
leaq 0x38(%rsp), %r15
movq 0x320(%rbp), %rsi
addq %r14, %rsi
movq %r15, %rdi
movq %r12, %rdx
callq 0x1b1b0
movq 0x320(%rbp), %rdi
addq %r14, %rdi
movq %r15, %rsi
callq 0x14f80
movq %r15, %rdi
callq 0x1acb6
movq %r12, %rdi
callq 0x1acb6
movq %rbx, %rdi
callq 0x1acb6
addq $0x18, %r14
movq 0x8(%rsp), %rsi
decq %rsi
jne 0x10065
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x18d71(%rip), %rdi     # 0x28eb9
leaq 0x175fb(%rip), %rsi     # 0x2774a
leaq 0x18d7b(%rip), %rcx     # 0x28ed1
movl $0xe7d, %edx            # imm = 0xE7D
callq 0x4160
leaq 0x18da4(%rip), %rdi     # 0x28f0b
leaq 0x175dc(%rip), %rsi     # 0x2774a
leaq 0x18d5c(%rip), %rcx     # 0x28ed1
movl $0xe7e, %edx            # imm = 0xE7E
callq 0x4160
leaq 0x18dc2(%rip), %rdi     # 0x28f48
leaq 0x175bd(%rip), %rsi     # 0x2774a
leaq 0x18d3d(%rip), %rcx     # 0x28ed1
movl $0xe7f, %edx            # imm = 0xE7F
callq 0x4160
jmp 0x101a2
jmp 0x101a7
movq %rax, %rbx
jmp 0x101c3
movq %rax, %rbx
jmp 0x101b9
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
callq 0x1acb6
leaq 0x20(%rsp), %rdi
callq 0x1acb6
leaq 0x50(%rsp), %rdi
callq 0x1acb6
movq %rbx, %rdi
callq 0x43e0
nop