// File: /workspace/github_cpp_makefile_star2_O2/toxaart[P]hierarchical_block_sparse_lib/source/HierarchicalBlockSparseMatrix.h
// Function: hbsm::HierarchicalBlockSparseMatrix<double>::add(hbsm::HierarchicalBlockSparseMatrix<double> const&, hbsm::HierarchicalBlockSparseMatrix<double> const&, hbsm::HierarchicalBlockSparseMatrix<double>&, unsigned long*)
void HierarchicalBlockSparseMatrix<Treal>::add(HierarchicalBlockSparseMatrix<Treal> const & A,
				       HierarchicalBlockSparseMatrix<Treal> const & B,
				       HierarchicalBlockSparseMatrix<Treal> & C,
					   size_t* no_of_resizes){

      if(!C.empty()) C.clear();

			if(A.empty() && B.empty()){
				return;
			}


			if(A.nRows_orig != B.nRows_orig || A.nCols_orig != B.nCols_orig ){
				throw std::runtime_error("Error in HierarchicalBlockSparseMatrix::add(): matrices to add have different sizes!");
			}

      C.set_params(A.get_params());

      C.resize(A.nRows_orig,A.nCols_orig, no_of_resizes);

			if(A.lowest_level()){


				int blocksize = A.blocksize;

				int noOfElements = blocksize*blocksize;
				Treal const ONEreal = 1.0;
				int  const ONEint  = 1;

				//copy A to C
				memcpy(&C.submatrix[0], &A.submatrix[0], sizeof(Treal) * noOfElements);

				// Add 1.0 * B to C.
				axpy(&noOfElements, &ONEreal, &B.submatrix[0], &ONEint, &C.submatrix[0], &ONEint);

				return;
			}

			for(int i = 0; i < 4; ++i){

				if(A.children[i] != NULL && B.children[i] != NULL){

					if(C.children[i] != NULL) throw std::runtime_error("Error in HierarchicalBlockSparseMatrix::add(): matrix C has non-null child!");


					C.children[i] = std::make_shared<HierarchicalBlockSparseMatrix<Treal> >();
					C.children[i]->parent = &C;

					add(*A.children[i], *B.children[i], *C.children[i], no_of_resizes);

				}

				if(A.children[i] == NULL && B.children[i] != NULL){

					if(C.children[i] != NULL) throw std::runtime_error("Error in HierarchicalBlockSparseMatrix::add(): matrix C has non-null child!");
					C.children[i] = B.children[i]; // smart pointer assignment. Now C.children[i] has parent in B, but it does not matter since they are of the  same size!

				}

				if(A.children[i] != NULL && B.children[i] == NULL){

					if(C.children[i] != NULL) throw std::runtime_error("Error in HierarchicalBlockSparseMatrix::add(): matrix C has non-null child!");
					C.children[i] = A.children[i]; // smart pointer assignment. Now C.children[i] has parent in A, but it does not matter since they are of the  same size!

				}

				if(A.children[i] == NULL && B.children[i] == NULL){

					if(C.children[i] != NULL) throw std::runtime_error("Error in HierarchicalBlockSparseMatrix::add(): matrix C has non-null child!");

					//nothing to do here, leave it NULL
				}

			}

			C.n_block_multiplies = A.get_n_block_multiplications() + B.get_n_block_multiplications();

			return;
		}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rcx, 0x10(%rsp)
