Question: Consider a function sum below designed to calculate the sum of the neighbors of an element at position ( i , j ) in a

Consider a function sum below designed to calculate the sum of the neighbors of an element at position (i, j) in a 2D array represented in a linear array format. Here's a quick breakdown:
val is a pointer to the array.
(i, j) are the coordinates of the current element.
n is the number of columns in the 2D array.
The function calculates the sum of the elements directly above, below, to the left, and to the right of (i, j)
long sum (long *val, long i, long j, long n){/* Sum neighbors of i,j */
long up, down, left, right, sum;
up = val[(i-1)*n + j ];
down = val[(i+1)*n + j ];
left = val[i*n + j-1];
right = val[i*n + j+1];
sum = up + down + left + right;
return sum;
}
The function is rewritten as an optimized function long sum_opt
long sum_opt(long *val, long i, long j, long n){
long up, down, left, right, sum, opt = i*n + j;
up = val[opt - n];
down = val[opt + n];
left = val[opt -1];
right = val[opt +1];
sum = up + down + left + right;
return sum;
}
Write assembly in RISC V codes for the function with and without optimization. Comment on the results.

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!