// File: /workspace/github_cpp_makefile_star2_O1/xbai0624[P]dnn/src/ConstructLayer.cpp
// Function: ConstructLayer::GetSigmaSquareFor2DLayer(std::vector<Images, std::allocator<Images>>&, Images&, Images&)
void ConstructLayer::GetSigmaSquareFor2DLayer(std::vector<Images> &source, Images &mu, Images &sigma_square)
{
    // get \sigma_B^2
    // --- 2D layer

    double batch_size = source.size();
    assert(batch_size > 0);
    int nKernel = source[0].OutputImageFromKernel.size();
    assert(nKernel > 0);
    int _n = mu.OutputImageFromKernel.size();
    assert(_n == nKernel);

    auto dim = mu.OutputImageFromKernel[0].Dimension();
    assert(dim.first == 1 && dim.second == 1);

    sigma_square.OutputImageFromKernel.clear();

    for(int i=0;i<nKernel;i++)
    {
        double average = (mu.OutputImageFromKernel[i])[0][0];
        Matrix tmp(dim, 0);
        auto kernel_image_dim = source[0].OutputImageFromKernel[i].Dimension();

        for(int ii=0;ii<(int)batch_size;ii++)
        {
            Matrix diff = source[ii].OutputImageFromKernel[i] - average;
            diff = diff^diff;
            tmp = tmp + diff.SumInSection(0, kernel_image_dim.first, 0, kernel_image_dim.second);
        }

        tmp  = tmp/batch_size / (double)kernel_image_dim.first / (double)kernel_image_dim.second; // = tmp / n / (p x q)
        tmp = tmp * batch_size / (batch_size - 1.0); // Var[x]^2 = E(\sigma)^2 * m / (m-1)

        sigma_square.OutputImageFromKernel.push_back(tmp);
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
movq %rcx, 0x8(%rsp)
movq %rdx, 0x18(%rsp)
movq (%rsi), %rax
movq %rsi, 0x28(%rsp)
movq 0x8(%rsi), %rbx
subq %rax, %rbx
je 0xbdbf
movq 0x8(%rax), %rcx
subq (%rax), %rcx
shrq $0x3, %rcx
imull $0xaaaaaaab, %ecx, %ebp # imm = 0xAAAAAAAB
testl %ebp, %ebp
jle 0xbdde
movq 0x18(%rsp), %rax
movq (%rax), %rdi
movq 0x8(%rax), %rax
subq %rdi, %rax
shrq $0x3, %rax
imull $0xaaaaaaab, %eax, %eax # imm = 0xAAAAAAAB
cmpl %ebp, %eax
jne 0xbdfd
callq 0x1ac7c
cmpq $0x1, %rax
jne 0xbe1c
cmpq $0x1, %rdx
jne 0xbe1c
sarq $0x3, %rbx
movabsq $-0x5555555555555555, %rcx # imm = 0xAAAAAAAAAAAAAAAB
imulq %rbx, %rcx
movq %rcx, 0x10(%rsp)
movq %rcx, %xmm0
punpckldq 0x1b53f(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x1b547(%rip), %xmm0    # 0x27050
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movapd %xmm1, 0x40(%rsp)
movq 0x8(%rsp), %rax
movq (%rax), %r14
movq 0x8(%rax), %r15
cmpq %r14, %r15
je 0xbb49
movq %r14, %rbx
movq %rbx, %rdi
callq 0x1acb6
addq $0x18, %rbx
cmpq %r15, %rbx
jne 0xbb2f
movq 0x8(%rsp), %rax
movq %r14, 0x8(%rax)
movsd 0x1ba57(%rip), %xmm0    # 0x275a8
addsd 0x40(%rsp), %xmm0
movsd %xmm0, 0x68(%rsp)
cmpl $0x2, %ebp
movl $0x1, %eax
cmovgel %ebp, %eax
movq %rax, 0x60(%rsp)
movq 0x10(%rsp), %rax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq %rax, 0x58(%rsp)
xorl %eax, %eax
leaq 0xa8(%rsp), %r14
leaq 0x90(%rsp), %r13
leaq 0x78(%rsp), %rbp
movq %rax, 0x20(%rsp)
leaq (,%rax,8), %rax
leaq (%rax,%rax,2), %rbx
movq 0x18(%rsp), %rax
movq (%rax), %rdi
addq %rbx, %rdi
xorl %esi, %esi
callq 0x1ae76
movq (%rax), %rax
movsd (%rax), %xmm0
movsd %xmm0, 0x70(%rsp)
movl $0x1, %esi
movl $0x1, %edx
xorpd %xmm0, %xmm0
movq %r14, %rdi
callq 0x1a9ec
movq 0x28(%rsp), %rax
movq (%rax), %rax
addq (%rax), %rbx
movq %rbx, %rdi
callq 0x1ac7c
movq %rax, 0x38(%rsp)
movq %rdx, 0x30(%rsp)
cmpl $0x0, 0x10(%rsp)
jle 0xbca4
movq 0x20(%rsp), %rax
leaq (%rax,%rax,2), %r15
shlq $0x3, %r15
movq 0x58(%rsp), %r12
xorl %ebx, %ebx
movq 0x28(%rsp), %rax
movq (%rax), %rax
movq (%rax,%rbx), %rsi
addq %r15, %rsi
movq %r13, %rdi
movsd 0x70(%rsp), %xmm0
callq 0x1b714
movq %rbp, %rdi
movq %r13, %rsi
movq %r13, %rdx
callq 0x1b2b2
movq %r13, %rdi
movq %rbp, %rsi
callq 0x14f80
movq %rbp, %rdi
callq 0x1acb6
movq %r13, %rdi
xorl %esi, %esi
movq 0x38(%rsp), %rdx
xorl %ecx, %ecx
movq 0x30(%rsp), %r8
callq 0x1d87e
leaq 0xa8(%rsp), %r14
movq %rbp, %rdi
movq %r14, %rsi
callq 0x1b4b6
movq %r14, %rdi
movq %rbp, %rsi
callq 0x14f80
movq %rbp, %rdi
callq 0x1acb6
movq %r13, %rdi
callq 0x1acb6
addq $0x18, %rbx
decq %r12
jne 0xbc15
leaq 0xc0(%rsp), %rbx
movq %rbx, %rdi
leaq 0xa8(%rsp), %r14
movq %r14, %rsi
movapd 0x40(%rsp), %xmm0
callq 0x1b88c
movsd 0x38(%rsp), %xmm1
movsd 0x1b36d(%rip), %xmm0    # 0x27040
unpcklps %xmm0, %xmm1            # xmm1 = xmm1[0],xmm0[0],xmm1[1],xmm0[1]
movapd 0x1b372(%rip), %xmm0    # 0x27050
subpd %xmm0, %xmm1
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
addsd %xmm1, %xmm0
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x1b88c
movsd 0x30(%rsp), %xmm1
unpcklps 0x1b33a(%rip), %xmm1    # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0x1b342(%rip), %xmm1    # 0x27050
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
addsd %xmm1, %xmm0
movq %r13, %rdi
movq %rbp, %rsi
callq 0x1b88c
movq %r14, %rdi
movq %r13, %rsi
callq 0x14f80
movq %r13, %rdi
callq 0x1acb6
movq %rbp, %rdi
callq 0x1acb6
movq %rbx, %rdi
callq 0x1acb6
movq %rbp, %rdi
movq %r14, %rsi
movapd 0x40(%rsp), %xmm0
callq 0x1b7d0
movq %r13, %rdi
movq %rbp, %rsi
movsd 0x68(%rsp), %xmm0
callq 0x1b88c
movq %r14, %rdi
movq %r13, %rsi
callq 0x14f80
movq %r13, %rdi
callq 0x1acb6
movq %rbp, %rdi
callq 0x1acb6
movq 0x8(%rsp), %rdi
movq %r14, %rsi
callq 0x13a14
movq %r14, %rdi
callq 0x1acb6
movq 0x20(%rsp), %rax
incq %rax
cmpq 0x60(%rsp), %rax
jne 0xbb93
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1c1c5(%rip), %rdi     # 0x27f8b
leaq 0x1b97d(%rip), %rsi     # 0x2774a
leaq 0x1c259(%rip), %rcx     # 0x2802d
movl $0x7cc, %edx            # imm = 0x7CC
callq 0x4160
leaq 0x1c2a9(%rip), %rdi     # 0x2808e
leaq 0x1b95e(%rip), %rsi     # 0x2774a
leaq 0x1c23a(%rip), %rcx     # 0x2802d
movl $0x7ce, %edx            # imm = 0x7CE
callq 0x4160
leaq 0x1c296(%rip), %rdi     # 0x2809a
leaq 0x1b93f(%rip), %rsi     # 0x2774a
leaq 0x1c21b(%rip), %rcx     # 0x2802d
movl $0x7d0, %edx            # imm = 0x7D0
callq 0x4160
leaq 0x1c285(%rip), %rdi     # 0x280a8
leaq 0x1b920(%rip), %rsi     # 0x2774a
leaq 0x1c1fc(%rip), %rcx     # 0x2802d
movl $0x7d3, %edx            # imm = 0x7D3
callq 0x4160
jmp 0xbe9e
movq %rax, %rbx
jmp 0xbe61
movq %rax, %rbx
jmp 0xbe57
movq %rax, %rbx
leaq 0x90(%rsp), %rdi
callq 0x1acb6
leaq 0x78(%rsp), %rdi
callq 0x1acb6
leaq 0xc0(%rsp), %rdi
jmp 0xbeae
jmp 0xbe9e
movq %rax, %rbx
jmp 0xbe82
movq %rax, %rbx
leaq 0x90(%rsp), %rdi
callq 0x1acb6
leaq 0x78(%rsp), %rdi
jmp 0xbeae
jmp 0xbe9e
jmp 0xbe8d
movq %rax, %rbx
leaq 0x78(%rsp), %rdi
callq 0x1acb6
jmp 0xbea6
jmp 0xbea3
movq %rax, %rbx
jmp 0xbeb3
movq %rax, %rbx
leaq 0x90(%rsp), %rdi
callq 0x1acb6
leaq 0xa8(%rsp), %rdi
callq 0x1acb6
movq %rbx, %rdi
callq 0x43e0