movq %rdx, %rbx
movq %rsi, %r15
movq %rdi, %rbp
movq %rdx, %rdi
callq 0x14e3a
testb %al, %al
jne 0x82c4
movq %rbx, %rdi
callq 0x14e82
movq %rbp, %rdi
callq 0x14e3a
testb %al, %al
je 0x82e0
movq %r15, %rdi
callq 0x14e3a
testb %al, %al
jne 0x84a7
movl 0x8(%rbp), %eax
cmpl 0x8(%r15), %eax
jne 0x8526
movl 0xc(%rbp), %eax
cmpl 0xc(%r15), %eax
jne 0x8526
movl 0x10(%rbp), %eax
leaq 0x30(%rsp), %rsi
movl %eax, (%rsi)
movq %rbx, %rdi
callq 0x7cc0
movl 0x8(%rbp), %esi
movl 0xc(%rbp), %edx
movq %rbx, %rdi
movq 0x10(%rsp), %rcx
callq 0x7d24
movq %rbp, %rdi
callq 0x14f94
testb %al, %al
je 0x8388
movl 0x10(%rbp), %edx
imull %edx, %edx
leaq 0x44(%rsp), %r14
movl %edx, (%r14)
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
leaq 0x30(%rsp), %r12
movq %rax, (%r12)
leaq 0x40(%rsp), %r13
movl $0x1, (%r13)
movq 0x70(%rbx), %rdi
movq 0x70(%rbp), %rsi
shlq $0x3, %rdx
callq 0x2170
movq 0x70(%r15), %rdx
movq 0x70(%rbx), %r8
movq %r14, %rdi
movq %r12, %rsi
movq %r13, %rcx
movq %r13, %r9
callq 0x21d0
jmp 0x84a7
movq %rbp, 0x18(%rsp)
leaq 0x28(%rbp), %r12
movq %r15, 0x20(%rsp)
addq $0x28, %r15
movq %rbx, 0x8(%rsp)
leaq 0x28(%rbx), %rbp
xorl %ebx, %ebx
movq %r12, 0x28(%rsp)
cmpq $0x40, %rbx
je 0x848c
leaq (%r12,%rbx), %r14
cmpq $0x0, (%r12,%rbx)
je 0x8425
cmpq $0x0, (%r15,%rbx)
je 0x844b
cmpq $0x0, (%rbp,%rbx)
jne 0x84b6
leaq (%rbx,%rbp), %r13
leaq 0x30(%rsp), %r12
movq %r12, %rdi
callq 0x152ec
movq %r13, %rdi
movq %r12, %rsi
callq 0x14fba
leaq 0x38(%rsp), %rdi
callq 0x14fe8
movq (%r13), %rdx
movq 0x8(%rsp), %rax
movq %rax, 0x68(%rdx)
movq (%r14), %rdi
movq (%r15,%rbx), %rsi
movq 0x10(%rsp), %rcx
callq 0x8294
cmpq $0x0, (%r14)
movq 0x28(%rsp), %r12
jne 0x844b
cmpq $0x0, (%r15,%rbx)
je 0x8470
cmpq $0x0, (%rbp,%rbx)
jne 0x84d2
leaq (%r15,%rbx), %rsi
leaq (%rbx,%rbp), %rdi
callq 0x162fc
cmpq $0x0, (%r14)
je 0x8470
cmpq $0x0, (%r15,%rbx)
jne 0x8483
cmpq $0x0, (%rbp,%rbx)
jne 0x84ee
leaq (%rbx,%rbp), %rdi
movq %r14, %rsi
callq 0x162fc
cmpq $0x0, (%r14)
jne 0x8483
cmpq $0x0, (%r15,%rbx)
jne 0x8483
cmpq $0x0, (%rbp,%rbx)
jne 0x850a
addq $0x10, %rbx
jmp 0x83aa
movq 0x20(%rsp), %rax
movq 0x20(%rax), %rax
movq 0x18(%rsp), %rcx
addq 0x20(%rcx), %rax
movq 0x8(%rsp), %rcx
movq %rax, 0x20(%rcx)
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x10
popq %rdi
callq 0x20d0
movq %rax, %r14
leaq 0x14761(%rip), %rsi     # 0x1cc29
movq %rax, %rdi
callq 0x2080
jmp 0x8540
pushq $0x10
popq %rdi
callq 0x20d0
movq %rax, %r14
leaq 0x14745(%rip), %rsi     # 0x1cc29
movq %rax, %rdi
callq 0x2080
jmp 0x8540
pushq $0x10
popq %rdi
callq 0x20d0
movq %rax, %r14
leaq 0x14729(%rip), %rsi     # 0x1cc29
movq %rax, %rdi
callq 0x2080
jmp 0x8540
pushq $0x10
popq %rdi
callq 0x20d0
movq %rax, %r14
leaq 0x1470d(%rip), %rsi     # 0x1cc29
movq %rax, %rdi
callq 0x2080
jmp 0x8540
pushq $0x10
popq %rdi
callq 0x20d0
movq %rax, %r14
leaq 0x1469c(%rip), %rsi     # 0x1cbd4
movq %rax, %rdi
callq 0x2080
movq 0x1daa9(%rip), %rsi     # 0x25ff0
movq 0x1da72(%rip), %rdx     # 0x25fc0
movq %r14, %rdi
callq 0x2270
jmp 0x855e
jmp 0x855e
jmp 0x855e
jmp 0x855e
movq %rax, %rbx
movq %r14, %rdi
callq 0x2130
movq %rbx, %rdi
callq 0x2290
nop