// File: /workspace/github_cpp_makefile_star2_O3/leonn[P]neuralNet/source/net.cpp
// Function: Net::backPropagation(std::vector<double, std::allocator<double>> const&, long double const&, long double const&)
void Net::backPropagation(const vector<double> &targetValues,const long double &eta ,const long double &alpha){

	//-----Calculate overall net error (RMS-root mean square error - of output neuron errors)
	Layer &outputLayer = layers.back();

	//overwall net error
	error=0.0;

 	for(unsigned n = 0; n < outputLayer.size() -1; n++){
 		long double delta= targetValues[n] - outputLayer[n].getOutputValue();
 		error+=delta*delta;
 	}
 	error*=0.5;

 	//error/=(* numPatterns); //get average error squared
	//error/=outputLayer.size()-1; //get average error squared
    //------ Implement a recent average measurement
    recentAverageError = error;

    //------ Calculate output layer gradients
	for(unsigned n = 0; n < outputLayer.size() -1; n++){
 		outputLayer[n].calculateOutputGradients(targetValues[n]);
  	}

    //------- Calculate hidden layer gradients
  	for(unsigned layerNumber = layers.size() -2; layerNumber >0 ; layerNumber--){
		Layer &hiddenLayer = layers[layerNumber];
		Layer &nextLayer = layers[layerNumber+1];

		for(unsigned n = 0; n < hiddenLayer.size(); n++){
			hiddenLayer[n].calculateHiddenGradients(nextLayer);
  		}
  	}

    //------- For all layers from outputs to first hidden layer,
    //------- update connection weights
  	for(unsigned layerNumber = layers.size() -1; layerNumber >0 ; layerNumber--){
		Layer &layer = layers[layerNumber];
		Layer &prevLayer = layers[layerNumber-1];

		for(unsigned n = 0; n < layer.size() -1; n++){
			layer[n].updateInputWeights(prevLayer,eta,alpha);
		}
  	}

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rcx, -0x30(%rbp)
movq %rdx, -0x50(%rbp)
movq %rdi, %r13
movq 0x28(%rdi), %r12
fldz
fld %st(0)
fstpt (%rdi)
movq -0x18(%r12), %rax
movq -0x10(%r12), %rcx
subq %rax, %rcx
movabsq $-0x5555555555555555, %rbx # imm = 0xAAAAAAAAAAAAAAAB
cmpq $0x60, %rcx
jne 0x2962
fstpt 0x10(%r13)
jmp 0x2a24
movq %rsi, %r14
fstp %st(0)
movl $0x1, %r15d
xorl %ecx, %ecx
fldz
fstp %st(0)
movq (%r14), %rdx
fldl (%rdx,%rcx,8)
fstpt -0x40(%rbp)
leaq (%rcx,%rcx,2), %rdi
shlq $0x5, %rdi
addq %rax, %rdi
callq 0x46b4
fldt -0x40(%rbp)
fsubp %st, %st(1)
fldt (%r13)
fxch %st(1)
fmul %st, %st(0)
faddp %st, %st(1)
fld %st(0)
fstpt (%r13)
movl %r15d, %ecx
movq -0x18(%r12), %rax
movq -0x10(%r12), %rdx
subq %rax, %rdx
movq %rdx, %rsi
sarq $0x5, %rsi
imulq %rbx, %rsi
decq %rsi
incl %r15d
cmpq %rcx, %rsi
ja 0x2971
fmuls 0x3653(%rip)            # 0x6020
fld %st(0)
fstpt (%r13)
fstpt 0x10(%r13)
cmpq $0x60, %rdx
je 0x2a24
movq %r14, %r15
movq %rbx, %r14
movl $0x1, %ebx
xorl %ecx, %ecx
leaq (%rcx,%rcx,2), %rdi
shlq $0x5, %rdi
addq %rax, %rdi
movq (%r15), %rax
fldl (%rax,%rcx,8)
fstpt (%rsp)
callq 0x4824
movl %ebx, %ecx
movq -0x18(%r12), %rax
movq -0x10(%r12), %rdx
subq %rax, %rdx
sarq $0x5, %rdx
imulq %r14, %rdx
decq %rdx
incl %ebx
cmpq %rcx, %rdx
ja 0x29ea
movq 0x28(%r13), %rax
subq 0x20(%r13), %rax
shrq $0x3, %rax
imull $0xaaaaaaab, %eax, %eax # imm = 0xAAAAAAAB
addl $-0x2, %eax
movq %r13, -0x48(%rbp)
je 0x2ae1
movl %eax, %r15d
movq 0x20(%r13), %rcx
leaq (%r15,%r15,2), %rdx
movq (%rcx,%rdx,8), %rax
cmpq %rax, 0x8(%rcx,%rdx,8)
movabsq $-0x5555555555555555, %r13 # imm = 0xAAAAAAAAAAAAAAAB
je 0x2aab
leaq (%rcx,%rdx,8), %rbx
leal 0x1(%r15), %edx
leaq (%rdx,%rdx,2), %rdx
leaq (%rcx,%rdx,8), %r12
movl $0x1, %r14d
xorl %ecx, %ecx
leaq (%rcx,%rcx,2), %rdi
shlq $0x5, %rdi
addq %rax, %rdi
movq %r12, %rsi
callq 0x491e
movl %r14d, %ecx
movq (%rbx), %rax
movq 0x8(%rbx), %rdx
subq %rax, %rdx
sarq $0x5, %rdx
imulq %r13, %rdx
incl %r14d
cmpq %rcx, %rdx
ja 0x2a7b
decq %r15
testl %r15d, %r15d
movq -0x48(%rbp), %r13
jne 0x2a46
movq 0x28(%r13), %r8
subq 0x20(%r13), %r8
sarq $0x3, %r8
movabsq $-0x5555555555555555, %rax # imm = 0xAAAAAAAAAAAAAAAB
imulq %rax, %r8
movl %r8d, %eax
decl %eax
movq -0x30(%rbp), %rcx
jne 0x2af0
jmp 0x2b8b
movl $0x2, %r8d
movl $0x1, %eax
movq -0x30(%rbp), %rcx
movl %eax, %eax
movabsq $-0x5555555555555555, %r12 # imm = 0xAAAAAAAAAAAAAAAB
movq 0x20(%r13), %r9
movq %rax, -0x30(%rbp)
leaq (%rax,%rax,2), %rsi
movq (%r9,%rsi,8), %rax
movq 0x8(%r9,%rsi,8), %rdi
subq %rax, %rdi
cmpq $0x60, %rdi
movq -0x50(%rbp), %rdx
je 0x2b77
leaq (%r9,%rsi,8), %r15
addl $-0x2, %r8d
leaq (%r8,%r8,2), %rsi
leaq (%r9,%rsi,8), %rsi
movq %rsi, -0x40(%rbp)
movl $0x1, %ebx
xorl %esi, %esi
leaq (%rsi,%rsi,2), %rdi
shlq $0x5, %rdi
addq %rax, %rdi
movq -0x40(%rbp), %rsi
movq %rcx, %r13
movq %rdx, %r14
callq 0x49b0
movq %r14, %rdx
movq %r13, %rcx
movl %ebx, %esi
movq (%r15), %rax
movq 0x8(%r15), %rdi
subq %rax, %rdi
sarq $0x5, %rdi
imulq %r12, %rdi
decq %rdi
incl %ebx
cmpq %rsi, %rdi
ja 0x2b39
movq -0x30(%rbp), %r8
leaq -0x1(%r8), %rax
testl %eax, %eax
movq -0x48(%rbp), %r13
jne 0x2afc
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq