Question: The following code performs matrix multiplication in c + + . It takes integer pointers int dot _ product _ i ( int * m

The following code performs matrix multiplication in c++.
It takes integer pointers
int dot_product_i(int* m1, int* m2, int r, int c, int l)
{
int product =0;
for(int i=0; i < l; i++)
{
int a = m1[i + r * l]; //row of matrix 1
int b = m2[c + i * l]; //col of matrix 2
product += a *b;
}
return product;
}
The following is the assembly for the above c++ code. The requirement is to remove redundant instructions to improve performance. Please provide new working assembly file with improved code. Please explain the improvement (changes) Thanks.
.text
.file "dp_int.cpp"
.globl _Z13dot_product_iPiS_iii # -- Begin function _Z13dot_product_iPiS_iii
.p2align 4,0x90
.type _Z13dot_product_iPiS_iii,@function
_Z13dot_product_iPiS_iii: # @_Z13dot_product_iPiS_iii
.cfi_startproc
# %bb.0:
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset %rbp,-16
movq %rsp,%rbp
.cfi_def_cfa_register %rbp
movq %rdi, -8(%rbp)
movq %rsi, -16(%rbp)
movl %edx, -20(%rbp)
movl %ecx, -24(%rbp)
movl %r8d,-28(%rbp)
movl $0,-32(%rbp)
movl $0,-36(%rbp)
.LBB0_1: # =>This Inner Loop Header: Depth=1
movl -36(%rbp),%eax
cmpl -28(%rbp),%eax
jge .LBB0_4
# %bb.2: # in Loop: Header=BB0_1 Depth=1
movq -8(%rbp),%rax
movl -36(%rbp),%ecx
movl -20(%rbp),%edx
imull -28(%rbp),%edx
addl %edx, %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4),%eax
movl %eax, -40(%rbp)
movq -16(%rbp),%rax
movl -24(%rbp),%ecx
movl -36(%rbp),%edx
imull -28(%rbp),%edx
addl %edx, %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4),%eax
movl %eax, -44(%rbp)
movl -40(%rbp),%eax
imull -44(%rbp),%eax
addl -32(%rbp),%eax
movl %eax, -32(%rbp)
# %bb.3: # in Loop: Header=BB0_1 Depth=1
movl -36(%rbp),%eax
addl $1,%eax
movl %eax, -36(%rbp)
jmp .LBB0_1
.LBB0_4:
movl -32(%rbp),%eax
popq %rbp
.cfi_def_cfa %rsp,8
retq
.Lfunc_end0:
.size _Z13dot_product_iPiS_iii, .Lfunc_end0-_Z13dot_product_iPiS_iii
.cfi_endproc
# -- End function
.ident "clang version 17.0.6(CentOS 17.0.6-5.el9)"
.section ".note.GNU-stack","",@progbits
.addrsig

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!