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

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

    auto dim = source[0].OutputImageFromKernel[0].Dimension();
    Matrix tmp_sigma_square(dim, 0);

    Matrix & average = mu.OutputImageFromKernel[0];

    for(int i=0;i<(int)batch_size;i++)
    {
        Matrix & tmp_kernel_image = source[i].OutputImageFromKernel[0];

        Matrix _t1 = tmp_kernel_image - average;
        Matrix _t1_square = _t1^_t1;

        tmp_sigma_square = tmp_sigma_square + _t1_square;
    }

    tmp_sigma_square = tmp_sigma_square / batch_size;
    tmp_sigma_square = tmp_sigma_square * batch_size / (batch_size - 1.); // Var[x]^2 = E(\sigma)^2 * m / (m-1)

    sigma_square.OutputImageFromKernel.clear();
    sigma_square.OutputImageFromKernel.push_back(tmp_sigma_square);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %rdx, %rbx
movq (%rsi), %r8
movq %rsi, 0x50(%rsp)
movq 0x8(%rsi), %rsi
movq %rsi, %rax
subq %r8, %rax
pushq $0x18
popq %rdi
cqto
idivq %rdi
cmpq %r8, %rsi
je 0xabb2
movq %rax, %r13
movq (%r8), %rdi
movq 0x8(%r8), %rax
subq %rdi, %rax
cmpq $0x18, %rax
jne 0xabd1
movq %rcx, 0x40(%rsp)
movq 0x8(%rbx), %rax
subq (%rbx), %rax
cmpq $0x18, %rax
jne 0xabf0
movq %r13, %xmm0
punpckldq 0x165db(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x165e3(%rip), %xmm0    # 0x21030
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movapd %xmm1, 0x30(%rsp)
callq 0x17a6a
leaq 0x18(%rsp), %rdi
xorpd %xmm0, %xmm0
movq %rax, %rsi
callq 0x1787a
movq (%rbx), %rax
movq %rax, 0x48(%rsp)
xorl %r14d, %r14d
testl %r13d, %r13d
movl $0x0, %eax
cmovgl %r13d, %eax
imulq $0x18, %rax, %r15
movq %rsp, %r13
leaq 0x58(%rsp), %rbp
leaq 0x70(%rsp), %rbx
cmpq %r14, %r15
je 0xab05
movq 0x50(%rsp), %rax
movq (%rax), %rax
movq (%rax,%r14), %rsi
movq %r13, %rdi
movq 0x48(%rsp), %rdx
callq 0x17eb0
movq %rbp, %rdi
movq %r13, %rsi
movq %r13, %rdx
callq 0x17f64
movq %rbx, %rdi
leaq 0x18(%rsp), %r12
movq %r12, %rsi
movq %rbp, %rdx
callq 0x17cc4
movq %r12, %rdi
movq %rbx, %rsi
callq 0x125b4
movq %rbx, %rdi
callq 0x17a9a
movq %rbp, %rdi
callq 0x17a9a
movq %r13, %rdi
callq 0x17a9a
addq $0x18, %r14
jmp 0xaa9d
movq %rsp, %rdi
leaq 0x18(%rsp), %rsi
movapd 0x30(%rsp), %xmm0
callq 0x18348
leaq 0x18(%rsp), %rdi
movq %rsp, %rsi
callq 0x125b4
movq %rsp, %rdi
callq 0x17a9a
leaq 0x58(%rsp), %rdi
leaq 0x18(%rsp), %rsi
movapd 0x30(%rsp), %xmm0
callq 0x182dc
movapd 0x30(%rsp), %xmm0
addsd 0x169e8(%rip), %xmm0    # 0x21538
movq %rsp, %rdi
leaq 0x58(%rsp), %rsi
callq 0x18348
leaq 0x18(%rsp), %rdi
movq %rsp, %rsi
callq 0x125b4
movq %rsp, %rdi
callq 0x17a9a
leaq 0x58(%rsp), %rdi
callq 0x17a9a
movq 0x40(%rsp), %rbx
movq %rbx, %rdi
callq 0x11394
leaq 0x18(%rsp), %rsi
movq %rbx, %rdi
callq 0x1139c
leaq 0x18(%rsp), %rdi
callq 0x17a9a
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x17323(%rip), %rdi     # 0x21edc
leaq 0x16b1a(%rip), %rsi     # 0x216da
leaq 0x17324(%rip), %rcx     # 0x21eeb
movl $0x7ab, %edx            # imm = 0x7AB
callq 0x4180
leaq 0x17374(%rip), %rdi     # 0x21f4c
leaq 0x16afb(%rip), %rsi     # 0x216da
leaq 0x17305(%rip), %rcx     # 0x21eeb
movl $0x7ad, %edx            # imm = 0x7AD
callq 0x4180
leaq 0x17362(%rip), %rdi     # 0x21f59
leaq 0x16adc(%rip), %rsi     # 0x216da
leaq 0x172e6(%rip), %rcx     # 0x21eeb
movl $0x7ae, %edx            # imm = 0x7AE
callq 0x4180
jmp 0xac2e
movq %rax, %rbx
movq %rsp, %rdi
callq 0x17a9a
jmp 0xac21
movq %rax, %rbx
leaq 0x58(%rsp), %rdi
jmp 0xac57
jmp 0xac2e
jmp 0xac33
jmp 0xac2e
movq %rax, %rbx
jmp 0xac5c
movq %rax, %rbx
jmp 0xac54
movq %rax, %rbx
jmp 0xac4a
movq %rax, %rbx
leaq 0x70(%rsp), %rdi
callq 0x17a9a
leaq 0x58(%rsp), %rdi
callq 0x17a9a
movq %rsp, %rdi
callq 0x17a9a
leaq 0x18(%rsp), %rdi
callq 0x17a9a
movq %rbx, %rdi
callq 0x43b0