// File: /workspace/github_cpp_makefile_star2_O1/xbai0624[P]dnn/src/ConstructLayer.cpp
// Function: ConstructLayer::ComputeCostInOutputLayerForCurrentSample(int)
void ConstructLayer::ComputeCostInOutputLayerForCurrentSample(int sample_index)
{
    if(__type != LayerType::output)
    {
        std::cout<<"Error: ComputeCostInOutputLayerForCurrentSample() only works for output layer."
            <<std::endl;
        std::exit(0);
    }

    // --- 2) compute the cost function C(a_i, y_i)
    Images & sample_image = __imageA[sample_index]; // output layer drop out is not used for sure, so use __imageA is OK.
    assert(sample_image.GetNumberOfKernels() == 1); // output layer must be a fully connected layer, so one kernel
    // now get a_i
    Matrix & sample_A = sample_image.OutputImageFromKernel[0];
    assert(sample_A.Dimension().second == 1); // must be a collum matrix
    //cout<<sample_A<<endl;

    //assert(sample_number >= 1); // obsolete
    //Matrix sample_label = (__p_data_interface->GetCurrentBatchLabel())[sample_number-1];
    Matrix & sample_label = (__p_data_interface->GetCurrentBatchLabel())[sample_index];
    assert(sample_label.Dimension()  == sample_A.Dimension());

    double cost = 0.;
    if(__cost_func_type == CostFuncType::cross_entropy)
    {
        cost = cross_entropy(sample_A, sample_label);
    }
    else if(__cost_func_type == CostFuncType::log_likelihood)
    {
        cost = log_likelihood(sample_A, sample_label);
    }
    else if(__cost_func_type == CostFuncType::quadratic_sum)
    {
        cost = quadratic_sum(sample_A, sample_label);
    }
    else {
        std::cout<<"Error: cost function only supports cross_entropy, loglikelihood, quadratic_sum"
            <<std::endl;
        exit(0);
    }
    // push cost for current sample to memory
    //__outputLayerCost.push_back(cost);
    __outputLayerCost[sample_index]= cost;
    //cout<<"sample cost: "<<cost<<endl;
    //getchar();


    // --- 3) then calculate delta: delta = delta(a_i, y_i) for this sample
    // -------- please note: this function only calculate \delta for current sample, 
    // ----------- when doing back propagation for hidden layers, only delta with the same sample_index should be updated 
    // ----------- the overall delta for this batch should be an average of all deltas in this batch
    // ----------- this is due to the characteristic of Cost function, which is an averaged sum over all samples

    Matrix delta = Matrix(sample_A.Dimension());
    if(__cost_func_type == CostFuncType::cross_entropy)
    {
        delta = sample_A - sample_label; // softmax and sigmoid all have this form
    }
    else if(__cost_func_type == CostFuncType::log_likelihood)
    {
        delta = sample_A - sample_label;
    }
    else if(__cost_func_type == CostFuncType::quadratic_sum)
    {
        delta = sample_A - sample_label; // this is a place holder, not used
    }
    else {
        std::cout<<"Error: cost function only supports cross_entropy, loglikelihood, quadratic_sum"
            <<std::endl;
        exit(0);
    }
    // push cost for current sample to memory
    Images images_delta_from_current_sample;
    images_delta_from_current_sample.OutputImageFromKernel.push_back(delta); // only one kernel in fc layer
    __imageDelta[sample_index]=images_delta_from_current_sample;
    __imageDeltaFull[sample_index]=images_delta_from_current_sample; // in output layer, dropout is not used for sure
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
cmpl $0x4, 0xc(%rdi)
jne 0xade0
movq %rdi, %rbx
movslq %esi, %rcx
movq 0x138(%rdi), %rax
leaq (%rcx,%rcx,2), %r14
movq (%rax,%r14,8), %r15
movq 0x8(%rax,%r14,8), %rax
subq %r15, %rax
cmpq $0x18, %rax
jne 0xadf0
movq %rcx, 0x48(%rsp)
movq %r15, %rdi
callq 0x1ac7c
cmpq $0x1, %rdx
jne 0xae0f
movq 0x400(%rbx), %rax
shlq $0x3, %r14
movq 0x28(%rax), %r12
addq %r14, %r12
movq %r12, %rdi
callq 0x1ac7c
movq %rax, %r13
movq %rdx, %rbp
movq %r15, %rdi
callq 0x1ac7c
movq %rdx, %rcx
movq %r13, %rdi
movq %rbp, %rsi
movq %rax, %rdx
callq 0x1a680
testb %al, %al
je 0xae2e
movl 0x3e8(%rbx), %eax
xorpd %xmm0, %xmm0
cmpl $0x2, %eax
je 0xabfd
cmpl $0x1, %eax
je 0xaccd
testl %eax, %eax
jne 0xae4d
movq %r15, %rdi
callq 0x1ac7c
movq %rax, %r13
movq %rdx, %rbp
movq %r12, %rdi
callq 0x1ac7c
movq %rdx, %rcx
movq %r13, %rdi
movq %rbp, %rsi
movq %rax, %rdx
callq 0x1a680
testb %al, %al
je 0xae95
cmpq $0x1, %rbp
xorpd %xmm0, %xmm0
jne 0xaed3
testq %r13, %r13
je 0xaccd
xorl %ebp, %ebp
movsd %xmm0, 0x28(%rsp)
movq %r15, %rdi
movq %rbp, %rsi
callq 0x1ae76
movq (%rax), %rax
movsd 0x1ca41(%rip), %xmm0    # 0x275b8
ucomisd (%rax), %xmm0
jae 0xabde
movq %r15, %rdi
movq %rbp, %rsi
callq 0x1ae76
movq (%rax), %rax
movsd (%rax), %xmm0
ucomisd 0x1ca29(%rip), %xmm0    # 0x275c0
jae 0xabde
movq %r12, %rdi
movq %rbp, %rsi
callq 0x1ae76
movq (%rax), %rax
movsd (%rax), %xmm0
movsd %xmm0, 0x8(%rsp)
movq %r15, %rdi
movq %rbp, %rsi
callq 0x1ae76
movq (%rax), %rax
movsd (%rax), %xmm0
callq 0x41e0
mulsd 0x8(%rsp), %xmm0
movsd 0x28(%rsp), %xmm1
addsd %xmm0, %xmm1
movapd %xmm1, %xmm0
jmp 0xabec
movsd 0x28(%rsp), %xmm0
addsd 0x1c9a4(%rip), %xmm0    # 0x27590
incq %rbp
cmpq %rbp, %r13
jne 0xab5b
jmp 0xaccd
movq %r15, %rdi
callq 0x1ac7c
movq %rax, %r13
movq %rdx, %rbp
movq %r12, %rdi
callq 0x1ac7c
movq %rdx, %rcx
movq %r13, %rdi
movq %rbp, %rsi
movq %rax, %rdx
callq 0x1a680
testb %al, %al
je 0xaeb4
cmpq $0x1, %rbp
jne 0xaef2
testq %r13, %r13
xorpd %xmm0, %xmm0
je 0xaccd
xorl %ebp, %ebp
movsd %xmm0, 0x28(%rsp)
movq %r15, %rdi
movq %rbp, %rsi
callq 0x1ae76
movq (%rax), %rax
movsd (%rax), %xmm0
movsd %xmm0, 0x8(%rsp)
movq %r12, %rdi
movq %rbp, %rsi
callq 0x1ae76
movq (%rax), %rax
movsd 0x8(%rsp), %xmm0
subsd (%rax), %xmm0
movsd %xmm0, 0x8(%rsp)
movq %r15, %rdi
movq %rbp, %rsi
callq 0x1ae76
movq (%rax), %rax
movsd (%rax), %xmm0
movsd %xmm0, 0x50(%rsp)
movq %r12, %rdi
movq %rbp, %rsi
callq 0x1ae76
movsd 0x28(%rsp), %xmm0
movq (%rax), %rax
movsd 0x50(%rsp), %xmm1
subsd (%rax), %xmm1
mulsd 0x8(%rsp), %xmm1
addsd %xmm1, %xmm0
incq %rbp
cmpq %rbp, %r13
jne 0xac45
movq 0x1f8(%rbx), %rax
movq 0x48(%rsp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movq %r15, %rdi
callq 0x1ac7c
leaq 0x30(%rsp), %rdi
movq %rax, %rsi
callq 0x1a8c0
movl 0x3e8(%rbx), %eax
cmpl $0x2, %eax
je 0xad4d
cmpl $0x1, %eax
je 0xad2c
testl %eax, %eax
jne 0xae6f
leaq 0x10(%rsp), %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x1b1b0
leaq 0x30(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x14f80
jmp 0xad6c
leaq 0x10(%rsp), %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x1b1b0
leaq 0x30(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x14f80
jmp 0xad6c
leaq 0x10(%rsp), %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x1b1b0
leaq 0x30(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x14f80
leaq 0x10(%rsp), %rdi
callq 0x1acb6
xorpd %xmm0, %xmm0
leaq 0x10(%rsp), %rdi
movapd %xmm0, (%rdi)
movq $0x0, 0x10(%rdi)
leaq 0x30(%rsp), %rsi
callq 0x13a14
movq 0x180(%rbx), %rdi
addq %r14, %rdi
leaq 0x10(%rsp), %rsi
callq 0x1429e
addq 0x1e0(%rbx), %r14
leaq 0x10(%rsp), %rsi
movq %r14, %rdi
callq 0x1429e
leaq 0x10(%rsp), %rdi
callq 0x136aa
leaq 0x30(%rsp), %rdi
callq 0x1acb6
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x2c1f1(%rip), %rdi     # 0x36fd8
leaq 0x1cd7f(%rip), %rsi     # 0x27b6d
jmp 0xae5b
leaq 0x1cdc5(%rip), %rdi     # 0x27bbc
leaq 0x1c94c(%rip), %rsi     # 0x2774a
leaq 0x1cdde(%rip), %rcx     # 0x27be3
movl $0x591, %edx            # imm = 0x591
callq 0x4160
leaq 0x1ce18(%rip), %rdi     # 0x27c2e
leaq 0x1c92d(%rip), %rsi     # 0x2774a
leaq 0x1cdbf(%rip), %rcx     # 0x27be3
movl $0x594, %edx            # imm = 0x594
callq 0x4160
leaq 0x1ce1a(%rip), %rdi     # 0x27c4f
leaq 0x1c90e(%rip), %rsi     # 0x2774a
leaq 0x1cda0(%rip), %rcx     # 0x27be3
movl $0x59a, %edx            # imm = 0x59A
callq 0x4160
movq 0x2c184(%rip), %rdi     # 0x36fd8
leaq 0x1ce25(%rip), %rsi     # 0x27c80
callq 0x4260
movq %rax, %rdi
callq 0x4100
xorl %edi, %edi
callq 0x4290
movq 0x2c162(%rip), %rdi     # 0x36fd8
leaq 0x1ce03(%rip), %rsi     # 0x27c80
movl $0x4e, %edx
callq 0x4280
movq 0x2c14a(%rip), %rdi     # 0x36fd8
callq 0x4100
jmp 0xae68
leaq 0x1e71a(%rip), %rdi     # 0x295b6
leaq 0x1c8a7(%rip), %rsi     # 0x2774a
leaq 0x1e721(%rip), %rcx     # 0x295cb
movl $0x55a, %edx            # imm = 0x55A
callq 0x4160
leaq 0x1e6fb(%rip), %rdi     # 0x295b6
leaq 0x1c888(%rip), %rsi     # 0x2774a
leaq 0x1e72b(%rip), %rcx     # 0x295f4
movl $0x579, %edx            # imm = 0x579
callq 0x4160
leaq 0x1d1e0(%rip), %rdi     # 0x280ba
leaq 0x1c869(%rip), %rsi     # 0x2774a
leaq 0x1e6e3(%rip), %rcx     # 0x295cb
movl $0x55b, %edx            # imm = 0x55B
callq 0x4160
leaq 0x1d1c1(%rip), %rdi     # 0x280ba
leaq 0x1c84a(%rip), %rsi     # 0x2774a
leaq 0x1e6ed(%rip), %rcx     # 0x295f4
movl $0x57a, %edx            # imm = 0x57A
callq 0x4160
jmp 0xaf2a
jmp 0xaf17
jmp 0xaf17
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x1acb6
jmp 0xaf3c
jmp 0xaf2a
jmp 0xaf2a
movq %rax, %rbx
jmp 0xaf3c
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x136aa
leaq 0x30(%rsp), %rdi
callq 0x1acb6
movq %rbx, %rdi
callq 0x43e0